Skip to content

Commit

Permalink
network: Move PodIP status test to network package
Browse files Browse the repository at this point in the history
As part of the test suite refactoring, move the first simple test under
the new network test package. This should help us figure out the desired
format and missing test helpers.

On top of that, this new test covers both masquerade and bridge binding
unlike the original test which was checking only the masquerade binding.

Signed-off-by: Petr Horacek <[email protected]>
  • Loading branch information
phoracek committed Aug 12, 2020
1 parent 911f053 commit c3049f8
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 29 deletions.
16 changes: 14 additions & 2 deletions tests/network/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["placeholder.go"],
srcs = [
"framework.go",
"primary_pod_network.go",
],
importpath = "kubevirt.io/kubevirt/tests/network",
visibility = ["//visibility:public"],
deps = ["//vendor/github.com/onsi/ginkgo:go_default_library"],
deps = [
"//staging/src/kubevirt.io/client-go/api/v1:go_default_library",
"//staging/src/kubevirt.io/client-go/kubecli:go_default_library",
"//tests:go_default_library",
"//tests/containerdisk:go_default_library",
"//vendor/github.com/onsi/ginkgo:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
],
)
10 changes: 7 additions & 3 deletions tests/network/placeholder.go → tests/network/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
. "github.com/onsi/ginkgo"
)

var _ = Describe("Placeholder for tests to come", func() {
It("should run this", func() {})
})
func SIGDescribe(text string, body func()) bool {
return Describe("[sig-network] "+text, body)
}

func FSIGDescribe(text string, body func()) bool {
return FDescribe("[sig-network] "+text, body)
}
146 changes: 146 additions & 0 deletions tests/network/primary_pod_network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* 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 2020 Red Hat, Inc.
*
*/

package network

import (
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

v1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/client-go/kubecli"

"kubevirt.io/kubevirt/tests"
cd "kubevirt.io/kubevirt/tests/containerdisk"
)

var _ = SIGDescribe("Primary Pod Network", func() {
var virtClient kubecli.KubevirtClient

BeforeEach(func() {
var err error
virtClient, err = kubecli.GetKubevirtClient()
Expect(err).NotTo(HaveOccurred(), "Should successfully initialize an API client")
})

Describe("Status", func() {
AssertReportedIP := func(vmi *v1.VirtualMachineInstance) {
By("Getting pod of the VMI")
vmiPod := tests.GetRunningPodByVirtualMachineInstance(vmi, tests.NamespaceTestDefault)

By("Making sure IP reported on the VMI matches the one on the pod")
Expect(vmi.Status.Interfaces[0].IP).To(Equal(vmiPod.Status.PodIP))
Expect(vmi.Status.Interfaces[0].IPs).NotTo(BeEmpty())
Expect(vmi.Status.Interfaces[0].IPs[0]).To(Equal(vmiPod.Status.PodIP))
}

Context("VMI connected to the pod network using the default (implicit) binding", func() {
var vmi *v1.VirtualMachineInstance

BeforeEach(func() {
vmi = setupVMI(virtClient, vmiWithDefaultBinding())
})

AfterEach(func() {
cleanupVMI(virtClient, vmi)
})

It("should report PodIP as its own on interface status", func() { AssertReportedIP(vmi) })
})

Context("VMI connected to the pod network using bridge binding", func() {
var vmi *v1.VirtualMachineInstance

BeforeEach(func() {
vmi = setupVMI(virtClient, vmiWithBridgeBinding())
})

AfterEach(func() {
cleanupVMI(virtClient, vmi)
})

It("should report PodIP as its own on interface status", func() { AssertReportedIP(vmi) })
})

Context("VMI connected to the pod network using masquerade binding", func() {
var vmi *v1.VirtualMachineInstance

BeforeEach(func() {
vmi = setupVMI(virtClient, vmiWithMasqueradeBinding())
})

AfterEach(func() {
cleanupVMI(virtClient, vmi)
})

It("should report PodIP as its own on interface status", func() { AssertReportedIP(vmi) })
})
})
})

