github.com/toplink-cn/moby@v0.0.0-20240305205811-460b4aebdf81/registry/config_test.go (about)

     1  package registry // import "github.com/docker/docker/registry"
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/docker/docker/errdefs"
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  )
    13  
    14  func TestLoadAllowNondistributableArtifacts(t *testing.T) {
    15  	testCases := []struct {
    16  		registries []string
    17  		cidrStrs   []string
    18  		hostnames  []string
    19  		err        string
    20  	}{
    21  		{
    22  			registries: []string{"1.2.3.0/24"},
    23  			cidrStrs:   []string{"1.2.3.0/24"},
    24  		},
    25  		{
    26  			registries: []string{"2001:db8::/120"},
    27  			cidrStrs:   []string{"2001:db8::/120"},
    28  		},
    29  		{
    30  			registries: []string{"127.0.0.1"},
    31  			hostnames:  []string{"127.0.0.1"},
    32  		},
    33  		{
    34  			registries: []string{"127.0.0.1:8080"},
    35  			hostnames:  []string{"127.0.0.1:8080"},
    36  		},
    37  		{
    38  			registries: []string{"2001:db8::1"},
    39  			hostnames:  []string{"2001:db8::1"},
    40  		},
    41  		{
    42  			registries: []string{"[2001:db8::1]:80"},
    43  			hostnames:  []string{"[2001:db8::1]:80"},
    44  		},
    45  		{
    46  			registries: []string{"[2001:db8::1]:80"},
    47  			hostnames:  []string{"[2001:db8::1]:80"},
    48  		},
    49  		{
    50  			registries: []string{"1.2.3.0/24", "2001:db8::/120", "127.0.0.1", "127.0.0.1:8080"},
    51  			cidrStrs:   []string{"1.2.3.0/24", "2001:db8::/120"},
    52  			hostnames:  []string{"127.0.0.1", "127.0.0.1:8080"},
    53  		},
    54  
    55  		{
    56  			registries: []string{"http://myregistry.example.com"},
    57  			err:        "allow-nondistributable-artifacts registry http://myregistry.example.com should not contain '://'",
    58  		},
    59  		{
    60  			registries: []string{"https://myregistry.example.com"},
    61  			err:        "allow-nondistributable-artifacts registry https://myregistry.example.com should not contain '://'",
    62  		},
    63  		{
    64  			registries: []string{"HTTP://myregistry.example.com"},
    65  			err:        "allow-nondistributable-artifacts registry HTTP://myregistry.example.com should not contain '://'",
    66  		},
    67  		{
    68  			registries: []string{"svn://myregistry.example.com"},
    69  			err:        "allow-nondistributable-artifacts registry svn://myregistry.example.com should not contain '://'",
    70  		},
    71  		{
    72  			registries: []string{"-invalid-registry"},
    73  			err:        "Cannot begin or end with a hyphen",
    74  		},
    75  		{
    76  			registries: []string{`mytest-.com`},
    77  			err:        `allow-nondistributable-artifacts registry mytest-.com is not valid: invalid host "mytest-.com"`,
    78  		},
    79  		{
    80  			registries: []string{`1200:0000:AB00:1234:0000:2552:7777:1313:8080`},
    81  			err:        `allow-nondistributable-artifacts registry 1200:0000:AB00:1234:0000:2552:7777:1313:8080 is not valid: invalid host "1200:0000:AB00:1234:0000:2552:7777:1313:8080"`,
    82  		},
    83  		{
    84  			registries: []string{`myregistry.example.com:500000`},
    85  			err:        `allow-nondistributable-artifacts registry myregistry.example.com:500000 is not valid: invalid port "500000"`,
    86  		},
    87  		{
    88  			registries: []string{`"myregistry.example.com"`},
    89  			err:        `allow-nondistributable-artifacts registry "myregistry.example.com" is not valid: invalid host "\"myregistry.example.com\""`,
    90  		},
    91  		{
    92  			registries: []string{`"myregistry.example.com:5000"`},
    93  			err:        `allow-nondistributable-artifacts registry "myregistry.example.com:5000" is not valid: invalid host "\"myregistry.example.com"`,
    94  		},
    95  	}
    96  	for _, testCase := range testCases {
    97  		config := emptyServiceConfig
    98  		err := config.loadAllowNondistributableArtifacts(testCase.registries)
    99  		if testCase.err == "" {
   100  			if err != nil {
   101  				t.Fatalf("expect no error, got '%s'", err)
   102  			}
   103  
   104  			var cidrStrs []string
   105  			for _, c := range config.AllowNondistributableArtifactsCIDRs {
   106  				cidrStrs = append(cidrStrs, c.String())
   107  			}
   108  
   109  			sort.Strings(testCase.cidrStrs)
   110  			sort.Strings(cidrStrs)
   111  			if (len(testCase.cidrStrs) > 0 || len(cidrStrs) > 0) && !reflect.DeepEqual(testCase.cidrStrs, cidrStrs) {
   112  				t.Fatalf("expect AllowNondistributableArtifactsCIDRs to be '%+v', got '%+v'", testCase.cidrStrs, cidrStrs)
   113  			}
   114  
   115  			sort.Strings(testCase.hostnames)
   116  			sort.Strings(config.AllowNondistributableArtifactsHostnames)
   117  			if (len(testCase.hostnames) > 0 || len(config.AllowNondistributableArtifactsHostnames) > 0) && !reflect.DeepEqual(testCase.hostnames, config.AllowNondistributableArtifactsHostnames) {
   118  				t.Fatalf("expect AllowNondistributableArtifactsHostnames to be '%+v', got '%+v'", testCase.hostnames, config.AllowNondistributableArtifactsHostnames)
   119  			}
   120  		} else {
   121  			if err == nil {
   122  				t.Fatalf("expect error '%s', got no error", testCase.err)
   123  			}
   124  			if !strings.Contains(err.Error(), testCase.err) {
   125  				t.Fatalf("expect error '%s', got '%s'", testCase.err, err)
   126  			}
   127  		}
   128  	}
   129  }
   130  
   131  func TestValidateMirror(t *testing.T) {
   132  	valid := []string{
   133  		"http://mirror-1.example.com",
   134  		"http://mirror-1.example.com/",
   135  		"https://mirror-1.example.com",
   136  		"https://mirror-1.example.com/",
   137  		"http://localhost",
   138  		"https://localhost",
   139  		"http://localhost:5000",
   140  		"https://localhost:5000",
   141  		"http://127.0.0.1",
   142  		"https://127.0.0.1",
   143  		"http://127.0.0.1:5000",
   144  		"https://127.0.0.1:5000",
   145  		"http://mirror-1.example.com/v1/",
   146  		"https://mirror-1.example.com/v1/",
   147  	}
   148  
   149  	invalid := []string{
   150  		"!invalid!://%as%",
   151  		"ftp://mirror-1.example.com",
   152  		"http://mirror-1.example.com/?q=foo",
   153  		"http://mirror-1.example.com/v1/?q=foo",
   154  		"http://mirror-1.example.com/v1/?q=foo#frag",
   155  		"http://mirror-1.example.com?q=foo",
   156  		"https://mirror-1.example.com#frag",
   157  		"https://mirror-1.example.com/#frag",
   158  		"http://foo:bar@mirror-1.example.com/",
   159  		"https://mirror-1.example.com/v1/#frag",
   160  		"https://mirror-1.example.com?q",
   161  	}
   162  
   163  	for _, address := range valid {
   164  		if ret, err := ValidateMirror(address); err != nil || ret == "" {
   165  			t.Errorf("ValidateMirror(`"+address+"`) got %s %s", ret, err)
   166  		}
   167  	}
   168  
   169  	for _, address := range invalid {
   170  		if ret, err := ValidateMirror(address); err == nil || ret != "" {
   171  			t.Errorf("ValidateMirror(`"+address+"`) got %s %s", ret, err)
   172  		}
   173  	}
   174  }
   175  
   176  func TestLoadInsecureRegistries(t *testing.T) {
   177  	testCases := []struct {
   178  		registries []string
   179  		index      string
   180  		err        string
   181  	}{
   182  		{
   183  			registries: []string{"127.0.0.1"},
   184  			index:      "127.0.0.1",
   185  		},
   186  		{
   187  			registries: []string{"127.0.0.1:8080"},
   188  			index:      "127.0.0.1:8080",
   189  		},
   190  		{
   191  			registries: []string{"2001:db8::1"},
   192  			index:      "2001:db8::1",
   193  		},
   194  		{
   195  			registries: []string{"[2001:db8::1]:80"},
   196  			index:      "[2001:db8::1]:80",
   197  		},
   198  		{
   199  			registries: []string{"http://myregistry.example.com"},
   200  			index:      "myregistry.example.com",
   201  		},
   202  		{
   203  			registries: []string{"https://myregistry.example.com"},
   204  			index:      "myregistry.example.com",
   205  		},
   206  		{
   207  			registries: []string{"HTTP://myregistry.example.com"},
   208  			index:      "myregistry.example.com",
   209  		},
   210  		{
   211  			registries: []string{"svn://myregistry.example.com"},
   212  			err:        "insecure registry svn://myregistry.example.com should not contain '://'",
   213  		},
   214  		{
   215  			registries: []string{"-invalid-registry"},
   216  			err:        "Cannot begin or end with a hyphen",
   217  		},
   218  		{
   219  			registries: []string{`mytest-.com`},
   220  			err:        `insecure registry mytest-.com is not valid: invalid host "mytest-.com"`,
   221  		},
   222  		{
   223  			registries: []string{`1200:0000:AB00:1234:0000:2552:7777:1313:8080`},
   224  			err:        `insecure registry 1200:0000:AB00:1234:0000:2552:7777:1313:8080 is not valid: invalid host "1200:0000:AB00:1234:0000:2552:7777:1313:8080"`,
   225  		},
   226  		{
   227  			registries: []string{`myregistry.example.com:500000`},
   228  			err:        `insecure registry myregistry.example.com:500000 is not valid: invalid port "500000"`,
   229  		},
   230  		{
   231  			registries: []string{`"myregistry.example.com"`},
   232  			err:        `insecure registry "myregistry.example.com" is not valid: invalid host "\"myregistry.example.com\""`,
   233  		},
   234  		{
   235  			registries: []string{`"myregistry.example.com:5000"`},
   236  			err:        `insecure registry "myregistry.example.com:5000" is not valid: invalid host "\"myregistry.example.com"`,
   237  		},
   238  	}
   239  	for _, testCase := range testCases {
   240  		config := emptyServiceConfig
   241  		err := config.loadInsecureRegistries(testCase.registries)
   242  		if testCase.err == "" {
   243  			if err != nil {
   244  				t.Fatalf("expect no error, got '%s'", err)
   245  			}
   246  			match := false
   247  			for index := range config.IndexConfigs {
   248  				if index == testCase.index {
   249  					match = true
   250  				}
   251  			}
   252  			if !match {
   253  				t.Fatalf("expect index configs to contain '%s', got %+v", testCase.index, config.IndexConfigs)
   254  			}
   255  		} else {
   256  			if err == nil {
   257  				t.Fatalf("expect error '%s', got no error", testCase.err)
   258  			}
   259  			assert.ErrorContains(t, err, testCase.err)
   260  			assert.Check(t, errdefs.IsInvalidParameter(err))
   261  		}
   262  	}
   263  }
   264  
   265  func TestNewServiceConfig(t *testing.T) {
   266  	testCases := []struct {
   267  		opts   ServiceOptions
   268  		errStr string
   269  	}{
   270  		{
   271  			ServiceOptions{},
   272  			"",
   273  		},
   274  		{
   275  			ServiceOptions{
   276  				Mirrors: []string{"example.com:5000"},
   277  			},
   278  			`invalid mirror: unsupported scheme "example.com" in "example.com:5000"`,
   279  		},
   280  		{
   281  			ServiceOptions{
   282  				Mirrors: []string{"http://example.com:5000"},
   283  			},
   284  			"",
   285  		},
   286  		{
   287  			ServiceOptions{
   288  				InsecureRegistries: []string{"[fe80::]/64"},
   289  			},
   290  			`insecure registry [fe80::]/64 is not valid: invalid host "[fe80::]/64"`,
   291  		},
   292  		{
   293  			ServiceOptions{
   294  				InsecureRegistries: []string{"102.10.8.1/24"},
   295  			},
   296  			"",
   297  		},
   298  		{
   299  			ServiceOptions{
   300  				AllowNondistributableArtifacts: []string{"[fe80::]/64"},
   301  			},
   302  			`allow-nondistributable-artifacts registry [fe80::]/64 is not valid: invalid host "[fe80::]/64"`,
   303  		},
   304  		{
   305  			ServiceOptions{
   306  				AllowNondistributableArtifacts: []string{"102.10.8.1/24"},
   307  			},
   308  			"",
   309  		},
   310  	}
   311  
   312  	for _, testCase := range testCases {
   313  		_, err := newServiceConfig(testCase.opts)
   314  		if testCase.errStr != "" {
   315  			assert.Check(t, is.Error(err, testCase.errStr))
   316  			assert.Check(t, errdefs.IsInvalidParameter(err))
   317  		} else {
   318  			assert.Check(t, err)
   319  		}
   320  	}
   321  }
   322  
   323  func TestValidateIndexName(t *testing.T) {
   324  	valid := []struct {
   325  		index  string
   326  		expect string
   327  	}{
   328  		{
   329  			index:  "index.docker.io",
   330  			expect: "docker.io",
   331  		},
   332  		{
   333  			index:  "example.com",
   334  			expect: "example.com",
   335  		},
   336  		{
   337  			index:  "127.0.0.1:8080",
   338  			expect: "127.0.0.1:8080",
   339  		},
   340  		{
   341  			index:  "mytest-1.com",
   342  			expect: "mytest-1.com",
   343  		},
   344  		{
   345  			index:  "mirror-1.example.com/v1/?q=foo",
   346  			expect: "mirror-1.example.com/v1/?q=foo",
   347  		},
   348  	}
   349  
   350  	for _, testCase := range valid {
   351  		result, err := ValidateIndexName(testCase.index)
   352  		if assert.Check(t, err) {
   353  			assert.Check(t, is.Equal(testCase.expect, result))
   354  		}
   355  	}
   356  }
   357  
   358  func TestValidateIndexNameWithError(t *testing.T) {
   359  	invalid := []struct {
   360  		index string
   361  		err   string
   362  	}{
   363  		{
   364  			index: "docker.io-",
   365  			err:   "invalid index name (docker.io-). Cannot begin or end with a hyphen",
   366  		},
   367  		{
   368  			index: "-example.com",
   369  			err:   "invalid index name (-example.com). Cannot begin or end with a hyphen",
   370  		},
   371  		{
   372  			index: "mirror-1.example.com/v1/?q=foo-",
   373  			err:   "invalid index name (mirror-1.example.com/v1/?q=foo-). Cannot begin or end with a hyphen",
   374  		},
   375  	}
   376  	for _, testCase := range invalid {
   377  		_, err := ValidateIndexName(testCase.index)
   378  		assert.Check(t, is.Error(err, testCase.err))
   379  		assert.Check(t, errdefs.IsInvalidParameter(err))
   380  	}
   381  }