Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
tests: Add additional assertion to check contents of restored SQL data
Browse files Browse the repository at this point in the history
  • Loading branch information
blackandred committed Apr 10, 2022
1 parent b094211 commit d8e23ec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
45 changes: 41 additions & 4 deletions generate/e2e_mysql_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -38,7 +41,7 @@ Repository:
`)
generateMySQLSnippet("backup")
subTestMySQLDumpBackup(t)
subTestMySQLDumpBackup(t, dbHostname, dbPort)
_ = c.Terminate(ctx)

// =================================================================================
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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) {
Expand All @@ -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()
}
7 changes: 7 additions & 0 deletions resources/test/mysql-example-structure.sql
Original file line number Diff line number Diff line change
@@ -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");

0 comments on commit d8e23ec

Please sign in to comment.