Skip to content

Commit

Permalink
utils_virtual: unit tests
Browse files Browse the repository at this point in the history
Co-Authored-By: Andrea Panattoni <[email protected]>
Co-Authored-By: Emilien Macchi <[email protected]>
  • Loading branch information
3 people committed Oct 18, 2023
1 parent c94bef3 commit 19901ce
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 2 deletions.
25 changes: 25 additions & 0 deletions pkg/utils/testdata/meta_data.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
71 changes: 71 additions & 0 deletions pkg/utils/testdata/network_data.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
7 changes: 5 additions & 2 deletions pkg/utils/utils_virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ const (
ospMetaDataBaseURL = "http://169.254.169.254/openstack/2018-08-27"
ospHostNetworkDataFile = ospHostMetaDataDir + "/network_data.json"
ospHostMetaDataFile = ospHostMetaDataDir + "/meta_data.json"
ospNetworkDataFile = ospMetaDataDir + "/network_data.json"
ospMetaDataFile = ospMetaDataDir + "/meta_data.json"
ospNetworkDataURL = ospMetaDataBaseURL + "/network_data.json"
ospMetaDataURL = ospMetaDataBaseURL + "/meta_data.json"
)

var (
ospNetworkDataFile = ospMetaDataDir + "/network_data.json"
ospMetaDataFile = ospMetaDataDir + "/meta_data.json"
)

// OSPMetaDataDevice -- Device structure within meta_data.json
type OSPMetaDataDevice struct {
Vlan int `json:"vlan,omitempty"`
Expand Down
58 changes: 58 additions & 0 deletions pkg/utils/utils_virtual_test.go
Original file line number Diff line number Diff line change
@@ -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"))

})
})
})

0 comments on commit 19901ce

Please sign in to comment.