github.imxd.top/hashicorp/consul@v1.4.5/command/intention/get/get_test.go (about)

     1  package get
     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_id(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  	var id string
    72  	{
    73  		var err error
    74  		id, _, err = client.Connect().IntentionCreate(&api.Intention{
    75  			SourceName:      "web",
    76  			DestinationName: "db",
    77  			Action:          api.IntentionActionAllow,
    78  		}, nil)
    79  		require.NoError(err)
    80  	}
    81  
    82  	// Get it
    83  	ui := cli.NewMockUi()
    84  	c := New(ui)
    85  
    86  	args := []string{
    87  		"-http-addr=" + a.HTTPAddr(),
    88  		id,
    89  	}
    90  	require.Equal(0, c.Run(args), ui.ErrorWriter.String())
    91  	require.Contains(ui.OutputWriter.String(), id)
    92  }
    93  
    94  func TestCommand_srcDst(t *testing.T) {
    95  	t.Parallel()
    96  
    97  	require := require.New(t)
    98  	a := agent.NewTestAgent(t, t.Name(), ``)
    99  	defer a.Shutdown()
   100  	client := a.Client()
   101  
   102  	// Create the intention
   103  	var id string
   104  	{
   105  		var err error
   106  		id, _, err = client.Connect().IntentionCreate(&api.Intention{
   107  			SourceName:      "web",
   108  			DestinationName: "db",
   109  			Action:          api.IntentionActionAllow,
   110  		}, nil)
   111  		require.NoError(err)
   112  	}
   113  
   114  	// Get it
   115  	ui := cli.NewMockUi()
   116  	c := New(ui)
   117  
   118  	args := []string{
   119  		"-http-addr=" + a.HTTPAddr(),
   120  		"web", "db",
   121  	}
   122  	require.Equal(0, c.Run(args), ui.ErrorWriter.String())
   123  	require.Contains(ui.OutputWriter.String(), id)
   124  }