Skip to content

Commit

Permalink
Seprate network interfaces unit test
Browse files Browse the repository at this point in the history
Signed-off-by: assafad <aadmi@redhat.com>
  • Loading branch information
assafad committed Apr 16, 2024
1 parent 26b0cf9 commit f42e025
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions pkg/virt-api/webhooks/mutating-webhook/mutators/vmi-mutator_test.go
Expand Up @@ -42,6 +42,7 @@ import (
v1 "kubevirt.io/api/core/v1"

"kubevirt.io/kubevirt/pkg/apimachinery/patch"
kvpointer "kubevirt.io/kubevirt/pkg/pointer"
"kubevirt.io/kubevirt/pkg/testutils"
"kubevirt.io/kubevirt/pkg/virt-api/webhooks"
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"
Expand Down Expand Up @@ -414,42 +415,45 @@ var _ = Describe("VirtualMachineInstance Mutator", func() {
),
)

DescribeTable("should add the default network interface",
func(iface string) {
expectedIface := "bridge"
switch iface {
case "masquerade":
expectedIface = "masquerade"
case "slirp":
expectedIface = "slirp"
}
When("Kubevirt has network-interface defined", func() {
var kvConfig *v1.KubeVirt

permit := true
testutils.UpdateFakeKubeVirtClusterConfig(kvInformer, &v1.KubeVirt{
BeforeEach(func() {
kvConfig = &v1.KubeVirt{
Spec: v1.KubeVirtSpec{
Configuration: v1.KubeVirtConfiguration{
NetworkConfiguration: &v1.NetworkConfiguration{
NetworkInterface: expectedIface,
PermitSlirpInterface: &permit,
},
NetworkConfiguration: &v1.NetworkConfiguration{},
},
},
})
}
})

It("should use the bridge network interface", func() {
kvConfig.Spec.Configuration.NetworkConfiguration.NetworkInterface = "bridge"
testutils.UpdateFakeKubeVirtClusterConfig(kvInformer, kvConfig)

_, vmiSpec, _ := getMetaSpecStatusFromAdmit(rt.GOARCH)
switch expectedIface {
case "bridge":
Expect(vmiSpec.Domain.Devices.Interfaces[0].Bridge).NotTo(BeNil())
case "masquerade":
Expect(vmiSpec.Domain.Devices.Interfaces[0].Masquerade).NotTo(BeNil())
case "slirp":
Expect(vmiSpec.Domain.Devices.Interfaces[0].Slirp).NotTo(BeNil())
}
},
Entry("as bridge", "bridge"),
Entry("as masquerade", "masquerade"),
Entry("as slirp", "slirp"),
)
Expect(vmiSpec.Domain.Devices.Interfaces[0].Bridge).NotTo(BeNil())
})

It("should use the masquerade network interface", func() {
kvConfig.Spec.Configuration.NetworkConfiguration.NetworkInterface = "masquerade"
testutils.UpdateFakeKubeVirtClusterConfig(kvInformer, kvConfig)

_, vmiSpec, _ := getMetaSpecStatusFromAdmit(rt.GOARCH)
Expect(vmiSpec.Domain.Devices.Interfaces[0].Masquerade).NotTo(BeNil())
})

It("should use the slirp network interface", func() {
kvConfig.Spec.Configuration.NetworkConfiguration.NetworkInterface = "slirp"
kvConfig.Spec.Configuration.NetworkConfiguration.PermitSlirpInterface = kvpointer.P(true)
testutils.UpdateFakeKubeVirtClusterConfig(kvInformer, kvConfig)

_, vmiSpec, _ := getMetaSpecStatusFromAdmit(rt.GOARCH)
Expect(vmiSpec.Domain.Devices.Interfaces[0].Slirp).NotTo(BeNil())
})

})

DescribeTable("should not add the default interfaces if", func(interfaces []v1.Interface, networks []v1.Network) {
vmi.Spec.Domain.Devices.Interfaces = append([]v1.Interface{}, interfaces...)
Expand Down

0 comments on commit f42e025

Please sign in to comment.