Skip to content

Commit a5c842e

Browse files
Adding function to verify Backup / Restore data (#624)
* Adding function to verify BackUp / Restore data * Adding verification for mysql * Adding timeout / retry function * Changing VerifyBackupRestoreData fn name
1 parent 69ce063 commit a5c842e

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

tests/e2e/backup_restore_suite_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var _ = Describe("AWS backup restore tests", func() {
4747

4848
parksAppReady := VerificationFunction(func(ocClient client.Client, namespace string) error {
4949
Eventually(IsDCReady(ocClient, "parks-app", "restify"), timeoutMultiplier*time.Minute*10, time.Second*10).Should(BeTrue())
50+
// err := VerifyBackupRestoreData(artifact_dir, namespace, "restify", "parks-app") // TODO: VERIFY PARKS APP DATA
5051
return nil
5152
})
5253
mysqlReady := VerificationFunction(func(ocClient client.Client, namespace string) error {
@@ -60,7 +61,8 @@ var _ = Describe("AWS backup restore tests", func() {
6061
if !exists {
6162
return errors.New("did not find MYSQL scc")
6263
}
63-
return nil
64+
err = VerifyBackupRestoreData(artifact_dir, namespace, "todolist-route", "todolist")
65+
return err
6466
})
6567

6668
DescribeTable("backup and restore applications",

tests/e2e/lib/apps.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package lib
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
67
"fmt"
8+
"io/ioutil"
9+
"log"
10+
"net/http"
711
"os"
812
"os/exec"
13+
"time"
914

1015
"github.com/onsi/ginkgo/v2"
1116
ocpappsv1 "github.com/openshift/api/apps/v1"
17+
routev1 "github.com/openshift/api/route/v1"
1218
security "github.com/openshift/api/security/v1"
1319
appsv1 "k8s.io/api/apps/v1"
1420
corev1 "k8s.io/api/core/v1"
@@ -18,6 +24,7 @@ import (
1824
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
1925
"k8s.io/apimachinery/pkg/util/wait"
2026
"sigs.k8s.io/controller-runtime/pkg/client"
27+
"sigs.k8s.io/controller-runtime/pkg/client/config"
2128
)
2229

2330
func InstallApplication(ocClient client.Client, file string) error {
@@ -197,3 +204,70 @@ func RunMustGather(oc_cli string, artifact_dir string) error {
197204
_, err := cmd.Output()
198205
return err
199206
}
207+
208+
func VerifyBackupRestoreData(artifact_dir string, namespace string, routeName string, app string) error {
209+
log.Printf("Verifying backup/restore data of %s", app)
210+
appRoute := &routev1.Route{}
211+
clientv1, err := client.New(config.GetConfigOrDie(), client.Options{})
212+
if err != nil {
213+
return err
214+
}
215+
backupFile := artifact_dir + "/backup-data.txt"
216+
routev1.AddToScheme(clientv1.Scheme())
217+
err = clientv1.Get(context.Background(), client.ObjectKey{
218+
Namespace: namespace,
219+
Name: routeName,
220+
}, appRoute)
221+
if err != nil {
222+
return err
223+
}
224+
appApi := "http://" + appRoute.Spec.Host
225+
switch app {
226+
case "todolist":
227+
appApi += "/todo-completed"
228+
case "parks-app":
229+
appApi += "/parks"
230+
}
231+
resp, err := http.Get(appApi)
232+
if err != nil {
233+
return err
234+
}
235+
defer resp.Body.Close()
236+
237+
if resp.StatusCode != 200 { // # TODO: NEED TO FIND A BETTER WAY TO DEBUG RESPONSE
238+
var retrySchedule = []time.Duration{
239+
15 * time.Second,
240+
1 * time.Minute,
241+
2 * time.Minute,
242+
}
243+
for _, backoff := range retrySchedule {
244+
resp, err = http.Get(appApi)
245+
if resp.StatusCode != 200 {
246+
log.Printf("Request error: %+v\n", err)
247+
log.Printf("Retrying in %v\n", backoff)
248+
time.Sleep(backoff)
249+
}
250+
}
251+
}
252+
253+
respData, err := ioutil.ReadAll(resp.Body)
254+
if err != nil {
255+
return err
256+
}
257+
if _, err := os.Stat(backupFile); err == nil {
258+
backupData, err := os.ReadFile(backupFile)
259+
if err != nil {
260+
return err
261+
}
262+
os.Remove(backupFile)
263+
if !bytes.Equal(backupData, respData) {
264+
return errors.New("Backup and Restore Data are not the same")
265+
}
266+
} else if errors.Is(err, os.ErrNotExist) {
267+
err := os.WriteFile(backupFile, respData, 0644)
268+
if err != nil {
269+
return err
270+
}
271+
}
272+
return nil
273+
}

0 commit comments

Comments
 (0)