github.com/cloudberrydb/gpbackup@v1.0.3-0.20240118031043-5410fd45eed6/integration/integration_suite_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"os"
     7  	"os/exec"
     8  	"testing"
     9  
    10  	"github.com/cloudberrydb/gp-common-go-libs/cluster"
    11  	"github.com/cloudberrydb/gp-common-go-libs/dbconn"
    12  	"github.com/cloudberrydb/gp-common-go-libs/testhelper"
    13  	"github.com/cloudberrydb/gpbackup/backup"
    14  	"github.com/cloudberrydb/gpbackup/restore"
    15  	"github.com/cloudberrydb/gpbackup/testutils"
    16  	"github.com/cloudberrydb/gpbackup/toc"
    17  	"github.com/cloudberrydb/gpbackup/utils"
    18  	"github.com/spf13/pflag"
    19  
    20  	. "github.com/onsi/ginkgo/v2"
    21  	. "github.com/onsi/gomega"
    22  	. "github.com/onsi/gomega/gbytes"
    23  	. "github.com/onsi/gomega/gexec"
    24  )
    25  
    26  var (
    27  	buffer             *bytes.Buffer
    28  	connectionPool     *dbconn.DBConn
    29  	tocfile            *toc.TOC
    30  	backupfile         *utils.FileWithByteCount
    31  	testCluster        *cluster.Cluster
    32  	gpbackupHelperPath string
    33  	stderr, logFile    *Buffer
    34  
    35  	// GUC defaults. Initially set to GPDB4 values
    36  	concurrencyDefault    = "20"
    37  	memSharedDefault      = "20"
    38  	memSpillDefault       = "20"
    39  	memAuditDefault       = "0"
    40  	cpuSetDefault         = "-1"
    41  	includeSecurityLabels = false
    42  )
    43  
    44  func TestQueries(t *testing.T) {
    45  	RegisterFailHandler(Fail)
    46  	RunSpecs(t, "database query tests")
    47  }
    48  
    49  var _ = BeforeSuite(func() {
    50  	_ = exec.Command("dropdb", "testdb").Run()
    51  	err := exec.Command("createdb", "testdb").Run()
    52  	if err != nil {
    53  		Fail("Cannot create database testdb; is GPDB running?")
    54  	}
    55  	Expect(err).To(BeNil())
    56  	_, stderr, logFile = testhelper.SetupTestLogger()
    57  	connectionPool = testutils.SetupTestDbConn("testdb")
    58  	// We can't use AssertQueryRuns since if a role already exists it will error
    59  	_, _ = connectionPool.Exec("CREATE ROLE testrole SUPERUSER")
    60  	_, _ = connectionPool.Exec("CREATE ROLE anothertestrole SUPERUSER")
    61  	backup.InitializeMetadataParams(connectionPool)
    62  	backup.SetConnection(connectionPool)
    63  	segConfig := cluster.MustGetSegmentConfiguration(connectionPool)
    64  	testCluster = cluster.NewCluster(segConfig)
    65  	testhelper.AssertQueryRuns(connectionPool, "SET ROLE testrole")
    66  	testhelper.AssertQueryRuns(connectionPool, "ALTER DATABASE testdb OWNER TO anothertestrole")
    67  	testhelper.AssertQueryRuns(connectionPool, "ALTER SCHEMA public OWNER TO anothertestrole")
    68  	testhelper.AssertQueryRuns(connectionPool, "DROP PROTOCOL IF EXISTS gphdfs")
    69  	testhelper.AssertQueryRuns(connectionPool, `SET standard_conforming_strings TO "on"`)
    70  	testhelper.AssertQueryRuns(connectionPool, `SET search_path=pg_catalog`)
    71  	if false {
    72  		testhelper.AssertQueryRuns(connectionPool, "SET allow_system_table_mods = 'DML'")
    73  		testutils.SetupTestFilespace(connectionPool, testCluster)
    74  	} else {
    75  		testhelper.AssertQueryRuns(connectionPool, "SET allow_system_table_mods = true")
    76  
    77  		remoteOutput := testCluster.GenerateAndExecuteCommand(
    78  			"Creating filespace test directories on all hosts",
    79  			cluster.ON_HOSTS|cluster.INCLUDE_COORDINATOR,
    80  			func(contentID int) string {
    81  				return fmt.Sprintf("mkdir -p /tmp/test_dir && mkdir -p /tmp/test_dir1 && mkdir -p /tmp/test_dir2")
    82  			})
    83  		if remoteOutput.NumErrors != 0 {
    84  			Fail("Could not create filespace test directory on 1 or more hosts")
    85  		}
    86  	}
    87  
    88  	gpbackupHelperPath = buildAndInstallBinaries()
    89  
    90  	// Set GUC Defaults and version logic
    91  	if true {
    92  		memSharedDefault = "80"
    93  		memSpillDefault = "0"
    94  
    95  		includeSecurityLabels = true
    96  	}
    97  })
    98  
    99  var backupCmdFlags *pflag.FlagSet
   100  var restoreCmdFlags *pflag.FlagSet
   101  
   102  var _ = BeforeEach(func() {
   103  	buffer = bytes.NewBuffer([]byte(""))
   104  
   105  	backupCmdFlags = pflag.NewFlagSet("gpbackup", pflag.ExitOnError)
   106  	backup.SetCmdFlags(backupCmdFlags)
   107  	backup.SetFilterRelationClause("")
   108  
   109  	restoreCmdFlags = pflag.NewFlagSet("gprestore", pflag.ExitOnError)
   110  	restore.SetCmdFlags(restoreCmdFlags)
   111  })
   112  
   113  var _ = AfterSuite(func() {
   114  	CleanupBuildArtifacts()
   115  	if false {
   116  		testutils.DestroyTestFilespace(connectionPool)
   117  	} else {
   118  		remoteOutput := testCluster.GenerateAndExecuteCommand(
   119  			"Removing /tmp/test_dir* directories on all hosts",
   120  			cluster.ON_HOSTS|cluster.INCLUDE_COORDINATOR,
   121  			func(contentID int) string {
   122  				return fmt.Sprintf("rm -rf /tmp/test_dir*")
   123  			})
   124  		if remoteOutput.NumErrors != 0 {
   125  			Fail("Could not remove /tmp/testdir* directories on 1 or more hosts")
   126  		}
   127  	}
   128  	if connectionPool != nil {
   129  		connectionPool.Close()
   130  		err := exec.Command("dropdb", "testdb").Run()
   131  		Expect(err).To(BeNil())
   132  	}
   133  	connection1 := testutils.SetupTestDbConn("template1")
   134  	testhelper.AssertQueryRuns(connection1, "DROP ROLE testrole")
   135  	testhelper.AssertQueryRuns(connection1, "DROP ROLE anothertestrole")
   136  	connection1.Close()
   137  	_ = os.RemoveAll("/tmp/helper_test")
   138  	_ = os.RemoveAll("/tmp/plugin_dest")
   139  })