...

Source file src/github.com/google/go-containerregistry/pkg/name/registry_test.go

Documentation: github.com/google/go-containerregistry/pkg/name

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

View as plain text