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 "Invalid value type",
174 `{"IDTAGANIMALS":1}`,
176 "value type for tag key.* was.*, but expected a string or list of strings",
179 "Value list of invalid type",
181 `{"IDTAGANIMALS":[1]}`,
183 "value list element type for tag key.* was.*, but expected a string",
186 for _, tt := range tests {
187 c.Log(c.TestName()+" ", tt.name)
188 s.testVoc.StrictTags = tt.strictVoc
190 var data map[string]interface{}
191 err := json.Unmarshal([]byte(tt.props), &data)
192 c.Assert(err, check.IsNil)
193 err = s.testVoc.Check(data)
194 if tt.expectSuccess {
195 c.Assert(err, check.IsNil)
197 c.Assert(err, check.NotNil)
198 c.Assert(err.Error(), check.Matches, tt.errMatches)
203 func (s *VocabularySuite) TestNewVocabulary(c *check.C) {
211 {"Empty data", "", true, "", &Vocabulary{}},
212 {"Invalid JSON", "foo", false, "invalid JSON format.*", nil},
213 {"Valid, empty JSON", "{}", false, ".*doesn't match Vocabulary format.*", nil},
214 {"Valid JSON, wrong data", `{"foo":"bar"}`, false, ".*doesn't match Vocabulary format.*", nil},
216 "Simple valid example",
220 "labels": [{"label": "Animal"}, {"label": "Creature"}],
222 "IDVALANIMAL1":{"labels":[{"label":"Human"}, {"label":"Homo sapiens"}]},
223 "IDVALANIMAL2":{"labels":[{"label":"Elephant"}, {"label":"Loxodonta"}]}
229 reservedTagKeys: map[string]bool{
231 "template_uuid": true,
234 "image_timestamp": true,
235 "docker-image-repo-tag": true,
237 "container_request": true,
240 Tags: map[string]VocabularyTag{
243 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
244 Values: map[string]VocabularyTagValue{
246 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Homo sapiens"}},
249 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "Loxodonta"}},
257 "Valid data, but uses reserved key",
261 "labels": [{"label": "Type"}]
264 false, "tag key.*is reserved", nil,
268 for _, tt := range tests {
269 c.Log(c.TestName()+" ", tt.name)
270 voc, err := NewVocabulary([]byte(tt.data), []string{})
272 c.Assert(err, check.IsNil)
274 c.Assert(err, check.NotNil)
275 if tt.errMatches != "" {
276 c.Assert(err, check.ErrorMatches, tt.errMatches)
279 c.Assert(voc, check.DeepEquals, tt.expect)
283 func (s *VocabularySuite) TestValidationErrors(c *check.C) {
290 "Strict vocabulary, no keys",
294 "vocabulary is strict but no tags are defined",
297 "Collision between tag key and tag key label",
300 Tags: map[string]VocabularyTag{
303 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
307 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "IDTAGANIMALS"}},
311 "", // Depending on how the map is sorted, this could be one of two errors
314 "Collision between tag key and tag key label (case-insensitive)",
317 Tags: map[string]VocabularyTag{
320 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
324 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "IdTagAnimals"}},
328 "", // Depending on how the map is sorted, this could be one of two errors
331 "Collision between tag key labels",
334 Tags: map[string]VocabularyTag{
337 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
341 Labels: []VocabularyLabel{{Label: "Comment"}, {Label: "Animal"}},
345 "tag label.*for key.*already seen.*",
348 "Collision between tag value and tag value label",
351 Tags: map[string]VocabularyTag{
354 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
355 Values: map[string]VocabularyTagValue{
357 Labels: []VocabularyLabel{{Label: "Human"}, {Label: "Mammal"}},
360 Labels: []VocabularyLabel{{Label: "Elephant"}, {Label: "IDVALANIMAL1"}},
366 "", // Depending on how the map is sorted, this could be one of two errors
369 "Collision between tag value and tag value label (case-insensitive)",
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 "", // Depending on how the map is sorted, this could be one of two errors
390 "Collision between tag value labels",
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: "Mammal"}},
408 "tag value label.*for pair.*already seen.*",
411 "Strict tag key, with no values",
414 Tags: map[string]VocabularyTag{
417 Labels: []VocabularyLabel{{Label: "Animal"}, {Label: "Creature"}},
421 "tag key.*is configured as strict but doesn't provide values",
424 for _, tt := range tests {
425 c.Log(c.TestName()+" ", tt.name)
426 err := tt.voc.validate()
427 c.Assert(err, check.NotNil)
428 if tt.errMatches != "" {
429 c.Assert(err, check.ErrorMatches, tt.errMatches)