1
16
17 package parsers
18
19 import (
20 "strings"
21 "testing"
22 )
23
24
25
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 }
61
View as plain text