forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
subresource_api_test.go
267 lines (224 loc) · 9.26 KB
/
subresource_api_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright 2018 Red Hat, Inc.
*
*/
package tests_test
import (
"fmt"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
k8sv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/flags"
)
var _ = Describe("Subresource Api", func() {
var err error
var virtCli kubecli.KubevirtClient
manual := v1.RunStrategyManual
restartOnError := v1.RunStrategyRerunOnFailure
BeforeEach(func() {
virtCli, err = kubecli.GetKubevirtClient()
tests.PanicOnError(err)
tests.BeforeTestCleanup()
})
Describe("[rfe_id:1195][crit:medium][vendor:[email protected]][level:component] Rbac Authorization", func() {
resource := "virtualmachineinstances"
Context("with correct permissions", func() {
It("[test_id:3170]should be allowed to access subresource endpoint", func() {
testClientJob(virtCli, true, resource)
}, 15)
})
Context("Without permissions", func() {
It("[test_id:3171]should not be able to access subresource endpoint", func() {
testClientJob(virtCli, false, resource)
}, 15)
})
})
Describe("[rfe_id:1195][crit:medium][vendor:[email protected]][level:component] Rbac Authorization For Version Command", func() {
resource := "version"
Context("with authenticated user", func() {
It("[test_id:3172]should be allowed to access subresource version endpoint", func() {
testClientJob(virtCli, true, resource)
}, 15)
})
Context("Without permissions", func() {
It("[test_id:3173]should be able to access subresource version endpoint", func() {
testClientJob(virtCli, false, resource)
}, 15)
})
})
Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:component] VirtualMachine subresource", func() {
Context("with a restart endpoint", func() {
It("[test_id:1304] should restart a VM", func() {
vm := tests.NewRandomVirtualMachine(tests.NewRandomVMI(), false)
vm, err := virtCli.VirtualMachine(tests.NamespaceTestDefault).Create(vm)
Expect(err).NotTo(HaveOccurred())
tests.StartVirtualMachine(vm)
vmi, err := virtCli.VirtualMachineInstance(tests.NamespaceTestDefault).Get(vm.Name, &metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(vmi.Status.Phase).To(Equal(v1.Running))
err = virtCli.VirtualMachine(tests.NamespaceTestDefault).Restart(vm.Name)
Expect(err).NotTo(HaveOccurred())
Eventually(func() v1.VirtualMachineInstancePhase {
newVMI, err := virtCli.VirtualMachineInstance(tests.NamespaceTestDefault).Get(vm.Name, &metav1.GetOptions{})
if err != nil || vmi.UID == newVMI.UID {
return v1.VmPhaseUnset
}
return newVMI.Status.Phase
}, 90*time.Second, 1*time.Second).Should(Equal(v1.Running))
})
It("[test_id:1305][posneg:negative] should return an error when VM is not running", func() {
vm := tests.NewRandomVirtualMachine(tests.NewRandomVMI(), false)
vm, err := virtCli.VirtualMachine(tests.NamespaceTestDefault).Create(vm)
Expect(err).NotTo(HaveOccurred())
err = virtCli.VirtualMachine(tests.NamespaceTestDefault).Restart(vm.Name)
Expect(err).To(HaveOccurred())
})
It("[test_id:2265][posneg:negative] should return an error when VM has not been found but VMI is running", func() {
vmi := tests.NewRandomVMI()
tests.RunVMIAndExpectLaunch(vmi, 60)
err := virtCli.VirtualMachine(tests.NamespaceTestDefault).Restart(vmi.Name)
Expect(err).To(HaveOccurred())
})
})
Context("With manual RunStrategy", func() {
It("[test_id:3174]Should not restart when VM is not running", func() {
vm := tests.NewRandomVirtualMachine(tests.NewRandomVMI(), false)
vm.Spec.RunStrategy = &manual
vm.Spec.Running = nil
By("Creating VM")
vm, err := virtCli.VirtualMachine(tests.NamespaceTestDefault).Create(vm)
Expect(err).ToNot(HaveOccurred())
By("Trying to start VM via Restart subresource")
err = virtCli.VirtualMachine(tests.NamespaceTestDefault).Restart(vm.Name)
Expect(err).To(HaveOccurred())
})
It("[test_id:3175]Should restart when VM is running", func() {
vm := tests.NewRandomVirtualMachine(tests.NewRandomVMI(), false)
vm.Spec.RunStrategy = &manual
vm.Spec.Running = nil
By("Creating VM")
vm, err := virtCli.VirtualMachine(tests.NamespaceTestDefault).Create(vm)
Expect(err).ToNot(HaveOccurred())
By("Starting VM via Start subresource")
err = virtCli.VirtualMachine(tests.NamespaceTestDefault).Start(vm.Name)
Expect(err).ToNot(HaveOccurred())
By("Waiting for VMI to start")
Eventually(func() v1.VirtualMachineInstancePhase {
newVMI, err := virtCli.VirtualMachineInstance(tests.NamespaceTestDefault).Get(vm.Name, &metav1.GetOptions{})
if err != nil {
return v1.VmPhaseUnset
}
return newVMI.Status.Phase
}, 90*time.Second, 1*time.Second).Should(Equal(v1.Running))
vmi, err := virtCli.VirtualMachineInstance(tests.NamespaceTestDefault).Get(vm.Name, &metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
By("Restarting VM")
err = virtCli.VirtualMachine(tests.NamespaceTestDefault).Restart(vm.Name)
Expect(err).ToNot(HaveOccurred())
Eventually(func() v1.VirtualMachineInstancePhase {
newVMI, err := virtCli.VirtualMachineInstance(tests.NamespaceTestDefault).Get(vm.Name, &metav1.GetOptions{})
if err != nil || vmi.UID == newVMI.UID {
return v1.VmPhaseUnset
}
return newVMI.Status.Phase
}, 90*time.Second, 1*time.Second).Should(Equal(v1.Running))
})
})
Context("With RunStrategy RerunOnFailure", func() {
It("[test_id:3176]Should restart the VM", func() {
vm := tests.NewRandomVirtualMachine(tests.NewRandomVMI(), false)
vm.Spec.RunStrategy = &restartOnError
vm.Spec.Running = nil
By("Creating VM")
vm, err := virtCli.VirtualMachine(tests.NamespaceTestDefault).Create(vm)
Expect(err).ToNot(HaveOccurred())
By("Waiting for VMI to start")
Eventually(func() v1.VirtualMachineInstancePhase {
newVMI, err := virtCli.VirtualMachineInstance(tests.NamespaceTestDefault).Get(vm.Name, &metav1.GetOptions{})
if err != nil {
return v1.VmPhaseUnset
}
return newVMI.Status.Phase
}, 90*time.Second, 1*time.Second).Should(Equal(v1.Running))
vmi, err := virtCli.VirtualMachineInstance(tests.NamespaceTestDefault).Get(vm.Name, &metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
By("Restarting VM")
err = virtCli.VirtualMachine(tests.NamespaceTestDefault).Restart(vm.Name)
Expect(err).ToNot(HaveOccurred())
Eventually(func() v1.VirtualMachineInstancePhase {
newVMI, err := virtCli.VirtualMachineInstance(tests.NamespaceTestDefault).Get(vm.Name, &metav1.GetOptions{})
if err != nil || vmi.UID == newVMI.UID {
return v1.VmPhaseUnset
}
return newVMI.Status.Phase
}, 90*time.Second, 1*time.Second).Should(Equal(v1.Running))
})
})
})
Describe("[rfe_id:1195][crit:medium][vendor:[email protected]][level:component] the openapi spec for the subresources", func() {
It("[test_id:3177]should be aggregated into the apiserver openapi spec", func() {
Eventually(func() string {
spec, err := virtCli.RestClient().Get().AbsPath("/openapi/v2").DoRaw()
Expect(err).ToNot(HaveOccurred())
return string(spec)
// The first item in the SubresourceGroupVersions array is the preferred version
}, 60*time.Second, 1*time.Second).Should(ContainSubstring("subresources.kubevirt.io/" + v1.SubresourceGroupVersions[0].Version))
})
})
})
func testClientJob(virtCli kubecli.KubevirtClient, withServiceAccount bool, resource string) {
namespace := tests.NamespaceTestDefault
expectedPhase := k8sv1.PodFailed
name := "subresource-access-tester"
job := &k8sv1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: name,
Labels: map[string]string{
v1.AppLabel: tests.SubresourceTestLabel,
},
},
Spec: k8sv1.PodSpec{
RestartPolicy: k8sv1.RestartPolicyNever,
Containers: []k8sv1.Container{
{
Name: name,
Image: fmt.Sprintf("%s/subresource-access-test:%s", flags.KubeVirtUtilityRepoPrefix, flags.KubeVirtUtilityVersionTag),
Command: []string{"/subresource-access-test", resource},
},
},
},
}
if withServiceAccount {
job.Spec.ServiceAccountName = tests.SubresourceServiceAccountName
expectedPhase = k8sv1.PodSucceeded
} else if resource == "version" {
expectedPhase = k8sv1.PodSucceeded
}
pod, err := virtCli.CoreV1().Pods(namespace).Create(job)
Expect(err).ToNot(HaveOccurred())
getStatus := func() k8sv1.PodPhase {
pod, err := virtCli.CoreV1().Pods(namespace).Get(pod.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return pod.Status.Phase
}
Eventually(getStatus, 30, 0.5).Should(Equal(expectedPhase))
}