Skip to content

Commit

Permalink
add functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ksimon1 committed Mar 4, 2019
1 parent 6032ec0 commit c321929
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 11 deletions.
1 change: 1 addition & 0 deletions pkg/virt-api/webhooks/mutating-webhook/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
deps = [
"//pkg/api/v1:go_default_library",
"//pkg/log:go_default_library",
"//pkg/util:go_default_library",
"//pkg/virt-api/webhooks:go_default_library",
"//vendor/k8s.io/api/admission/v1beta1:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
Expand Down
8 changes: 6 additions & 2 deletions pkg/virt-api/webhooks/mutating-webhook/preset.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ import (

kubev1 "kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/log"
"kubevirt.io/kubevirt/pkg/util"
)

const (
exclusionMarking = "virtualmachineinstancepresets.admission.kubevirt.io/exclude"
namespaceKubevirt = "kubevirt"
configMapName = "kubevirt-config"
defaultCPUModelKey = "default-cpu-model"
)
Expand Down Expand Up @@ -285,8 +285,12 @@ func isVMIExcluded(vmi *kubev1.VirtualMachineInstance) bool {
func setDefaultCPUModel(vmi *kubev1.VirtualMachineInstance, configMapStore cache.Store) {
//if vmi doesn't have cpu topology or cpu model set
if vmi.Spec.Domain.CPU == nil || vmi.Spec.Domain.CPU.Model == "" {
namespace, err := util.GetNamespace()
if err != nil {
return
}
// if default cluster cpu model is defined
if obj, exists, err := configMapStore.GetByKey(namespaceKubevirt + "/" + configMapName); err == nil && exists {
if obj, exists, err := configMapStore.GetByKey(namespace + "/" + configMapName); err == nil && exists {
if obj.(*k8sv1.ConfigMap).Data[defaultCPUModelKey] != "" {
// create cpu topology struct
if vmi.Spec.Domain.CPU == nil {
Expand Down
101 changes: 92 additions & 9 deletions tests/vmi_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import (
"kubevirt.io/kubevirt/tests"
)

const kubevirtConfig = "kubevirt-config"

func newCirrosVMI() *v1.VirtualMachineInstance {
return tests.NewRandomVMIWithEphemeralDiskAndUserdata(tests.ContainerDiskFor(tests.ContainerDiskCirros), "#!/bin/bash\necho 'hello'\n")
}
Expand Down Expand Up @@ -632,6 +634,91 @@ var _ = Describe("[rfe_id:273][crit:high][vendor:[email protected]][level:compon

})

Context("with default cpu model", func() {
var cfgMap *k8sv1.ConfigMap
var originalData map[string]string
var options metav1.GetOptions
var defaultCPUModelKey = "default-cpu-model"
var defaultCPUModel = "Conroe"
var vmiCPUModel = "SandyBridge"

//store old kubevirt-config
BeforeEach(func() {
cfgMap, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Get(kubevirtConfig, options)
Expect(err).ToNot(HaveOccurred())
originalData = cfgMap.Data
})

//replace new kubevirt-config with old config
AfterEach(func() {
cfgMap, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Get(kubevirtConfig, options)
Expect(err).ToNot(HaveOccurred())
cfgMap.Data = originalData
_, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Update(cfgMap)
Expect(err).ToNot(HaveOccurred())
time.Sleep(5 * time.Second)
})

It("should set default cpu model when vmi doesn't have it set", func() {
cfgMap, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Get(kubevirtConfig, options)
Expect(err).ToNot(HaveOccurred(), "Expect config map to be loaded without error")

cfgMap.Data[defaultCPUModelKey] = defaultCPUModel
_, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Update(cfgMap)
Expect(err).ToNot(HaveOccurred(), "Expect config map to be updated without error")

time.Sleep(5 * time.Second)

vmi := tests.NewRandomVMIWithEphemeralDiskAndUserdata(tests.ContainerDiskFor(tests.ContainerDiskCirros), "#!/bin/bash\necho 'hello'\n")

_, err = virtClient.VirtualMachineInstance(vmi.Namespace).Create(vmi)
Expect(err).ToNot(HaveOccurred(), "Should create VMI")
tests.WaitForSuccessfulVMIStart(vmi)

curVMI, err := virtClient.VirtualMachineInstance(vmi.Namespace).Get(vmi.Name, &metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred(), "Should get VMI")
Expect(curVMI.Spec.Domain.CPU.Model).To(Equal("Conroe"), "Expected default CPU model")

})

It("should not set default cpu model when vmi has it set", func() {
cfgMap, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Get(kubevirtConfig, options)
Expect(err).ToNot(HaveOccurred(), "Expect config map to be loaded without error")

cfgMap.Data[defaultCPUModelKey] = defaultCPUModel

_, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Update(cfgMap)
Expect(err).ToNot(HaveOccurred(), "Expect config map to be updated without error")

time.Sleep(5 * time.Second)

vmi := tests.NewRandomVMIWithEphemeralDiskAndUserdata(tests.ContainerDiskFor(tests.ContainerDiskCirros), "#!/bin/bash\necho 'hello'\n")
vmi.Spec.Domain.CPU = &v1.CPU{
Model: vmiCPUModel,
}
_, err = virtClient.VirtualMachineInstance(vmi.Namespace).Create(vmi)
Expect(err).ToNot(HaveOccurred(), "Should create VMI")
tests.WaitForSuccessfulVMIStart(vmi)

curVMI, err := virtClient.VirtualMachineInstance(vmi.Namespace).Get(vmi.Name, &metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred(), "Should get VMI")
Expect(curVMI.Spec.Domain.CPU.Model).To(Equal(vmiCPUModel), "Expected vmi CPU model")

})

It("should not set cpu model when vmi does not have it set and default cpu model is not set", func() {
vmi := tests.NewRandomVMIWithEphemeralDiskAndUserdata(tests.ContainerDiskFor(tests.ContainerDiskCirros), "#!/bin/bash\necho 'hello'\n")
_, err = virtClient.VirtualMachineInstance(vmi.Namespace).Create(vmi)
Expect(err).ToNot(HaveOccurred(), "Should create VMI")

tests.WaitForSuccessfulVMIStart(vmi)

curVMI, err := virtClient.VirtualMachineInstance(vmi.Namespace).Get(vmi.Name, &metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred(), "Should get VMI")
Expect(curVMI.Spec.Domain.CPU).To(BeNil(), "Expected CPU to be nil")
})
})

Context("with node feature discovery", func() {

var options metav1.GetOptions
Expand All @@ -648,20 +735,17 @@ var _ = Describe("[rfe_id:273][crit:high][vendor:[email protected]][level:compon
originalLabels = node.GetObjectMeta().GetLabels()

options = metav1.GetOptions{}
cfgMap, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Get("kubevirt-config", options)
cfgMap, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Get(kubevirtConfig, options)
Expect(err).ToNot(HaveOccurred())
originalFeatureGates = cfgMap.Data[virtconfig.FeatureGatesKey]
cfgMap.Data[virtconfig.FeatureGatesKey] = virtconfig.CPUNodeDiscoveryGate
_, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Update(cfgMap)
Expect(err).ToNot(HaveOccurred())

//FIXME improve the detection if virt-controller already received the config-map change
time.Sleep(time.Millisecond * 500)

time.Sleep(5 * time.Second)
})

AfterEach(func() {
cfgMap, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Get("kubevirt-config", options)
cfgMap, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Get(kubevirtConfig, options)
Expect(err).ToNot(HaveOccurred())
cfgMap.Data[virtconfig.FeatureGatesKey] = originalFeatureGates
_, err = virtClient.CoreV1().ConfigMaps(namespaceKubevirt).Update(cfgMap)
Expand All @@ -673,8 +757,7 @@ var _ = Describe("[rfe_id:273][crit:high][vendor:[email protected]][level:compon
_, err = virtClient.CoreV1().Nodes().Update(n)
Expect(err).ToNot(HaveOccurred())

//FIXME improve the detection if virt-controller already received the config-map change
time.Sleep(time.Millisecond * 500)
time.Sleep(5 * time.Second)
})

It("[test_id:1639]the vmi with cpu.model matching a nfd label on a node should be scheduled", func() {
Expand Down Expand Up @@ -1339,7 +1422,7 @@ var _ = Describe("[rfe_id:273][crit:high][vendor:[email protected]][level:compon
func shouldUseEmulation(virtClient kubecli.KubevirtClient) bool {
useEmulation := false
options := metav1.GetOptions{}
cfgMap, err := virtClient.CoreV1().ConfigMaps(tests.KubeVirtInstallNamespace).Get("kubevirt-config", options)
cfgMap, err := virtClient.CoreV1().ConfigMaps(tests.KubeVirtInstallNamespace).Get(kubevirtConfig, options)
if err == nil {
val, ok := cfgMap.Data["debug.useEmulation"]
useEmulation = ok && (val == "true")
Expand Down

0 comments on commit c321929

Please sign in to comment.