func setupVMI(virtClient kubecli.KubevirtClient, vmi *v1.VirtualMachineInstance) *v1.VirtualMachineInstance {
By("Creating the VMI")
var err error
vmi, err = virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Create(vmi)
Expect(err).NotTo(HaveOccurred(), "VMI should be successfully created")

By("Waiting until the VMI gets ready")
vmi = tests.WaitUntilVMIReady(vmi, tests.LoggedInAlpineExpecter)

return vmi
}

func cleanupVMI(virtClient kubecli.KubevirtClient, vmi *v1.VirtualMachineInstance) {
if vmi != nil {
By("Deleting the VMI")
Expect(virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Delete(vmi.GetName(), &metav1.DeleteOptions{})).To(Succeed())

By("Waiting for the VMI to be gone")
Eventually(func() error {
_, err := virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Get(vmi.GetName(), &metav1.GetOptions{})
return err
}, 2*time.Minute, time.Second).Should(SatisfyAll(HaveOccurred(), WithTransform(errors.IsNotFound, BeTrue())), "The VMI should be gone within the given timeout")
}
}

func vmiWithDefaultBinding() *v1.VirtualMachineInstance {
vmi := tests.NewRandomVMIWithEphemeralDisk(cd.ContainerDiskFor(cd.ContainerDiskAlpine))
vmi.Spec.Domain.Devices.Interfaces = nil
vmi.Spec.Networks = nil
return vmi
}

func vmiWithBridgeBinding() *v1.VirtualMachineInstance {
vmi := tests.NewRandomVMIWithEphemeralDisk(cd.ContainerDiskFor(cd.ContainerDiskAlpine))
vmi.Spec.Domain.Devices.Interfaces = []v1.Interface{*v1.DefaultBridgeNetworkInterface()}
vmi.Spec.Networks = []v1.Network{*v1.DefaultPodNetwork()}
return vmi
}

func vmiWithMasqueradeBinding() *v1.VirtualMachineInstance {
vmi := tests.NewRandomVMIWithEphemeralDisk(cd.ContainerDiskFor(cd.ContainerDiskAlpine))
vmi.Spec.Domain.Devices.Interfaces = []v1.Interface{*v1.DefaultMasqueradeNetworkInterface()}
vmi.Spec.Networks = []v1.Network{*v1.DefaultPodNetwork()}
return vmi
}
24 changes: 0 additions & 24 deletions tests/vmi_networking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,30 +844,6 @@ sockfd = None`})
Expect(err.Error()).To(ContainSubstring("Bridge interface is not enabled in kubevirt-config"))
})
})

Context("VirtualMachineInstance connected to the pod network", func() {
var vmi *v1.VirtualMachineInstance

BeforeEach(func() {
tests.BeforeTestCleanup()

vmi = tests.NewRandomVMIWithEphemeralDisk(cd.ContainerDiskFor(cd.ContainerDiskAlpine))
tests.AddExplicitPodNetworkInterface(vmi)

By("Starting tested VMI")
vmi, err = virtClient.VirtualMachineInstance(tests.NamespaceTestDefault).Create(vmi)
Expect(err).NotTo(HaveOccurred(), "VMI should be successfully created")
vmi = tests.WaitUntilVMIReady(vmi, tests.LoggedInAlpineExpecter)
})

It("should report IP address matching the launcher PodIP in its status", func() {
vmiPod := tests.GetRunningPodByVirtualMachineInstance(vmi, tests.NamespaceTestDefault)

Expect(vmi.Status.Interfaces[0].IP).To(Equal(vmiPod.Status.PodIP), "The address reported in VMI's IP should match the PodIP")
Expect(vmi.Status.Interfaces[0].IPs).NotTo(BeEmpty(), "VMI should report a list of its IP addresses")
Expect(vmi.Status.Interfaces[0].IPs[0]).To(Equal(vmiPod.Status.PodIP), "The first address reported in VMI's IPs should match the PodIP")
})
})
})

func waitUntilVMIReady(vmi *v1.VirtualMachineInstance, expecterFactory tests.VMIExpecterFactory) *v1.VirtualMachineInstance {
Expand Down

0 comments on commit c3049f8

Please sign in to comment.