2018-11-09 09:10:41 -05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2021-06-16 03:08:42 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
2019-01-28 08:30:31 -05:00
|
|
|
"github.com/docker/cli/cli/streams"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2018-11-09 09:10:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExportImportWithFile(t *testing.T) {
|
|
|
|
contextDir, err := ioutil.TempDir("", t.Name()+"context")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(contextDir)
|
|
|
|
contextFile := filepath.Join(contextDir, "exported")
|
|
|
|
cli, cleanup := makeFakeCli(t)
|
|
|
|
defer cleanup()
|
2021-06-16 03:08:42 -04:00
|
|
|
createTestContext(t, cli)
|
2018-11-09 09:10:41 -05:00
|
|
|
cli.ErrBuffer().Reset()
|
2019-01-21 03:37:20 -05:00
|
|
|
assert.NilError(t, RunExport(cli, &ExportOptions{
|
|
|
|
ContextName: "test",
|
|
|
|
Dest: contextFile,
|
2018-11-09 09:10:41 -05:00
|
|
|
}))
|
|
|
|
assert.Equal(t, cli.ErrBuffer().String(), fmt.Sprintf("Written file %q\n", contextFile))
|
|
|
|
cli.OutBuffer().Reset()
|
|
|
|
cli.ErrBuffer().Reset()
|
2019-01-21 03:37:20 -05:00
|
|
|
assert.NilError(t, RunImport(cli, "test2", contextFile))
|
2019-04-18 09:12:30 -04:00
|
|
|
context1, err := cli.ContextStore().GetMetadata("test")
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
context2, err := cli.ContextStore().GetMetadata("test2")
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, context1.Endpoints, context2.Endpoints)
|
|
|
|
assert.DeepEqual(t, context1.Metadata, context2.Metadata)
|
|
|
|
assert.Equal(t, "test", context1.Name)
|
|
|
|
assert.Equal(t, "test2", context2.Name)
|
|
|
|
|
|
|
|
assert.Equal(t, "test2\n", cli.OutBuffer().String())
|
|
|
|
assert.Equal(t, "Successfully imported context \"test2\"\n", cli.ErrBuffer().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExportImportPipe(t *testing.T) {
|
|
|
|
cli, cleanup := makeFakeCli(t)
|
|
|
|
defer cleanup()
|
2021-06-16 03:08:42 -04:00
|
|
|
createTestContext(t, cli)
|
2018-11-09 09:10:41 -05:00
|
|
|
cli.ErrBuffer().Reset()
|
|
|
|
cli.OutBuffer().Reset()
|
2019-01-21 03:37:20 -05:00
|
|
|
assert.NilError(t, RunExport(cli, &ExportOptions{
|
|
|
|
ContextName: "test",
|
|
|
|
Dest: "-",
|
2018-11-09 09:10:41 -05:00
|
|
|
}))
|
|
|
|
assert.Equal(t, cli.ErrBuffer().String(), "")
|
2019-01-28 08:30:31 -05:00
|
|
|
cli.SetIn(streams.NewIn(ioutil.NopCloser(bytes.NewBuffer(cli.OutBuffer().Bytes()))))
|
2018-11-09 09:10:41 -05:00
|
|
|
cli.OutBuffer().Reset()
|
|
|
|
cli.ErrBuffer().Reset()
|
2019-01-21 03:37:20 -05:00
|
|
|
assert.NilError(t, RunImport(cli, "test2", "-"))
|
2019-04-18 09:12:30 -04:00
|
|
|
context1, err := cli.ContextStore().GetMetadata("test")
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.NilError(t, err)
|
2019-04-18 09:12:30 -04:00
|
|
|
context2, err := cli.ContextStore().GetMetadata("test2")
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, context1.Endpoints, context2.Endpoints)
|
|
|
|
assert.DeepEqual(t, context1.Metadata, context2.Metadata)
|
|
|
|
assert.Equal(t, "test", context1.Name)
|
|
|
|
assert.Equal(t, "test2", context2.Name)
|
|
|
|
|
|
|
|
assert.Equal(t, "test2\n", cli.OutBuffer().String())
|
|
|
|
assert.Equal(t, "Successfully imported context \"test2\"\n", cli.ErrBuffer().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExportExistingFile(t *testing.T) {
|
|
|
|
contextDir, err := ioutil.TempDir("", t.Name()+"context")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(contextDir)
|
|
|
|
contextFile := filepath.Join(contextDir, "exported")
|
|
|
|
cli, cleanup := makeFakeCli(t)
|
|
|
|
defer cleanup()
|
|
|
|
cli.ErrBuffer().Reset()
|
|
|
|
assert.NilError(t, ioutil.WriteFile(contextFile, []byte{}, 0644))
|
2019-01-21 03:37:20 -05:00
|
|
|
err = RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile})
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.Assert(t, os.IsExist(err))
|
|
|
|
}
|
2021-06-16 03:08:42 -04:00
|
|
|
|
|
|
|
func createTestContext(t *testing.T, cli command.Cli) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
err := RunCreate(cli, &CreateOptions{
|
|
|
|
Name: "test",
|
|
|
|
Docker: map[string]string{},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|