~juju-qa/ubuntu/yakkety/juju/juju-1.25.8

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/provider/maas/util_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-12-02 17:28:37 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161202172837-jkrbdlyjcxtrii2n
Initial commit of 1.25.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/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
}