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{
234 "template_uuid": true,
237 "image_timestamp": true,
238 "docker-image-repo-tag": true,
240 "container_request": true,
243 Tags: map[string]VocabularyTag{
246 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
247 Values: map[string]VocabularyTagValue{
249 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Homo sapiens"}},
252 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "Loxodonta"}},
255 Labels: []VocabularyLabel{{Label: "Dog"}, {Label: "Canis lupus familiaris"}, {Label: "dOg"}},
263 "Invalid JSON error with line & column numbers",
266 "labels": [,{"label": "A label"}]
269 false, `invalid JSON format:.*\(line \d+, column \d+\)`, nil,
272 "Invalid JSON with duplicate & reserved keys",
276 "labels": [{"label": "Class", "label": "Type"}]
282 false, "(?s).*duplicate JSON key \"tags.type.labels.0.label\"\nduplicate JSON key \"tags.type\"\ntag key \"type\" is reserved", nil,
286 for _, tt := range tests {
287 c.Log(c.TestName()+" ", tt.name)
288 voc, err := NewVocabulary([]byte(tt.data), []string{})
290 c.Assert(err, check.IsNil)
292 c.Assert(err, check.NotNil)
293 if tt.errMatches != "" {
294 c.Assert(err, check.ErrorMatches, tt.errMatches)
297 c.Assert(voc, check.DeepEquals, tt.expect)
301 func (s *VocabularySuite) TestValidationErrors(c *check.C) {
308 "Strict vocabulary, no keys",
312 []string{"vocabulary is strict but no tags are defined"},
315 "Collision between tag key and tag key label",
318 Tags: map[string]VocabularyTag{
321 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
325 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "IDTAGANIMALS"}},
329 nil, // Depending on how the map is sorted, this could be one of two errors
332 "Collision between tag key and tag key label (case-insensitive)",
335 Tags: map[string]VocabularyTag{
338 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
342 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "IdTagAnimals"}},
346 nil, // Depending on how the map is sorted, this could be one of two errors
349 "Collision between tag key labels",
352 Tags: map[string]VocabularyTag{
355 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
359 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "Animal"}},
363 []string{"(?s).*tag label.*for key.*already seen.*"},
366 "Collision between tag value and tag value label",
369 Tags: map[string]VocabularyTag{
372 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
373 Values: map[string]VocabularyTagValue{
375 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
378 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "IDVALANIMAL1"}},
384 nil, // Depending on how the map is sorted, this could be one of two errors
387 "Collision between tag value and tag value label (case-insensitive)",
390 Tags: map[string]VocabularyTag{
393 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
394 Values: map[string]VocabularyTagValue{
396 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
399 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "IDValAnimal1"}},
405 nil, // Depending on how the map is sorted, this could be one of two errors
408 "Collision between tag value labels",
411 Tags: map[string]VocabularyTag{
414 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
415 Values: map[string]VocabularyTagValue{
417 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
420 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "Mammal"}},
426 []string{"(?s).*tag value label.*for pair.*already seen.*on value.*"},
429 "Collision between tag value labels (case-insensitive)",
432 Tags: map[string]VocabularyTag{
435 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
436 Values: map[string]VocabularyTagValue{
438 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
441 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "mAMMAL"}},
447 []string{"(?s).*tag value label.*for pair.*already seen.*on value.*"},
450 "Strict tag key, with no values",
453 Tags: map[string]VocabularyTag{
456 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
460 []string{"(?s).*tag key.*is configured as strict but doesn't provide values"},
463 "Multiple errors reported",
466 Tags: map[string]VocabularyTag{
469 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
472 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Size"}},
477 "(?s).*tag key.*is configured as strict but doesn't provide values.*",
478 "(?s).*tag label.*for key.*already seen.*",
482 for _, tt := range tests {
483 c.Log(c.TestName()+" ", tt.name)
484 validationErrs, err := tt.voc.validate()
485 c.Assert(err, check.NotNil)
486 for _, errMatch := range tt.errMatches {
488 for _, validationErr := range validationErrs {
489 if regexp.MustCompile(errMatch).MatchString(validationErr) {
494 if len(validationErrs) == 0 {
495 c.Assert(err, check.ErrorMatches, errMatch)
497 c.Assert(seen, check.Equals, true,
498 check.Commentf("Expected to see error matching %q:\n%s",
499 errMatch, strings.Join(validationErrs, "\n")))