|
| 1 | +package view_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + cmdRoot "github.com/OctopusDeploy/cli/pkg/cmd/root" |
| 8 | + "github.com/OctopusDeploy/cli/pkg/question" |
| 9 | + "github.com/OctopusDeploy/cli/test/fixtures" |
| 10 | + "github.com/OctopusDeploy/cli/test/testutil" |
| 11 | + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments" |
| 12 | + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tenants" |
| 13 | + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/workerpools" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +const spaceID = "Spaces-1" |
| 19 | + |
| 20 | +var development = fixtures.NewEnvironment(spaceID, "Environments-1", "Development") |
| 21 | + |
| 22 | +func TestDeploymentTargetViewShowsDisabledState(t *testing.T) { |
| 23 | + space1 := fixtures.NewSpace(spaceID, "Default Space") |
| 24 | + |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + run func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) |
| 28 | + }{ |
| 29 | + {"basic output reports a disabled target", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { |
| 30 | + cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { |
| 31 | + defer api.Close() |
| 32 | + rootCmd.SetArgs([]string{"deployment-target", "view", "Machines-100", "--no-prompt", "-f", "basic"}) |
| 33 | + return rootCmd.ExecuteC() |
| 34 | + }) |
| 35 | + |
| 36 | + respondWithTarget(t, api, true) |
| 37 | + // the basic renderer re-resolves the lookups it needs |
| 38 | + api.ExpectRequest(t, "GET", "/api/Spaces-1/environments/all").RespondWith([]*environments.Environment{development}) |
| 39 | + api.ExpectRequest(t, "GET", "/api/Spaces-1/workerpools/all").RespondWith([]*workerpools.WorkerPoolListResult{}) |
| 40 | + |
| 41 | + _, err := testutil.ReceivePair(cmdReceiver) |
| 42 | + assert.Nil(t, err) |
| 43 | + assert.Contains(t, stdOut.String(), "Disabled: true") |
| 44 | + assert.Equal(t, "", stdErr.String()) |
| 45 | + }}, |
| 46 | + |
| 47 | + {"basic output reports an enabled target", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { |
| 48 | + cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { |
| 49 | + defer api.Close() |
| 50 | + rootCmd.SetArgs([]string{"deployment-target", "view", "Machines-100", "--no-prompt", "-f", "basic"}) |
| 51 | + return rootCmd.ExecuteC() |
| 52 | + }) |
| 53 | + |
| 54 | + respondWithTarget(t, api, false) |
| 55 | + api.ExpectRequest(t, "GET", "/api/Spaces-1/environments/all").RespondWith([]*environments.Environment{development}) |
| 56 | + api.ExpectRequest(t, "GET", "/api/Spaces-1/workerpools/all").RespondWith([]*workerpools.WorkerPoolListResult{}) |
| 57 | + |
| 58 | + _, err := testutil.ReceivePair(cmdReceiver) |
| 59 | + assert.Nil(t, err) |
| 60 | + assert.Contains(t, stdOut.String(), "Disabled: false") |
| 61 | + assert.Equal(t, "", stdErr.String()) |
| 62 | + }}, |
| 63 | + |
| 64 | + {"json output carries IsDisabled", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { |
| 65 | + cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { |
| 66 | + defer api.Close() |
| 67 | + rootCmd.SetArgs([]string{"deployment-target", "view", "Machines-100", "--no-prompt", "-f", "json"}) |
| 68 | + return rootCmd.ExecuteC() |
| 69 | + }) |
| 70 | + |
| 71 | + respondWithTarget(t, api, true) |
| 72 | + |
| 73 | + _, err := testutil.ReceivePair(cmdReceiver) |
| 74 | + assert.Nil(t, err) |
| 75 | + assert.Contains(t, stdOut.String(), `"IsDisabled": true`) |
| 76 | + assert.Equal(t, "", stdErr.String()) |
| 77 | + }}, |
| 78 | + |
| 79 | + {"table output has an IS DISABLED column", func(t *testing.T, api *testutil.MockHttpServer, qa *testutil.AskMocker, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { |
| 80 | + cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { |
| 81 | + defer api.Close() |
| 82 | + rootCmd.SetArgs([]string{"deployment-target", "view", "Machines-100", "--no-prompt", "-f", "table"}) |
| 83 | + return rootCmd.ExecuteC() |
| 84 | + }) |
| 85 | + |
| 86 | + respondWithTarget(t, api, true) |
| 87 | + |
| 88 | + _, err := testutil.ReceivePair(cmdReceiver) |
| 89 | + assert.Nil(t, err) |
| 90 | + assert.Contains(t, stdOut.String(), "IS DISABLED") |
| 91 | + assert.Regexp(t, `web-server.*true`, stdOut.String()) |
| 92 | + assert.Equal(t, "", stdErr.String()) |
| 93 | + }}, |
| 94 | + } |
| 95 | + |
| 96 | + for _, test := range tests { |
| 97 | + t.Run(test.name, func(t *testing.T) { |
| 98 | + stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} |
| 99 | + api, qa := testutil.NewMockServerAndAsker() |
| 100 | + askProvider := question.NewAskProvider(qa.AsAsker()) |
| 101 | + fac := testutil.NewMockFactoryWithSpaceAndPrompt(api, space1, askProvider) |
| 102 | + rootCmd := cmdRoot.NewCmdRoot(fac, nil, askProvider) |
| 103 | + rootCmd.SetOut(stdout) |
| 104 | + rootCmd.SetErr(stderr) |
| 105 | + test.run(t, api, qa, rootCmd, stdout, stderr) |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func respondWithTarget(t *testing.T, api *testutil.MockHttpServer, isDisabled bool) { |
| 111 | + api.ExpectRequest(t, "GET", "/api/").RespondWith(rootResource) |
| 112 | + api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource) |
| 113 | + api.ExpectRequest(t, "GET", "/api/Spaces-1/machines/Machines-100"). |
| 114 | + RespondWith(fixtures.NewDeploymentTarget(spaceID, "Machines-100", "web-server", isDisabled)) |
| 115 | + api.ExpectRequest(t, "GET", "/api/Spaces-1/environments/all").RespondWith([]*environments.Environment{development}) |
| 116 | + api.ExpectRequest(t, "GET", "/api/Spaces-1/workerpools/all").RespondWith([]*workerpools.WorkerPoolListResult{}) |
| 117 | + api.ExpectRequest(t, "GET", "/api/Spaces-1/tenants/all").RespondWith([]*tenants.Tenant{}) |
| 118 | +} |
0 commit comments