github.com/cloudfoundry/postgres-release/src/acceptance-tests@v0.0.0-20240511030151-872bdd2e0dba/deploy/backup_test.go (about)

     1  package deploy_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  
     9  	"github.com/cloudfoundry/postgres-release/src/acceptance-tests/testing/helpers"
    10  
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Backup and restore a deployment", func() {
    16  
    17  	var caCertPath string
    18  	var tempDir string
    19  	var pwd string
    20  
    21  	JustBeforeEach(func() {
    22  		var err error
    23  		err = deployHelper.Deploy()
    24  		Expect(err).NotTo(HaveOccurred())
    25  
    26  		caCertPath, err = helpers.WriteFile(configParams.Bosh.Credentials.CACert)
    27  		Expect(err).NotTo(HaveOccurred())
    28  
    29  		tempDir, err = helpers.CreateTempDir()
    30  		Expect(err).NotTo(HaveOccurred())
    31  		pwd, err = os.Getwd()
    32  		Expect(err).NotTo(HaveOccurred())
    33  		err = os.Chdir(tempDir)
    34  		Expect(err).NotTo(HaveOccurred())
    35  
    36  		os.Setenv("BOSH_CLIENT_SECRET", configParams.Bosh.Credentials.ClientSecret)
    37  		os.Setenv("CA_CERT", caCertPath)
    38  	})
    39  
    40  	AfterEach(func() {
    41  		err := os.Chdir(pwd)
    42  		Expect(err).NotTo(HaveOccurred())
    43  		os.Remove(caCertPath)
    44  		os.RemoveAll(tempDir)
    45  	})
    46  
    47  	Context("BBR is disabled", func() {
    48  
    49  		BeforeEach(func() {
    50  			deployHelper.SetOpDefs(nil)
    51  		})
    52  
    53  		It("Fails to backup the database", func() {
    54  			var err error
    55  			var cmd *exec.Cmd
    56  			cmd = exec.Command("bbr", "deployment", "--target", configParams.Bosh.Target, "--username", configParams.Bosh.Credentials.Client, "--deployment", deployHelper.GetDeploymentName(), "backup")
    57  			err = cmd.Run()
    58  			Expect(err).To(HaveOccurred())
    59  		})
    60  
    61  		It("Fails to restore the database", func() {
    62  			var err error
    63  			var cmd *exec.Cmd
    64  			cmd = exec.Command("bbr", "deployment", "--target", configParams.Bosh.Target, "--username", configParams.Bosh.Credentials.Client, "--deployment", deployHelper.GetDeploymentName(), "restore", "--artifact-path", fmt.Sprintf("%s/doesnotexist", tempDir))
    65  			err = cmd.Run()
    66  			Expect(err).To(HaveOccurred())
    67  		})
    68  
    69  	})
    70  
    71  	Context("BBR is enabled", func() {
    72  
    73  		var pgprops helpers.Properties
    74  		var pgHost string
    75  		var db helpers.PGData
    76  		JustBeforeEach(func() {
    77  			var err error
    78  
    79  			pgprops, pgHost, err = deployHelper.GetPGPropsAndHost()
    80  			Expect(err).NotTo(HaveOccurred())
    81  			db, err = deployHelper.ConnectToPostgres(pgHost, pgprops)
    82  			Expect(err).NotTo(HaveOccurred())
    83  		})
    84  
    85  		AssertBackupRestoreSuccessful := func() func() {
    86  			return func() {
    87  				var err error
    88  				var cmd *exec.Cmd
    89  				By("Running pre-backup-checks")
    90  				cmd = exec.Command("bbr", "deployment", "--target", configParams.Bosh.Target, "--username", configParams.Bosh.Credentials.Client, "--deployment", deployHelper.GetDeploymentName(), "pre-backup-check")
    91  				stdout, stderr, err := helpers.RunCommand(cmd)
    92  				Expect(err).NotTo(HaveOccurred(), "stderr was: '%v', stdout was: '%v'", stderr, stdout)
    93  
    94  				By("Changing content")
    95  				err = db.CreateAndPopulateTablesWithPrefix(pgprops.Databases.Databases[0].Name, helpers.Test1Load, "restore")
    96  				Expect(err).NotTo(HaveOccurred())
    97  				result, err := db.CheckTableExist("restore_0", pgprops.Databases.Databases[0].Name)
    98  				Expect(err).NotTo(HaveOccurred())
    99  				Expect(result).To(BeTrue())
   100  
   101  				By("Running backup")
   102  				cmd = exec.Command("bbr", "deployment", "--target", configParams.Bosh.Target, "--username", configParams.Bosh.Credentials.Client, "--deployment", deployHelper.GetDeploymentName(), "backup")
   103  				stdout, stderr, err = helpers.RunCommand(cmd)
   104  				Expect(err).NotTo(HaveOccurred(), "stderr was: '%v', stdout was: '%v'", stderr, stdout)
   105  				tarBackupFile := fmt.Sprintf("%s/%s*/*-bbr-postgres-db.tar", tempDir, deployHelper.GetDeploymentName())
   106  				files, err := filepath.Glob(tarBackupFile)
   107  				Expect(err).NotTo(HaveOccurred())
   108  				Expect(files).NotTo(BeEmpty())
   109  
   110  				By("Dropping the table")
   111  				err = db.DropTable(pgprops.Databases.Databases[0].Name, "restore_0")
   112  				Expect(err).NotTo(HaveOccurred())
   113  
   114  				By("Restoring the database")
   115  				cmd = exec.Command("bbr", "deployment", "--target", configParams.Bosh.Target, "--username", configParams.Bosh.Credentials.Client, "--deployment", deployHelper.GetDeploymentName(), "restore", "--artifact-path", filepath.Dir(files[0]))
   116  				stdout, stderr, err = helpers.RunCommand(cmd)
   117  				Expect(err).NotTo(HaveOccurred())
   118  
   119  				By("Validating that the dropped table has been restored")
   120  				result, err = db.CheckTableExist("restore_0", pgprops.Databases.Databases[0].Name)
   121  				Expect(err).NotTo(HaveOccurred())
   122  				Expect(result).To(BeTrue())
   123  
   124  				By("Dropping the table")
   125  				err = db.DropTable(pgprops.Databases.Databases[0].Name, "restore_0")
   126  				Expect(err).NotTo(HaveOccurred())
   127  			}
   128  		}
   129  
   130  		Context("BBR job is colocated", func() {
   131  			Context("When using BOSH links", func() {
   132  				BeforeEach(func() {
   133  					deployHelper.SetOpDefs(helpers.Define_bbr_ops())
   134  				})
   135  
   136  				It("Fails to restore the database", func() {
   137  					var err error
   138  					var cmd *exec.Cmd
   139  					cmd = exec.Command("bbr", "deployment", "--target", configParams.Bosh.Target, "--username", configParams.Bosh.Credentials.Client, "--deployment", deployHelper.GetDeploymentName(), "restore", "--artifact-path", fmt.Sprintf("%s/doesnotexist", tempDir))
   140  					err = cmd.Run()
   141  					Expect(err).To(HaveOccurred())
   142  				})
   143  
   144  				It("Successfully backup and restore the database", AssertBackupRestoreSuccessful())
   145  			})
   146  
   147  			Context("When not using BOSH links", func() {
   148  				BeforeEach(func() {
   149  					deployHelper.SetOpDefs(helpers.Define_bbr_no_link_ops())
   150  				})
   151  
   152  				It("Successfully backup and restore the database", AssertBackupRestoreSuccessful())
   153  			})
   154  		})
   155  
   156  		Context("BBR job is not colocated", func() {
   157  			Context("With SSL disabled", func() {
   158  				BeforeEach(func() {
   159  					deployHelper.SetOpDefs(helpers.Define_bbr_not_colocated_ops())
   160  				})
   161  
   162  				It("Successfully backup and restore the database", AssertBackupRestoreSuccessful())
   163  			})
   164  
   165  			Context("With SSL", func() {
   166  				BeforeEach(func() {
   167  					deployHelper.SetOpDefs(helpers.Define_bbr_ssl_verify_ca())
   168  				})
   169  
   170  				It("Successfully backup and restore the database", AssertBackupRestoreSuccessful())
   171  			})
   172  
   173  			Context("With SSL enforced with hostname validation", func() {
   174  				BeforeEach(func() {
   175  					deployHelper.SetOpDefs(helpers.Define_bbr_ssl_verify_full())
   176  				})
   177  
   178  				It("Successfully backup and restore the database", AssertBackupRestoreSuccessful())
   179  			})
   180  
   181  			Context("With SSL authentication", func() {
   182  				BeforeEach(func() {
   183  					deployHelper.SetOpDefs(helpers.Define_bbr_client_certs())
   184  				})
   185  
   186  				It("Successfully backup and restore the database", AssertBackupRestoreSuccessful())
   187  			})
   188  		})
   189  	})
   190  })