Skip to content

Commit

Permalink
tests, libnet: Move cloudinit to its own package
Browse files Browse the repository at this point in the history
Signed-off-by: Edward Haas <[email protected]>
  • Loading branch information
EdDev committed Feb 7, 2024
1 parent 417acc2 commit 6708f32
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 52 deletions.
3 changes: 2 additions & 1 deletion tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ go_library(
"//tests/framework/kubevirt:go_default_library",
"//tests/framework/matcher:go_default_library",
"//tests/libdv:go_default_library",
"//tests/libnet:go_default_library",
"//tests/libnet/cloudinit:go_default_library",
"//tests/libnode:go_default_library",
"//tests/libpod:go_default_library",
"//tests/libstorage:go_default_library",
Expand Down Expand Up @@ -210,6 +210,7 @@ go_test(
"//tests/libinstancetype:go_default_library",
"//tests/libmigration:go_default_library",
"//tests/libnet:go_default_library",
"//tests/libnet/cloudinit:go_default_library",
"//tests/libnode:go_default_library",
"//tests/libpod:go_default_library",
"//tests/libreplicaset:go_default_library",
Expand Down
2 changes: 0 additions & 2 deletions tests/libnet/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = [
"cloudinit.go",
"dns.go",
"expose_util.go",
"interface.go",
Expand Down Expand Up @@ -33,6 +32,5 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/strategicpatch:go_default_library",
"//vendor/k8s.io/utils/net:go_default_library",
"//vendor/sigs.k8s.io/yaml:go_default_library",
],
)
12 changes: 12 additions & 0 deletions tests/libnet/cloudinit/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["cloudinit.go"],
importpath = "kubevirt.io/kubevirt/tests/libnet/cloudinit",
visibility = ["//visibility:public"],
deps = [
"//tests/libnet:go_default_library",
"//vendor/sigs.k8s.io/yaml:go_default_library",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
*
*/

package libnet
package cloudinit

import (
"fmt"

"kubevirt.io/kubevirt/tests/libnet"

"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -108,13 +110,13 @@ func WithGateway6(gateway6 string) NetworkDataInterfaceOption {

func WithNameserverFromCluster() NetworkDataInterfaceOption {
return func(networkDataInterface *CloudInitInterface) error {
dnsServerIP, err := ClusterDNSServiceIP()
dnsServerIP, err := libnet.ClusterDNSServiceIP()
if err != nil {
return fmt.Errorf("failed defining network data nameservers when retrieving cluster DNS service IP: %w", err)
}
networkDataInterface.Nameservers = CloudInitNameservers{
Addresses: []string{dnsServerIP},
Search: SearchDomains(),
Search: libnet.SearchDomains(),
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion tests/libvmi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ go_library(
"//tests/containerdisk:go_default_library",
"//tests/framework/checks:go_default_library",
"//tests/framework/kubevirt:go_default_library",
"//tests/libnet:go_default_library",
"//tests/libnet/cloudinit:go_default_library",
"//tests/testsuite:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions tests/libvmi/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package libvmi
import (
kvirtv1 "kubevirt.io/api/core/v1"

"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libnet/cloudinit"
)

// WithInterface adds a Domain Device Interface.
Expand All @@ -42,7 +42,7 @@ func WithNetwork(network *kvirtv1.Network) Option {
}

func WithMasqueradeNetworking(ports ...kvirtv1.Port) []Option {
networkData := libnet.CreateDefaultCloudInitNetworkData()
networkData := cloudinit.CreateDefaultCloudInitNetworkData()
return []Option{
WithInterface(InterfaceDeviceWithMasqueradeBinding(ports...)),
WithNetwork(kvirtv1.DefaultPodNetwork()),
Expand Down
1 change: 1 addition & 0 deletions tests/network/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ go_library(
"//tests/libkvconfig:go_default_library",
"//tests/libmigration:go_default_library",
"//tests/libnet:go_default_library",
"//tests/libnet/cloudinit:go_default_library",
"//tests/libnet/cluster:go_default_library",
"//tests/libnet/job:go_default_library",
"//tests/libnet/service:go_default_library",
Expand Down
9 changes: 5 additions & 4 deletions tests/network/bindingplugin_passt.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"kubevirt.io/kubevirt/tests/libkvconfig"
"kubevirt.io/kubevirt/tests/libmigration"
"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libnet/cloudinit"
"kubevirt.io/kubevirt/tests/libvmi"
"kubevirt.io/kubevirt/tests/libwait"
"kubevirt.io/kubevirt/tests/testsuite"
Expand Down Expand Up @@ -405,10 +406,10 @@ func assertSourcePodContainersTerminate(labelSelector, fieldSelector string, vmi
}

func startPasstVMI(vmiBuilder func(opts ...libvmi.Option) *v1.VirtualMachineInstance, loginTo console.LoginToFunction) *v1.VirtualMachineInstance {
networkData, err := libnet.NewNetworkData(
libnet.WithEthernet("eth0",
libnet.WithDHCP4Enabled(),
libnet.WithDHCP6Enabled(),
networkData, err := cloudinit.NewNetworkData(
cloudinit.WithEthernet("eth0",
cloudinit.WithDHCP4Enabled(),
cloudinit.WithDHCP6Enabled(),
),
)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
Expand Down
3 changes: 2 additions & 1 deletion tests/network/primary_pod_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/console"
"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libnet/cloudinit"
"kubevirt.io/kubevirt/tests/libvmi"
"kubevirt.io/kubevirt/tests/libwait"
)
Expand Down Expand Up @@ -186,7 +187,7 @@ func setupVMI(virtClient kubecli.KubevirtClient, vmi *v1.VirtualMachineInstance)
}

func newFedoraWithGuestAgentAndDefaultInterface(iface v1.Interface) (*v1.VirtualMachineInstance, error) {
networkData := libnet.CreateDefaultCloudInitNetworkData()
networkData := cloudinit.CreateDefaultCloudInitNetworkData()

vmi := libvmi.NewFedora(
libvmi.WithInterface(iface),
Expand Down
3 changes: 2 additions & 1 deletion tests/network/sriov.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"kubevirt.io/kubevirt/tests/framework/checks"
"kubevirt.io/kubevirt/tests/framework/matcher"
"kubevirt.io/kubevirt/tests/libnet"
netcloudinit "kubevirt.io/kubevirt/tests/libnet/cloudinit"
"kubevirt.io/kubevirt/tests/libnode"
"kubevirt.io/kubevirt/tests/libvmi"
"kubevirt.io/kubevirt/tests/libwait"
Expand Down Expand Up @@ -649,7 +650,7 @@ func validatePodKubevirtResourceNameByVMI(virtClient kubecli.KubevirtClient, vmi
}

func defaultCloudInitNetworkData() string {
networkData := libnet.CreateDefaultCloudInitNetworkData()
networkData := netcloudinit.CreateDefaultCloudInitNetworkData()
return networkData
}

Expand Down
5 changes: 3 additions & 2 deletions tests/network/vmi_istio.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"kubevirt.io/kubevirt/tests/libkvconfig"
"kubevirt.io/kubevirt/tests/libmigration"
"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libnet/cloudinit"
"kubevirt.io/kubevirt/tests/libnet/job"
netservice "kubevirt.io/kubevirt/tests/libnet/service"
"kubevirt.io/kubevirt/tests/libvmi"
Expand Down Expand Up @@ -341,7 +342,7 @@ var istioTests = func(vmType VmType) {
}

BeforeEach(func() {
networkData := libnet.CreateDefaultCloudInitNetworkData()
networkData := cloudinit.CreateDefaultCloudInitNetworkData()

serverVMI = libvmi.NewAlpineWithTestTooling(
libvmi.WithNetwork(v1.DefaultPodNetwork()),
Expand Down Expand Up @@ -504,7 +505,7 @@ func newVMIWithIstioSidecar(ports []v1.Port, vmType VmType) (*v1.VirtualMachineI
}

func createMasqueradeVm(ports []v1.Port) *v1.VirtualMachineInstance {
networkData := libnet.CreateDefaultCloudInitNetworkData()
networkData := cloudinit.CreateDefaultCloudInitNetworkData()
vmi := libvmi.NewAlpineWithTestTooling(
libvmi.WithNetwork(v1.DefaultPodNetwork()),
libvmi.WithInterface(libvmi.InterfaceDeviceWithMasqueradeBinding(ports...)),
Expand Down
35 changes: 18 additions & 17 deletions tests/network/vmi_multus.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/console"
"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libnet/cloudinit"
"kubevirt.io/kubevirt/tests/libnode"
"kubevirt.io/kubevirt/tests/libvmi"
"kubevirt.io/kubevirt/tests/libwait"
Expand Down Expand Up @@ -193,10 +194,10 @@ var _ = SIGDescribe("[Serial]Multus", Serial, decorators.Multus, func() {
var networkData string
BeforeEach(func() {
libnet.SkipWhenClusterNotSupportIpv4()
networkData, err = libnet.NewNetworkData(
libnet.WithEthernet("eth0",
libnet.WithDHCP4Enabled(),
libnet.WithNameserverFromCluster(),
networkData, err = cloudinit.NewNetworkData(
cloudinit.WithEthernet("eth0",
cloudinit.WithDHCP4Enabled(),
cloudinit.WithNameserverFromCluster(),
),
)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -280,10 +281,10 @@ var _ = SIGDescribe("[Serial]Multus", Serial, decorators.Multus, func() {
Context("VirtualMachineInstance with multus network as default network", func() {
It("[test_id:1751]should create a virtual machine with one interface with multus default network definition", func() {
libnet.SkipWhenClusterNotSupportIpv4()
networkData, err := libnet.NewNetworkData(
libnet.WithEthernet("eth0",
libnet.WithDHCP4Enabled(),
libnet.WithNameserverFromCluster(),
networkData, err := cloudinit.NewNetworkData(
cloudinit.WithEthernet("eth0",
cloudinit.WithDHCP4Enabled(),
cloudinit.WithNameserverFromCluster(),
),
)
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -779,22 +780,22 @@ func checkMacAddress(vmi *v1.VirtualMachineInstance, interfaceName, macAddress s
}

func cloudInitNetworkDataWithStaticIPsByMac(nicName, macAddress, ipAddress string) string {
networkData, err := libnet.NewNetworkData(
libnet.WithEthernet(nicName,
libnet.WithAddresses(ipAddress),
libnet.WithNameserverFromCluster(),
libnet.WithMatchingMAC(macAddress),
networkData, err := cloudinit.NewNetworkData(
cloudinit.WithEthernet(nicName,
cloudinit.WithAddresses(ipAddress),
cloudinit.WithNameserverFromCluster(),
cloudinit.WithMatchingMAC(macAddress),
),
)
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "should successfully create static IPs by mac address cloud init network data")
return networkData
}

func cloudInitNetworkDataWithStaticIPsByDevice(deviceName, ipAddress string) string {
networkData, err := libnet.NewNetworkData(
libnet.WithEthernet(deviceName,
libnet.WithAddresses(ipAddress),
libnet.WithNameserverFromCluster(),
networkData, err := cloudinit.NewNetworkData(
cloudinit.WithEthernet(deviceName,
cloudinit.WithAddresses(ipAddress),
cloudinit.WithNameserverFromCluster(),
),
)
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "should successfully create static IPs by device name cloud init network data")
Expand Down
25 changes: 13 additions & 12 deletions tests/network/vmi_networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import (
"kubevirt.io/kubevirt/tests/events"
"kubevirt.io/kubevirt/tests/flags"
"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libnet/cloudinit"
"kubevirt.io/kubevirt/tests/libvmi"
"kubevirt.io/kubevirt/tests/libwait"
)
Expand Down Expand Up @@ -358,7 +359,7 @@ var _ = SIGDescribe("[rfe_id:694][crit:medium][vendor:[email protected]][level:c
By(checkingEth0MACAddr)
masqIface := libvmi.InterfaceDeviceWithMasqueradeBinding()
masqIface.MacAddress = "de:ad:00:00:be:af"
networkData := libnet.CreateDefaultCloudInitNetworkData()
networkData := cloudinit.CreateDefaultCloudInitNetworkData()

deadbeafVMI := libvmi.NewAlpineWithTestTooling(
libvmi.WithInterface(masqIface),
Expand Down Expand Up @@ -655,13 +656,13 @@ var _ = SIGDescribe("[rfe_id:694][crit:medium][vendor:[email protected]][level:c

fedoraMasqueradeVMI := func(ports []v1.Port, ipv6NetworkCIDR string) (*v1.VirtualMachineInstance, error) {
if ipv6NetworkCIDR == "" {
ipv6NetworkCIDR = libnet.DefaultIPv6CIDR
ipv6NetworkCIDR = cloudinit.DefaultIPv6CIDR
}
networkData, err := libnet.NewNetworkData(
libnet.WithEthernet("eth0",
libnet.WithDHCP4Enabled(),
libnet.WithAddresses(ipv6NetworkCIDR),
libnet.WithGateway6(gatewayIPFromCIDR(ipv6NetworkCIDR)),
networkData, err := cloudinit.NewNetworkData(
cloudinit.WithEthernet("eth0",
cloudinit.WithDHCP4Enabled(),
cloudinit.WithAddresses(ipv6NetworkCIDR),
cloudinit.WithGateway6(gatewayIPFromCIDR(ipv6NetworkCIDR)),
),
)
if err != nil {
Expand Down Expand Up @@ -976,11 +977,11 @@ var _ = SIGDescribe("[rfe_id:694][crit:medium][vendor:[email protected]][level:c
var err error

By("Create masquerade VMI")
networkData, err := libnet.NewNetworkData(
libnet.WithEthernet("eth0",
libnet.WithDHCP4Enabled(),
libnet.WithDHCP6Enabled(),
libnet.WithAddresses(""), // This is a workaround o make fedora client to configure local IPv6
networkData, err := cloudinit.NewNetworkData(
cloudinit.WithEthernet("eth0",
cloudinit.WithDHCP4Enabled(),
cloudinit.WithDHCP6Enabled(),
cloudinit.WithAddresses(""), // This is a workaround o make fedora client to configure local IPv6
),
)
Expect(err).ToNot(HaveOccurred())
Expand Down
6 changes: 3 additions & 3 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import (
"kubevirt.io/kubevirt/tests/flags"
. "kubevirt.io/kubevirt/tests/framework/matcher"
"kubevirt.io/kubevirt/tests/libdv"
"kubevirt.io/kubevirt/tests/libnet"
"kubevirt.io/kubevirt/tests/libnet/cloudinit"
"kubevirt.io/kubevirt/tests/libnode"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libstorage"
Expand Down Expand Up @@ -626,7 +626,7 @@ func AddEphemeralDisk(vmi *v1.VirtualMachineInstance, name string, bus v1.DiskBu
//
// Deprecated: Use libvmi directly
func NewRandomFedoraVMI(opts ...libvmi.Option) *v1.VirtualMachineInstance {
networkData := libnet.CreateDefaultCloudInitNetworkData()
networkData := cloudinit.CreateDefaultCloudInitNetworkData()

return libvmi.NewFedora(append([]libvmi.Option{
libvmi.WithInterface(libvmi.InterfaceDeviceWithMasqueradeBinding()),
Expand All @@ -640,7 +640,7 @@ func NewRandomFedoraVMI(opts ...libvmi.Option) *v1.VirtualMachineInstance {
//
// Deprecated: Use libvmi directly
func NewRandomFedoraVMIWithBlacklistGuestAgent(commands string) *v1.VirtualMachineInstance {
networkData := libnet.CreateDefaultCloudInitNetworkData()
networkData := cloudinit.CreateDefaultCloudInitNetworkData()

return libvmi.NewFedora(
libvmi.WithInterface(libvmi.InterfaceDeviceWithMasqueradeBinding()),
Expand Down
5 changes: 2 additions & 3 deletions tests/vm_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

"kubevirt.io/kubevirt/tests/testsuite"

"kubevirt.io/kubevirt/tests/libnet"

"kubevirt.io/kubevirt/tests/libvmi"

expect "github.com/google/goexpect"
Expand All @@ -26,6 +24,7 @@ import (
"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/console"
"kubevirt.io/kubevirt/tests/decorators"
"kubevirt.io/kubevirt/tests/libnet/cloudinit"
"kubevirt.io/kubevirt/tests/libwait"
)

Expand Down Expand Up @@ -121,7 +120,7 @@ var _ = Describe("[sig-storage]VM state", decorators.SigStorage, decorators.Requ
vmi := libvmi.NewFedora(
libvmi.WithInterface(libvmi.InterfaceDeviceWithMasqueradeBinding()),
libvmi.WithNetwork(v1.DefaultPodNetwork()),
libvmi.WithCloudInitNoCloudNetworkData(libnet.CreateDefaultCloudInitNetworkData()),
libvmi.WithCloudInitNoCloudNetworkData(cloudinit.CreateDefaultCloudInitNetworkData()),
libvmi.WithUefi(false),
libvmi.WithResourceMemory("1Gi"),
)
Expand Down

0 comments on commit 6708f32

Please sign in to comment.