1package get 2 3import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/consul/agent" 8 "github.com/hashicorp/consul/api" 9 "github.com/hashicorp/consul/testrpc" 10 "github.com/mitchellh/cli" 11 "github.com/stretchr/testify/require" 12) 13 14// TODO(intentions): add test for viewing permissions and ID-less 15 16func TestIntentionGet_noTabs(t *testing.T) { 17 t.Parallel() 18 if strings.ContainsRune(New(nil).Help(), '\t') { 19 t.Fatal("help has tabs") 20 } 21} 22 23func TestIntentionGet_Validation(t *testing.T) { 24 t.Parallel() 25 26 ui := cli.NewMockUi() 27 c := New(ui) 28 29 cases := map[string]struct { 30 args []string 31 output string 32 }{ 33 "0 args": { 34 []string{}, 35 "requires exactly 1 or 2", 36 }, 37 38 "3 args": { 39 []string{"a", "b", "c"}, 40 "requires exactly 1 or 2", 41 }, 42 } 43 44 for name, tc := range cases { 45 t.Run(name, func(t *testing.T) { 46 require := require.New(t) 47 48 c.init() 49 50 // Ensure our buffer is always clear 51 if ui.ErrorWriter != nil { 52 ui.ErrorWriter.Reset() 53 } 54 if ui.OutputWriter != nil { 55 ui.OutputWriter.Reset() 56 } 57 58 require.Equal(1, c.Run(tc.args)) 59 output := ui.ErrorWriter.String() 60 require.Contains(output, tc.output) 61 }) 62 } 63} 64 65func TestIntentionGet_id(t *testing.T) { 66 if testing.Short() { 67 t.Skip("too slow for testing.Short") 68 } 69 70 t.Parallel() 71 72 require := require.New(t) 73 a := agent.NewTestAgent(t, ``) 74 defer a.Shutdown() 75 client := a.Client() 76 77 testrpc.WaitForTestAgent(t, a.RPC, "dc1") 78 79 // Create the intention 80 var id string 81 { 82 var err error 83 //nolint:staticcheck 84 id, _, err = client.Connect().IntentionCreate(&api.Intention{ 85 SourceName: "web", 86 DestinationName: "db", 87 Action: api.IntentionActionAllow, 88 }, nil) 89 require.NoError(err) 90 } 91 92 // Get it 93 ui := cli.NewMockUi() 94 c := New(ui) 95 96 args := []string{ 97 "-http-addr=" + a.HTTPAddr(), 98 id, 99 } 100 require.Equal(0, c.Run(args), ui.ErrorWriter.String()) 101 require.Contains(ui.OutputWriter.String(), id) 102} 103 104func TestIntentionGet_srcDst(t *testing.T) { 105 if testing.Short() { 106 t.Skip("too slow for testing.Short") 107 } 108 109 t.Parallel() 110 111 require := require.New(t) 112 a := agent.NewTestAgent(t, ``) 113 defer a.Shutdown() 114 client := a.Client() 115 116 testrpc.WaitForTestAgent(t, a.RPC, "dc1") 117 118 // Create the intention 119 var id string 120 { 121 var err error 122 //nolint:staticcheck 123 id, _, err = client.Connect().IntentionCreate(&api.Intention{ 124 SourceName: "web", 125 DestinationName: "db", 126 Action: api.IntentionActionAllow, 127 }, nil) 128 require.NoError(err) 129 } 130 131 // Get it 132 ui := cli.NewMockUi() 133 c := New(ui) 134 135 args := []string{ 136 "-http-addr=" + a.HTTPAddr(), 137 "web", "db", 138 } 139 require.Equal(0, c.Run(args), ui.ErrorWriter.String()) 140 require.Contains(ui.OutputWriter.String(), id) 141} 142 143func TestIntentionGet_verticalBar(t *testing.T) { 144 if testing.Short() { 145 t.Skip("too slow for testing.Short") 146 } 147 148 t.Parallel() 149 150 require := require.New(t) 151 a := agent.NewTestAgent(t, ``) 152 defer a.Shutdown() 153 client := a.Client() 154 155 testrpc.WaitForTestAgent(t, a.RPC, "dc1") 156 157 sourceName := "source|name|with|bars" 158 159 // Create the intention 160 var id string 161 { 162 var err error 163 //nolint:staticcheck 164 id, _, err = client.Connect().IntentionCreate(&api.Intention{ 165 SourceName: sourceName, 166 DestinationName: "db", 167 Action: api.IntentionActionAllow, 168 }, nil) 169 require.NoError(err) 170 } 171 172 // Get it 173 ui := cli.NewMockUi() 174 c := New(ui) 175 176 args := []string{ 177 "-http-addr=" + a.HTTPAddr(), 178 id, 179 } 180 require.Equal(0, c.Run(args), ui.ErrorWriter.String()) 181 182 // Check for sourceName presense because it should not be parsed by 183 // columnize 184 require.Contains(ui.OutputWriter.String(), sourceName) 185} 186