github.com/criteo-forks/consul@v1.4.5-criteonogrpc/command/intention/delete/delete_test.go (about) 1 package delete 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/consul/agent" 8 "github.com/hashicorp/consul/api" 9 "github.com/mitchellh/cli" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestCommand_noTabs(t *testing.T) { 14 t.Parallel() 15 if strings.ContainsRune(New(nil).Help(), '\t') { 16 t.Fatal("help has tabs") 17 } 18 } 19 20 func TestCommand_Validation(t *testing.T) { 21 t.Parallel() 22 23 ui := cli.NewMockUi() 24 c := New(ui) 25 26 cases := map[string]struct { 27 args []string 28 output string 29 }{ 30 "0 args": { 31 []string{}, 32 "requires exactly 1 or 2", 33 }, 34 35 "3 args": { 36 []string{"a", "b", "c"}, 37 "requires exactly 1 or 2", 38 }, 39 } 40 41 for name, tc := range cases { 42 t.Run(name, func(t *testing.T) { 43 require := require.New(t) 44 45 c.init() 46 47 // Ensure our buffer is always clear 48 if ui.ErrorWriter != nil { 49 ui.ErrorWriter.Reset() 50 } 51 if ui.OutputWriter != nil { 52 ui.OutputWriter.Reset() 53 } 54 55 require.Equal(1, c.Run(tc.args)) 56 output := ui.ErrorWriter.String() 57 require.Contains(output, tc.output) 58 }) 59 } 60 } 61 62 func TestCommand(t *testing.T) { 63 t.Parallel() 64 65 require := require.New(t) 66 a := agent.NewTestAgent(t, t.Name(), ``) 67 defer a.Shutdown() 68 client := a.Client() 69 70 // Create the intention 71 { 72 _, _, err := client.Connect().IntentionCreate(&api.Intention{ 73 SourceName: "web", 74 DestinationName: "db", 75 Action: api.IntentionActionDeny, 76 }, nil) 77 require.NoError(err) 78 } 79 80 // Delete it 81 { 82 ui := cli.NewMockUi() 83 c := New(ui) 84 85 args := []string{ 86 "-http-addr=" + a.HTTPAddr(), 87 "web", "db", 88 } 89 require.Equal(0, c.Run(args), ui.ErrorWriter.String()) 90 require.Contains(ui.OutputWriter.String(), "deleted") 91 } 92 93 // Find it (should be gone) 94 { 95 ixns, _, err := client.Connect().Intentions(nil) 96 require.NoError(err) 97 require.Len(ixns, 0) 98 } 99 }