Debian Codegraph @

netboot/netconf_integ_test.go

raw
1 // +build integration
2
3 package netboot
4
5 import (
6 "fmt"
7 "log"
8 "net"
9 "os"
10 "testing"
11 "time"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 )
16
17 // The test assumes that the interface exists and is configurable.
18 // If you are running this test locally, you may need to adjust this value.
19 var ifname = "eth0"
20
21 func TestIfUp(t *testing.T) {
22 iface, err := IfUp(ifname, 2*time.Second)
23 require.NoError(t, err)
24 assert.Equal(t, ifname, iface.Name)
25 }
26
27 func TestIfUpTimeout(t *testing.T) {
28 _, err := IfUp(ifname, 0*time.Second)
29 require.Error(t, err)
30 }
31
32 func TestConfigureInterface(t *testing.T) {
33 // Linux-only. `netboot.ConfigureInterface` writes to /etc/resolv.conf when
34 // `NetConf.DNSServers` is set. In this test we make a backup of resolv.conf
35 // and subsequently restore it. This is really ugly, and not safe if
36 // multiple tests do the same.
37 resolvconf, err := os.ReadFile("/etc/resolv.conf")
38 if err != nil {
39 panic(fmt.Sprintf("Failed to read /etc/resolv.conf: %v", err))
40 }
41 type testCase struct {
42 Name string
43 NetConf *NetConf
44 }
45 testCases := []testCase{
46 {
47 Name: "just IP addr",
48 NetConf: &NetConf{
49 Addresses: []AddrConf{
50 AddrConf{IPNet: net.IPNet{IP: net.ParseIP("10.20.30.40")}},
51 },
52 },
53 },
54 {
55 Name: "IP addr, DNS, and routers",
56 NetConf: &NetConf{
57 Addresses: []AddrConf{
58 AddrConf{IPNet: net.IPNet{IP: net.ParseIP("10.20.30.40")}},
59 },
60 DNSServers: []net.IP{net.ParseIP("8.8.8.8")},
61 DNSSearchList: []string{"slackware.it"},
62 Routers: []net.IP{net.ParseIP("10.20.30.254")},
63 },
64 },
65 }
66 for _, tc := range testCases {
67 t.Run(tc.Name, func(t *testing.T) {
68 require.NoError(t, ConfigureInterface(ifname, tc.NetConf))
69
70 // after the test, restore the content of /etc/resolv.conf . The permissions
71 // are used only if it didn't exist.
72 if err = os.WriteFile("/etc/resolv.conf", resolvconf, 0644); err != nil {
73 panic(fmt.Sprintf("Failed to restore /etc/resolv.conf: %v", err))
74 }
75 log.Printf("Restored /etc/resolv.conf")
76 })
77 }
78 }