github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/context/update_test.go (about)

     1  package context
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/cli/cli/command"
     7  	"github.com/docker/cli/cli/context/docker"
     8  	"github.com/docker/cli/cli/context/kubernetes"
     9  	"gotest.tools/v3/assert"
    10  	"gotest.tools/v3/assert/cmp"
    11  )
    12  
    13  func TestUpdateDescriptionOnly(t *testing.T) {
    14  	cli, cleanup := makeFakeCli(t)
    15  	defer cleanup()
    16  	err := RunCreate(cli, &CreateOptions{
    17  		Name:                     "test",
    18  		DefaultStackOrchestrator: "swarm",
    19  		Docker:                   map[string]string{},
    20  	})
    21  	assert.NilError(t, err)
    22  	cli.OutBuffer().Reset()
    23  	cli.ErrBuffer().Reset()
    24  	assert.NilError(t, RunUpdate(cli, &UpdateOptions{
    25  		Name:        "test",
    26  		Description: "description",
    27  	}))
    28  	c, err := cli.ContextStore().GetMetadata("test")
    29  	assert.NilError(t, err)
    30  	dc, err := command.GetDockerContext(c)
    31  	assert.NilError(t, err)
    32  	assert.Equal(t, dc.StackOrchestrator, command.OrchestratorSwarm)
    33  	assert.Equal(t, dc.Description, "description")
    34  
    35  	assert.Equal(t, "test\n", cli.OutBuffer().String())
    36  	assert.Equal(t, "Successfully updated context \"test\"\n", cli.ErrBuffer().String())
    37  }
    38  
    39  func TestUpdateDockerOnly(t *testing.T) {
    40  	cli, cleanup := makeFakeCli(t)
    41  	defer cleanup()
    42  	createTestContextWithKubeAndSwarm(t, cli, "test", "swarm")
    43  	assert.NilError(t, RunUpdate(cli, &UpdateOptions{
    44  		Name: "test",
    45  		Docker: map[string]string{
    46  			keyHost: "tcp://some-host",
    47  		},
    48  	}))
    49  	c, err := cli.ContextStore().GetMetadata("test")
    50  	assert.NilError(t, err)
    51  	dc, err := command.GetDockerContext(c)
    52  	assert.NilError(t, err)
    53  	assert.Equal(t, dc.StackOrchestrator, command.OrchestratorSwarm)
    54  	assert.Equal(t, dc.Description, "description of test")
    55  	assert.Check(t, cmp.Contains(c.Endpoints, kubernetes.KubernetesEndpoint))
    56  	assert.Check(t, cmp.Contains(c.Endpoints, docker.DockerEndpoint))
    57  	assert.Equal(t, c.Endpoints[docker.DockerEndpoint].(docker.EndpointMeta).Host, "tcp://some-host")
    58  }
    59  
    60  func TestUpdateStackOrchestratorStrategy(t *testing.T) {
    61  	cli, cleanup := makeFakeCli(t)
    62  	defer cleanup()
    63  	err := RunCreate(cli, &CreateOptions{
    64  		Name:                     "test",
    65  		DefaultStackOrchestrator: "swarm",
    66  		Docker:                   map[string]string{},
    67  	})
    68  	assert.NilError(t, err)
    69  	err = RunUpdate(cli, &UpdateOptions{
    70  		Name:                     "test",
    71  		DefaultStackOrchestrator: "kubernetes",
    72  	})
    73  	assert.ErrorContains(t, err, `cannot specify orchestrator "kubernetes" without configuring a Kubernetes endpoint`)
    74  }
    75  
    76  func TestUpdateStackOrchestratorStrategyRemoveKubeEndpoint(t *testing.T) {
    77  	cli, cleanup := makeFakeCli(t)
    78  	defer cleanup()
    79  	createTestContextWithKubeAndSwarm(t, cli, "test", "kubernetes")
    80  	err := RunUpdate(cli, &UpdateOptions{
    81  		Name:       "test",
    82  		Kubernetes: map[string]string{},
    83  	})
    84  	assert.ErrorContains(t, err, `cannot specify orchestrator "kubernetes" without configuring a Kubernetes endpoint`)
    85  }
    86  
    87  func TestUpdateInvalidDockerHost(t *testing.T) {
    88  	cli, cleanup := makeFakeCli(t)
    89  	defer cleanup()
    90  	err := RunCreate(cli, &CreateOptions{
    91  		Name:   "test",
    92  		Docker: map[string]string{},
    93  	})
    94  	assert.NilError(t, err)
    95  	err = RunUpdate(cli, &UpdateOptions{
    96  		Name: "test",
    97  		Docker: map[string]string{
    98  			keyHost: "some///invalid/host",
    99  		},
   100  	})
   101  	assert.ErrorContains(t, err, "unable to parse docker host")
   102  }