aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/cli/cmd/destroy_test.go
blob: bbba593cc5d077b4de8831abc226c3c2e0cdc686 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package cmd

import (
	"bytes"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/vespa-engine/vespa/client/go/internal/mock"
)

func TestDestroy(t *testing.T) {
	cli, stdout, stderr := newTestCLI(t, "NO_COLOR=true", "CI=true")
	httpClient := &mock.HTTPClient{}
	httpClient.NextResponseString(200, "ok")
	httpClient.NextResponseString(200, "ok")
	cli.httpClient = httpClient
	cli.isTerminal = func() bool { return true }
	var buf bytes.Buffer
	cli.Stdin = &buf

	require.Nil(t, cli.Run("config", "set", "target", "cloud"))
	require.Nil(t, cli.Run("config", "set", "application", "foo.bar.baz"))
	require.Nil(t, cli.Run("auth", "api-key"))

	// No removal without confirmation
	stdout.Reset()
	stderr.Reset()
	buf.WriteString("\n")
	require.NotNil(t, cli.Run("destroy", "-z", "dev.aws-us-east-1c"))
	warning := "Warning: This operation will irrecoverably remove the deployment of foo.bar.baz in dev.aws-us-east-1c and all of its data"
	confirmation := "Proceed with removal? [y/N] "
	assert.Equal(t, warning+"\nError: refusing to remove deployment of foo.bar.baz in dev.aws-us-east-1c without confirmation\n", stderr.String())
	assert.Equal(t, confirmation, stdout.String())

	// Removes deployment with confirmation
	stdout.Reset()
	stderr.Reset()
	buf.WriteString("y\n")
	require.Nil(t, cli.Run("destroy", "-z", "dev.aws-us-east-1c"))
	success := "Success: Removed deployment of foo.bar.baz in dev.aws-us-east-1c\n"
	assert.Equal(t, confirmation+success, stdout.String())

	// Force flag always removes deployment
	stdout.Reset()
	stderr.Reset()
	require.Nil(t, cli.Run("destroy", "-z", "dev.aws-us-east-1c", "--force"))
	assert.Equal(t, success, stdout.String())

	// Cannot remove a prod deployment
	require.NotNil(t, cli.Run("destroy", "-z", "prod.aws-us-east-1c"))
	assert.Equal(t, "Error: cannot remove production deployment of foo.bar.baz in prod.aws-us-east-1c\nHint: See https://cloud.vespa.ai/en/deleting-applications\n", stderr.String())

	// Cannot remove a local deployment at all
	stderr.Reset()
	require.Nil(t, cli.Run("config", "set", "target", "local"))
	require.Nil(t, cli.Run("config", "set", "application", "foo.bar.baz"))
	require.NotNil(t, cli.Run("destroy", "-z", "prod.aws-us-east-1c"))
	assert.Equal(t, "Error: unsupported target local: this command only supports targets cloud and hosted\n", stderr.String())
}