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) TestValidationErrors(c *check.C) {
311 "Strict vocabulary, no keys",
315 []string{"vocabulary is strict but no tags are defined"},
318 "Collision between tag key and tag key label",
321 Tags: map[string]VocabularyTag{
324 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
328 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "IDTAGANIMALS"}},
332 nil, // Depending on how the map is sorted, this could be one of two errors
335 "Collision between tag key and tag key label (case-insensitive)",
338 Tags: map[string]VocabularyTag{
341 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
345 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "IdTagAnimals"}},
349 nil, // Depending on how the map is sorted, this could be one of two errors
352 "Collision between tag key labels",
355 Tags: map[string]VocabularyTag{
358 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
362 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "Animal"}},
366 []string{"(?s).*tag label.*for key.*already seen.*"},
369 "Collision between tag value and tag value label",
372 Tags: map[string]VocabularyTag{
375 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
376 Values: map[string]VocabularyTagValue{
378 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
381 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "IDVALANIMAL1"}},
387 nil, // Depending on how the map is sorted, this could be one of two errors
390 "Collision between tag value and tag value label (case-insensitive)",
393 Tags: map[string]VocabularyTag{
396 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
397 Values: map[string]VocabularyTagValue{
399 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
402 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "IDValAnimal1"}},
408 nil, // Depending on how the map is sorted, this could be one of two errors
411 "Collision between tag value labels",
414 Tags: map[string]VocabularyTag{
417 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
418 Values: map[string]VocabularyTagValue{
420 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
423 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "Mammal"}},
429 []string{"(?s).*tag value label.*for pair.*already seen.*on value.*"},
432 "Collision between tag value labels (case-insensitive)",
435 Tags: map[string]VocabularyTag{
438 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
439 Values: map[string]VocabularyTagValue{
441 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
444 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "mAMMAL"}},
450 []string{"(?s).*tag value label.*for pair.*already seen.*on value.*"},
453 "Strict tag key, with no values",
456 Tags: map[string]VocabularyTag{
459 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
463 []string{"(?s).*tag key.*is configured as strict but doesn't provide values"},
466 "Multiple errors reported",
469 Tags: map[string]VocabularyTag{
472 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
475 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Size"}},
480 "(?s).*tag key.*is configured as strict but doesn't provide values.*",
481 "(?s).*tag label.*for key.*already seen.*",
485 for _, tt := range tests {
486 c.Log(c.TestName()+" ", tt.name)
487 validationErrs, err := tt.voc.validate()
488 c.Assert(err, check.NotNil)
489 for _, errMatch := range tt.errMatches {
491 for _, validationErr := range validationErrs {
492 if regexp.MustCompile(errMatch).MatchString(validationErr) {
497 if len(validationErrs) == 0 {
498 c.Assert(err, check.ErrorMatches, errMatch)
500 c.Assert(seen, check.Equals, true,
501 check.Commentf("Expected to see error matching %q:\n%s",
502 errMatch, strings.Join(validationErrs, "\n")))