github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/registry/auth_test.go (about)

     1  package registry
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/docker/api/types"
     7  	registrytypes "github.com/docker/docker/api/types/registry"
     8  )
     9  
    10  func buildAuthConfigs() map[string]types.AuthConfig {
    11  	authConfigs := map[string]types.AuthConfig{}
    12  
    13  	for _, registry := range []string{"testIndex", IndexServer} {
    14  		authConfigs[registry] = types.AuthConfig{
    15  			Username: "docker-user",
    16  			Password: "docker-pass",
    17  		}
    18  	}
    19  
    20  	return authConfigs
    21  }
    22  
    23  func TestSameAuthDataPostSave(t *testing.T) {
    24  	authConfigs := buildAuthConfigs()
    25  	authConfig := authConfigs["testIndex"]
    26  	if authConfig.Username != "docker-user" {
    27  		t.Fail()
    28  	}
    29  	if authConfig.Password != "docker-pass" {
    30  		t.Fail()
    31  	}
    32  	if authConfig.Auth != "" {
    33  		t.Fail()
    34  	}
    35  }
    36  
    37  func TestResolveAuthConfigIndexServer(t *testing.T) {
    38  	authConfigs := buildAuthConfigs()
    39  	indexConfig := authConfigs[IndexServer]
    40  
    41  	officialIndex := &registrytypes.IndexInfo{
    42  		Official: true,
    43  	}
    44  	privateIndex := &registrytypes.IndexInfo{
    45  		Official: false,
    46  	}
    47  
    48  	resolved := ResolveAuthConfig(authConfigs, officialIndex)
    49  	assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer")
    50  
    51  	resolved = ResolveAuthConfig(authConfigs, privateIndex)
    52  	assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServer")
    53  }
    54  
    55  func TestResolveAuthConfigFullURL(t *testing.T) {
    56  	authConfigs := buildAuthConfigs()
    57  
    58  	registryAuth := types.AuthConfig{
    59  		Username: "foo-user",
    60  		Password: "foo-pass",
    61  	}
    62  	localAuth := types.AuthConfig{
    63  		Username: "bar-user",
    64  		Password: "bar-pass",
    65  	}
    66  	officialAuth := types.AuthConfig{
    67  		Username: "baz-user",
    68  		Password: "baz-pass",
    69  	}
    70  	authConfigs[IndexServer] = officialAuth
    71  
    72  	expectedAuths := map[string]types.AuthConfig{
    73  		"registry.example.com": registryAuth,
    74  		"localhost:8000":       localAuth,
    75  		"registry.com":         localAuth,
    76  	}
    77  
    78  	validRegistries := map[string][]string{
    79  		"registry.example.com": {
    80  			"https://registry.example.com/v1/",
    81  			"http://registry.example.com/v1/",
    82  			"registry.example.com",
    83  			"registry.example.com/v1/",
    84  		},
    85  		"localhost:8000": {
    86  			"https://localhost:8000/v1/",
    87  			"http://localhost:8000/v1/",
    88  			"localhost:8000",
    89  			"localhost:8000/v1/",
    90  		},
    91  		"registry.com": {
    92  			"https://registry.com/v1/",
    93  			"http://registry.com/v1/",
    94  			"registry.com",
    95  			"registry.com/v1/",
    96  		},
    97  	}
    98  
    99  	for configKey, registries := range validRegistries {
   100  		configured, ok := expectedAuths[configKey]
   101  		if !ok {
   102  			t.Fail()
   103  		}
   104  		index := &registrytypes.IndexInfo{
   105  			Name: configKey,
   106  		}
   107  		for _, registry := range registries {
   108  			authConfigs[registry] = configured
   109  			resolved := ResolveAuthConfig(authConfigs, index)
   110  			if resolved.Username != configured.Username || resolved.Password != configured.Password {
   111  				t.Errorf("%s -> %v != %v\n", registry, resolved, configured)
   112  			}
   113  			delete(authConfigs, registry)
   114  			resolved = ResolveAuthConfig(authConfigs, index)
   115  			if resolved.Username == configured.Username || resolved.Password == configured.Password {
   116  				t.Errorf("%s -> %v == %v\n", registry, resolved, configured)
   117  			}
   118  		}
   119  	}
   120  }