github.com/docker/docker-ce@v17.12.1-ce-rc2+incompatible/components/engine/registry/config_test.go (about)

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