1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
12 check "gopkg.in/check.v1"
15 type VocabularySuite struct {
19 var _ = check.Suite(&VocabularySuite{})
21 func (s *VocabularySuite) SetUpTest(c *check.C) {
22 s.testVoc = &Vocabulary{
23 reservedTagKeys: map[string]bool{
27 Tags: map[string]VocabularyTag{
30 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
31 Values: map[string]VocabularyTagValue{
33 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Homo sapiens"}},
36 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "Loxodonta"}},
42 Labels: []VocabularyLabel{{Label: "Importance"}, {Label: "Priority"}},
43 Values: map[string]VocabularyTagValue{
45 Labels: []VocabularyLabel{{Label: "Low"}, {Label: "Low priority"}},
48 Labels: []VocabularyLabel{{Label: "Medium"}, {Label: "Medium priority"}},
51 Labels: []VocabularyLabel{{Label: "High"}, {Label: "High priority"}},
57 Labels: []VocabularyLabel{{Label: "Comment"}},
61 _, err := s.testVoc.validate()
62 c.Assert(err, check.IsNil)
65 func (s *VocabularySuite) TestCheck(c *check.C) {
75 "Known key, known value",
77 `{"IDTAGANIMALS":"IDVALANIMAL1"}`,
82 "Unknown non-alias key on non-strict vocabulary",
89 "Known non-strict key, unknown non-alias value",
91 `{"IDTAGANIMALS":"IDVALANIMAL3"}`,
96 "Undefined but reserved key on strict vocabulary",
98 `{"reservedKey":"bar"}`,
103 "Known key, list of known values",
105 `{"IDTAGANIMALS":["IDVALANIMAL1","IDVALANIMAL2"]}`,
110 "Known non-strict key, list of unknown non-alias values",
112 `{"IDTAGCOMMENT":["hello world","lorem ipsum"]}`,
118 "Known first key & value; known 2nd key, unknown 2nd value",
120 `{"IDTAGANIMALS":"IDVALANIMAL1", "IDTAGIMPORTANCE": "blah blah"}`,
122 "tag value.*is not valid for key.*",
125 "Unknown non-alias key on strict vocabulary",
129 "tag key.*is not defined in the vocabulary",
132 "Known non-strict key, known value alias",
134 `{"IDTAGANIMALS":"Loxodonta"}`,
136 "tag value.*for key.* is an alias, must be provided as.*",
139 "Known strict key, unknown non-alias value",
141 `{"IDTAGIMPORTANCE":"Unimportant"}`,
143 "tag value.*is not valid for key.*",
146 "Known strict key, lowercase value regarded as alias",
148 `{"IDTAGIMPORTANCE":"idval1"}`,
150 "tag value.*for key.* is an alias, must be provided as.*",
153 "Known strict key, known value alias",
155 `{"IDTAGIMPORTANCE":"High"}`,
157 "tag value.* for key.*is an alias, must be provided as.*",
160 "Known strict key, list of known alias values",
162 `{"IDTAGIMPORTANCE":["High", "Low"]}`,
164 "tag value.*for key.*is an alias, must be provided as.*",
167 "Known strict key, list of unknown non-alias values",
169 `{"IDTAGIMPORTANCE":["foo","bar"]}`,
171 "tag value.*is not valid for key.*",
174 "Invalid value type",
176 `{"IDTAGANIMALS":1}`,
178 "value type for tag key.* was.*, but expected a string or list of strings",
181 "Value list of invalid type",
183 `{"IDTAGANIMALS":[1]}`,
185 "value list element type for tag key.* was.*, but expected a string",
188 for _, tt := range tests {
189 c.Log(c.TestName()+" ", tt.name)
190 s.testVoc.StrictTags = tt.strictVoc
192 var data map[string]interface{}
193 err := json.Unmarshal([]byte(tt.props), &data)
194 c.Assert(err, check.IsNil)
195 err = s.testVoc.Check(data)
196 if tt.expectSuccess {
197 c.Assert(err, check.IsNil)
199 c.Assert(err, check.NotNil)
200 c.Assert(err.Error(), check.Matches, tt.errMatches)
205 func (s *VocabularySuite) TestNewVocabulary(c *check.C) {
213 {"Empty data", "", true, "", &Vocabulary{}},
214 {"Invalid JSON", "foo", false, "invalid JSON format.*", nil},
215 {"Valid, empty JSON", "{}", false, ".*doesn't match Vocabulary format.*", nil},
216 {"Valid JSON, wrong data", `{"foo":"bar"}`, false, ".*doesn't match Vocabulary format.*", nil},
218 "Simple valid example",
222 "labels": [{"label": "Animal"}, {"label": "Creature"}],
224 "IDVALANIMAL1":{"labels":[{"label":"Human"}, {"label":"Homo sapiens"}]},
225 "IDVALANIMAL2":{"labels":[{"label":"Elephant"}, {"label":"Loxodonta"}]},
226 "DOG":{"labels":[{"label":"Dog"}, {"label":"Canis lupus familiaris"}, {"label":"dOg"}]}
232 reservedTagKeys: map[string]bool{
233 "container_request": true,
234 "container_uuid": true,
237 "docker-image-repo-tag": true,
240 "image_timestamp": true,
241 "template_uuid": true,
246 Tags: map[string]VocabularyTag{
249 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
250 Values: map[string]VocabularyTagValue{
252 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Homo sapiens"}},
255 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "Loxodonta"}},
258 Labels: []VocabularyLabel{{Label: "Dog"}, {Label: "Canis lupus familiaris"}, {Label: "dOg"}},
266 "Invalid JSON error with line & column numbers",
269 "labels": [,{"label": "A label"}]
272 false, `invalid JSON format:.*\(line \d+, column \d+\)`, nil,
275 "Invalid JSON with duplicate & reserved keys",
279 "labels": [{"label": "Class", "label": "Type"}]
285 false, "(?s).*duplicate JSON key \"tags.type.labels.0.label\"\nduplicate JSON key \"tags.type\"\ntag key \"type\" is reserved", nil,
289 for _, tt := range tests {
290 c.Log(c.TestName()+" ", tt.name)
291 voc, err := NewVocabulary([]byte(tt.data), []string{})
293 c.Assert(err, check.IsNil)
295 c.Assert(err, check.NotNil)
296 if tt.errMatches != "" {
297 c.Assert(err, check.ErrorMatches, tt.errMatches)
300 c.Assert(voc, check.DeepEquals, tt.expect)
304 func (s *VocabularySuite) TestValidSystemProperties(c *check.C) {
305 s.testVoc.StrictTags = true
306 properties := map[string]interface{}{
307 "arv:gitBranch": "main",
311 c.Check(s.testVoc.Check(properties), check.IsNil)
314 func (s *VocabularySuite) TestSystemPropertiesPrefixTypo(c *check.C) {
315 s.testVoc.StrictTags = true
316 for _, key := range []string{
317 // Extra characters in prefix
330 properties := map[string]interface{}{key: "value"}
331 c.Check(s.testVoc.Check(properties), check.NotNil)
335 func (s *VocabularySuite) TestValidationErrors(c *check.C) {
342 "Strict vocabulary, no keys",
346 []string{"vocabulary is strict but no tags are defined"},
349 "Collision between tag key and tag key label",
352 Tags: map[string]VocabularyTag{
355 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
359 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "IDTAGANIMALS"}},
363 nil, // Depending on how the map is sorted, this could be one of two errors
366 "Collision between tag key and tag key label (case-insensitive)",
369 Tags: map[string]VocabularyTag{
372 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
376 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "IdTagAnimals"}},
380 nil, // Depending on how the map is sorted, this could be one of two errors
383 "Collision between tag key labels",
386 Tags: map[string]VocabularyTag{
389 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
393 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "Animal"}},
397 []string{"(?s).*tag label.*for key.*already seen.*"},
400 "Collision between tag value and tag value label",
403 Tags: map[string]VocabularyTag{
406 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
407 Values: map[string]VocabularyTagValue{
409 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
412 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "IDVALANIMAL1"}},
418 nil, // Depending on how the map is sorted, this could be one of two errors
421 "Collision between tag value and tag value label (case-insensitive)",
424 Tags: map[string]VocabularyTag{
427 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
428 Values: map[string]VocabularyTagValue{
430 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
433 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "IDValAnimal1"}},
439 nil, // Depending on how the map is sorted, this could be one of two errors
442 "Collision between tag value labels",
445 Tags: map[string]VocabularyTag{
448 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
449 Values: map[string]VocabularyTagValue{
451 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
454 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "Mammal"}},
460 []string{"(?s).*tag value label.*for pair.*already seen.*on value.*"},
463 "Collision between tag value labels (case-insensitive)",
466 Tags: map[string]VocabularyTag{
469 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
470 Values: map[string]VocabularyTagValue{
472 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
475 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "mAMMAL"}},
481 []string{"(?s).*tag value label.*for pair.*already seen.*on value.*"},
484 "Strict tag key, with no values",
487 Tags: map[string]VocabularyTag{
490 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
494 []string{"(?s).*tag key.*is configured as strict but doesn't provide values"},
497 "Multiple errors reported",
500 Tags: map[string]VocabularyTag{
503 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
506 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Size"}},
511 "(?s).*tag key.*is configured as strict but doesn't provide values.*",
512 "(?s).*tag label.*for key.*already seen.*",
516 for _, tt := range tests {
517 c.Log(c.TestName()+" ", tt.name)
518 validationErrs, err := tt.voc.validate()
519 c.Assert(err, check.NotNil)
520 for _, errMatch := range tt.errMatches {
522 for _, validationErr := range validationErrs {
523 if regexp.MustCompile(errMatch).MatchString(validationErr) {
528 if len(validationErrs) == 0 {
529 c.Assert(err, check.ErrorMatches, errMatch)
531 c.Assert(seen, check.Equals, true,
532 check.Commentf("Expected to see error matching %q:\n%s",
533 errMatch, strings.Join(validationErrs, "\n")))