OLD | NEW |
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 gc "launchpad.net/gocheck" | 7 gc "launchpad.net/gocheck" |
8 | 8 |
9 "launchpad.net/juju-core/names" | 9 "launchpad.net/juju-core/names" |
10 ) | 10 ) |
11 | 11 |
12 type tagKindSuite struct{} | 12 type tagSuite struct{} |
13 | 13 |
14 var _ = gc.Suite(&tagKindSuite{}) | 14 var _ = gc.Suite(&tagSuite{}) |
15 | 15 |
16 var tagKindTests = []struct { | 16 var tagKindTests = []struct { |
17 tag string | 17 tag string |
18 kind string | 18 kind string |
19 err string | 19 err string |
20 }{ | 20 }{ |
21 {tag: "unit-wordpress-42", kind: names.UnitTagKind}, | 21 {tag: "unit-wordpress-42", kind: names.UnitTagKind}, |
22 {tag: "machine-42", kind: names.MachineTagKind}, | 22 {tag: "machine-42", kind: names.MachineTagKind}, |
23 {tag: "service-foo", kind: names.ServiceTagKind}, | 23 {tag: "service-foo", kind: names.ServiceTagKind}, |
24 {tag: "environment-42", kind: names.EnvironTagKind}, | 24 {tag: "environment-42", kind: names.EnvironTagKind}, |
25 {tag: "user-admin", kind: names.UserTagKind}, | 25 {tag: "user-admin", kind: names.UserTagKind}, |
26 {tag: "foo", err: `"foo" is not a valid tag`}, | 26 {tag: "foo", err: `"foo" is not a valid tag`}, |
27 {tag: "unit", err: `"unit" is not a valid tag`}, | 27 {tag: "unit", err: `"unit" is not a valid tag`}, |
28 } | 28 } |
29 | 29 |
30 func (s *tagKindSuite) TestTagKind(c *gc.C) { | 30 func (*tagSuite) TestTagKind(c *gc.C) { |
31 for i, test := range tagKindTests { | 31 for i, test := range tagKindTests { |
32 c.Logf("test %d: %q -> %q", i, test.tag, test.kind) | 32 c.Logf("test %d: %q -> %q", i, test.tag, test.kind) |
33 kind, err := names.TagKind(test.tag) | 33 kind, err := names.TagKind(test.tag) |
34 if test.err == "" { | 34 if test.err == "" { |
35 c.Assert(test.kind, gc.Equals, kind) | 35 c.Assert(test.kind, gc.Equals, kind) |
36 c.Assert(err, gc.IsNil) | 36 c.Assert(err, gc.IsNil) |
37 } else { | 37 } else { |
38 c.Assert(kind, gc.Equals, "") | 38 c.Assert(kind, gc.Equals, "") |
39 c.Assert(err, gc.ErrorMatches, test.err) | 39 c.Assert(err, gc.ErrorMatches, test.err) |
40 } | 40 } |
41 } | 41 } |
42 } | 42 } |
| 43 |
| 44 var parseTagTests = []struct { |
| 45 tag string |
| 46 expectKind string |
| 47 resultId string |
| 48 resultErr string |
| 49 }{{ |
| 50 tag: "machine-10", |
| 51 expectKind: names.MachineTagKind, |
| 52 resultId: "10", |
| 53 }, { |
| 54 tag: "machine-10-lxc-1", |
| 55 expectKind: names.MachineTagKind, |
| 56 resultId: "10/lxc/1", |
| 57 }, { |
| 58 tag: "foo", |
| 59 expectKind: names.MachineTagKind, |
| 60 resultErr: `"foo" is not a valid machine tag`, |
| 61 }, { |
| 62 tag: "machine-#", |
| 63 expectKind: names.MachineTagKind, |
| 64 resultErr: `"machine-#" is not a valid machine tag`, |
| 65 }, { |
| 66 tag: "unit-wordpress-0", |
| 67 expectKind: names.UnitTagKind, |
| 68 resultId: "wordpress/0", |
| 69 }, { |
| 70 tag: "unit-rabbitmq-server-0", |
| 71 expectKind: names.UnitTagKind, |
| 72 resultId: "rabbitmq-server/0", |
| 73 }, { |
| 74 tag: "foo", |
| 75 expectKind: names.UnitTagKind, |
| 76 resultErr: `"foo" is not a valid unit tag`, |
| 77 }, { |
| 78 tag: "unit-#", |
| 79 expectKind: names.UnitTagKind, |
| 80 resultErr: `"unit-#" is not a valid unit tag`, |
| 81 }, { |
| 82 tag: "service-wordpress", |
| 83 expectKind: names.ServiceTagKind, |
| 84 resultId: "wordpress", |
| 85 }, { |
| 86 tag: "service-#", |
| 87 expectKind: names.ServiceTagKind, |
| 88 resultErr: `"service-#" is not a valid service tag`, |
| 89 }, { |
| 90 tag: "unit-wordpress-0", |
| 91 expectKind: "machine", |
| 92 resultErr: `"unit-wordpress-0" is not a valid machine tag`, |
| 93 }, { |
| 94 tag: "environment-foo", |
| 95 expectKind: names.EnvironTagKind, |
| 96 resultId: "foo", |
| 97 }, { |
| 98 tag: "environment-/", |
| 99 expectKind: names.EnvironTagKind, |
| 100 resultErr: `"environment-/" is not a valid environment tag`, |
| 101 }, { |
| 102 tag: "user-foo", |
| 103 expectKind: names.UserTagKind, |
| 104 resultId: "foo", |
| 105 }, { |
| 106 tag: "user-/", |
| 107 expectKind: names.UserTagKind, |
| 108 resultErr: `"user-/" is not a valid user tag`, |
| 109 }, { |
| 110 tag: "foo", |
| 111 expectKind: "", |
| 112 resultErr: `"foo" is not a valid tag`, |
| 113 }} |
| 114 |
| 115 var makeTag = map[string]func(id string) string{ |
| 116 names.MachineTagKind: names.MachineTag, |
| 117 names.UnitTagKind: names.UnitTag, |
| 118 names.ServiceTagKind: names.ServiceTag, |
| 119 // TODO(rog) environment and user, when they have Tag functions. |
| 120 } |
| 121 |
| 122 func (*tagSuite) TestParseTag(c *gc.C) { |
| 123 for i, test := range parseTagTests { |
| 124 c.Logf("test %d: %q expectKind %q", i, test.tag, test.expectKind
) |
| 125 kind, id, err := names.ParseTag(test.tag, test.expectKind) |
| 126 if test.resultErr != "" { |
| 127 c.Assert(err, gc.ErrorMatches, test.resultErr) |
| 128 c.Assert(kind, gc.Equals, "") |
| 129 c.Assert(id, gc.Equals, "") |
| 130 } else { |
| 131 c.Assert(err, gc.IsNil) |
| 132 c.Assert(id, gc.Equals, test.resultId) |
| 133 if test.expectKind != "" { |
| 134 c.Assert(kind, gc.Equals, test.expectKind) |
| 135 } else { |
| 136 expectKind, err := names.TagKind(test.tag) |
| 137 c.Assert(err, gc.IsNil) |
| 138 c.Assert(kind, gc.Equals, expectKind) |
| 139 } |
| 140 // Check that it's reversible. |
| 141 if f := makeTag[kind]; f != nil { |
| 142 reversed := f(id) |
| 143 c.Assert(reversed, gc.Equals, test.tag) |
| 144 } |
| 145 // Check that it parses ok without an expectKind. |
| 146 kind1, id1, err1 := names.ParseTag(test.tag, "") |
| 147 c.Assert(err1, gc.IsNil) |
| 148 c.Assert(kind1, gc.Equals, test.expectKind) |
| 149 c.Assert(id1, gc.Equals, id) |
| 150 } |
| 151 } |
| 152 } |
OLD | NEW |