From 699ea72036b10ef05d5f22951d0c8c98c4cbf969 Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Wed, 18 Oct 2023 10:33:51 -0400 Subject: [PATCH] utils_virtual: unit tests Co-Authored-By: Andrea Panattoni Co-Authored-By: Emilien Macchi --- pkg/utils/testdata/meta_data.json | 25 ++++++++++ pkg/utils/testdata/network_data.json | 71 ++++++++++++++++++++++++++++ pkg/utils/utils_virtual_test.go | 58 +++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 pkg/utils/testdata/meta_data.json create mode 100644 pkg/utils/testdata/network_data.json create mode 100644 pkg/utils/utils_virtual_test.go diff --git a/pkg/utils/testdata/meta_data.json b/pkg/utils/testdata/meta_data.json new file mode 100644 index 0000000000..684e1ca78c --- /dev/null +++ b/pkg/utils/testdata/meta_data.json @@ -0,0 +1,25 @@ +{ + "uuid": "7114a452-2e00-4ade-856a-42d4dc7c894f", + "name": "1etsl9fpzrhocpnfv-7bhdj-worker-0-fnz68", + "launch_index": 0, + "availability_zone": "worker", + "project_id": "00552bf9217648d7a5714fbd25f92df2", + "devices": [ + { + "vlan": 177, + "vf_trusted": true, + "type": "nic", + "mac": "fa:16:3e:00:00:00", + "bus": "pci", + "address": "0000:04:00.0" + }, + { + "vlan": 178, + "vf_trusted": true, + "type": "nic", + "mac": "fa:16:3e:11:11:11", + "bus": "pci", + "address": "0000:05:00.0" + } + ] +} diff --git a/pkg/utils/testdata/network_data.json b/pkg/utils/testdata/network_data.json new file mode 100644 index 0000000000..738fda7344 --- /dev/null +++ b/pkg/utils/testdata/network_data.json @@ -0,0 +1,71 @@ +{ + "links": [ + { + "id": "tapa046a03c-49", + "vif_id": "a046a03c-4996-4212-8f23-3b8f197b03b3", + "type": "ovs", + "mtu": 1500, + "ethernet_mac_address": "fa:16:3e:9c:9f:89" + }, + { + "id": "tapdc2a9cd3-f7", + "vif_id": "dc2a9cd3-f7f5-40df-9d52-328f86e6011b", + "type": "hw_veb", + "mtu": 9000, + "ethernet_mac_address": "fa:16:3e:00:00:00" + }, + { + "id": "tapce5054e4-c6", + "vif_id": "ce5054e4-c65a-4c28-843c-155ab8fed825", + "type": "hw_veb", + "mtu": 9000, + "ethernet_mac_address": "fa:16:3e:11:11:11" + } + ], + "networks": [ + { + "id": "network0", + "type": "ipv4_dhcp", + "link": "tapa046a03c-49", + "network_id": "5765e37b-0a13-49d2-a598-537178ce254f" + }, + { + "id": "network1", + "type": "ipv4", + "link": "tapdc2a9cd3-f7", + "ip_address": "192.168.177.4", + "netmask": "255.255.255.0", + "routes": [ + { + "network": "0.0.0.0", + "netmask": "0.0.0.0", + "gateway": "192.168.177.1" + } + ], + "network_id": "b3ba899a-e06c-49da-93c5-c992048390b2", + "services": [] + }, + { + "id": "network2", + "type": "ipv4", + "link": "tapce5054e4-c6", + "ip_address": "192.168.178.107", + "netmask": "255.255.255.0", + "routes": [ + { + "network": "0.0.0.0", + "netmask": "0.0.0.0", + "gateway": "192.168.178.1" + } + ], + "network_id": "a81317cb-aa3d-4675-99cf-aa049f964a3c", + "services": [] + } + ], + "services": [ + { + "type": "dns", + "address": "10.1.2.3" + } + ] +} diff --git a/pkg/utils/utils_virtual_test.go b/pkg/utils/utils_virtual_test.go new file mode 100644 index 0000000000..6890d7304d --- /dev/null +++ b/pkg/utils/utils_virtual_test.go @@ -0,0 +1,58 @@ +package utils + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/jaypipes/ghw" + "github.com/jaypipes/ghw/pkg/net" + "github.com/jaypipes/ghw/pkg/option" + "k8s.io/utils/pointer" +) + +func TestUtils(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Utils") +} + +var _ = Describe("Virtual", func() { + + Context("GetOpenstackData", func() { + It("PCI address replacement based on MAC address", func() { + ospNetworkDataFile = "./testdata/network_data.json" + ospMetaDataFile = "./testdata/meta_data.json" + DeferCleanup(func() { + ospNetworkDataFile = ospMetaDataDir + "/network_data.json" + ospMetaDataFile = ospMetaDataDir + "/meta_data.json" + }) + + ghw.Network = func(opts ...*option.Option) (*net.Info, error) { + return &net.Info{ + NICs: []*net.NIC{{ + MacAddress: "fa:16:3e:00:00:00", + PCIAddress: pointer.String("0000:04:00.0"), + }, { + MacAddress: "fa:16:3e:11:11:11", + PCIAddress: pointer.String("0000:99:99.9"), + }}, + }, nil + } + + DeferCleanup(func() { + ghw.Network = net.New + }) + + metaData, _, err := GetOpenstackData(false) + Expect(err).ToNot(HaveOccurred()) + + Expect(metaData.Devices).To(HaveLen(2)) + Expect(metaData.Devices[0].Mac).To(Equal("fa:16:3e:00:00:00")) + Expect(metaData.Devices[0].Address).To(Equal("0000:04:00.0")) + Expect(metaData.Devices[1].Mac).To(Equal("fa:16:3e:11:11:11")) + Expect(metaData.Devices[1].Address).To(Equal("0000:99:99.9")) + + }) + }) +})