github.com/tencentyun/consul@v1.4.5/command/connect/proxy/proxy_test.go (about)

     1  package proxy
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/hashicorp/consul/agent"
     9  	"github.com/hashicorp/consul/connect/proxy"
    10  	"github.com/mitchellh/cli"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestCommandConfigWatcher(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	cases := []struct {
    18  		Name    string
    19  		Flags   []string
    20  		Test    func(*testing.T, *proxy.Config)
    21  		WantErr string
    22  	}{
    23  		{
    24  			Name:  "-service flag only",
    25  			Flags: []string{"-service", "web"},
    26  			Test: func(t *testing.T, cfg *proxy.Config) {
    27  				require.Equal(t, 0, cfg.PublicListener.BindPort)
    28  				require.Len(t, cfg.Upstreams, 0)
    29  			},
    30  		},
    31  
    32  		{
    33  			Name: "-service flag with upstreams",
    34  			Flags: []string{
    35  				"-service", "web",
    36  				"-upstream", "db:1234",
    37  				"-upstream", "db2:2345",
    38  			},
    39  			Test: func(t *testing.T, cfg *proxy.Config) {
    40  				require.Equal(t, 0, cfg.PublicListener.BindPort)
    41  				require.Len(t, cfg.Upstreams, 2)
    42  				require.Equal(t, 1234, cfg.Upstreams[0].LocalBindPort)
    43  				require.Equal(t, 2345, cfg.Upstreams[1].LocalBindPort)
    44  			},
    45  		},
    46  
    47  		{
    48  			Name:  "-service flag with -service-addr",
    49  			Flags: []string{"-service", "web"},
    50  			Test: func(t *testing.T, cfg *proxy.Config) {
    51  				// -service-addr has no affect since -listen isn't set
    52  				require.Equal(t, 0, cfg.PublicListener.BindPort)
    53  				require.Len(t, cfg.Upstreams, 0)
    54  			},
    55  		},
    56  
    57  		{
    58  			Name: "-service, -service-addr, -listen",
    59  			Flags: []string{
    60  				"-service", "web",
    61  				"-service-addr", "127.0.0.1:1234",
    62  				"-listen", ":4567",
    63  			},
    64  			Test: func(t *testing.T, cfg *proxy.Config) {
    65  				require.Len(t, cfg.Upstreams, 0)
    66  
    67  				require.Equal(t, "", cfg.PublicListener.BindAddress)
    68  				require.Equal(t, 4567, cfg.PublicListener.BindPort)
    69  				require.Equal(t, "127.0.0.1:1234", cfg.PublicListener.LocalServiceAddress)
    70  			},
    71  		},
    72  
    73  		{
    74  			Name: "-sidecar-for, no sidecar",
    75  			Flags: []string{
    76  				"-sidecar-for", "no-sidecar",
    77  			},
    78  			WantErr: "No sidecar proxy registered",
    79  		},
    80  
    81  		{
    82  			Name: "-sidecar-for, multiple sidecars",
    83  			Flags: []string{
    84  				"-sidecar-for", "two-sidecars",
    85  			},
    86  			// Order is non-deterministic so don't assert the list of proxy IDs here
    87  			WantErr: `More than one sidecar proxy registered for two-sidecars.
    88      Start proxy with -proxy-id and one of the following IDs: `,
    89  		},
    90  
    91  		{
    92  			Name: "-sidecar-for, non-existent",
    93  			Flags: []string{
    94  				"-sidecar-for", "foo",
    95  			},
    96  			WantErr: "No sidecar proxy registered",
    97  		},
    98  
    99  		{
   100  			Name: "-sidecar-for, one sidecar",
   101  			Flags: []string{
   102  				"-sidecar-for", "one-sidecar",
   103  			},
   104  			Test: func(t *testing.T, cfg *proxy.Config) {
   105  				// Sanity check we got the right instance.
   106  				require.Equal(t, 9999, cfg.PublicListener.BindPort)
   107  			},
   108  		},
   109  	}
   110  
   111  	for _, tc := range cases {
   112  		t.Run(tc.Name, func(t *testing.T) {
   113  			require := require.New(t)
   114  
   115  			// Register a few services with 0, 1 and 2 sidecars
   116  			a := agent.NewTestAgent(t, t.Name(), `
   117  			services {
   118  				name = "no-sidecar"
   119  				port = 1111
   120  			}
   121  			services {
   122  				name = "one-sidecar"
   123  				port = 2222
   124  				connect {
   125  					sidecar_service {
   126  						port = 9999
   127  					}
   128  				}
   129  			}
   130  			services {
   131  				name = "two-sidecars"
   132  				port = 3333
   133  				connect {
   134  					sidecar_service {}
   135  				}
   136  			}
   137  			services {
   138  				kind = "connect-proxy"
   139  				name = "other-sidecar-for-two-sidecars"
   140  				port = 4444
   141  				proxy {
   142  					destination_service_id = "two-sidecars"
   143  					destination_service_name = "two-sidecars"
   144  				}
   145  			}
   146  			`)
   147  			defer a.Shutdown()
   148  			client := a.Client()
   149  
   150  			ui := cli.NewMockUi()
   151  			c := New(ui, make(chan struct{}))
   152  			c.testNoStart = true
   153  
   154  			// Run the command
   155  			code := c.Run(append([]string{
   156  				"-http-addr=" + a.HTTPAddr(),
   157  			}, tc.Flags...))
   158  			if tc.WantErr == "" {
   159  				require.Equal(0, code, ui.ErrorWriter.String())
   160  			} else {
   161  				require.Equal(1, code, ui.ErrorWriter.String())
   162  				require.Contains(ui.ErrorWriter.String(), tc.WantErr)
   163  				return
   164  			}
   165  
   166  			// Get the configuration watcher
   167  			cw, err := c.configWatcher(client)
   168  			require.NoError(err)
   169  			if tc.Test != nil {
   170  				tc.Test(t, testConfig(t, cw))
   171  			}
   172  		})
   173  	}
   174  }
   175  
   176  func testConfig(t *testing.T, cw proxy.ConfigWatcher) *proxy.Config {
   177  	t.Helper()
   178  
   179  	select {
   180  	case cfg := <-cw.Watch():
   181  		return cfg
   182  
   183  	case <-time.After(1 * time.Second):
   184  		t.Fatal("no configuration loaded")
   185  		return nil // satisfy compiler
   186  	}
   187  }
   188  
   189  func TestCatalogCommand_noTabs(t *testing.T) {
   190  	t.Parallel()
   191  	if strings.ContainsRune(New(nil, nil).Help(), '\t') {
   192  		t.Fatal("help has tabs")
   193  	}
   194  }