1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
10 check "gopkg.in/check.v1"
13 type VocabularySuite struct {
17 var _ = check.Suite(&VocabularySuite{})
19 func (s *VocabularySuite) SetUpTest(c *check.C) {
20 s.testVoc = &Vocabulary{
21 reservedTagKeys: map[string]bool{
25 Tags: map[string]VocabularyTag{
28 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
29 Values: map[string]VocabularyTagValue{
31 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Homo sapiens"}},
34 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "Loxodonta"}},
40 Labels: []VocabularyLabel{{Label: "Importance"}, {Label: "Priority"}},
41 Values: map[string]VocabularyTagValue{
43 Labels: []VocabularyLabel{{Label: "Low"}, {Label: "Low priority"}},
46 Labels: []VocabularyLabel{{Label: "Medium"}, {Label: "Medium priority"}},
49 Labels: []VocabularyLabel{{Label: "High"}, {Label: "High priority"}},
55 Labels: []VocabularyLabel{{Label: "Comment"}},
59 err := s.testVoc.validate()
60 c.Assert(err, check.IsNil)
63 func (s *VocabularySuite) TestCheck(c *check.C) {
73 "Known key, known value",
75 `{"IDTAGANIMALS":"IDVALANIMAL1"}`,
80 "Unknown non-alias key on non-strict vocabulary",
87 "Known non-strict key, unknown non-alias value",
89 `{"IDTAGANIMALS":"IDVALANIMAL3"}`,
94 "Undefined but reserved key on strict vocabulary",
96 `{"reservedKey":"bar"}`,
101 "Known key, list of known values",
103 `{"IDTAGANIMALS":["IDVALANIMAL1","IDVALANIMAL2"]}`,
108 "Known non-strict key, list of unknown non-alias values",
110 `{"IDTAGCOMMENT":["hello world","lorem ipsum"]}`,
116 "Known first key & value; known 2nd key, unknown 2nd value",
118 `{"IDTAGANIMALS":"IDVALANIMAL1", "IDTAGIMPORTANCE": "blah blah"}`,
120 "tag value.*is not valid for key.*",
123 "Unknown non-alias key on strict vocabulary",
127 "tag key.*is not defined in the vocabulary",
130 "Known non-strict key, known value alias",
132 `{"IDTAGANIMALS":"Loxodonta"}`,
134 "tag value.*for key.* is an alias, must be provided as.*",
137 "Known strict key, unknown non-alias value",
139 `{"IDTAGIMPORTANCE":"Unimportant"}`,
141 "tag value.*is not valid for key.*",
144 "Known strict key, lowercase value regarded as alias",
146 `{"IDTAGIMPORTANCE":"idval1"}`,
148 "tag value.*for key.* is an alias, must be provided as.*",
151 "Known strict key, known value alias",
153 `{"IDTAGIMPORTANCE":"High"}`,
155 "tag value.* for key.*is an alias, must be provided as.*",
158 "Known strict key, list of known alias values",
160 `{"IDTAGIMPORTANCE":["High", "Low"]}`,
162 "tag value.*for key.*is an alias, must be provided as.*",
165 "Known strict key, list of unknown non-alias values",
167 `{"IDTAGIMPORTANCE":["foo","bar"]}`,
169 "tag value.*is not valid for key.*",
172 for _, tt := range tests {
173 c.Log(c.TestName()+" ", tt.name)
174 s.testVoc.StrictTags = tt.strictVoc
176 var data map[string]interface{}
177 err := json.Unmarshal([]byte(tt.props), &data)
178 c.Assert(err, check.IsNil)
179 err = s.testVoc.Check(data)
180 if tt.expectSuccess {
181 c.Assert(err, check.IsNil)
183 c.Assert(err, check.NotNil)
184 c.Assert(err.Error(), check.Matches, tt.errMatches)
189 func (s *VocabularySuite) TestNewVocabulary(c *check.C) {
197 {"Empty data", "", true, "", &Vocabulary{}},
198 {"Invalid JSON", "foo", false, "invalid JSON format.*", nil},
199 {"Valid, empty JSON", "{}", false, ".*doesn't match Vocabulary format.*", nil},
200 {"Valid JSON, wrong data", `{"foo":"bar"}`, false, ".*doesn't match Vocabulary format.*", nil},
202 "Simple valid example",
206 "labels": [{"label": "Animal"}, {"label": "Creature"}],
208 "IDVALANIMAL1":{"labels":[{"label":"Human"}, {"label":"Homo sapiens"}]},
209 "IDVALANIMAL2":{"labels":[{"label":"Elephant"}, {"label":"Loxodonta"}]}
215 reservedTagKeys: map[string]bool{
217 "template_uuid": true,
220 "image_timestamp": true,
221 "docker-image-repo-tag": true,
223 "container_request": true,
226 Tags: map[string]VocabularyTag{
229 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
230 Values: map[string]VocabularyTagValue{
232 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Homo sapiens"}},
235 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "Loxodonta"}},
243 "Valid data, but uses reserved key",
247 "labels": [{"label": "Type"}]
250 false, "tag key.*is reserved", nil,
254 for _, tt := range tests {
255 c.Log(c.TestName()+" ", tt.name)
256 voc, err := NewVocabulary([]byte(tt.data), []string{})
258 c.Assert(err, check.IsNil)
260 c.Assert(err, check.NotNil)
261 if tt.errMatches != "" {
262 c.Assert(err, check.ErrorMatches, tt.errMatches)
265 c.Assert(voc, check.DeepEquals, tt.expect)
269 func (s *VocabularySuite) TestValidationErrors(c *check.C) {
276 "Strict vocabulary, no keys",
280 "vocabulary is strict but no tags are defined",
283 "Collision between tag key and tag key label",
286 Tags: map[string]VocabularyTag{
289 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
293 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "IDTAGANIMALS"}},
297 "", // Depending on how the map is sorted, this could be one of two errors
300 "Collision between tag key and tag key label (case-insensitive)",
303 Tags: map[string]VocabularyTag{
306 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
310 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "IdTagAnimals"}},
314 "", // Depending on how the map is sorted, this could be one of two errors
317 "Collision between tag key labels",
320 Tags: map[string]VocabularyTag{
323 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
327 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "Animal"}},
331 "tag label.*for key.*already seen.*",
334 "Collision between tag value and tag value label",
337 Tags: map[string]VocabularyTag{
340 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
341 Values: map[string]VocabularyTagValue{
343 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
346 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "IDVALANIMAL1"}},
352 "", // Depending on how the map is sorted, this could be one of two errors
355 "Collision between tag value and tag value label (case-insensitive)",
358 Tags: map[string]VocabularyTag{
361 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
362 Values: map[string]VocabularyTagValue{
364 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
367 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "IDValAnimal1"}},
373 "", // Depending on how the map is sorted, this could be one of two errors
376 "Collision between tag value labels",
379 Tags: map[string]VocabularyTag{
382 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
383 Values: map[string]VocabularyTagValue{
385 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
388 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "Mammal"}},
394 "tag value label.*for pair.*already seen.*",
397 "Strict tag key, with no values",
400 Tags: map[string]VocabularyTag{
403 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
407 "tag key.*is configured as strict but doesn't provide values",
410 for _, tt := range tests {
411 c.Log(c.TestName()+" ", tt.name)
412 err := tt.voc.validate()
413 c.Assert(err, check.NotNil)
414 if tt.errMatches != "" {
415 c.Assert(err, check.ErrorMatches, tt.errMatches)