diff --git a/registry/config_test.go b/registry/config_test.go index 0899e17f67..40aa03dbc6 100644 --- a/registry/config_test.go +++ b/registry/config_test.go @@ -317,3 +317,64 @@ func TestNewServiceConfig(t *testing.T) { } } } + +func TestValidateIndexName(t *testing.T) { + valid := []struct { + index string + expect string + }{ + { + index: "index.docker.io", + expect: "docker.io", + }, + { + index: "example.com", + expect: "example.com", + }, + { + index: "127.0.0.1:8080", + expect: "127.0.0.1:8080", + }, + { + index: "mytest-1.com", + expect: "mytest-1.com", + }, + { + index: "mirror-1.com/v1/?q=foo", + expect: "mirror-1.com/v1/?q=foo", + }, + } + + for _, testCase := range valid { + result, err := ValidateIndexName(testCase.index) + if assert.NoError(t, err) { + assert.Equal(t, testCase.expect, result) + } + + } + +} + +func TestValidateIndexNameWithError(t *testing.T) { + invalid := []struct { + index string + err string + }{ + { + index: "docker.io-", + err: "invalid index name (docker.io-). Cannot begin or end with a hyphen", + }, + { + index: "-example.com", + err: "invalid index name (-example.com). Cannot begin or end with a hyphen", + }, + { + index: "mirror-1.com/v1/?q=foo-", + err: "invalid index name (mirror-1.com/v1/?q=foo-). Cannot begin or end with a hyphen", + }, + } + for _, testCase := range invalid { + _, err := ValidateIndexName(testCase.index) + assert.EqualError(t, err, testCase.err) + } +}