github.com/duglin/docker@v1.13.1/registry/auth_test.go (about)

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