This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathrbac_test.go
215 lines (175 loc) · 11 KB
/
rbac_test.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package tests_test
import (
"flag"
"fmt"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
tests "kubevirt.io/kubevirt-ansible/tests/framework"
"kubevirt.io/kubevirt/pkg/kubecli"
ktests "kubevirt.io/kubevirt/tests"
)
var _ = Describe("RBAC", func() {
flag.Parse()
var vm tests.VirtualMachine
const NamespaceTestSystem = "kubevirt-test-system"
vm.Name = "vm-cirros"
vm.Manifest = "tests/manifests/vm-cirros.yaml"
virtClient, err := kubecli.GetKubevirtClient()
ktests.PanicOnError(err)
BeforeEach(func() {
By(fmt.Sprintf("Login with the user %s", tests.UsernameAdminUser))
_, _, err := ktests.RunCommandWithNS("", "oc", "login", "-u", tests.UsernameAdminUser, "-p", "123456")
Expect(err).ToNot(HaveOccurred(), "Should login as %s user", tests.UsernameAdminUser)
ktests.BeforeTestCleanup()
By("Creating a user")
err = tests.CreateUserWithParameter(tests.UsernameTestUser)
Expect(err).ToNot(HaveOccurred(), "Creating user %s should not fail: %v", tests.UsernameTestUser, err)
})
AfterEach(func() {
By(fmt.Sprintf("Login with the user %s", tests.UsernameAdminUser))
_, _, err := ktests.RunCommandWithNS("", "oc", "login", "-u", tests.UsernameAdminUser, "-p", "123456")
Expect(err).ToNot(HaveOccurred(), "Should login as %s user", tests.UsernameAdminUser)
By("Deleting a user")
err = tests.DeleteUserWithParameter(tests.UsernameTestUser)
Expect(err).ToNot(HaveOccurred(), "Deleting user %s should not fail: %v", tests.UsernameTestUser, err)
By(fmt.Sprintf("Deleting the identity associated with %s user", tests.UsernameTestUser))
_, _, err = ktests.RunCommandWithNS("", "oc", "delete", "identity", "allow_all_auth:"+tests.UsernameTestUser)
Expect(err).ToNot(HaveOccurred(), "Should delete identity of the %s user", tests.UsernameTestUser)
})
Describe("RBAC with default permission", func() {
It("should allow to access subresource endpoint.", func() {
By(fmt.Sprintf("Login with the user %s", tests.UsernameTestUser))
_, _, err := ktests.RunCommandWithNS("", "oc", "login", "-u", tests.UsernameTestUser, "-p", "123456")
Expect(err).ToNot(HaveOccurred(), "Should login as %s user", tests.UsernameTestUser)
By("Creating a project/namespace")
err = tests.CreateNamespaceWithParameter(NamespaceTestSystem)
Expect(err).ToNot(HaveOccurred(), "Should create %s project/namespace: %v", NamespaceTestSystem, err)
By("Creating a VM from manifest")
_, _, err = ktests.RunCommandWithNS(NamespaceTestSystem, "oc", "create", "-f", vm.Manifest)
Expect(err).ToNot(HaveOccurred(), "Should create the VM %q in %s namespace", vm.Manifest, NamespaceTestSystem)
By("Starting the VM")
_, _, err = ktests.RunCommandWithNS(NamespaceTestSystem, "virtctl", "start", vm.Name)
Expect(err).ToNot(HaveOccurred(), "Should schedule the VM %q in %s namespace to start", vm.Name, NamespaceTestSystem)
By("Checking if the VMI is running")
Eventually(func() string {
output, _, err := ktests.RunCommandWithNS(NamespaceTestSystem, "oc", "get", "vmi", vm.Name, "--template", "{{.status.phase}}")
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "Should get the phase of the VMI %q in %s namespace: output %q: %v", vm.Name, NamespaceTestSystem, output, err)
return output
}, time.Minute*10, time.Second*1).Should(Equal("Running"), "The VMI %q in %s namespace should reach \"Running\" phase within 10 minutes", vm.Name, NamespaceTestSystem)
By("Checking if the VMI's console is accessible")
vmi, err := virtClient.VirtualMachineInstance(NamespaceTestSystem).Get(vm.Name, &metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred(), "Should be able to fetch the object of the VMI %q in %s namespace", vm.Name, NamespaceTestSystem)
expecter, err := ktests.LoggedInCirrosExpecter(vmi)
Expect(err).ToNot(HaveOccurred(), "Should be able to access the console of the VMI %q in %s namespace", vm.Name, NamespaceTestSystem)
expecter.Close()
By("Checking if the VMI's VNC server gives the valid response")
response, err := tests.VNCConnection(NamespaceTestSystem, vm.Name)
Expect(err).ToNot(HaveOccurred(), "Should open VNC connection to VMI %q in %s namespace", vm.Name, NamespaceTestSystem)
Expect(response).To(Equal("RFB 003.008"), "Should receive valid response from the VNC connection to the VMI %q in %s namespace", vm.Name, NamespaceTestSystem)
By("Stopping the VM")
_, _, err = ktests.RunCommandWithNS(NamespaceTestSystem, "virtctl", "stop", vm.Name)
Expect(err).ToNot(HaveOccurred(), "VM %q in %s namespace should be scheduled to stop: %v", vm.Name, NamespaceTestSystem, err)
By("Deleting the VM specified by manifest")
_, _, err = ktests.RunCommandWithNS(NamespaceTestSystem, "oc", "delete", "-f", vm.Manifest)
Expect(err).ToNot(HaveOccurred(), "VM %q in %s namespace should be deleted", vm.Name, NamespaceTestSystem)
By("Deleting the namespace")
err = tests.RemoveNamespaceWithParameter(NamespaceTestSystem)
Expect(err).ToNot(HaveOccurred(), "Namespace %s should be deleted: %v", NamespaceTestSystem, err)
})
})
DescribeTable("RBAC Authorization", func(role, namespace string, con bool) {
By(fmt.Sprintf("Add %s to user %s", role, tests.UsernameTestUser))
_, _, err := ktests.RunCommandWithNS(namespace, "oc", "adm", "policy", "add-role-to-user", role, tests.UsernameTestUser)
Expect(err).ToNot(HaveOccurred(), "Role %s should be added to %s user", role, tests.UsernameTestUser)
if con == false {
createResourcesToTestViewRole()
}
By(fmt.Sprintf("Login with the user %s", tests.UsernameTestUser))
_, _, err = ktests.RunCommandWithNS("", "oc", "login", "-u", tests.UsernameTestUser, "-p", "123456")
Expect(err).ToNot(HaveOccurred(), "Should login as %s user", tests.UsernameTestUser)
By("Creating a VM from manifest")
_, _, err = ktests.RunCommandWithNS(namespace, "oc", "create", "-f", vm.Manifest)
if con == true {
Expect(err).ToNot(HaveOccurred(), "Should schedule the VM %q in %s namespace to start", vm.Name, namespace)
} else {
Expect(err).To(HaveOccurred(), "Should not create the VM %q in %s namespace", vm.Manifest, namespace)
}
By("Starting the VM")
_, _, err = ktests.RunCommandWithNS(namespace, "virtctl", "start", vm.Name)
if con == true {
Expect(err).ToNot(HaveOccurred(), "Should schedule the VM %q in %s namespace to start", vm.Name, namespace)
} else {
Expect(err).To(HaveOccurred(), "Should not schedule the VM %q in %s namespace to start", vm.Name, namespace)
}
By("Checking if the VMI is running")
getVMIStatus := func() string {
output, _, err := ktests.RunCommandWithNS(namespace, "oc", "get", "vmi", vm.Name, "--template", "{{.status.phase}}")
if con == true {
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "Should get the phase of the VMI %q in %s namespace: output %s: %v", vm.Name, namespace, output, err)
} else {
ExpectWithOffset(1, err).To(HaveOccurred(), "The phase of the VMI %q in %s namespace shouldn't be accessible: output %s", vm.Name, namespace, output)
}
return output
}
if con == true {
Eventually(getVMIStatus, 10*time.Minute, 1*time.Second).Should(Equal("Running"), "The VMI %q in %s namespace should reach the \"Running\" phase within 10 minutes", vm.Name, namespace)
}
By("Checking if the VMI's console is accessible")
vmi, err := virtClient.VirtualMachineInstance(namespace).Get(vm.Name, &metav1.GetOptions{})
if con == true {
expecter, err := ktests.LoggedInCirrosExpecter(vmi)
Expect(err).ToNot(HaveOccurred(), "Should be able to login to the console of the VMI %q in %s namespace", vm.Name, namespace)
expecter.Close()
} else {
_, err = ktests.LoggedInCirrosExpecter(vmi)
Expect(err).To(HaveOccurred(), "Should not login to the console of the VMI %q in %s namespace", vm.Name, namespace)
}
By("Checking if the VMI's VNC server gives the valid response")
response, err := tests.VNCConnection(namespace, vm.Name)
if con == true {
Expect(err).ToNot(HaveOccurred(), "Should open VNC connection to VMI %q in %s namespace", vm.Name, namespace)
Expect(response).To(Equal("RFB 003.008"), "Should receive a valid VNC connection response from the VMI %q in %s namespace", vm.Name, namespace)
} else {
Expect(err).To(HaveOccurred(), "Should not open VNC connection to VMI %q in %s namespace", vm.Name, namespace)
Expect(response).ToNot(Equal("RFB 003.008"), "Should not receive a valid response from the VNC connection to the VMI %q in %s namespace", vm.Name, namespace)
}
By("Check if we can stop VM ")
_, _, err = ktests.RunCommandWithNS(namespace, "virtctl", "stop", vm.Name)
if con == true {
Expect(err).ToNot(HaveOccurred(), "The VM %q in %s namespace should be scheduled to stop: %v", vm.Name, namespace, err)
} else {
Expect(err).To(HaveOccurred(), "The VM %q in %s namespace should not be scheduled to stop", vm.Name, namespace)
}
By("Deleting the VM specified by manifest")
_, _, err = ktests.RunCommandWithNS(namespace, "oc", "delete", "-f", vm.Manifest)
if con == true {
Expect(err).ToNot(HaveOccurred(), "The VM %q in %s namespace should be deleted", vm.Name, namespace)
} else {
Expect(err).To(HaveOccurred(), "The VM %q in %s namespace should not be deleted", vm.Name, namespace)
}
},
Entry("with admin permission should allow to access subresource endpoint", "admin", ktests.NamespaceTestDefault, true),
Entry("with edit permission should allow to access subresource endpoint", "edit", ktests.NamespaceTestDefault, true))
// TODO: Investigate fix in kubevirt/kubevirt. Regression occured in when moving to kubevirt-0.13.0 - https://github.com/kubevirt/kubevirt-ansible/pull/556
// Entry("with view permission should not allow to access subresource endpoint", "view", ktests.NamespaceTestAlternative, false))
})
func createResourcesToTestViewRole() {
// function will setup the enviroment to test view role.
var vm tests.VirtualMachine
vm.Name = "vm-cirros"
vm.Manifest = "tests/manifests/vm-cirros.yaml"
_, _, err := ktests.RunCommandWithNS("", "oc", "login", "-u", tests.UsernameAdminUser, "-p", "123456")
Expect(err).ToNot(HaveOccurred(), "Should login as %s user", tests.UsernameAdminUser)
_, _, err = ktests.RunCommandWithNS(ktests.NamespaceTestAlternative, "oc", "create", "-f", vm.Manifest)
Expect(err).ToNot(HaveOccurred(), "Should create VM %q in %s namespace", vm.Name, ktests.NamespaceTestAlternative)
_, _, err = ktests.RunCommandWithNS(ktests.NamespaceTestAlternative, "virtctl", "start", vm.Name)
Expect(err).ToNot(HaveOccurred(), "Should schedule VM %q in %s namespace to start", vm.Name, ktests.NamespaceTestAlternative)
Eventually(func() string {
output, _, err := ktests.RunCommandWithNS(ktests.NamespaceTestAlternative, "oc", "get", "vmi", vm.Name, "--template", "{{.status.phase}}")
Expect(err).ToNot(HaveOccurred(), "Should get the phase of the VMI %q in %s namespace: output %s: %v", vm.Name, ktests.NamespaceTestAlternative, output, err)
return output
}, time.Minute*10, time.Second*1).Should(Equal("Running"), "The VMI %q in %s namespace should reach the \"Running\" phase within 10 minutes", vm.Name, ktests.NamespaceTestAlternative)
}