k8s.io/kubernetes@v1.29.3/pkg/util/parsers/parsers_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package parsers 18 19 import ( 20 "strings" 21 "testing" 22 ) 23 24 // Based on Docker test case removed in: 25 // https://github.com/docker/docker/commit/4352da7803d182a6013a5238ce20a7c749db979a 26 func TestParseImageName(t *testing.T) { 27 testCases := []struct { 28 Input string 29 Repo string 30 Tag string 31 Digest string 32 expectedError string 33 }{ 34 {Input: "root", Repo: "docker.io/library/root", Tag: "latest"}, 35 {Input: "root:tag", Repo: "docker.io/library/root", Tag: "tag"}, 36 {Input: "root@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Repo: "docker.io/library/root", Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}, 37 {Input: "user/repo", Repo: "docker.io/user/repo", Tag: "latest"}, 38 {Input: "user/repo:tag", Repo: "docker.io/user/repo", Tag: "tag"}, 39 {Input: "user/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Repo: "docker.io/user/repo", Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}, 40 {Input: "url:5000/repo", Repo: "url:5000/repo", Tag: "latest"}, 41 {Input: "url:5000/repo:tag", Repo: "url:5000/repo", Tag: "tag"}, 42 {Input: "url:5000/repo@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Repo: "url:5000/repo", Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}, 43 {Input: "url:5000/repo:latest@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", Tag: "latest", Repo: "url:5000/repo", Digest: "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"}, 44 {Input: "ROOT", expectedError: "must be lowercase"}, 45 {Input: "http://root", expectedError: "invalid reference format"}, 46 {Input: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", expectedError: "cannot specify 64-byte hexadecimal strings"}, 47 } 48 for _, testCase := range testCases { 49 repo, tag, digest, err := ParseImageName(testCase.Input) 50 switch { 51 case testCase.expectedError != "" && !strings.Contains(err.Error(), testCase.expectedError): 52 t.Errorf("ParseImageName(%s) expects error %v but did not get one", testCase.Input, err) 53 case testCase.expectedError == "" && err != nil: 54 t.Errorf("ParseImageName(%s) failed: %v", testCase.Input, err) 55 case repo != testCase.Repo || tag != testCase.Tag || digest != testCase.Digest: 56 t.Errorf("Expected repo: %q, tag: %q and digest: %q, got %q, %q and %q", testCase.Repo, testCase.Tag, testCase.Digest, 57 repo, tag, digest) 58 } 59 } 60 }