github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/var_purge_test.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/nomad/ci"
     9  	"github.com/mitchellh/cli"
    10  	"github.com/posener/complete"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestVarPurgeCommand_Implements(t *testing.T) {
    15  	ci.Parallel(t)
    16  	var _ cli.Command = &VarPurgeCommand{}
    17  }
    18  
    19  func TestVarPurgeCommand_Fails(t *testing.T) {
    20  	ci.Parallel(t)
    21  	t.Run("bad_args", func(t *testing.T) {
    22  		ci.Parallel(t)
    23  		ui := cli.NewMockUi()
    24  		cmd := &VarPurgeCommand{Meta: Meta{Ui: ui}}
    25  		code := cmd.Run([]string{"some", "bad", "args"})
    26  		out := ui.ErrorWriter.String()
    27  		require.Equal(t, 1, code, "expected exit code 1, got: %d")
    28  		require.Contains(t, out, commandErrorText(cmd), "expected help output, got: %s", out)
    29  	})
    30  	t.Run("bad_address", func(t *testing.T) {
    31  		ci.Parallel(t)
    32  		ui := cli.NewMockUi()
    33  		cmd := &VarPurgeCommand{Meta: Meta{Ui: ui}}
    34  		code := cmd.Run([]string{"-address=nope", "foo"})
    35  		out := ui.ErrorWriter.String()
    36  		require.Equal(t, 1, code, "expected exit code 1, got: %d")
    37  		require.Contains(t, ui.ErrorWriter.String(), "purging variable", "connection error, got: %s", out)
    38  		require.Zero(t, ui.OutputWriter.String())
    39  	})
    40  	t.Run("bad_check_index/syntax", func(t *testing.T) {
    41  		ci.Parallel(t)
    42  		ui := cli.NewMockUi()
    43  		cmd := &VarPurgeCommand{Meta: Meta{Ui: ui}}
    44  		code := cmd.Run([]string{`-check-index=a`, "foo"})
    45  		out := strings.TrimSpace(ui.ErrorWriter.String())
    46  		require.Equal(t, 1, code, "expected exit code 1, got: %d", code)
    47  		require.Equal(t, `Invalid -check-index value "a": not parsable as uint64`, out)
    48  		require.Zero(t, ui.OutputWriter.String())
    49  	})
    50  	t.Run("bad_check_index/range", func(t *testing.T) {
    51  		ci.Parallel(t)
    52  		ui := cli.NewMockUi()
    53  		cmd := &VarPurgeCommand{Meta: Meta{Ui: ui}}
    54  		code := cmd.Run([]string{`-check-index=18446744073709551616`, "foo"})
    55  		out := strings.TrimSpace(ui.ErrorWriter.String())
    56  		require.Equal(t, 1, code, "expected exit code 1, got: %d", code)
    57  		require.Equal(t, `Invalid -check-index value "18446744073709551616": out of range for uint64`, out)
    58  		require.Zero(t, ui.OutputWriter.String())
    59  	})
    60  }
    61  
    62  func TestVarPurgeCommand_Online(t *testing.T) {
    63  	ci.Parallel(t)
    64  
    65  	// Create a server
    66  	srv, client, url := testServer(t, true, nil)
    67  	t.Cleanup(func() {
    68  		srv.Shutdown()
    69  	})
    70  
    71  	t.Run("unchecked", func(t *testing.T) {
    72  		ui := cli.NewMockUi()
    73  		cmd := &VarPurgeCommand{Meta: Meta{Ui: ui}}
    74  
    75  		// Create a var to delete
    76  		sv := testVariable()
    77  		_, _, err := client.Variables().Create(sv, nil)
    78  		require.NoError(t, err)
    79  		t.Cleanup(func() { _, _ = client.Variables().Delete(sv.Path, nil) })
    80  
    81  		// Delete the variable
    82  		code := cmd.Run([]string{"-address=" + url, sv.Path})
    83  		require.Equal(t, 0, code, "expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
    84  
    85  		vars, _, err := client.Variables().List(nil)
    86  		require.NoError(t, err)
    87  		require.Len(t, vars, 0)
    88  	})
    89  
    90  	t.Run("unchecked", func(t *testing.T) {
    91  		ci.Parallel(t)
    92  		ui := cli.NewMockUi()
    93  		cmd := &VarPurgeCommand{Meta: Meta{Ui: ui}}
    94  
    95  		// Create a var to delete
    96  		sv := testVariable()
    97  		sv, _, err := client.Variables().Create(sv, nil)
    98  		require.NoError(t, err)
    99  
   100  		// Delete a variable
   101  		code := cmd.Run([]string{"-address=" + url, "-check-index=1", sv.Path})
   102  		stderr := ui.ErrorWriter.String()
   103  		require.Equal(t, 1, code, "expected exit 1, got: %d; %v", code, stderr)
   104  		require.Contains(t, stderr, "\nCheck-and-Set conflict\n\n    Your provided check-index (1)")
   105  
   106  		code = cmd.Run([]string{"-address=" + url, fmt.Sprintf("-check-index=%v", sv.ModifyIndex), sv.Path})
   107  		require.Equal(t, 0, code, "expected exit 0, got: %d; %v", code, ui.ErrorWriter.String())
   108  
   109  		vars, _, err := client.Variables().List(nil)
   110  		require.NoError(t, err)
   111  		require.Len(t, vars, 0)
   112  	})
   113  
   114  	t.Run("autocompleteArgs", func(t *testing.T) {
   115  		ci.Parallel(t)
   116  		ui := cli.NewMockUi()
   117  		cmd := &VarPurgeCommand{Meta: Meta{Ui: ui, flagAddress: url}}
   118  
   119  		// Create a var
   120  		sv := testVariable()
   121  		sv.Path = "autocomplete/test"
   122  		_, _, err := client.Variables().Create(sv, nil)
   123  		require.NoError(t, err)
   124  		t.Cleanup(func() { client.Variables().Delete(sv.Path, nil) })
   125  
   126  		args := complete.Args{Last: "aut"}
   127  		predictor := cmd.AutocompleteArgs()
   128  
   129  		res := predictor.Predict(args)
   130  		require.Equal(t, 1, len(res))
   131  		require.Equal(t, sv.Path, res[0])
   132  	})
   133  }