LEFT | RIGHT |
1 // Copyright 2013 Canonical Ltd. | 1 // Copyright 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package names_test | 4 package names_test |
5 | 5 |
6 import ( | 6 import ( |
7 stdtesting "testing" | 7 stdtesting "testing" |
8 | 8 |
9 gc "launchpad.net/gocheck" | 9 gc "launchpad.net/gocheck" |
10 | 10 |
11 "launchpad.net/juju-core/names" | 11 "launchpad.net/juju-core/names" |
12 ) | 12 ) |
13 | 13 |
14 type machineSuite struct{} | 14 type machineSuite struct{} |
15 | 15 |
16 var _ = gc.Suite(&machineSuite{}) | 16 var _ = gc.Suite(&machineSuite{}) |
17 | 17 |
18 func Test(t *stdtesting.T) { | 18 func Test(t *stdtesting.T) { |
19 gc.TestingT(t) | 19 gc.TestingT(t) |
20 } | 20 } |
21 | 21 |
22 func (s *machineSuite) TestMachineTag(c *gc.C) { | 22 func (s *machineSuite) TestMachineTag(c *gc.C) { |
23 c.Assert(names.MachineTag("10"), gc.Equals, "machine-10") | 23 c.Assert(names.MachineTag("10"), gc.Equals, "machine-10") |
24 // Check a container id. | 24 // Check a container id. |
25 c.Assert(names.MachineTag("10/lxc/1"), gc.Equals, "machine-10-lxc-1") | 25 c.Assert(names.MachineTag("10/lxc/1"), gc.Equals, "machine-10-lxc-1") |
26 } | |
27 | |
28 func (s *machineSuite) TestMachineFromTag(c *gc.C) { | |
29 kind, id, err := names.ParseTag("machine-10", names.MachineTagKind) | |
30 c.Assert(kind, gc.Equals, names.MachineTagKind) | |
31 c.Assert(err, gc.IsNil) | |
32 c.Assert(id, gc.Equals, "10") | |
33 | |
34 // Check a container id. | |
35 kind, id, err = names.ParseTag("machine-10-lxc-1", names.MachineTagKind) | |
36 c.Assert(kind, gc.Equals, names.MachineTagKind) | |
37 c.Assert(err, gc.IsNil) | |
38 c.Assert(id, gc.Equals, "10/lxc/1") | |
39 | |
40 // Check reversability. | |
41 nested := "2/kvm/0/lxc/3" | |
42 kind, id, err = names.ParseTag(names.MachineTag(nested), names.MachineTa
gKind) | |
43 c.Assert(kind, gc.Equals, names.MachineTagKind) | |
44 c.Assert(err, gc.IsNil) | |
45 c.Assert(id, gc.Equals, nested) | |
46 | |
47 // Try with an invalid tag formats. | |
48 kind, id, err = names.ParseTag("foo", names.MachineTagKind) | |
49 c.Assert(err, gc.ErrorMatches, `"foo" is not a valid machine tag`) | |
50 c.Assert(kind, gc.Equals, "") | |
51 c.Assert(id, gc.Equals, "") | |
52 | |
53 kind, id, err = names.ParseTag("machine-#", names.MachineTagKind) | |
54 c.Assert(err, gc.ErrorMatches, `"machine-#" is not a valid machine tag`) | |
55 c.Assert(kind, gc.Equals, "") | |
56 c.Assert(id, gc.Equals, "") | |
57 } | 26 } |
58 | 27 |
59 var machineIdTests = []struct { | 28 var machineIdTests = []struct { |
60 pattern string | 29 pattern string |
61 valid bool | 30 valid bool |
62 }{ | 31 }{ |
63 {pattern: "42", valid: true}, | 32 {pattern: "42", valid: true}, |
64 {pattern: "042", valid: false}, | 33 {pattern: "042", valid: false}, |
65 {pattern: "0", valid: true}, | 34 {pattern: "0", valid: true}, |
66 {pattern: "foo", valid: false}, | 35 {pattern: "foo", valid: false}, |
(...skipping 15 matching lines...) Expand all Loading... |
82 {pattern: "6/lxc/42/kvm/00", valid: false}, | 51 {pattern: "6/lxc/42/kvm/00", valid: false}, |
83 {pattern: "06/lxc/042/kvm/00", valid: false}, | 52 {pattern: "06/lxc/042/kvm/00", valid: false}, |
84 } | 53 } |
85 | 54 |
86 func (s *machineSuite) TestMachineIdFormats(c *gc.C) { | 55 func (s *machineSuite) TestMachineIdFormats(c *gc.C) { |
87 for i, test := range machineIdTests { | 56 for i, test := range machineIdTests { |
88 c.Logf("test %d: %q", i, test.pattern) | 57 c.Logf("test %d: %q", i, test.pattern) |
89 c.Assert(names.IsMachine(test.pattern), gc.Equals, test.valid) | 58 c.Assert(names.IsMachine(test.pattern), gc.Equals, test.valid) |
90 } | 59 } |
91 } | 60 } |
LEFT | RIGHT |