2019-03-06 09:01:12 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
|
|
|
"github.com/docker/cli/cli/context/docker"
|
|
|
|
"github.com/docker/cli/cli/context/store"
|
|
|
|
cliflags "github.com/docker/cli/cli/flags"
|
|
|
|
"github.com/docker/go-connections/tlsconfig"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/env"
|
|
|
|
"gotest.tools/v3/golden"
|
2019-03-06 09:01:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type endpoint struct {
|
|
|
|
Foo string `json:"a_very_recognizable_field_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type testContext struct {
|
|
|
|
Bar string `json:"another_very_recognizable_field_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCfg = store.NewConfig(func() interface{} { return &testContext{} },
|
|
|
|
store.EndpointTypeGetter("ep1", func() interface{} { return &endpoint{} }),
|
|
|
|
store.EndpointTypeGetter("ep2", func() interface{} { return &endpoint{} }),
|
|
|
|
)
|
|
|
|
|
2019-04-18 09:12:30 -04:00
|
|
|
func testDefaultMetadata() store.Metadata {
|
|
|
|
return store.Metadata{
|
2019-03-06 09:01:12 -05:00
|
|
|
Endpoints: map[string]interface{}{
|
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: testContext{Bar: "baz"},
|
|
|
|
Name: DefaultContextName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 08:35:28 -05:00
|
|
|
func testStore(t *testing.T, meta store.Metadata, tls store.ContextTLSData) store.Store {
|
|
|
|
t.Helper()
|
|
|
|
return &ContextStoreWithDefault{
|
|
|
|
Store: store.New(t.TempDir(), testCfg),
|
2019-03-06 09:01:12 -05:00
|
|
|
Resolver: func() (*DefaultContext, error) {
|
|
|
|
return &DefaultContext{
|
|
|
|
Meta: meta,
|
|
|
|
TLS: tls,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultContextInitializer(t *testing.T) {
|
|
|
|
cli, err := NewDockerCli()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer env.Patch(t, "DOCKER_HOST", "ssh://someswarmserver")()
|
2022-02-24 07:35:03 -05:00
|
|
|
cli.configFile = &configfile.ConfigFile{}
|
2019-05-16 09:52:37 -04:00
|
|
|
ctx, err := ResolveDefaultContext(&cliflags.CommonOptions{
|
2019-03-06 09:01:12 -05:00
|
|
|
TLS: true,
|
|
|
|
TLSOptions: &tlsconfig.Options{
|
|
|
|
CAFile: "./testdata/ca.pem",
|
|
|
|
},
|
2022-03-03 12:47:36 -05:00
|
|
|
}, DefaultContextStoreConfig())
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, "default", ctx.Meta.Name)
|
|
|
|
assert.DeepEqual(t, "ssh://someswarmserver", ctx.Meta.Endpoints[docker.DockerEndpoint].(docker.EndpointMeta).Host)
|
|
|
|
golden.Assert(t, string(ctx.TLS.Endpoints[docker.DockerEndpoint].Files["ca.pem"]), "ca.pem")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExportDefaultImport(t *testing.T) {
|
|
|
|
file1 := make([]byte, 1500)
|
|
|
|
rand.Read(file1)
|
|
|
|
file2 := make([]byte, 3700)
|
|
|
|
rand.Read(file2)
|
2022-02-25 08:35:28 -05:00
|
|
|
s := testStore(t, testDefaultMetadata(), store.ContextTLSData{
|
2019-03-06 09:01:12 -05:00
|
|
|
Endpoints: map[string]store.EndpointTLSData{
|
|
|
|
"ep2": {
|
|
|
|
Files: map[string][]byte{
|
|
|
|
"file1": file1,
|
|
|
|
"file2": file2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
r := store.Export("default", s)
|
|
|
|
defer r.Close()
|
|
|
|
err := store.Import("dest", s, r)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2019-04-18 09:12:30 -04:00
|
|
|
srcMeta, err := s.GetMetadata("default")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
destMeta, err := s.GetMetadata("dest")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, destMeta.Metadata, srcMeta.Metadata)
|
|
|
|
assert.DeepEqual(t, destMeta.Endpoints, srcMeta.Endpoints)
|
|
|
|
|
2019-04-18 09:12:30 -04:00
|
|
|
srcFileList, err := s.ListTLSFiles("default")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
destFileList, err := s.ListTLSFiles("dest")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, 1, len(destFileList))
|
|
|
|
assert.Equal(t, 1, len(srcFileList))
|
|
|
|
assert.Equal(t, 2, len(destFileList["ep2"]))
|
|
|
|
assert.Equal(t, 2, len(srcFileList["ep2"]))
|
|
|
|
|
2019-04-18 09:12:30 -04:00
|
|
|
srcData1, err := s.GetTLSData("default", "ep2", "file1")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, file1, srcData1)
|
2019-04-18 09:12:30 -04:00
|
|
|
srcData2, err := s.GetTLSData("default", "ep2", "file2")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, file2, srcData2)
|
|
|
|
|
2019-04-18 09:12:30 -04:00
|
|
|
destData1, err := s.GetTLSData("dest", "ep2", "file1")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, file1, destData1)
|
2019-04-18 09:12:30 -04:00
|
|
|
destData2, err := s.GetTLSData("dest", "ep2", "file2")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, file2, destData2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListDefaultContext(t *testing.T) {
|
|
|
|
meta := testDefaultMetadata()
|
2022-02-25 08:35:28 -05:00
|
|
|
s := testStore(t, meta, store.ContextTLSData{})
|
2019-04-18 09:12:30 -04:00
|
|
|
result, err := s.List()
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, 1, len(result))
|
|
|
|
assert.DeepEqual(t, meta, result[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDefaultContextStorageInfo(t *testing.T) {
|
2022-02-25 08:35:28 -05:00
|
|
|
s := testStore(t, testDefaultMetadata(), store.ContextTLSData{})
|
2019-04-18 09:12:30 -04:00
|
|
|
result := s.GetStorageInfo(DefaultContextName)
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.Equal(t, "<IN MEMORY>", result.MetadataPath)
|
|
|
|
assert.Equal(t, "<IN MEMORY>", result.TLSPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDefaultContextMetadata(t *testing.T) {
|
|
|
|
meta := testDefaultMetadata()
|
2022-02-25 08:35:28 -05:00
|
|
|
s := testStore(t, meta, store.ContextTLSData{})
|
2019-04-18 09:12:30 -04:00
|
|
|
result, err := s.GetMetadata(DefaultContextName)
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, DefaultContextName, result.Name)
|
|
|
|
assert.DeepEqual(t, meta.Metadata, result.Metadata)
|
|
|
|
assert.DeepEqual(t, meta.Endpoints, result.Endpoints)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrCreateDefault(t *testing.T) {
|
|
|
|
meta := testDefaultMetadata()
|
2022-02-25 08:35:28 -05:00
|
|
|
s := testStore(t, meta, store.ContextTLSData{})
|
2019-04-18 09:12:30 -04:00
|
|
|
err := s.CreateOrUpdate(store.Metadata{
|
2019-03-06 09:01:12 -05:00
|
|
|
Endpoints: map[string]interface{}{
|
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: testContext{Bar: "baz"},
|
|
|
|
Name: "default",
|
|
|
|
})
|
|
|
|
assert.Error(t, err, "default context cannot be created nor updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrRemoveDefault(t *testing.T) {
|
|
|
|
meta := testDefaultMetadata()
|
2022-02-25 08:35:28 -05:00
|
|
|
s := testStore(t, meta, store.ContextTLSData{})
|
2019-04-18 09:12:30 -04:00
|
|
|
err := s.Remove("default")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.Error(t, err, "default context cannot be removed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrTLSDataError(t *testing.T) {
|
|
|
|
meta := testDefaultMetadata()
|
2022-02-25 08:35:28 -05:00
|
|
|
s := testStore(t, meta, store.ContextTLSData{})
|
2019-04-18 09:12:30 -04:00
|
|
|
_, err := s.GetTLSData("default", "noop", "noop")
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.Check(t, store.IsErrTLSDataDoesNotExist(err))
|
|
|
|
}
|