-
Notifications
You must be signed in to change notification settings - Fork 35
/
roleBinding.go
106 lines (87 loc) · 3.53 KB
/
roleBinding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright 2022 The SODA Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package k8sresources
import (
"context"
"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
log "github.com/sirupsen/logrus"
k8s "github.com/soda-cdm/kahu/test/e2e/util/k8s"
kahu "github.com/soda-cdm/kahu/test/e2e/util/kahu"
)
//testcase for E2E deployment backup and restore
var _ = Describe("roleBackup", Label("role"), func() {
Context("Create backup of role and restore", func() {
It("role", func() {
kubeClient, kahuClient := kahu.Clients()
//Create role to test
ns := kahu.BackupNameSpace
saNamespace := kahu.BackupNameSpace
ctx := context.TODO()
UUIDgen, err := uuid.NewRandom()
Expect(err).To(BeNil())
rolename := "role" + "-" + UUIDgen.String()
serviceAccount := "serviceaccount" + "-" + UUIDgen.String()
rolebinding := "rolebinding" + "-" + UUIDgen.String()
podName := "pod" + "-" + UUIDgen.String()
//create serviceAccount
err = k8s.CreateServiceAccount(ctx, kubeClient, ns, serviceAccount)
Expect(err).To(BeNil())
//create pod with the serviceAccount
pod, err := k8s.CreatePodWithServiceAccount(kubeClient, ns, podName, serviceAccount)
log.Infof("pod with sa is %v\n", pod)
Expect(err).To(BeNil())
err = k8s.CreateRBACWithBindingSAWithRole(ctx, kubeClient, saNamespace, ns, serviceAccount, rolename, rolebinding)
Expect(err).To(BeNil())
//create backup for the role
backupName := "backup" + "role" + "-" + UUIDgen.String()
includeNs := kahu.BackupNameSpace
resourceType := "Pod"
backup, err := kahu.CreateBackup(kahuClient, backupName, includeNs, resourceType)
Expect(err).To(BeNil())
err = kahu.WaitForBackupCreate(kahuClient, backupName)
Expect(err).To(BeNil())
log.Infof("backup %v is done\n", backup)
// create restore for the backup
restoreName := "restore" + "role" + "-" + UUIDgen.String()
nsRestore := kahu.RestoreNameSpace
restore, err := kahu.CreateRestore(kahuClient, restoreName, backupName, includeNs, nsRestore)
log.Debugf("restore1 is %v\n", restore)
Expect(err).To(BeNil())
err = kahu.WaitForRestoreCreate(kahuClient, restoreName)
Expect(err).To(BeNil())
log.Infof("restore is created\n")
//check if the restored role is up
_, err = k8s.GetRole(ctx, kubeClient, rolename, nsRestore)
Expect(err).To(BeNil())
err = k8s.WaitForPodComplete(kubeClient, nsRestore, podName)
Expect(err).To(BeNil())
_, err = k8s.GetRoleBinding(ctx, kubeClient, rolebinding, nsRestore)
Expect(err).To(BeNil())
log.Infof("pod restored is up\n")
//Delete the restore
err = kahu.DeleteRestore(kahuClient, restoreName)
Expect(err).To(BeNil())
err = kahu.WaitForRestoreDelete(kahuClient, restoreName)
Expect(err).To(BeNil())
log.Infof("restore of pod %v is deleted\n", podName)
//Delete the backup
err = kahu.DeleteBackup(kahuClient, backupName)
Expect(err).To(BeNil())
err = kahu.WaitForBackupDelete(kahuClient, backupName)
Expect(err).To(BeNil())
log.Infof("backup of roleBinding %v is deleted\n", podName)
})
})
})