github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/provider/maas/util_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  	goyaml "gopkg.in/yaml.v1"
    12  
    13  	"github.com/juju/juju/instance"
    14  	"github.com/juju/juju/juju/paths"
    15  )
    16  
    17  type utilSuite struct{}
    18  
    19  var _ = gc.Suite(&utilSuite{})
    20  
    21  func (*utilSuite) TestExtractSystemId(c *gc.C) {
    22  	instanceId := instance.Id("/MAAS/api/1.0/nodes/system_id/")
    23  
    24  	systemId := extractSystemId(instanceId)
    25  
    26  	c.Check(systemId, gc.Equals, "system_id")
    27  }
    28  
    29  func (*utilSuite) TestGetSystemIdValues(c *gc.C) {
    30  	instanceId1 := instance.Id("/MAAS/api/1.0/nodes/system_id1/")
    31  	instanceId2 := instance.Id("/MAAS/api/1.0/nodes/system_id2/")
    32  	instanceIds := []instance.Id{instanceId1, instanceId2}
    33  
    34  	values := getSystemIdValues("id", instanceIds)
    35  
    36  	c.Check(values["id"], gc.DeepEquals, []string{"system_id1", "system_id2"})
    37  }
    38  
    39  func (*utilSuite) TestMachineInfoCloudinitRunCmd(c *gc.C) {
    40  	hostname := "hostname"
    41  	info := machineInfo{hostname}
    42  	filename := "/var/lib/juju/MAASmachine.txt"
    43  	dataDir, err := paths.DataDir("quantal")
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	script, err := info.cloudinitRunCmd("quantal")
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	yaml, err := goyaml.Marshal(info)
    48  	c.Assert(err, jc.ErrorIsNil)
    49  	expected := fmt.Sprintf("mkdir -p '%s'\ninstall -m 755 /dev/null '%s'\nprintf '%%s\\n' ''\"'\"'%s'\"'\"'' > '%s'", dataDir, filename, yaml, filename)
    50  	c.Check(script, gc.Equals, expected)
    51  }
    52  
    53  func (*utilSuite) TestMachineInfoLoad(c *gc.C) {
    54  	hostname := "hostname"
    55  	yaml := fmt.Sprintf("hostname: %s\n", hostname)
    56  	filename := createTempFile(c, []byte(yaml))
    57  	old_MAASInstanceFilename := _MAASInstanceFilename
    58  	_MAASInstanceFilename = filename
    59  	defer func() { _MAASInstanceFilename = old_MAASInstanceFilename }()
    60  	info := machineInfo{}
    61  
    62  	err := info.load()
    63  
    64  	c.Assert(err, jc.ErrorIsNil)
    65  	c.Check(info.Hostname, gc.Equals, hostname)
    66  }