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