github.com/sneal/packer@v0.5.2/builder/vmware/common/guest_ip_test.go (about)

     1  package common
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func TestDHCPLeaseGuestLookup_impl(t *testing.T) {
    10  	var _ GuestIPFinder = new(DHCPLeaseGuestLookup)
    11  }
    12  
    13  func TestDHCPLeaseGuestLookup(t *testing.T) {
    14  	tf, err := ioutil.TempFile("", "packer")
    15  	if err != nil {
    16  		t.Fatalf("err: %s", err)
    17  	}
    18  	if _, err := tf.Write([]byte(testLeaseContents)); err != nil {
    19  		t.Fatalf("err: %s", err)
    20  	}
    21  	tf.Close()
    22  	defer os.Remove(tf.Name())
    23  
    24  	driver := new(DriverMock)
    25  	driver.DhcpLeasesPathResult = tf.Name()
    26  
    27  	finder := &DHCPLeaseGuestLookup{
    28  		Driver:     driver,
    29  		Device:     "vmnet8",
    30  		MACAddress: "00:0c:29:59:91:02",
    31  	}
    32  
    33  	ip, err := finder.GuestIP()
    34  	if err != nil {
    35  		t.Fatalf("err: %s", err)
    36  	}
    37  
    38  	if !driver.DhcpLeasesPathCalled {
    39  		t.Fatal("should ask for DHCP leases path")
    40  	}
    41  	if driver.DhcpLeasesPathDevice != "vmnet8" {
    42  		t.Fatal("should be vmnet8")
    43  	}
    44  
    45  	if ip != "192.168.126.130" {
    46  		t.Fatalf("bad: %#v", ip)
    47  	}
    48  }
    49  
    50  const testLeaseContents = `
    51  # All times in this file are in UTC (GMT), not your local timezone.   This is
    52  # not a bug, so please don't ask about it.   There is no portable way to
    53  # store leases in the local timezone, so please don't request this as a
    54  # feature.   If this is inconvenient or confusing to you, we sincerely
    55  # apologize.   Seriously, though - don't ask.
    56  # The format of this file is documented in the dhcpd.leases(5) manual page.
    57  
    58  lease 192.168.126.129 {
    59  	starts 0 2013/09/15 23:58:51;
    60  	ends 1 2013/09/16 00:28:51;
    61  	hardware ethernet 00:0c:29:59:91:02;
    62  	client-hostname "precise64";
    63  }
    64  lease 192.168.126.130 {
    65  	starts 2 2013/09/17 21:39:07;
    66  	ends 2 2013/09/17 22:09:07;
    67  	hardware ethernet 00:0c:29:59:91:02;
    68  	client-hostname "precise64";
    69  }
    70  lease 192.168.126.128 {
    71  	starts 0 2013/09/15 20:09:59;
    72  	ends 0 2013/09/15 20:21:58;
    73  	hardware ethernet 00:0c:29:59:91:02;
    74  	client-hostname "precise64";
    75  }
    76  lease 192.168.126.127 {
    77  	starts 0 2013/09/15 20:09:59;
    78  	ends 0 2013/09/15 20:21:58;
    79  	hardware ethernet 01:0c:29:59:91:02;
    80  	client-hostname "precise64";
    81  
    82  `