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

« back to all changes in this revision

Viewing changes to src/github.com/juju/names/machine_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 LGPLv3, see LICENCE file for details.
 
3
 
 
4
package names_test
 
5
 
 
6
import (
 
7
        stdtesting "testing"
 
8
 
 
9
        gc "gopkg.in/check.v1"
 
10
 
 
11
        "github.com/juju/names"
 
12
)
 
13
 
 
14
type machineSuite struct{}
 
15
 
 
16
var _ = gc.Suite(&machineSuite{})
 
17
 
 
18
func Test(t *stdtesting.T) {
 
19
        gc.TestingT(t)
 
20
}
 
21
 
 
22
func (s *machineSuite) TestMachineTag(c *gc.C) {
 
23
        c.Assert(names.NewMachineTag("10").String(), gc.Equals, "machine-10")
 
24
        // Check a container id.
 
25
        c.Assert(names.NewMachineTag("10/lxc/1").String(), gc.Equals, "machine-10-lxc-1")
 
26
}
 
27
 
 
28
var machineIdTests = []struct {
 
29
        pattern   string
 
30
        valid     bool
 
31
        container bool
 
32
}{
 
33
        {pattern: "42", valid: true},
 
34
        {pattern: "042", valid: false},
 
35
        {pattern: "0", valid: true},
 
36
        {pattern: "foo", valid: false},
 
37
        {pattern: "/", valid: false},
 
38
        {pattern: "55/", valid: false},
 
39
        {pattern: "1/foo", valid: false},
 
40
        {pattern: "2/foo/", valid: false},
 
41
        {pattern: "3/lxc/42", valid: true, container: true},
 
42
        {pattern: "3/lxc-nodash/42", valid: false},
 
43
        {pattern: "0/lxc/00", valid: false},
 
44
        {pattern: "0/lxc/0/", valid: false},
 
45
        {pattern: "03/lxc/42", valid: false},
 
46
        {pattern: "3/lxc/042", valid: false},
 
47
        {pattern: "4/foo/bar", valid: false},
 
48
        {pattern: "5/lxc/42/foo", valid: false},
 
49
        {pattern: "6/lxc/42/kvm/0", valid: true, container: true},
 
50
        {pattern: "06/lxc/42/kvm/0", valid: false},
 
51
        {pattern: "6/lxc/042/kvm/0", valid: false},
 
52
        {pattern: "6/lxc/42/kvm/00", valid: false},
 
53
        {pattern: "06/lxc/042/kvm/00", valid: false},
 
54
}
 
55
 
 
56
func (s *machineSuite) TestMachineIdFormats(c *gc.C) {
 
57
        for i, test := range machineIdTests {
 
58
                c.Logf("test %d: %q", i, test.pattern)
 
59
                c.Assert(names.IsValidMachine(test.pattern), gc.Equals, test.valid)
 
60
                c.Assert(names.IsContainerMachine(test.pattern), gc.Equals, test.container)
 
61
        }
 
62
}
 
63
 
 
64
var parseMachineTagTests = []struct {
 
65
        tag      string
 
66
        expected names.Tag
 
67
        err      error
 
68
}{{
 
69
        tag: "",
 
70
        err: names.InvalidTagError("", ""),
 
71
}, {
 
72
        tag:      "machine-0",
 
73
        expected: names.NewMachineTag("0"),
 
74
}, {
 
75
        tag: "machine-one",
 
76
        err: names.InvalidTagError("machine-one", names.MachineTagKind),
 
77
}, {
 
78
        tag: "dave",
 
79
        err: names.InvalidTagError("dave", ""),
 
80
}, {
 
81
        tag: "user-one",
 
82
        err: names.InvalidTagError("user-one", names.MachineTagKind),
 
83
}}
 
84
 
 
85
func (s *machineSuite) TestParseMachineTag(c *gc.C) {
 
86
        for i, t := range parseMachineTagTests {
 
87
                c.Logf("test %d: %s", i, t.tag)
 
88
                got, err := names.ParseMachineTag(t.tag)
 
89
                if err != nil || t.err != nil {
 
90
                        c.Check(err, gc.DeepEquals, t.err)
 
91
                        continue
 
92
                }
 
93
                c.Check(got, gc.FitsTypeOf, t.expected)
 
94
                c.Check(got, gc.Equals, t.expected)
 
95
        }
 
96
}