github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/provider/maas/instance_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package maas
     5  
     6  import (
     7  	"fmt"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/network"
    13  )
    14  
    15  type instanceTest struct {
    16  	providerSuite
    17  }
    18  
    19  var _ = gc.Suite(&instanceTest{})
    20  
    21  func (s *instanceTest) TestId(c *gc.C) {
    22  	jsonValue := `{"system_id": "system_id", "test": "test"}`
    23  	obj := s.testMAASObject.TestServer.NewNode(jsonValue)
    24  	resourceURI, _ := obj.GetField("resource_uri")
    25  	instance := maasInstance{maasObject: &obj, environ: s.makeEnviron()}
    26  
    27  	c.Check(string(instance.Id()), gc.Equals, resourceURI)
    28  }
    29  
    30  func (s *instanceTest) TestString(c *gc.C) {
    31  	jsonValue := `{"hostname": "thethingintheplace", "system_id": "system_id", "test": "test"}`
    32  	obj := s.testMAASObject.TestServer.NewNode(jsonValue)
    33  	instance := &maasInstance{maasObject: &obj, environ: s.makeEnviron()}
    34  	hostname, err := instance.hostname()
    35  	c.Assert(err, jc.ErrorIsNil)
    36  	expected := hostname + ":" + string(instance.Id())
    37  	c.Assert(fmt.Sprint(instance), gc.Equals, expected)
    38  }
    39  
    40  func (s *instanceTest) TestStringWithoutHostname(c *gc.C) {
    41  	// For good measure, test what happens if we don't have a hostname.
    42  	jsonValue := `{"system_id": "system_id", "test": "test"}`
    43  	obj := s.testMAASObject.TestServer.NewNode(jsonValue)
    44  	instance := &maasInstance{maasObject: &obj, environ: s.makeEnviron()}
    45  	_, err := instance.hostname()
    46  	c.Assert(err, gc.NotNil)
    47  	expected := fmt.Sprintf("<DNSName failed: %q>", err) + ":" + string(instance.Id())
    48  	c.Assert(fmt.Sprint(instance), gc.Equals, expected)
    49  }
    50  
    51  func (s *instanceTest) TestRefreshInstance(c *gc.C) {
    52  	jsonValue := `{"system_id": "system_id", "test": "test"}`
    53  	obj := s.testMAASObject.TestServer.NewNode(jsonValue)
    54  	s.testMAASObject.TestServer.ChangeNode("system_id", "test2", "test2")
    55  	instance := maasInstance{maasObject: &obj, environ: s.makeEnviron()}
    56  
    57  	err := instance.Refresh()
    58  
    59  	c.Check(err, jc.ErrorIsNil)
    60  	testField, err := (*instance.maasObject).GetField("test2")
    61  	c.Check(err, jc.ErrorIsNil)
    62  	c.Check(testField, gc.Equals, "test2")
    63  }
    64  
    65  func (s *instanceTest) TestAddresses(c *gc.C) {
    66  	jsonValue := `{
    67  			"hostname": "testing.invalid",
    68  			"system_id": "system_id",
    69  			"ip_addresses": [ "1.2.3.4", "fe80::d806:dbff:fe23:1199" ]
    70  		}`
    71  	obj := s.testMAASObject.TestServer.NewNode(jsonValue)
    72  	inst := maasInstance{maasObject: &obj, environ: s.makeEnviron()}
    73  
    74  	expected := []network.Address{
    75  		{Value: "testing.invalid", Type: network.HostName, Scope: network.ScopePublic},
    76  		{Value: "testing.invalid", Type: network.HostName, Scope: network.ScopeCloudLocal},
    77  		network.NewAddress("1.2.3.4", network.ScopeUnknown),
    78  		network.NewAddress("fe80::d806:dbff:fe23:1199", network.ScopeUnknown),
    79  	}
    80  
    81  	addr, err := inst.Addresses()
    82  
    83  	c.Assert(err, jc.ErrorIsNil)
    84  	c.Check(addr, gc.DeepEquals, expected)
    85  }
    86  
    87  func (s *instanceTest) TestAddressesMissing(c *gc.C) {
    88  	// Older MAAS versions do not have ip_addresses returned, for these
    89  	// just the DNS name should be returned without error.
    90  	jsonValue := `{
    91  		"hostname": "testing.invalid",
    92  		"system_id": "system_id"
    93  		}`
    94  	obj := s.testMAASObject.TestServer.NewNode(jsonValue)
    95  	inst := maasInstance{maasObject: &obj, environ: s.makeEnviron()}
    96  
    97  	addr, err := inst.Addresses()
    98  	c.Assert(err, jc.ErrorIsNil)
    99  	c.Check(addr, gc.DeepEquals, []network.Address{
   100  		{Value: "testing.invalid", Type: network.HostName, Scope: network.ScopePublic},
   101  		{Value: "testing.invalid", Type: network.HostName, Scope: network.ScopeCloudLocal},
   102  	})
   103  }
   104  
   105  func (s *instanceTest) TestAddressesInvalid(c *gc.C) {
   106  	jsonValue := `{
   107  		"hostname": "testing.invalid",
   108  		"system_id": "system_id",
   109  		"ip_addresses": "incompatible"
   110  		}`
   111  	obj := s.testMAASObject.TestServer.NewNode(jsonValue)
   112  	inst := maasInstance{maasObject: &obj, environ: s.makeEnviron()}
   113  
   114  	_, err := inst.Addresses()
   115  	c.Assert(err, gc.NotNil)
   116  }
   117  
   118  func (s *instanceTest) TestAddressesInvalidContents(c *gc.C) {
   119  	jsonValue := `{
   120  		"hostname": "testing.invalid",
   121  		"system_id": "system_id",
   122  		"ip_addresses": [42]
   123  		}`
   124  	obj := s.testMAASObject.TestServer.NewNode(jsonValue)
   125  	inst := maasInstance{maasObject: &obj, environ: s.makeEnviron()}
   126  
   127  	_, err := inst.Addresses()
   128  	c.Assert(err, gc.NotNil)
   129  }
   130  
   131  func (s *instanceTest) TestHardwareCharacteristics(c *gc.C) {
   132  	jsonValue := `{
   133  		"system_id": "system_id",
   134          "architecture": "amd64/generic",
   135          "cpu_count": 6,
   136          "memory": 16384
   137  	}`
   138  	obj := s.testMAASObject.TestServer.NewNode(jsonValue)
   139  	inst := maasInstance{maasObject: &obj, environ: s.makeEnviron()}
   140  	hc, err := inst.hardwareCharacteristics()
   141  	c.Assert(err, jc.ErrorIsNil)
   142  	c.Assert(hc, gc.NotNil)
   143  	c.Assert(hc.String(), gc.Equals, `arch=amd64 cpu-cores=6 mem=16384M`)
   144  }
   145  
   146  func (s *instanceTest) TestHardwareCharacteristicsWithTags(c *gc.C) {
   147  	jsonValue := `{
   148  		"system_id": "system_id",
   149          "architecture": "amd64/generic",
   150          "cpu_count": 6,
   151          "memory": 16384,
   152          "tag_names": ["a", "b"]
   153  	}`
   154  	obj := s.testMAASObject.TestServer.NewNode(jsonValue)
   155  	inst := maasInstance{maasObject: &obj, environ: s.makeEnviron()}
   156  	hc, err := inst.hardwareCharacteristics()
   157  	c.Assert(err, jc.ErrorIsNil)
   158  	c.Assert(hc, gc.NotNil)
   159  	c.Assert(hc.String(), gc.Equals, `arch=amd64 cpu-cores=6 mem=16384M tags=a,b`)
   160  }
   161  
   162  func (s *instanceTest) TestHardwareCharacteristicsMissing(c *gc.C) {
   163  	s.testHardwareCharacteristicsMissing(c, `{"system_id": "id", "cpu_count": 6, "memory": 16384}`,
   164  		`error determining architecture: Requested string, got <nil>.`)
   165  	s.testHardwareCharacteristicsMissing(c, `{"system_id": "id", "architecture": "amd64", "memory": 16384}`,
   166  		`error determining cpu count: Requested float64, got <nil>.`)
   167  	s.testHardwareCharacteristicsMissing(c, `{"system_id": "id", "architecture": "armhf", "cpu_count": 6}`,
   168  		`error determining available memory: Requested float64, got <nil>.`)
   169  	s.testHardwareCharacteristicsMissing(c, `{"system_id": "id", "architecture": "armhf", "cpu_count": 6, "memory": 1, "tag_names": "wot"}`,
   170  		`error determining tag names: Requested array, got string.`)
   171  }
   172  
   173  func (s *instanceTest) testHardwareCharacteristicsMissing(c *gc.C, json, expect string) {
   174  	obj := s.testMAASObject.TestServer.NewNode(json)
   175  	inst := maasInstance{maasObject: &obj, environ: s.makeEnviron()}
   176  	_, err := inst.hardwareCharacteristics()
   177  	c.Assert(err, gc.ErrorMatches, expect)
   178  }