1
1
package generate_test
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"fmt"
6
7
"github.com/pkg/errors"
7
8
"github.com/riotkit-org/backup-maker/generate"
8
9
log "github.com/sirupsen/logrus"
9
10
"github.com/stretchr/testify/assert"
11
+ "os"
10
12
"os/exec"
11
13
"testing"
14
+ "time"
12
15
)
13
16
14
17
// TestEndToEnd_MariaDBBackupAndRestore an End-To-End testing procedure for MariaDB/MySQL
@@ -38,7 +41,7 @@ Repository:
38
41
39
42
` )
40
43
generateMySQLSnippet ("backup" )
41
- subTestMySQLDumpBackup (t )
44
+ subTestMySQLDumpBackup (t , dbHostname , dbPort )
42
45
_ = c .Terminate (ctx )
43
46
44
47
// =================================================================================
@@ -63,11 +66,15 @@ Repository:
63
66
64
67
` )
65
68
generateMySQLSnippet ("restore" )
66
- subTestMySQLRestoreBackup (t )
69
+ subTestMySQLRestoreBackup (t , dbHostname , dbPort )
67
70
})
68
71
}
69
72
70
- func subTestMySQLDumpBackup (t * testing.T ) {
73
+ func subTestMySQLDumpBackup (t * testing.T , mysqlHost string , mysqlPort int ) {
74
+ // inject example data
75
+ time .Sleep (time .Second * 5 )
76
+ execAndAssert ("mysql" , "-u" , "rojava" , "-h" , mysqlHost , "-projava" , "-P" , fmt .Sprintf ("%v" , mysqlPort ), "emma_goldman" , "-e" , "source ../resources/test/mysql-example-structure.sql" )
77
+
71
78
// run backup.sh
72
79
cmd := exec .Command ("/bin/bash" , "-c" , "export PATH=$PATH:./; bash backup.sh 2>&1" )
73
80
cmd .Dir = "../.build"
@@ -78,7 +85,7 @@ func subTestMySQLDumpBackup(t *testing.T) {
78
85
assert .Contains (t , string (out ), "Version uploaded" )
79
86
}
80
87
81
- func subTestMySQLRestoreBackup (t * testing.T ) {
88
+ func subTestMySQLRestoreBackup (t * testing.T , mysqlHost string , mysqlPort int ) {
82
89
// run restore.sh
83
90
cmd := exec .Command ("/bin/bash" , "-c" , "export PATH=$PATH:./; bash restore.sh 2>&1" )
84
91
cmd .Dir = "../.build"
@@ -87,6 +94,10 @@ func subTestMySQLRestoreBackup(t *testing.T) {
87
94
88
95
assert .Nil (t , err )
89
96
assert .Contains (t , string (out ), "Backup restored" )
97
+
98
+ // check that data in database exists - `resources/test/mysql-example-structure.sql` inserts a one record with "Mikhail Bakunin"
99
+ sqlCheck := execAndReturn ("mysql" , "-u" , "rojava" , "-h" , mysqlHost , "-projava" , "-P" , fmt .Sprintf ("%v" , mysqlPort ), "emma_goldman" , "-e" , "SELECT * FROM Persons;" )
100
+ assert .Contains (t , sqlCheck , "Bakunin" )
90
101
}
91
102
92
103
func generateMySQLSnippet (operation string ) {
@@ -107,3 +118,29 @@ func generateMySQLSnippet(operation string) {
107
118
log .Fatal (errors .Wrap (err , "Cannot generate backup snippet" ))
108
119
}
109
120
}
121
+
122
+ func execAndAssert (command string , args ... string ) {
123
+ cmd := exec .Command (command , args ... )
124
+ cmd .Stdout = os .Stdout
125
+ cmd .Stderr = os .Stderr
126
+ if err := cmd .Start (); err != nil {
127
+ log .Fatal (errors .Wrap (err , "Failed to start process" ))
128
+ }
129
+ if err := cmd .Wait (); err != nil {
130
+ log .Fatal (errors .Wrap (err , "Process failed" ))
131
+ }
132
+ }
133
+
134
+ func execAndReturn (command string , args ... string ) string {
135
+ cmd := exec .Command (command , args ... )
136
+ var buf bytes.Buffer
137
+ cmd .Stdout = & buf
138
+ cmd .Stderr = & buf
139
+ if err := cmd .Start (); err != nil {
140
+ log .Fatal (errors .Wrap (err , "Failed to start process" ))
141
+ }
142
+ if err := cmd .Wait (); err != nil {
143
+ log .Fatal (errors .Wrap (err , "Process failed" ))
144
+ }
145
+ return buf .String ()
146
+ }
0 commit comments