diff --git a/cmd/oras/root/manifest/index/create_test.go b/cmd/oras/root/manifest/index/create_test.go new file mode 100644 index 000000000..4b2d16f5d --- /dev/null +++ b/cmd/oras/root/manifest/index/create_test.go @@ -0,0 +1,56 @@ +/* +Copyright The ORAS Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package index + +import ( + "reflect" + "testing" +) + +func Test_parseAnnotations(t *testing.T) { + tests := []struct { + name string + input []string + annotations map[string]string + wantErr bool + wantAnnotations map[string]string + }{ + { + name: "valid input", + input: []string{"a=b", "c=d", "e=f"}, + annotations: make(map[string]string), + wantErr: false, + wantAnnotations: map[string]string{"a": "b", "c": "d", "e": "f"}, + }, + { + name: "invalid input", + input: []string{"c:d", "e=f"}, + annotations: make(map[string]string), + wantErr: true, + wantAnnotations: map[string]string{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := parseAnnotations(tt.input, tt.annotations); (err != nil) != tt.wantErr { + t.Errorf("parseAnnotations() error = %v, wantErr %v", err, tt.wantErr) + } + if !reflect.DeepEqual(tt.annotations, tt.wantAnnotations) { + t.Errorf("parseAnnotations() annotations = %v, want %v", tt.annotations, tt.wantAnnotations) + } + }) + } +} diff --git a/test/e2e/suite/command/manifest_index.go b/test/e2e/suite/command/manifest_index.go index 336e2bae5..e90a8f2ed 100644 --- a/test/e2e/suite/command/manifest_index.go +++ b/test/e2e/suite/command/manifest_index.go @@ -172,6 +172,14 @@ var _ = Describe("1.1 registry users:", func() { "does-not-exist").ExpectFailure(). MatchErrKeyWords("Error", "could not find", "does-not-exist").Exec() }) + + It("should fail if given annotation input of wrong format", func() { + testRepo := indexTestRepo("create", "bad-annotations") + CopyZOTRepo(ImageRepo, testRepo) + ORAS("manifest", "index", "create", RegistryRef(ZOTHost, testRepo, ""), + string(multi_arch.LinuxAMD64.Digest), "-a", "foo:bar").ExpectFailure(). + MatchErrKeyWords("Error", "annotation value doesn't match the required format").Exec() + }) }) When("running `manifest index update`", func() {