From d8e23ec14e8106702f6bfb760a3de103171421ef Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Sun, 10 Apr 2022 22:31:13 +0200 Subject: [PATCH] tests: Add additional assertion to check contents of restored SQL data --- generate/e2e_mysql_test.go | 45 ++++++++++++++++++++-- resources/test/mysql-example-structure.sql | 7 ++++ 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 resources/test/mysql-example-structure.sql diff --git a/generate/e2e_mysql_test.go b/generate/e2e_mysql_test.go index 749c2ef..54301e0 100644 --- a/generate/e2e_mysql_test.go +++ b/generate/e2e_mysql_test.go @@ -1,14 +1,17 @@ package generate_test import ( + "bytes" "context" "fmt" "github.com/pkg/errors" "github.com/riotkit-org/backup-maker/generate" log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" + "os" "os/exec" "testing" + "time" ) // TestEndToEnd_MariaDBBackupAndRestore an End-To-End testing procedure for MariaDB/MySQL @@ -38,7 +41,7 @@ Repository: `) generateMySQLSnippet("backup") - subTestMySQLDumpBackup(t) + subTestMySQLDumpBackup(t, dbHostname, dbPort) _ = c.Terminate(ctx) // ================================================================================= @@ -63,11 +66,15 @@ Repository: `) generateMySQLSnippet("restore") - subTestMySQLRestoreBackup(t) + subTestMySQLRestoreBackup(t, dbHostname, dbPort) }) } -func subTestMySQLDumpBackup(t *testing.T) { +func subTestMySQLDumpBackup(t *testing.T, mysqlHost string, mysqlPort int) { + // inject example data + time.Sleep(time.Second * 5) + execAndAssert("mysql", "-u", "rojava", "-h", mysqlHost, "-projava", "-P", fmt.Sprintf("%v", mysqlPort), "emma_goldman", "-e", "source ../resources/test/mysql-example-structure.sql") + // run backup.sh cmd := exec.Command("/bin/bash", "-c", "export PATH=$PATH:./; bash backup.sh 2>&1") cmd.Dir = "../.build" @@ -78,7 +85,7 @@ func subTestMySQLDumpBackup(t *testing.T) { assert.Contains(t, string(out), "Version uploaded") } -func subTestMySQLRestoreBackup(t *testing.T) { +func subTestMySQLRestoreBackup(t *testing.T, mysqlHost string, mysqlPort int) { // run restore.sh cmd := exec.Command("/bin/bash", "-c", "export PATH=$PATH:./; bash restore.sh 2>&1") cmd.Dir = "../.build" @@ -87,6 +94,10 @@ func subTestMySQLRestoreBackup(t *testing.T) { assert.Nil(t, err) assert.Contains(t, string(out), "Backup restored") + + // check that data in database exists - `resources/test/mysql-example-structure.sql` inserts a one record with "Mikhail Bakunin" + sqlCheck := execAndReturn("mysql", "-u", "rojava", "-h", mysqlHost, "-projava", "-P", fmt.Sprintf("%v", mysqlPort), "emma_goldman", "-e", "SELECT * FROM Persons;") + assert.Contains(t, sqlCheck, "Bakunin") } func generateMySQLSnippet(operation string) { @@ -107,3 +118,29 @@ func generateMySQLSnippet(operation string) { log.Fatal(errors.Wrap(err, "Cannot generate backup snippet")) } } + +func execAndAssert(command string, args ...string) { + cmd := exec.Command(command, args...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Start(); err != nil { + log.Fatal(errors.Wrap(err, "Failed to start process")) + } + if err := cmd.Wait(); err != nil { + log.Fatal(errors.Wrap(err, "Process failed")) + } +} + +func execAndReturn(command string, args ...string) string { + cmd := exec.Command(command, args...) + var buf bytes.Buffer + cmd.Stdout = &buf + cmd.Stderr = &buf + if err := cmd.Start(); err != nil { + log.Fatal(errors.Wrap(err, "Failed to start process")) + } + if err := cmd.Wait(); err != nil { + log.Fatal(errors.Wrap(err, "Process failed")) + } + return buf.String() +} diff --git a/resources/test/mysql-example-structure.sql b/resources/test/mysql-example-structure.sql new file mode 100644 index 0000000..2be2c0d --- /dev/null +++ b/resources/test/mysql-example-structure.sql @@ -0,0 +1,7 @@ +CREATE TABLE Persons ( + PersonID int, + LastName varchar(255), + FirstName varchar(255) +); + +INSERT INTO Persons (PersonID, LastName, FirstName) VALUES (1, "Bakunin", "Mikhail");