1
1
package lib
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"errors"
6
7
"fmt"
8
+ "io/ioutil"
9
+ "log"
10
+ "net/http"
7
11
"os"
8
12
"os/exec"
13
+ "time"
9
14
10
15
"github.com/onsi/ginkgo/v2"
11
16
ocpappsv1 "github.com/openshift/api/apps/v1"
17
+ routev1 "github.com/openshift/api/route/v1"
12
18
security "github.com/openshift/api/security/v1"
13
19
appsv1 "k8s.io/api/apps/v1"
14
20
corev1 "k8s.io/api/core/v1"
@@ -18,6 +24,7 @@ import (
18
24
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
19
25
"k8s.io/apimachinery/pkg/util/wait"
20
26
"sigs.k8s.io/controller-runtime/pkg/client"
27
+ "sigs.k8s.io/controller-runtime/pkg/client/config"
21
28
)
22
29
23
30
func InstallApplication (ocClient client.Client , file string ) error {
@@ -197,3 +204,70 @@ func RunMustGather(oc_cli string, artifact_dir string) error {
197
204
_ , err := cmd .Output ()
198
205
return err
199
206
}
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