1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package name
16
17 import (
18 "errors"
19 "strings"
20 "testing"
21 )
22
23 var goodStrictValidationRepositoryNames = []string{
24 "gcr.io/g-convoy/hello-world",
25 "gcr.io/google.com/project-id/hello-world",
26 "us.gcr.io/project-id/sub-repo",
27 "example.text/foo/bar",
28 "mirror.gcr.io/ubuntu",
29 "index.docker.io/library/ubuntu",
30 }
31
32 var goodWeakValidationRepositoryNames = []string{
33 "namespace/pathcomponent/image",
34 "library/ubuntu",
35 "ubuntu",
36 }
37
38 var badRepositoryNames = []string{
39 "white space",
40 "b@char/image",
41 "",
42 }
43
44 func TestNewRepositoryStrictValidation(t *testing.T) {
45 t.Parallel()
46
47 for _, name := range goodStrictValidationRepositoryNames {
48 if repository, err := NewRepository(name, StrictValidation); err != nil {
49 t.Errorf("`%s` should be a valid Repository name, got error: %v", name, err)
50 } else if repository.Name() != name {
51 t.Errorf("`%v` .Name() should reproduce the original name. Wanted: %s Got: %s", repository, name, repository.Name())
52 }
53 }
54
55 for _, name := range append(goodWeakValidationRepositoryNames, badRepositoryNames...) {
56 if repo, err := NewRepository(name, StrictValidation); err == nil {
57 t.Errorf("`%s` should be an invalid repository name, got Repository: %#v", name, repo)
58 }
59 }
60 }
61
62 func TestNewRepository(t *testing.T) {
63 t.Parallel()
64
65 for _, name := range append(goodStrictValidationRepositoryNames, goodWeakValidationRepositoryNames...) {
66 if _, err := NewRepository(name, WeakValidation); err != nil {
67 t.Errorf("`%s` should be a valid repository name, got error: %v", name, err)
68 }
69 }
70
71 for _, name := range badRepositoryNames {
72 if repo, err := NewRepository(name, WeakValidation); err == nil {
73 t.Errorf("`%s` should be an invalid repository name, got Repository: %#v", name, repo)
74 }
75 }
76 }
77
78 func TestRepositoryComponents(t *testing.T) {
79 t.Parallel()
80 testRegistry := "gcr.io"
81 testRepository := "project-id/image"
82
83 repositoryNameStr := testRegistry + "/" + testRepository
84 repository, err := NewRepository(repositoryNameStr, StrictValidation)
85 if err != nil {
86 t.Fatalf("`%s` should be a valid Repository name, got error: %v", repositoryNameStr, err)
87 }
88
89 actualRegistry := repository.RegistryStr()
90 if actualRegistry != testRegistry {
91 t.Errorf("RegistryStr() was incorrect for %v. Wanted: `%s` Got: `%s`", repository, testRegistry, actualRegistry)
92 }
93 actualRepository := repository.RepositoryStr()
94 if actualRepository != testRepository {
95 t.Errorf("RepositoryStr() was incorrect for %v. Wanted: `%s` Got: `%s`", repository, testRepository, actualRepository)
96 }
97 }
98
99 func TestRepositoryScopes(t *testing.T) {
100 t.Parallel()
101 testRegistry := "gcr.io"
102 testRepo := "project-id/image"
103 testAction := "pull"
104
105 expectedScope := strings.Join([]string{"repository", testRepo, testAction}, ":")
106
107 repositoryNameStr := testRegistry + "/" + testRepo
108 repository, err := NewRepository(repositoryNameStr, StrictValidation)
109 if err != nil {
110 t.Fatalf("`%s` should be a valid Repository name, got error: %v", repositoryNameStr, err)
111 }
112
113 actualScope := repository.Scope(testAction)
114 if actualScope != expectedScope {
115 t.Errorf("scope was incorrect for %v. Wanted: `%s` Got: `%s`", repository, expectedScope, actualScope)
116 }
117 }
118
119 func TestRepositoryBadDefaulting(t *testing.T) {
120 var berr *ErrBadName
121 if _, err := NewRepository("index.docker.io/foo", StrictValidation); !errors.As(err, &berr) {
122 t.Errorf("Not an ErrBadName: %v", err)
123 }
124 }
125
126 func TestRepositoryChildren(t *testing.T) {
127 repo, err := NewRepository("example.com/repo", Insecure)
128 if err != nil {
129 t.Fatal(err)
130 }
131 tag := repo.Tag("foo")
132 if got, want := tag.Scheme(), "http"; got != want {
133 t.Errorf("tag.Scheme(): got %s want %s", got, want)
134 }
135 if got, want := tag.String(), "example.com/repo:foo"; got != want {
136 t.Errorf("tag.String(): got %s want %s", got, want)
137 }
138 digest := repo.Digest("badf00d")
139 if got, want := digest.Scheme(), "http"; got != want {
140 t.Errorf("digest.Scheme(): got %s want %s", got, want)
141 }
142 if got, want := digest.String(), "example.com/repo@badf00d"; got != want {
143 t.Errorf("digest.String(): got %s want %s", got, want)
144 }
145 }
146
View as plain text