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"
|
|
|
|
|
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) {
|
2022-02-25 07:07:16 -05:00
|
|
|
contextFile := filepath.Join(t.TempDir(), "exported")
|
|
|
|
cli := makeFakeCli(t)
|
2022-02-24 06:59:15 -05:00
|
|
|
createTestContext(t, cli, "test")
|
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) {
|
2022-02-25 07:07:16 -05:00
|
|
|
cli := makeFakeCli(t)
|
2022-02-24 06:59:15 -05:00
|
|
|
createTestContext(t, cli, "test")
|
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)
|
|
|
|
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) {
|
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-02-25 07:07:16 -05:00
|
|
|
assert.NilError(t, os.WriteFile(contextFile, []byte{}, 0644))
|
|
|
|
err := RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile})
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.Assert(t, os.IsExist(err))
|
|
|
|
}
|