Export `DefaultContextStoreConfig()` and `ResolveDefaultContext()`

These are needed by any dynamically registered (via
`RegisterDefaultStoreEndpoints`) endpoint type to write a useful/sensible unit
test.

Signed-off-by: Ian Campbell <ijc@docker.com>
This commit is contained in:
Ian Campbell 2019-05-16 14:52:37 +01:00
parent 1433e27420
commit f820766f6a
3 changed files with 10 additions and 9 deletions

View File

@ -214,7 +214,7 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
cli.contextStore = &ContextStoreWithDefault{ cli.contextStore = &ContextStoreWithDefault{
Store: baseContextStore, Store: baseContextStore,
Resolver: func() (*DefaultContext, error) { Resolver: func() (*DefaultContext, error) {
return resolveDefaultContext(opts.Common, cli.ConfigFile(), cli.contextStoreConfig, cli.Err()) return ResolveDefaultContext(opts.Common, cli.ConfigFile(), cli.contextStoreConfig, cli.Err())
}, },
} }
cli.currentContext, err = resolveContextName(opts.Common, cli.configFile, cli.contextStore) cli.currentContext, err = resolveContextName(opts.Common, cli.configFile, cli.contextStore)
@ -259,11 +259,11 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...Initialize
// NewAPIClientFromFlags creates a new APIClient from command line flags // NewAPIClientFromFlags creates a new APIClient from command line flags
func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) { func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.ConfigFile) (client.APIClient, error) {
storeConfig := defaultContextStoreConfig() storeConfig := DefaultContextStoreConfig()
store := &ContextStoreWithDefault{ store := &ContextStoreWithDefault{
Store: store.New(cliconfig.ContextStoreDir(), storeConfig), Store: store.New(cliconfig.ContextStoreDir(), storeConfig),
Resolver: func() (*DefaultContext, error) { Resolver: func() (*DefaultContext, error) {
return resolveDefaultContext(opts, configFile, storeConfig, ioutil.Discard) return ResolveDefaultContext(opts, configFile, storeConfig, ioutil.Discard)
}, },
} }
contextName, err := resolveContextName(opts, configFile, store) contextName, err := resolveContextName(opts, configFile, store)
@ -454,7 +454,7 @@ func NewDockerCli(ops ...DockerCliOption) (*DockerCli, error) {
WithContentTrustFromEnv(), WithContentTrustFromEnv(),
WithContainerizedClient(containerizedengine.NewClient), WithContainerizedClient(containerizedengine.NewClient),
} }
cli.contextStoreConfig = defaultContextStoreConfig() cli.contextStoreConfig = DefaultContextStoreConfig()
ops = append(defaultOps, ops...) ops = append(defaultOps, ops...)
if err := cli.Apply(ops...); err != nil { if err := cli.Apply(ops...); err != nil {
return nil, err return nil, err
@ -540,7 +540,8 @@ func RegisterDefaultStoreEndpoints(ep ...store.NamedTypeGetter) {
defaultStoreEndpoints = append(defaultStoreEndpoints, ep...) defaultStoreEndpoints = append(defaultStoreEndpoints, ep...)
} }
func defaultContextStoreConfig() store.Config { // DefaultContextStoreConfig returns a new store.Config with the default set of endpoints configured.
func DefaultContextStoreConfig() store.Config {
return store.NewConfig( return store.NewConfig(
func() interface{} { return &DockerContext{} }, func() interface{} { return &DockerContext{} },
defaultStoreEndpoints..., defaultStoreEndpoints...,

View File

@ -43,8 +43,8 @@ type EndpointDefaultResolver interface {
ResolveDefault() (interface{}, *store.EndpointTLSData) ResolveDefault() (interface{}, *store.EndpointTLSData)
} }
// resolveDefaultContext creates a Metadata for the current CLI invocation parameters // ResolveDefaultContext creates a Metadata for the current CLI invocation parameters
func resolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.ConfigFile, storeconfig store.Config, stderr io.Writer) (*DefaultContext, error) { func ResolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.ConfigFile, storeconfig store.Config, stderr io.Writer) (*DefaultContext, error) {
stackOrchestrator, err := GetStackOrchestrator("", "", config.StackOrchestrator, stderr) stackOrchestrator, err := GetStackOrchestrator("", "", config.StackOrchestrator, stderr)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -67,12 +67,12 @@ func TestDefaultContextInitializer(t *testing.T) {
cli.configFile = &configfile.ConfigFile{ cli.configFile = &configfile.ConfigFile{
StackOrchestrator: "all", StackOrchestrator: "all",
} }
ctx, err := resolveDefaultContext(&cliflags.CommonOptions{ ctx, err := ResolveDefaultContext(&cliflags.CommonOptions{
TLS: true, TLS: true,
TLSOptions: &tlsconfig.Options{ TLSOptions: &tlsconfig.Options{
CAFile: "./testdata/ca.pem", CAFile: "./testdata/ca.pem",
}, },
}, cli.ConfigFile(), defaultContextStoreConfig(), cli.Err()) }, cli.ConfigFile(), DefaultContextStoreConfig(), cli.Err())
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, "default", ctx.Meta.Name) assert.Equal(t, "default", ctx.Meta.Name)
assert.Equal(t, OrchestratorAll, ctx.Meta.Metadata.(DockerContext).StackOrchestrator) assert.Equal(t, OrchestratorAll, ctx.Meta.Metadata.(DockerContext).StackOrchestrator)