github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/tools/lxdclient/client_instance_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  // +build go1.3
     5  
     6  package lxdclient_test
     7  
     8  import (
     9  	jc "github.com/juju/testing/checkers"
    10  	lxdshared "github.com/lxc/lxd/shared"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/network"
    14  	jujutesting "github.com/juju/juju/testing"
    15  	"github.com/juju/juju/tools/lxdclient"
    16  )
    17  
    18  type addressesSuite struct {
    19  	jujutesting.BaseSuite
    20  }
    21  
    22  var _ = gc.Suite(&addressesSuite{})
    23  
    24  type addressTester struct {
    25  	// Stub out all the APIs so we conform to the interface,
    26  	// we only implement the ones that we are going to be testing
    27  	lxdclient.RawInstanceClient
    28  
    29  	ContainerStateResult *lxdshared.ContainerState
    30  }
    31  
    32  func (a *addressTester) ContainerState(name string) (*lxdshared.ContainerState, error) {
    33  	return a.ContainerStateResult, nil
    34  }
    35  
    36  var _ lxdclient.RawInstanceClient = (*addressTester)(nil)
    37  
    38  // containerStateSample was captured from a real response
    39  var containerStateSample = lxdshared.ContainerState{
    40  	Status:     "Running",
    41  	StatusCode: lxdshared.Running,
    42  	Disk:       map[string]lxdshared.ContainerStateDisk{},
    43  	Memory: lxdshared.ContainerStateMemory{
    44  		Usage:         66486272,
    45  		UsagePeak:     92405760,
    46  		SwapUsage:     0,
    47  		SwapUsagePeak: 0,
    48  	},
    49  	Network: map[string]lxdshared.ContainerStateNetwork{
    50  		"eth0": lxdshared.ContainerStateNetwork{
    51  			Addresses: []lxdshared.ContainerStateNetworkAddress{
    52  				lxdshared.ContainerStateNetworkAddress{
    53  					Family:  "inet",
    54  					Address: "10.0.8.173",
    55  					Netmask: "24",
    56  					Scope:   "global",
    57  				},
    58  				lxdshared.ContainerStateNetworkAddress{
    59  					Family:  "inet6",
    60  					Address: "fe80::216:3eff:fe3b:e582",
    61  					Netmask: "64",
    62  					Scope:   "link",
    63  				},
    64  			},
    65  			Counters: lxdshared.ContainerStateNetworkCounters{
    66  				BytesReceived:   16352,
    67  				BytesSent:       6238,
    68  				PacketsReceived: 69,
    69  				PacketsSent:     59,
    70  			},
    71  			Hwaddr:   "00:16:3e:3b:e5:82",
    72  			HostName: "vethYIEDPS",
    73  			Mtu:      1500,
    74  			State:    "up",
    75  			Type:     "broadcast",
    76  		},
    77  		"lo": lxdshared.ContainerStateNetwork{
    78  			Addresses: []lxdshared.ContainerStateNetworkAddress{
    79  				lxdshared.ContainerStateNetworkAddress{
    80  					Family:  "inet",
    81  					Address: "127.0.0.1",
    82  					Netmask: "8",
    83  					Scope:   "local",
    84  				},
    85  				lxdshared.ContainerStateNetworkAddress{
    86  					Family:  "inet6",
    87  					Address: "::1",
    88  					Netmask: "128",
    89  					Scope:   "local",
    90  				},
    91  			},
    92  			Counters: lxdshared.ContainerStateNetworkCounters{
    93  				BytesReceived:   0,
    94  				BytesSent:       0,
    95  				PacketsReceived: 0,
    96  				PacketsSent:     0,
    97  			},
    98  			Hwaddr:   "",
    99  			HostName: "",
   100  			Mtu:      65536,
   101  			State:    "up",
   102  			Type:     "loopback",
   103  		},
   104  		"lxcbr0": lxdshared.ContainerStateNetwork{
   105  			Addresses: []lxdshared.ContainerStateNetworkAddress{
   106  				lxdshared.ContainerStateNetworkAddress{
   107  					Family:  "inet",
   108  					Address: "10.0.5.12",
   109  					Netmask: "24",
   110  					Scope:   "global",
   111  				},
   112  				lxdshared.ContainerStateNetworkAddress{
   113  					Family:  "inet6",
   114  					Address: "fe80::216:3eff:fe3b:e432",
   115  					Netmask: "64",
   116  					Scope:   "link",
   117  				},
   118  			},
   119  			Counters: lxdshared.ContainerStateNetworkCounters{
   120  				BytesReceived:   0,
   121  				BytesSent:       500,
   122  				PacketsReceived: 0,
   123  				PacketsSent:     6,
   124  			},
   125  			Hwaddr:   "5e:9b:b2:af:4c:f2",
   126  			HostName: "",
   127  			Mtu:      1500,
   128  			State:    "up",
   129  			Type:     "broadcast",
   130  		},
   131  		"lxdbr0": lxdshared.ContainerStateNetwork{
   132  			Addresses: []lxdshared.ContainerStateNetworkAddress{
   133  				lxdshared.ContainerStateNetworkAddress{
   134  					Family:  "inet",
   135  					Address: "10.0.6.17",
   136  					Netmask: "24",
   137  					Scope:   "global",
   138  				},
   139  				lxdshared.ContainerStateNetworkAddress{
   140  					Family:  "inet6",
   141  					Address: "fe80::5c9b:b2ff:feaf:4cf2",
   142  					Netmask: "64",
   143  					Scope:   "link",
   144  				},
   145  			},
   146  			Counters: lxdshared.ContainerStateNetworkCounters{
   147  				BytesReceived:   0,
   148  				BytesSent:       500,
   149  				PacketsReceived: 0,
   150  				PacketsSent:     6,
   151  			},
   152  			Hwaddr:   "52:54:00:5c:54:8f",
   153  			HostName: "",
   154  			Mtu:      1500,
   155  			State:    "up",
   156  			Type:     "broadcast",
   157  		},
   158  	},
   159  	Pid:       46072,
   160  	Processes: 19,
   161  }
   162  
   163  func (s *addressesSuite) TestAddresses(c *gc.C) {
   164  	raw := &addressTester{
   165  		ContainerStateResult: &containerStateSample,
   166  	}
   167  	client := lxdclient.NewInstanceClient(raw)
   168  	addrs, err := client.Addresses("test")
   169  	c.Assert(err, jc.ErrorIsNil)
   170  	// We should filter out the MachineLocal addresses 127.0.0.1 and [::1]
   171  	// and filter out the LinkLocal address [fe80::216:3eff:fe3b:e582]
   172  	c.Check(addrs, jc.DeepEquals, []network.Address{
   173  		{
   174  			Value: "10.0.8.173",
   175  			Type:  network.IPv4Address,
   176  			Scope: network.ScopeCloudLocal,
   177  		},
   178  	})
   179  }