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

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