Skip to content

Commit

Permalink
cloudinit/network_test: test cases for newly added interface config
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Wischke (65278) committed Apr 9, 2024
1 parent 8b707ee commit 7a75234
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ spec:
description: RoutingPolicy is an interface-specific policy
inserted into FIB (forwarding information base).
items:
description: RoutingPolicySpec is a linux FIB rule.
description: RoutingPolicySpec is a Linux FIB rule.
properties:
from:
description: From is the subnet of the source.
Expand All @@ -278,7 +278,7 @@ spec:
kernel rules
rule: (self > 0 && self < 32765) || (self > 32766)
table:
description: Table is the routing table id.
description: Table is the routing table ID.
format: int32
type: integer
to:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ spec:
policy inserted into FIB (forwarding information
base).
items:
description: RoutingPolicySpec is a linux FIB
description: RoutingPolicySpec is a Linux FIB
rule.
properties:
from:
Expand All @@ -302,7 +302,7 @@ spec:
rule: (self > 0 && self < 32765) || (self
> 32766)
table:
description: Table is the routing table id.
description: Table is the routing table ID.
format: int32
type: integer
to:
Expand Down
215 changes: 215 additions & 0 deletions pkg/cloudinit/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ const (
- '8.8.8.8'
- '8.8.4.4'`

expectedValidNetworkConfigWithLinkMTU = `network:
version: 2
renderer: networkd
ethernets:
eth0:
match:
macaddress: 92:60:a0:5b:22:c2
dhcp4: false
dhcp6: false
addresses:
- 10.10.10.12/24
routes:
- to: 0.0.0.0/0
metric: 100
via: 10.10.10.1
nameservers:
addresses:
- '8.8.8.8'
- '8.8.4.4'
mtu: 9001`

expectedValidNetworkConfigWithoutDNS = `network:
version: 2
renderer: networkd
Expand Down Expand Up @@ -201,6 +222,92 @@ const (
- '8.8.8.8'
- '8.8.4.4'`

expectedValidNetworkConfigIPAndDHCP = `network:
version: 2
renderer: networkd
ethernets:
eth0:
match:
macaddress: 92:60:a0:5b:22:c2
dhcp4: true
dhcp6: false
addresses:
- 10.10.10.12/24
routes:
- to: 0.0.0.0/0
metric: 100
via: 10.10.10.1
nameservers:
addresses:
- '8.8.8.8'
- '8.8.4.4'`

expectedValidNetworkConfigWithRoutes = `network:
version: 2
renderer: networkd
ethernets:
eth0:
match:
macaddress: 92:60:a0:5b:22:c2
dhcp4: false
dhcp6: true
addresses:
- 10.10.10.12/24
routes:
- to: 0.0.0.0/0
metric: 100
via: 10.10.10.1
nameservers:
addresses:
- '8.8.8.8'
- '8.8.4.4'
eth1:
match:
macaddress: 92:60:a0:5b:22:c3
dhcp4: false
dhcp6: false
addresses:
- 10.10.11.12/24
routes:
- to: 0.0.0.0/0
metric: 200
via: 10.10.11.1
- { "to": "172.16.24.1/24", "via": "10.10.10.254", "metric": 50, }
- { "to": "2002::/64", "via": "2001:db8::1", }`

expectedValidNetworkConfigWithFIBRules = `network:
version: 2
renderer: networkd
ethernets:
eth0:
match:
macaddress: 92:60:a0:5b:22:c2
dhcp4: false
dhcp6: true
addresses:
- 10.10.10.12/24
routes:
- to: 0.0.0.0/0
metric: 100
via: 10.10.10.1
nameservers:
addresses:
- '8.8.8.8'
- '8.8.4.4'
eth1:
match:
macaddress: 92:60:a0:5b:22:c3
dhcp4: false
dhcp6: false
addresses:
- 10.10.11.12/24
routes:
- to: 0.0.0.0/0
metric: 200
via: 10.10.11.1
routing-policy:
- { "to": "0.0.0.0/0", "from": "192.168.178.1/24", "priority": 999, "table": 100, }`

expectedValidNetworkConfigMultipleNicsVRF = `network:
version: 2
renderer: networkd
Expand Down Expand Up @@ -343,6 +450,26 @@ func TestNetworkConfig_Render(t *testing.T) {
err: nil,
},
},
"ValidStaticNetworkConfigWithLinkMTU": {
reason: "render valid network-config with static ip and mtu",
args: args{
nics: []NetworkConfigData{
{
Type: "ethernet",
Name: "eth0",
MacAddress: "92:60:a0:5b:22:c2",
IPAddress: "10.10.10.12/24",
Gateway: "10.10.10.1",
DNSServers: []string{"8.8.8.8", "8.8.4.4"},
LinkMTU: func(i uint16) *uint16 { return &i }(9001),
},
},
},
want: want{
network: expectedValidNetworkConfigWithLinkMTU,
err: nil,
},
},
"ValidStaticNetworkConfigWithDHCP": {
reason: "render valid network-config with ipv6 static ip and dhcp",
args: args{
Expand All @@ -363,6 +490,94 @@ func TestNetworkConfig_Render(t *testing.T) {
err: nil,
},
},
"ValidStaticNetworkConfigIPWithDHCP": {
reason: "render valid network-config with ipv6 static ip and dhcp",
args: args{
nics: []NetworkConfigData{
{
Type: "ethernet",
Name: "eth0",
MacAddress: "92:60:a0:5b:22:c2",
DHCP4: true,
IPAddress: "10.10.10.12/24",
Gateway: "10.10.10.1",
DNSServers: []string{"8.8.8.8", "8.8.4.4"},
},
},
},
want: want{
network: expectedValidNetworkConfigIPAndDHCP,
err: nil,
},
},
"ValidStaticNetworkConfigWithRoutes": {
reason: "render valid network-config with ipv6 static ip and dhcp and routes",
args: args{
nics: []NetworkConfigData{
{
Type: "ethernet",
Name: "eth0",
MacAddress: "92:60:a0:5b:22:c2",
DHCP6: true,
IPAddress: "10.10.10.12/24",
Gateway: "10.10.10.1",
DNSServers: []string{"8.8.8.8", "8.8.4.4"},
}, {
Type: "ethernet",
Name: "eth1",
MacAddress: "92:60:a0:5b:22:c3",
IPAddress: "10.10.11.12/24",
Gateway: "10.10.11.1",
Routes: []RoutingData{{
To: "172.16.24.1/24",
Metric: 50,
Via: "10.10.10.254",
}, {
To: "2002::/64",
Via: "2001:db8::1",
},
},
},
},
},
want: want{
network: expectedValidNetworkConfigWithRoutes,
err: nil,
},
},
"ValidStaticNetworkConfigWithFIBRules": {
reason: "render valid network-config with FIB rules/routing policy",
args: args{
nics: []NetworkConfigData{
{
Type: "ethernet",
Name: "eth0",
MacAddress: "92:60:a0:5b:22:c2",
DHCP6: true,
IPAddress: "10.10.10.12/24",
Gateway: "10.10.10.1",
DNSServers: []string{"8.8.8.8", "8.8.4.4"},
}, {
Type: "ethernet",
Name: "eth1",
IPAddress: "10.10.11.12/24",
Gateway: "10.10.11.1",
MacAddress: "92:60:a0:5b:22:c3",
FIBRules: []FIBRuleData{{
To: "0.0.0.0/0",
From: "192.168.178.1/24",
Priority: 999,
Table: 100,
},
},
},
},
},
want: want{
network: expectedValidNetworkConfigWithFIBRules,
err: nil,
},
},
"InvalidNetworkConfigIp": {
reason: "ip address is not set",
args: args{
Expand Down

0 comments on commit 7a75234

Please sign in to comment.