github.com/shaardie/u-root@v4.0.1-0.20190127173353-f24a1c26aa2e+incompatible/integration/dhclient_test.go (about)

     1  // Copyright 2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package integration
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/u-root/u-root/pkg/qemu"
    12  )
    13  
    14  func TestDhclient(t *testing.T) {
    15  	// TODO: support arm
    16  	if TestArch() != "amd64" {
    17  		t.Skipf("test not supported on %s", TestArch())
    18  	}
    19  
    20  	network := qemu.NewNetwork()
    21  	var sb, cb wc
    22  	_, scleanup := QEMUTest(t, &Options{
    23  		Name:         "TestDhclient_Server",
    24  		SerialOutput: &sb,
    25  		Cmds: []string{
    26  			"github.com/u-root/u-root/cmds/echo",
    27  			"github.com/u-root/u-root/cmds/ip",
    28  			"github.com/u-root/u-root/cmds/init",
    29  			"github.com/u-root/u-root/cmds/sleep",
    30  			"github.com/u-root/u-root/cmds/shutdown",
    31  			"github.com/u-root/u-root/integration/testcmd/pxeserver",
    32  		},
    33  		Uinit: []string{
    34  			"ip addr add 192.168.0.1/24 dev eth0",
    35  			"ip link set eth0 up",
    36  			"ip route add 255.255.255.255/32 dev eth0",
    37  			"echo RUN THAT PXE SERVER",
    38  			"pxeserver",
    39  			"echo ALL DONE",
    40  			"sleep 15",
    41  			"shutdown -h",
    42  		},
    43  		Network: network,
    44  	})
    45  	defer scleanup()
    46  
    47  	dhcpClient, ccleanup := QEMUTest(t, &Options{
    48  		Name:         "TestDhclient_Client",
    49  		SerialOutput: &cb,
    50  		Cmds: []string{
    51  			"github.com/u-root/u-root/cmds/echo",
    52  			"github.com/u-root/u-root/cmds/ip",
    53  			"github.com/u-root/u-root/cmds/init",
    54  			"github.com/u-root/u-root/cmds/dhclient",
    55  			"github.com/u-root/u-root/cmds/shutdown",
    56  		},
    57  		Uinit: []string{
    58  			"echo DO THAT DHCLIENT",
    59  			"dhclient -ipv6=false -verbose",
    60  			"echo BACK, WHAT IP",
    61  			"ip a",
    62  			"echo OK, ALL DONE",
    63  			"shutdown -h",
    64  		},
    65  		Network: network,
    66  		Timeout: 30 * time.Second,
    67  	})
    68  	defer ccleanup()
    69  
    70  	t.Logf("Now we wait!")
    71  
    72  	if err := dhcpClient.Expect("err from done <nil>"); err != nil {
    73  		t.Error(err)
    74  	}
    75  
    76  	if err := dhcpClient.Expect("inet 192.168.1.0"); err != nil {
    77  		t.Error(err)
    78  	}
    79  	t.Logf("Server: %s\nClient: %s", sb.String(), cb.String())
    80  }
    81  
    82  func TestPxeboot(t *testing.T) {
    83  	// TODO: support arm
    84  	if TestArch() != "amd64" {
    85  		t.Skipf("test not supported on %s", TestArch())
    86  	}
    87  
    88  	network := qemu.NewNetwork()
    89  	dhcpServer, scleanup := QEMUTest(t, &Options{
    90  		Name: "TestPxeboot_Server",
    91  		Cmds: []string{
    92  			"github.com/u-root/u-root/cmds/init",
    93  			"github.com/u-root/u-root/cmds/ip",
    94  			"github.com/u-root/u-root/integration/testcmd/pxeserver",
    95  		},
    96  		Uinit: []string{
    97  			"ip addr add 192.168.0.1/24 dev eth0",
    98  			"ip link set eth0 up",
    99  			"ip route add 255.255.255.255/32 dev eth0",
   100  			"pxeserver -dir=/pxeroot",
   101  		},
   102  		Files: []string{
   103  			"./testdata/pxe:pxeroot",
   104  		},
   105  		Network: network,
   106  	})
   107  	defer scleanup()
   108  
   109  	dhcpClient, ccleanup := QEMUTest(t, &Options{
   110  		Name: "TestPxeboot_Client",
   111  		Cmds: []string{
   112  			"github.com/u-root/u-root/cmds/init",
   113  			"github.com/u-root/u-root/cmds/ip",
   114  			"github.com/u-root/u-root/cmds/shutdown",
   115  			"github.com/u-root/u-root/cmds/pxeboot",
   116  		},
   117  		Uinit: []string{
   118  			"ip route add 255.255.255.255/32 dev eth0",
   119  			"pxeboot --dry-run",
   120  			"shutdown -h",
   121  		},
   122  		Network: network,
   123  	})
   124  	defer ccleanup()
   125  
   126  	dhcpClient.Expect("")
   127  	dhcpServer.Expect("")
   128  }