2018-11-09 09:10:41 -05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-02-25 07:07:16 -05:00
|
|
|
"io"
|
2018-11-09 09:10:41 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2024-05-31 04:40:04 -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"
|
2024-05-31 04:40:04 -04:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-11-09 09:10:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExportImportWithFile(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
contextFile := filepath.Join(t.TempDir(), "exported")
|
|
|
|
cli := makeFakeCli(t)
|
2024-05-31 04:40:04 -04:00
|
|
|
createTestContext(t, cli, "test", map[string]any{
|
|
|
|
"MyCustomMetadata": t.Name(),
|
|
|
|
})
|
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)
|
|
|
|
|
2024-05-31 04:40:04 -04:00
|
|
|
assert.Check(t, is.DeepEqual(context1.Metadata, command.DockerContext{
|
|
|
|
Description: "description of test",
|
|
|
|
AdditionalFields: map[string]any{"MyCustomMetadata": t.Name()},
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert.Check(t, is.DeepEqual(context1.Endpoints, context2.Endpoints))
|
|
|
|
assert.Check(t, is.DeepEqual(context1.Metadata, context2.Metadata))
|
|
|
|
assert.Check(t, is.Equal("test", context1.Name))
|
|
|
|
assert.Check(t, is.Equal("test2", context2.Name))
|
|
|
|
|
|
|
|
assert.Check(t, is.Equal("test2\n", cli.OutBuffer().String()))
|
|
|
|
assert.Check(t, is.Equal("Successfully imported context \"test2\"\n", cli.ErrBuffer().String()))
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExportImportPipe(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
cli := makeFakeCli(t)
|
2024-05-31 04:40:04 -04:00
|
|
|
createTestContext(t, cli, "test", map[string]any{
|
|
|
|
"MyCustomMetadata": t.Name(),
|
|
|
|
})
|
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(), "")
|
2022-02-25 07:07:16 -05:00
|
|
|
cli.SetIn(streams.NewIn(io.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)
|
|
|
|
|
2024-05-31 04:40:04 -04:00
|
|
|
assert.Check(t, is.DeepEqual(context1.Metadata, command.DockerContext{
|
|
|
|
Description: "description of test",
|
|
|
|
AdditionalFields: map[string]any{"MyCustomMetadata": t.Name()},
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert.Check(t, is.DeepEqual(context1.Endpoints, context2.Endpoints))
|
|
|
|
assert.Check(t, is.DeepEqual(context1.Metadata, context2.Metadata))
|
|
|
|
assert.Check(t, is.Equal("test", context1.Name))
|
|
|
|
assert.Check(t, is.Equal("test2", context2.Name))
|
|
|
|
|
|
|
|
assert.Check(t, is.Equal("test2\n", cli.OutBuffer().String()))
|
|
|
|
assert.Check(t, is.Equal("Successfully imported context \"test2\"\n", cli.ErrBuffer().String()))
|
2018-11-09 09:10:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExportExistingFile(t *testing.T) {
|
2022-02-25 07:07:16 -05:00
|
|
|
contextFile := filepath.Join(t.TempDir(), "exported")
|
|
|
|
cli := makeFakeCli(t)
|
2018-11-09 09:10:41 -05:00
|
|
|
cli.ErrBuffer().Reset()
|
2022-09-30 13:13:22 -04:00
|
|
|
assert.NilError(t, os.WriteFile(contextFile, []byte{}, 0o644))
|
2022-02-25 07:07:16 -05:00
|
|
|
err := RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile})
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.Assert(t, os.IsExist(err))
|
|
|
|
}
|