1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
// Copyright 2018 Google LLC All Rights Reserved.
//
// 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 name
import (
"testing"
)
var goodStrictValidationRegistryNames = []string{
"gcr.io",
"gcr.io:9001",
"index.docker.io",
"us.gcr.io",
"example.text",
"localhost",
"localhost:9090",
}
var goodWeakValidationRegistryNames = []string{
"",
}
var badRegistryNames = []string{
"white space",
"gcr?com",
}
func TestNewRegistryStrictValidation(t *testing.T) {
t.Parallel()
for _, name := range goodStrictValidationRegistryNames {
if registry, err := NewRegistry(name, StrictValidation); err != nil {
t.Errorf("`%s` should be a valid Registry name, got error: %v", name, err)
} else {
if registry.Name() != name {
t.Errorf("`%v` .Name() should reproduce the original name. Wanted: %s Got: %s", registry, name, registry.Name())
}
if registry.String() != name {
t.Errorf("`%v` .String() should reproduce the original name. Wanted: %s Got: %s", registry, name, registry.String())
}
}
}
for _, name := range append(goodWeakValidationRegistryNames, badRegistryNames...) {
if repo, err := NewRegistry(name, StrictValidation); err == nil {
t.Errorf("`%s` should be an invalid Registry name, got Registry: %#v", name, repo)
}
}
}
func TestNewRegistry(t *testing.T) {
t.Parallel()
for _, name := range append(goodStrictValidationRegistryNames, goodWeakValidationRegistryNames...) {
if _, err := NewRegistry(name, WeakValidation); err != nil {
t.Errorf("`%s` should be a valid Registry name, got error: %v", name, err)
}
}
for _, name := range badRegistryNames {
if repo, err := NewRegistry(name, WeakValidation); err == nil {
t.Errorf("`%s` should be an invalid Registry name, got Registry: %#v", name, repo)
}
}
}
func TestNewInsecureRegistry(t *testing.T) {
t.Parallel()
for _, name := range append(goodStrictValidationRegistryNames, goodWeakValidationRegistryNames...) {
if _, err := NewInsecureRegistry(name, WeakValidation); err != nil {
t.Errorf("`%s` should be a valid Registry name, got error: %v", name, err)
}
}
for _, name := range badRegistryNames {
if repo, err := NewInsecureRegistry(name, WeakValidation); err == nil {
t.Errorf("`%s` should be an invalid Registry name, got Registry: %#v", name, repo)
}
}
}
func TestDefaultRegistryNames(t *testing.T) {
testRegistries := []string{"docker.io", ""}
for _, testRegistry := range testRegistries {
registry, err := NewRegistry(testRegistry, WeakValidation)
if err != nil {
t.Fatalf("`%s` should be a valid Registry name, got error: %v", testRegistry, err)
}
actualRegistry := registry.RegistryStr()
if actualRegistry != DefaultRegistry {
t.Errorf("RegistryStr() was incorrect for %v. Wanted: `%s` Got: `%s`", registry, DefaultRegistry, actualRegistry)
}
}
}
func TestOverrideDefaultRegistryNames(t *testing.T) {
testRegistries := []string{"docker.io", ""}
expectedRegistries := []string{"index.docker.io", "gcr.io"}
overrideDefault := "gcr.io"
for i, testRegistry := range testRegistries {
registry, err := NewRegistry(testRegistry, WeakValidation, WithDefaultRegistry(overrideDefault))
if err != nil {
t.Fatalf("`%s` should be a valid Registry name, got error: %v", testRegistry, err)
}
actualRegistry := registry.RegistryStr()
if actualRegistry != expectedRegistries[i] {
t.Errorf("RegistryStr() was incorrect for %v. Wanted: `%s` Got: `%s`", registry, expectedRegistries[i], actualRegistry)
}
}
}
func TestRegistryComponents(t *testing.T) {
t.Parallel()
testRegistry := "gcr.io"
registry, err := NewRegistry(testRegistry, StrictValidation)
if err != nil {
t.Fatalf("`%s` should be a valid Registry name, got error: %v", testRegistry, err)
}
actualRegistry := registry.RegistryStr()
if actualRegistry != testRegistry {
t.Errorf("RegistryStr() was incorrect for %v. Wanted: `%s` Got: `%s`", registry, testRegistry, actualRegistry)
}
}
func TestRegistryScopes(t *testing.T) {
t.Parallel()
testRegistry := "gcr.io"
testAction := "whatever"
expectedScope := "registry:catalog:*"
registry, err := NewRegistry(testRegistry, StrictValidation)
if err != nil {
t.Fatalf("`%s` should be a valid Registry name, got error: %v", testRegistry, err)
}
actualScope := registry.Scope(testAction)
if actualScope != expectedScope {
t.Errorf("scope was incorrect for %v. Wanted: `%s` Got: `%s`", registry, expectedScope, actualScope)
}
}
func TestIsRFC1918(t *testing.T) {
t.Parallel()
tests := []struct {
reg string
result bool
}{{
reg: "index.docker.io",
result: false,
}, {
reg: "10.2.3.4:5000",
result: true,
}, {
reg: "8.8.8.8",
result: false,
}, {
reg: "172.16.3.4:3000",
result: true,
}, {
reg: "192.168.3.4",
result: true,
}, {
reg: "10.256.0.0:5000",
result: false,
}}
for _, test := range tests {
reg, err := NewRegistry(test.reg, WeakValidation)
if err != nil {
t.Errorf("NewRegistry(%s) = %v", test.reg, err)
}
got := reg.isRFC1918()
if got != test.result {
t.Errorf("isRFC1918(); got %v, want %v", got, test.result)
}
}
}
func TestRegistryScheme(t *testing.T) {
t.Parallel()
tests := []struct {
domain string
scheme string
}{{
domain: "foo.svc.local:1234",
scheme: "http",
}, {
domain: "127.0.0.1:1234",
scheme: "http",
}, {
domain: "127.0.0.1",
scheme: "http",
}, {
domain: "localhost:8080",
scheme: "http",
}, {
domain: "gcr.io",
scheme: "https",
}, {
domain: "index.docker.io",
scheme: "https",
}, {
domain: "::1",
scheme: "http",
}, {
domain: "10.2.3.4:5000",
scheme: "http",
}}
for _, test := range tests {
reg, err := NewRegistry(test.domain, WeakValidation)
if err != nil {
t.Errorf("NewRegistry(%s) = %v", test.domain, err)
}
if got, want := reg.Scheme(), test.scheme; got != want {
t.Errorf("scheme(%v); got %v, want %v", reg, got, want)
}
}
}
func TestRegistryInsecureScheme(t *testing.T) {
t.Parallel()
domain := "gcr.io"
reg, err := NewInsecureRegistry(domain, WeakValidation)
if err != nil {
t.Errorf("NewRegistry(%s) = %v", domain, err)
}
if got := reg.Scheme(); got != "http" {
t.Errorf("scheme(%v); got %v, want http", reg, got)
}
}
|