launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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 gc "launchpad.net/gocheck" 10 "launchpad.net/goyaml" 11 12 "launchpad.net/juju-core/environs" 13 "launchpad.net/juju-core/instance" 14 ) 15 16 type utilSuite struct{} 17 18 var _ = gc.Suite(&utilSuite{}) 19 20 func (*utilSuite) TestExtractSystemId(c *gc.C) { 21 instanceId := instance.Id("/MAAS/api/1.0/nodes/system_id/") 22 23 systemId := extractSystemId(instanceId) 24 25 c.Check(systemId, gc.Equals, "system_id") 26 } 27 28 func (*utilSuite) TestGetSystemIdValues(c *gc.C) { 29 instanceId1 := instance.Id("/MAAS/api/1.0/nodes/system_id1/") 30 instanceId2 := instance.Id("/MAAS/api/1.0/nodes/system_id2/") 31 instanceIds := []instance.Id{instanceId1, instanceId2} 32 33 values := getSystemIdValues(instanceIds) 34 35 c.Check(values["id"], gc.DeepEquals, []string{"system_id1", "system_id2"}) 36 } 37 38 func (*utilSuite) TestMachineInfoCloudinitRunCmd(c *gc.C) { 39 hostname := "hostname" 40 filename := "path/to/file" 41 old_MAASInstanceFilename := _MAASInstanceFilename 42 _MAASInstanceFilename = filename 43 defer func() { _MAASInstanceFilename = old_MAASInstanceFilename }() 44 info := machineInfo{hostname} 45 46 script, err := info.cloudinitRunCmd() 47 48 c.Assert(err, gc.IsNil) 49 yaml, err := goyaml.Marshal(info) 50 c.Assert(err, gc.IsNil) 51 expected := fmt.Sprintf("mkdir -p '%s'; echo -n '%s' > '%s'", environs.DataDir, yaml, filename) 52 c.Check(script, gc.Equals, expected) 53 } 54 55 func (*utilSuite) TestMachineInfoLoad(c *gc.C) { 56 hostname := "hostname" 57 yaml := fmt.Sprintf("hostname: %s\n", hostname) 58 filename := createTempFile(c, []byte(yaml)) 59 old_MAASInstanceFilename := _MAASInstanceFilename 60 _MAASInstanceFilename = filename 61 defer func() { _MAASInstanceFilename = old_MAASInstanceFilename }() 62 info := machineInfo{} 63 64 err := info.load() 65 66 c.Assert(err, gc.IsNil) 67 c.Check(info.Hostname, gc.Equals, hostname) 68 }