2019-03-06 09:01:12 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 09:12:30 -04:00
|
|
|
func testStore(t *testing.T, meta store.Metadata, tls store.ContextTLSData) (store.Store, func()) {
|
2019-03-06 09:01:12 -05:00
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
2020-01-16 06:47:12 -05:00
|
|
|
s := &ContextStoreWithDefault{
|
2019-03-06 09:01:12 -05:00
|
|
|
Store: store.New(testDir, testCfg),
|
|
|
|
Resolver: func() (*DefaultContext, error) {
|
|
|
|
return &DefaultContext{
|
|
|
|
Meta: meta,
|
|
|
|
TLS: tls,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
2020-01-16 06:47:12 -05:00
|
|
|
return s, func() {
|
|
|
|
_ = os.RemoveAll(testDir)
|
2019-03-06 09:01:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
2019-05-16 09:52:37 -04:00
|
|
|
}, cli.ConfigFile(), DefaultContextStoreConfig(), cli.Err())
|
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)
|
|
|
|
s, cleanup := testStore(t, testDefaultMetadata(), store.ContextTLSData{
|
|
|
|
Endpoints: map[string]store.EndpointTLSData{
|
|
|
|
"ep2": {
|
|
|
|
Files: map[string][]byte{
|
|
|
|
"file1": file1,
|
|
|
|
"file2": file2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
defer cleanup()
|
|
|
|
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()
|
|
|
|
s, cleanup := testStore(t, meta, store.ContextTLSData{})
|
|
|
|
defer cleanup()
|
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) {
|
|
|
|
s, cleanup := testStore(t, testDefaultMetadata(), store.ContextTLSData{})
|
|
|
|
defer cleanup()
|
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()
|
|
|
|
s, cleanup := testStore(t, meta, store.ContextTLSData{})
|
|
|
|
defer cleanup()
|
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()
|
|
|
|
s, cleanup := testStore(t, meta, store.ContextTLSData{})
|
|
|
|
defer cleanup()
|
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()
|
|
|
|
s, cleanup := testStore(t, meta, store.ContextTLSData{})
|
|
|
|
defer cleanup()
|
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()
|
|
|
|
s, cleanup := testStore(t, meta, store.ContextTLSData{})
|
|
|
|
defer cleanup()
|
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))
|
|
|
|
}
|