Merge pull request #3442 from thaJeztah/drop_kube_cleanup_test

update/remove various tests and options related to kubernetes support
This commit is contained in:
Sebastiaan van Stijn 2022-02-25 11:15:14 +01:00 committed by GitHub
commit 1c5256d8e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 40 additions and 153 deletions

View File

@ -19,11 +19,6 @@ type CreateOptions struct {
Description string Description string
Docker map[string]string Docker map[string]string
From string From string
// Deprecated
DefaultStackOrchestrator string
// Deprecated
Kubernetes map[string]string
} }
func longCreateDescription() string { func longCreateDescription() string {
@ -53,14 +48,15 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
} }
flags := cmd.Flags() flags := cmd.Flags()
flags.StringVar(&opts.Description, "description", "", "Description of the context") flags.StringVar(&opts.Description, "description", "", "Description of the context")
flags.StringVar( flags.String(
&opts.DefaultStackOrchestrator,
"default-stack-orchestrator", "", "default-stack-orchestrator", "",
"Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)") "Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)",
)
flags.SetAnnotation("default-stack-orchestrator", "deprecated", nil)
flags.SetAnnotation("default-stack-orchestrator", "deprecated", nil) flags.SetAnnotation("default-stack-orchestrator", "deprecated", nil)
flags.MarkDeprecated("default-stack-orchestrator", "option will be ignored") flags.MarkDeprecated("default-stack-orchestrator", "option will be ignored")
flags.StringToStringVar(&opts.Docker, "docker", nil, "set the docker endpoint") flags.StringToStringVar(&opts.Docker, "docker", nil, "set the docker endpoint")
flags.StringToStringVar(&opts.Kubernetes, "kubernetes", nil, "set the kubernetes endpoint") flags.StringToString("kubernetes", nil, "set the kubernetes endpoint")
flags.SetAnnotation("kubernetes", "kubernetes", nil) flags.SetAnnotation("kubernetes", "kubernetes", nil)
flags.SetAnnotation("kubernetes", "deprecated", nil) flags.SetAnnotation("kubernetes", "deprecated", nil)
flags.MarkDeprecated("kubernetes", "option will be ignored") flags.MarkDeprecated("kubernetes", "option will be ignored")
@ -76,7 +72,7 @@ func RunCreate(cli command.Cli, o *CreateOptions) error {
return err return err
} }
switch { switch {
case o.From == "" && o.Docker == nil && o.Kubernetes == nil: case o.From == "" && o.Docker == nil:
err = createFromExistingContext(s, cli.CurrentContext(), o) err = createFromExistingContext(s, cli.CurrentContext(), o)
case o.From != "": case o.From != "":
err = createFromExistingContext(s, o.From, o) err = createFromExistingContext(s, o.From, o)
@ -132,8 +128,8 @@ func checkContextNameForCreation(s store.Reader, name string) error {
} }
func createFromExistingContext(s store.ReaderWriter, fromContextName string, o *CreateOptions) error { func createFromExistingContext(s store.ReaderWriter, fromContextName string, o *CreateOptions) error {
if len(o.Docker) != 0 || len(o.Kubernetes) != 0 { if len(o.Docker) != 0 {
return errors.New("cannot use --docker or --kubernetes flags when --from is set") return errors.New("cannot use --docker flag when --from is set")
} }
reader := store.Export(fromContextName, &descriptionDecorator{ reader := store.Export(fromContextName, &descriptionDecorator{
Reader: s, Reader: s,

View File

@ -95,21 +95,6 @@ func TestCreate(t *testing.T) {
}, },
expecterErr: `unable to parse docker host`, expecterErr: `unable to parse docker host`,
}, },
{
options: CreateOptions{
Name: "invalid-orchestrator",
DefaultStackOrchestrator: "invalid",
},
expecterErr: "",
},
{
options: CreateOptions{
Name: "orchestrator-all-no-endpoint",
DefaultStackOrchestrator: "all",
Docker: map[string]string{},
},
expecterErr: "",
},
} }
for _, tc := range tests { for _, tc := range tests {
tc := tc tc := tc
@ -129,19 +114,6 @@ func assertContextCreateLogging(t *testing.T, cli *test.FakeCli, n string) {
assert.Equal(t, fmt.Sprintf("Successfully created context %q\n", n), cli.ErrBuffer().String()) assert.Equal(t, fmt.Sprintf("Successfully created context %q\n", n), cli.ErrBuffer().String())
} }
func TestCreateOrchestratorSwarm(t *testing.T) {
cli, cleanup := makeFakeCli(t)
defer cleanup()
err := RunCreate(cli, &CreateOptions{
Name: "test",
DefaultStackOrchestrator: "swarm",
Docker: map[string]string{},
})
assert.NilError(t, err)
assertContextCreateLogging(t, cli, "test")
}
func TestCreateOrchestratorEmpty(t *testing.T) { func TestCreateOrchestratorEmpty(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
@ -160,7 +132,6 @@ func TestCreateFromContext(t *testing.T) {
description string description string
expectedDescription string expectedDescription string
docker map[string]string docker map[string]string
kubernetes map[string]string
}{ }{
{ {
name: "no-override", name: "no-override",

View File

@ -8,7 +8,6 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/streams" "github.com/docker/cli/cli/streams"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
) )
@ -20,7 +19,7 @@ func TestExportImportWithFile(t *testing.T) {
contextFile := filepath.Join(contextDir, "exported") contextFile := filepath.Join(contextDir, "exported")
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
createTestContext(t, cli) createTestContext(t, cli, "test")
cli.ErrBuffer().Reset() cli.ErrBuffer().Reset()
assert.NilError(t, RunExport(cli, &ExportOptions{ assert.NilError(t, RunExport(cli, &ExportOptions{
ContextName: "test", ContextName: "test",
@ -46,7 +45,7 @@ func TestExportImportWithFile(t *testing.T) {
func TestExportImportPipe(t *testing.T) { func TestExportImportPipe(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
createTestContext(t, cli) createTestContext(t, cli, "test")
cli.ErrBuffer().Reset() cli.ErrBuffer().Reset()
cli.OutBuffer().Reset() cli.OutBuffer().Reset()
assert.NilError(t, RunExport(cli, &ExportOptions{ assert.NilError(t, RunExport(cli, &ExportOptions{
@ -83,13 +82,3 @@ func TestExportExistingFile(t *testing.T) {
err = RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile}) err = RunExport(cli, &ExportOptions{ContextName: "test", Dest: contextFile})
assert.Assert(t, os.IsExist(err)) assert.Assert(t, os.IsExist(err))
} }
func createTestContext(t *testing.T, cli command.Cli) {
t.Helper()
err := RunCreate(cli, &CreateOptions{
Name: "test",
Docker: map[string]string{},
})
assert.NilError(t, err)
}

View File

@ -11,7 +11,7 @@ import (
func TestInspect(t *testing.T) { func TestInspect(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all") createTestContext(t, cli, "current")
cli.OutBuffer().Reset() cli.OutBuffer().Reset()
assert.NilError(t, runInspect(cli, inspectOptions{ assert.NilError(t, runInspect(cli, inspectOptions{
refs: []string{"current"}, refs: []string{"current"},

View File

@ -5,20 +5,16 @@ import (
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
"gotest.tools/v3/env"
"gotest.tools/v3/golden" "gotest.tools/v3/golden"
) )
func createTestContextWithKubeAndSwarm(t *testing.T, cli command.Cli, name string, orchestrator string) { func createTestContext(t *testing.T, cli command.Cli, name string) {
revert := env.Patch(t, "KUBECONFIG", "./testdata/test-kubeconfig") t.Helper()
defer revert()
err := RunCreate(cli, &CreateOptions{ err := RunCreate(cli, &CreateOptions{
Name: name, Name: name,
DefaultStackOrchestrator: orchestrator, Description: "description of " + name,
Description: "description of " + name, Docker: map[string]string{keyHost: "https://someswarmserver.example.com"},
Kubernetes: map[string]string{keyFrom: "default"},
Docker: map[string]string{keyHost: "https://someswarmserver.example.com"},
}) })
assert.NilError(t, err) assert.NilError(t, err)
} }
@ -26,9 +22,9 @@ func createTestContextWithKubeAndSwarm(t *testing.T, cli command.Cli, name strin
func TestList(t *testing.T) { func TestList(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all") createTestContext(t, cli, "current")
createTestContextWithKubeAndSwarm(t, cli, "other", "all") createTestContext(t, cli, "other")
createTestContextWithKubeAndSwarm(t, cli, "unset", "unset") createTestContext(t, cli, "unset")
cli.SetCurrentContext("current") cli.SetCurrentContext("current")
cli.OutBuffer().Reset() cli.OutBuffer().Reset()
assert.NilError(t, runList(cli, &listOptions{})) assert.NilError(t, runList(cli, &listOptions{}))
@ -38,8 +34,8 @@ func TestList(t *testing.T) {
func TestListQuiet(t *testing.T) { func TestListQuiet(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all") createTestContext(t, cli, "current")
createTestContextWithKubeAndSwarm(t, cli, "other", "all") createTestContext(t, cli, "other")
cli.SetCurrentContext("current") cli.SetCurrentContext("current")
cli.OutBuffer().Reset() cli.OutBuffer().Reset()
assert.NilError(t, runList(cli, &listOptions{quiet: true})) assert.NilError(t, runList(cli, &listOptions{quiet: true}))

View File

@ -15,8 +15,8 @@ import (
func TestRemove(t *testing.T) { func TestRemove(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all") createTestContext(t, cli, "current")
createTestContextWithKubeAndSwarm(t, cli, "other", "all") createTestContext(t, cli, "other")
assert.NilError(t, RunRemove(cli, RemoveOptions{}, []string{"other"})) assert.NilError(t, RunRemove(cli, RemoveOptions{}, []string{"other"}))
_, err := cli.ContextStore().GetMetadata("current") _, err := cli.ContextStore().GetMetadata("current")
assert.NilError(t, err) assert.NilError(t, err)
@ -27,8 +27,8 @@ func TestRemove(t *testing.T) {
func TestRemoveNotAContext(t *testing.T) { func TestRemoveNotAContext(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all") createTestContext(t, cli, "current")
createTestContextWithKubeAndSwarm(t, cli, "other", "all") createTestContext(t, cli, "other")
err := RunRemove(cli, RemoveOptions{}, []string{"not-a-context"}) err := RunRemove(cli, RemoveOptions{}, []string{"not-a-context"})
assert.ErrorContains(t, err, `context "not-a-context" does not exist`) assert.ErrorContains(t, err, `context "not-a-context" does not exist`)
} }
@ -36,8 +36,8 @@ func TestRemoveNotAContext(t *testing.T) {
func TestRemoveCurrent(t *testing.T) { func TestRemoveCurrent(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all") createTestContext(t, cli, "current")
createTestContextWithKubeAndSwarm(t, cli, "other", "all") createTestContext(t, cli, "other")
cli.SetCurrentContext("current") cli.SetCurrentContext("current")
err := RunRemove(cli, RemoveOptions{}, []string{"current"}) err := RunRemove(cli, RemoveOptions{}, []string{"current"})
assert.ErrorContains(t, err, "current: context is in use, set -f flag to force remove") assert.ErrorContains(t, err, "current: context is in use, set -f flag to force remove")
@ -54,8 +54,8 @@ func TestRemoveCurrentForce(t *testing.T) {
cli, cleanup := makeFakeCli(t, withCliConfig(testCfg)) cli, cleanup := makeFakeCli(t, withCliConfig(testCfg))
defer cleanup() defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "current", "all") createTestContext(t, cli, "current")
createTestContextWithKubeAndSwarm(t, cli, "other", "all") createTestContext(t, cli, "other")
cli.SetCurrentContext("current") cli.SetCurrentContext("current")
assert.NilError(t, RunRemove(cli, RemoveOptions{Force: true}, []string{"current"})) assert.NilError(t, RunRemove(cli, RemoveOptions{Force: true}, []string{"current"}))
reloadedConfig, err := config.Load(configDir) reloadedConfig, err := config.Load(configDir)
@ -66,7 +66,7 @@ func TestRemoveCurrentForce(t *testing.T) {
func TestRemoveDefault(t *testing.T) { func TestRemoveDefault(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "other", "all") createTestContext(t, cli, "other")
cli.SetCurrentContext("current") cli.SetCurrentContext("current")
err := RunRemove(cli, RemoveOptions{}, []string{"default"}) err := RunRemove(cli, RemoveOptions{}, []string{"default"})
assert.ErrorContains(t, err, `default: context "default" cannot be removed`) assert.ErrorContains(t, err, `default: context "default" cannot be removed`)

View File

@ -1,19 +0,0 @@
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: dGhlLWNh
server: https://someserver.example.com
name: test-cluster
contexts:
- context:
cluster: test-cluster
user: test-user
name: test
current-context: test
kind: Config
preferences: {}
users:
- name: test-user
user:
client-certificate-data: dGhlLWNlcnQ=
client-key-data: dGhlLWtleQ==

View File

@ -18,11 +18,6 @@ type UpdateOptions struct {
Name string Name string
Description string Description string
Docker map[string]string Docker map[string]string
// Deprecated
DefaultStackOrchestrator string
// Deprecated
Kubernetes map[string]string
} }
func longUpdateDescription() string { func longUpdateDescription() string {
@ -52,14 +47,14 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
} }
flags := cmd.Flags() flags := cmd.Flags()
flags.StringVar(&opts.Description, "description", "", "Description of the context") flags.StringVar(&opts.Description, "description", "", "Description of the context")
flags.StringVar( flags.String(
&opts.DefaultStackOrchestrator,
"default-stack-orchestrator", "", "default-stack-orchestrator", "",
"Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)") "Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)",
)
flags.SetAnnotation("default-stack-orchestrator", "deprecated", nil) flags.SetAnnotation("default-stack-orchestrator", "deprecated", nil)
flags.MarkDeprecated("default-stack-orchestrator", "option will be ignored") flags.MarkDeprecated("default-stack-orchestrator", "option will be ignored")
flags.StringToStringVar(&opts.Docker, "docker", nil, "set the docker endpoint") flags.StringToStringVar(&opts.Docker, "docker", nil, "set the docker endpoint")
flags.StringToStringVar(&opts.Kubernetes, "kubernetes", nil, "set the kubernetes endpoint") flags.StringToString("kubernetes", nil, "set the kubernetes endpoint")
flags.SetAnnotation("kubernetes", "kubernetes", nil) flags.SetAnnotation("kubernetes", "kubernetes", nil)
flags.SetAnnotation("kubernetes", "deprecated", nil) flags.SetAnnotation("kubernetes", "deprecated", nil)
flags.MarkDeprecated("kubernetes", "option will be ignored") flags.MarkDeprecated("kubernetes", "option will be ignored")

View File

@ -13,9 +13,8 @@ func TestUpdateDescriptionOnly(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
err := RunCreate(cli, &CreateOptions{ err := RunCreate(cli, &CreateOptions{
Name: "test", Name: "test",
DefaultStackOrchestrator: "swarm", Docker: map[string]string{},
Docker: map[string]string{},
}) })
assert.NilError(t, err) assert.NilError(t, err)
cli.OutBuffer().Reset() cli.OutBuffer().Reset()
@ -37,7 +36,7 @@ func TestUpdateDescriptionOnly(t *testing.T) {
func TestUpdateDockerOnly(t *testing.T) { func TestUpdateDockerOnly(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
createTestContextWithKubeAndSwarm(t, cli, "test", "swarm") createTestContext(t, cli, "test")
assert.NilError(t, RunUpdate(cli, &UpdateOptions{ assert.NilError(t, RunUpdate(cli, &UpdateOptions{
Name: "test", Name: "test",
Docker: map[string]string{ Docker: map[string]string{

View File

@ -14,9 +14,7 @@
// //
// The context store itself has absolutely no knowledge about what a docker endpoint should contain in term of metadata or TLS config. // The context store itself has absolutely no knowledge about what a docker endpoint should contain in term of metadata or TLS config.
// Client code is responsible for generating and parsing endpoint metadata and TLS files. // Client code is responsible for generating and parsing endpoint metadata and TLS files.
// The multi-endpoints approach of this package allows to combine many different endpoints in the same "context" (e.g., the Docker CLI // The multi-endpoints approach of this package allows to combine many different endpoints in the same "context".
// is able for a single context to define both a docker endpoint and a Kubernetes endpoint for the same cluster, and also specify which
// orchestrator to use by default when deploying a compose stack on this cluster).
// //
// Context IDs are actually SHA256 hashes of the context name, and are there only to avoid dealing with special characters in context names. // Context IDs are actually SHA256 hashes of the context name, and are there only to avoid dealing with special characters in context names.
package store package store

View File

@ -12,10 +12,7 @@ import (
func TestContextList(t *testing.T) { func TestContextList(t *testing.T) {
cmd := icmd.Command("docker", "context", "ls") cmd := icmd.Command("docker", "context", "ls")
cmd.Env = append(cmd.Env, cmd.Env = append(cmd.Env, "DOCKER_CONFIG=./testdata/test-dockerconfig")
"DOCKER_CONFIG=./testdata/test-dockerconfig",
"KUBECONFIG=./testdata/test-kubeconfig",
)
result := icmd.RunCmd(cmd).Assert(t, icmd.Expected{ result := icmd.RunCmd(cmd).Assert(t, icmd.Expected{
Err: icmd.None, Err: icmd.None,
ExitCode: 0, ExitCode: 0,
@ -35,10 +32,7 @@ func TestContextImportNoTLS(t *testing.T) {
icmd.RunCmd(cmd).Assert(t, icmd.Success) icmd.RunCmd(cmd).Assert(t, icmd.Success)
cmd = icmd.Command("docker", "context", "ls") cmd = icmd.Command("docker", "context", "ls")
cmd.Env = append(cmd.Env, cmd.Env = append(cmd.Env, "DOCKER_CONFIG="+d)
"DOCKER_CONFIG="+d,
"KUBECONFIG=./testdata/test-kubeconfig", // Allows reuse of context-ls.golden
)
result := icmd.RunCmd(cmd).Assert(t, icmd.Success) result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
golden.Assert(t, result.Stdout(), "context-ls.golden") golden.Assert(t, result.Stdout(), "context-ls.golden")
} }

View File

@ -1,20 +0,0 @@
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: dGhlLWNh
server: https://someserver
name: test-cluster
contexts:
- context:
cluster: test-cluster
user: test-user
namespace: zoinx
name: test
current-context: test
kind: Config
preferences: {}
users:
- name: test-user
user:
client-certificate-data: dGhlLWNlcnQ=
client-key-data: dGhlLWtleQ==

View File

@ -32,12 +32,6 @@ func Setup() error {
} }
} }
if kubeConfig := os.Getenv("TEST_KUBECONFIG"); kubeConfig != "" {
if err := os.Setenv("KUBECONFIG", kubeConfig); err != nil {
return err
}
}
if val := boolFromString(os.Getenv("TEST_REMOTE_DAEMON")); val { if val := boolFromString(os.Getenv("TEST_REMOTE_DAEMON")); val {
if err := os.Setenv("REMOTE_DAEMON", "1"); err != nil { if err := os.Setenv("REMOTE_DAEMON", "1"); err != nil {
return err return err
@ -53,11 +47,6 @@ func Setup() error {
return nil return nil
} }
// KubernetesEnabled returns if Kubernetes testing is enabled
func KubernetesEnabled() bool {
return os.Getenv("KUBECONFIG") != ""
}
// RemoteDaemon returns true if running against a remote daemon // RemoteDaemon returns true if running against a remote daemon
func RemoteDaemon() bool { func RemoteDaemon() bool {
return os.Getenv("REMOTE_DAEMON") != "" return os.Getenv("REMOTE_DAEMON") != ""

View File

@ -64,7 +64,6 @@ runtests() {
env -i \ env -i \
TEST_DOCKER_HOST="$engine_host" \ TEST_DOCKER_HOST="$engine_host" \
TEST_DOCKER_CERT_PATH="${DOCKER_CERT_PATH-}" \ TEST_DOCKER_CERT_PATH="${DOCKER_CERT_PATH-}" \
TEST_KUBECONFIG="${KUBECONFIG-}" \
TEST_REMOTE_DAEMON="${REMOTE_DAEMON-}" \ TEST_REMOTE_DAEMON="${REMOTE_DAEMON-}" \
TEST_SKIP_PLUGIN_TESTS="${SKIP_PLUGIN_TESTS-}" \ TEST_SKIP_PLUGIN_TESTS="${SKIP_PLUGIN_TESTS-}" \
GOPATH="$GOPATH" \ GOPATH="$GOPATH" \