github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/machine/list_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for info.
     3  
     4  package machine_test
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/cmd/cmdtesting"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/cmd/juju/machine"
    14  	"github.com/juju/juju/testing"
    15  )
    16  
    17  type MachineListCommandSuite struct {
    18  	testing.FakeJujuXDGDataHomeSuite
    19  }
    20  
    21  var _ = gc.Suite(&MachineListCommandSuite{})
    22  
    23  func newMachineListCommand() cmd.Command {
    24  	return machine.NewListCommandForTest(&fakeStatusAPI{})
    25  }
    26  
    27  type fakeStatusAPI struct{}
    28  
    29  func (*fakeStatusAPI) Status(c []string) (*params.FullStatus, error) {
    30  	result := &params.FullStatus{
    31  		Model: params.ModelStatusInfo{
    32  			Name:    "dummyenv",
    33  			Version: "1.2.3",
    34  		},
    35  		Machines: map[string]params.MachineStatus{
    36  			"0": {
    37  				Id: "0",
    38  				AgentStatus: params.DetailedStatus{
    39  					Status: "started",
    40  				},
    41  				DNSName: "10.0.0.1",
    42  				IPAddresses: []string{
    43  					"10.0.0.1",
    44  					"10.0.1.1",
    45  				},
    46  				InstanceId: "juju-badd06-0",
    47  				Series:     "trusty",
    48  				NetworkInterfaces: map[string]params.NetworkInterface{
    49  					"eth0": {
    50  						IPAddresses: []string{
    51  							"10.0.0.1",
    52  							"10.0.1.1",
    53  						},
    54  						MACAddress: "aa:bb:cc:dd:ee:ff",
    55  						IsUp:       true,
    56  					},
    57  				},
    58  				Constraints: "mem=3584M",
    59  				Hardware:    "availability-zone=us-east-1",
    60  			},
    61  			"1": {
    62  				Id: "1",
    63  				AgentStatus: params.DetailedStatus{
    64  					Status: "started",
    65  				},
    66  				DNSName: "10.0.0.2",
    67  				IPAddresses: []string{
    68  					"10.0.0.2",
    69  					"10.0.1.2",
    70  				},
    71  				InstanceId: "juju-badd06-1",
    72  				Series:     "trusty",
    73  				NetworkInterfaces: map[string]params.NetworkInterface{
    74  					"eth0": {
    75  						IPAddresses: []string{
    76  							"10.0.0.2",
    77  							"10.0.1.2",
    78  						},
    79  						MACAddress: "aa:bb:cc:dd:ee:ff",
    80  						IsUp:       true,
    81  					},
    82  				},
    83  				Containers: map[string]params.MachineStatus{
    84  					"1/lxd/0": {
    85  						Id: "1/lxd/0",
    86  						AgentStatus: params.DetailedStatus{
    87  							Status: "pending",
    88  						},
    89  						DNSName: "10.0.0.3",
    90  						IPAddresses: []string{
    91  							"10.0.0.3",
    92  							"10.0.1.3",
    93  						},
    94  						InstanceId: "juju-badd06-1-lxd-0",
    95  						Series:     "trusty",
    96  						NetworkInterfaces: map[string]params.NetworkInterface{
    97  							"eth0": {
    98  								IPAddresses: []string{
    99  									"10.0.0.3",
   100  									"10.0.1.3",
   101  								},
   102  								MACAddress: "aa:bb:cc:dd:ee:ff",
   103  								IsUp:       true,
   104  							},
   105  						},
   106  					},
   107  				},
   108  				LXDProfiles: map[string]params.LXDProfile{
   109  					"lxd-profile-name": {
   110  						Config: map[string]string{
   111  							"environment.http_proxy": "",
   112  						},
   113  						Description: "lxd-profile description",
   114  						Devices: map[string]map[string]string{
   115  							"tun": {
   116  								"path": "/dev/net/tun",
   117  								"type": "unix-char",
   118  							},
   119  						},
   120  					},
   121  				},
   122  			},
   123  		},
   124  	}
   125  	return result, nil
   126  
   127  }
   128  func (*fakeStatusAPI) Close() error {
   129  	return nil
   130  }
   131  
   132  func (s *MachineListCommandSuite) SetUpTest(c *gc.C) {
   133  	s.FakeJujuXDGDataHomeSuite.SetUpTest(c)
   134  }
   135  
   136  func (s *MachineListCommandSuite) TestMachine(c *gc.C) {
   137  	context, err := cmdtesting.RunCommand(c, newMachineListCommand())
   138  	c.Assert(err, jc.ErrorIsNil)
   139  	c.Assert(cmdtesting.Stdout(context), gc.Equals, ""+
   140  		"Machine  State    DNS       Inst id              Series  AZ         Message\n"+
   141  		"0        started  10.0.0.1  juju-badd06-0        trusty  us-east-1  \n"+
   142  		"1        started  10.0.0.2  juju-badd06-1        trusty             \n"+
   143  		"1/lxd/0  pending  10.0.0.3  juju-badd06-1-lxd-0  trusty             \n"+
   144  		"\n")
   145  }
   146  
   147  func (s *MachineListCommandSuite) TestListMachineYaml(c *gc.C) {
   148  	context, err := cmdtesting.RunCommand(c, newMachineListCommand(), "--format", "yaml")
   149  	c.Assert(err, jc.ErrorIsNil)
   150  	c.Assert(cmdtesting.Stdout(context), gc.Equals, ""+
   151  		"model: dummyenv\n"+
   152  		"machines:\n"+
   153  		"  \"0\":\n"+
   154  		"    juju-status:\n"+
   155  		"      current: started\n"+
   156  		"    dns-name: 10.0.0.1\n"+
   157  		"    ip-addresses:\n"+
   158  		"    - 10.0.0.1\n"+
   159  		"    - 10.0.1.1\n"+
   160  		"    instance-id: juju-badd06-0\n"+
   161  		"    series: trusty\n"+
   162  		"    network-interfaces:\n"+
   163  		"      eth0:\n"+
   164  		"        ip-addresses:\n"+
   165  		"        - 10.0.0.1\n"+
   166  		"        - 10.0.1.1\n"+
   167  		"        mac-address: aa:bb:cc:dd:ee:ff\n"+
   168  		"        is-up: true\n"+
   169  		"    constraints: mem=3584M\n"+
   170  		"    hardware: availability-zone=us-east-1\n"+
   171  		"  \"1\":\n"+
   172  		"    juju-status:\n"+
   173  		"      current: started\n"+
   174  		"    dns-name: 10.0.0.2\n"+
   175  		"    ip-addresses:\n"+
   176  		"    - 10.0.0.2\n"+
   177  		"    - 10.0.1.2\n"+
   178  		"    instance-id: juju-badd06-1\n"+
   179  		"    series: trusty\n"+
   180  		"    network-interfaces:\n"+
   181  		"      eth0:\n"+
   182  		"        ip-addresses:\n"+
   183  		"        - 10.0.0.2\n"+
   184  		"        - 10.0.1.2\n"+
   185  		"        mac-address: aa:bb:cc:dd:ee:ff\n"+
   186  		"        is-up: true\n"+
   187  		"    containers:\n"+
   188  		"      1/lxd/0:\n"+
   189  		"        juju-status:\n"+
   190  		"          current: pending\n"+
   191  		"        dns-name: 10.0.0.3\n"+
   192  		"        ip-addresses:\n"+
   193  		"        - 10.0.0.3\n"+
   194  		"        - 10.0.1.3\n"+
   195  		"        instance-id: juju-badd06-1-lxd-0\n"+
   196  		"        series: trusty\n"+
   197  		"        network-interfaces:\n"+
   198  		"          eth0:\n"+
   199  		"            ip-addresses:\n"+
   200  		"            - 10.0.0.3\n"+
   201  		"            - 10.0.1.3\n"+
   202  		"            mac-address: aa:bb:cc:dd:ee:ff\n"+
   203  		"            is-up: true\n"+
   204  		"    lxd-profiles:\n"+
   205  		"      lxd-profile-name:\n"+
   206  		"        config:\n"+
   207  		"          environment.http_proxy: \"\"\n"+
   208  		"        description: lxd-profile description\n"+
   209  		"        devices:\n"+
   210  		"          tun:\n"+
   211  		"            path: /dev/net/tun\n"+
   212  		"            type: unix-char\n",
   213  	)
   214  }
   215  
   216  func (s *MachineListCommandSuite) TestListMachineJson(c *gc.C) {
   217  	context, err := cmdtesting.RunCommand(c, newMachineListCommand(), "--format", "json")
   218  	c.Assert(err, jc.ErrorIsNil)
   219  	c.Assert(cmdtesting.Stdout(context), gc.Equals, ""+
   220  		"{\"model\":\"dummyenv\",\"machines\":{\"0\":{\"juju-status\":{\"current\":\"started\"},\"dns-name\":\"10.0.0.1\",\"ip-addresses\":[\"10.0.0.1\",\"10.0.1.1\"],\"instance-id\":\"juju-badd06-0\",\"machine-status\":{},\"series\":\"trusty\",\"network-interfaces\":{\"eth0\":{\"ip-addresses\":[\"10.0.0.1\",\"10.0.1.1\"],\"mac-address\":\"aa:bb:cc:dd:ee:ff\",\"is-up\":true}},\"constraints\":\"mem=3584M\",\"hardware\":\"availability-zone=us-east-1\"},\"1\":{\"juju-status\":{\"current\":\"started\"},\"dns-name\":\"10.0.0.2\",\"ip-addresses\":[\"10.0.0.2\",\"10.0.1.2\"],\"instance-id\":\"juju-badd06-1\",\"machine-status\":{},\"series\":\"trusty\",\"network-interfaces\":{\"eth0\":{\"ip-addresses\":[\"10.0.0.2\",\"10.0.1.2\"],\"mac-address\":\"aa:bb:cc:dd:ee:ff\",\"is-up\":true}},\"containers\":{\"1/lxd/0\":{\"juju-status\":{\"current\":\"pending\"},\"dns-name\":\"10.0.0.3\",\"ip-addresses\":[\"10.0.0.3\",\"10.0.1.3\"],\"instance-id\":\"juju-badd06-1-lxd-0\",\"machine-status\":{},\"series\":\"trusty\",\"network-interfaces\":{\"eth0\":{\"ip-addresses\":[\"10.0.0.3\",\"10.0.1.3\"],\"mac-address\":\"aa:bb:cc:dd:ee:ff\",\"is-up\":true}}}},\"lxd-profiles\":{\"lxd-profile-name\":{\"config\":{\"environment.http_proxy\":\"\"},\"description\":\"lxd-profile description\",\"devices\":{\"tun\":{\"path\":\"/dev/net/tun\",\"type\":\"unix-char\"}}}}}}}\n")
   221  }
   222  
   223  func (s *MachineListCommandSuite) TestListMachineArgsError(c *gc.C) {
   224  	_, err := cmdtesting.RunCommand(c, newMachineListCommand(), "0")
   225  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["0"\]`)
   226  }