github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/core/instance" 15 "github.com/juju/juju/core/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 := paths.DataDir(paths.OSUnixLike) 45 cloudcfg, err := cloudinit.New("ubuntu") 46 c.Assert(err, jc.ErrorIsNil) 47 script, err := info.cloudinitRunCmd(cloudcfg) 48 c.Assert(err, jc.ErrorIsNil) 49 yaml, err := goyaml.Marshal(info) 50 c.Assert(err, jc.ErrorIsNil) 51 expected := fmt.Sprintf("mkdir -p '%s'\ncat > '%s' << 'EOF'\n'%s'\nEOF\nchmod 0755 '%s'", dataDir, filename, 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, jc.ErrorIsNil) 67 c.Check(info.Hostname, gc.Equals, hostname) 68 }