Merge pull request #10253 from hashicorp/dnephin/fix-test-flake

redirecttraffic: fix a flaky test
This commit is contained in:
Daniel Nephin 2021-05-19 11:00:59 -04:00 committed by hc-github-team-consul-core
parent f69c4a2501
commit 510d81ac4d

View File

@ -1,18 +1,18 @@
package redirecttraffic package redirecttraffic
import ( import (
"sort"
"testing" "testing"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/iptables" "github.com/hashicorp/consul/sdk/iptables"
"github.com/hashicorp/consul/sdk/testutil" "github.com/hashicorp/consul/sdk/testutil"
"github.com/mitchellh/cli"
"github.com/stretchr/testify/require"
) )
func TestRun_FlagValidation(t *testing.T) { func TestRun_FlagValidation(t *testing.T) {
t.Parallel()
cases := []struct { cases := []struct {
name string name string
args []string args []string
@ -59,8 +59,6 @@ func TestRun_FlagValidation(t *testing.T) {
} }
func TestGenerateConfigFromFlags(t *testing.T) { func TestGenerateConfigFromFlags(t *testing.T) {
t.Parallel()
cases := []struct { cases := []struct {
name string name string
command func() cmd command func() cmd
@ -69,15 +67,15 @@ func TestGenerateConfigFromFlags(t *testing.T) {
expError string expError string
}{ }{
{ {
"proxyID with service port provided", name: "proxyID with service port provided",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
Kind: api.ServiceKindConnectProxy, Kind: api.ServiceKindConnectProxy,
ID: "test-proxy-id", ID: "test-proxy-id",
@ -89,23 +87,22 @@ func TestGenerateConfigFromFlags(t *testing.T) {
}, },
}, },
}, },
iptables.Config{ expCfg: iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 20000, ProxyInboundPort: 20000,
ProxyOutboundPort: iptables.DefaultTProxyOutboundPort, ProxyOutboundPort: iptables.DefaultTProxyOutboundPort,
}, },
"",
}, },
{ {
"proxyID with bind_port(int) provided", name: "proxyID with bind_port(int) provided",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
Kind: api.ServiceKindConnectProxy, Kind: api.ServiceKindConnectProxy,
ID: "test-proxy-id", ID: "test-proxy-id",
@ -120,23 +117,22 @@ func TestGenerateConfigFromFlags(t *testing.T) {
}, },
}, },
}, },
iptables.Config{ expCfg: iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 21000, ProxyInboundPort: 21000,
ProxyOutboundPort: iptables.DefaultTProxyOutboundPort, ProxyOutboundPort: iptables.DefaultTProxyOutboundPort,
}, },
"",
}, },
{ {
"proxyID with bind_port(string) provided", name: "proxyID with bind_port(string) provided",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
Kind: api.ServiceKindConnectProxy, Kind: api.ServiceKindConnectProxy,
ID: "test-proxy-id", ID: "test-proxy-id",
@ -151,23 +147,22 @@ func TestGenerateConfigFromFlags(t *testing.T) {
}, },
}, },
}, },
iptables.Config{ expCfg: iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 21000, ProxyInboundPort: 21000,
ProxyOutboundPort: iptables.DefaultTProxyOutboundPort, ProxyOutboundPort: iptables.DefaultTProxyOutboundPort,
}, },
"",
}, },
{ {
"proxyID with bind_port(invalid type) provided", name: "proxyID with bind_port(invalid type) provided",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
Kind: api.ServiceKindConnectProxy, Kind: api.ServiceKindConnectProxy,
ID: "test-proxy-id", ID: "test-proxy-id",
@ -182,19 +177,18 @@ func TestGenerateConfigFromFlags(t *testing.T) {
}, },
}, },
}, },
iptables.Config{}, expError: "failed parsing Proxy.Config: 1 error(s) decoding:\n\n* cannot parse 'bind_port' as int:",
"failed parsing Proxy.Config: 1 error(s) decoding:\n\n* cannot parse 'bind_port' as int:",
}, },
{ {
"proxyID with proxy outbound port", name: "proxyID with proxy outbound port",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
Kind: api.ServiceKindConnectProxy, Kind: api.ServiceKindConnectProxy,
ID: "test-proxy-id", ID: "test-proxy-id",
@ -209,36 +203,33 @@ func TestGenerateConfigFromFlags(t *testing.T) {
}, },
}, },
}, },
iptables.Config{ expCfg: iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 20000, ProxyInboundPort: 20000,
ProxyOutboundPort: 21000, ProxyOutboundPort: 21000,
}, },
"",
}, },
{ {
"proxyID provided, but Consul is not reachable", name: "proxyID provided, but Consul is not reachable",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
nil, expError: "failed to fetch proxy service from Consul Agent: ",
iptables.Config{},
"failed to fetch proxy service from Consul Agent: ",
}, },
{ {
"proxyID of a non-proxy service", name: "proxyID of a non-proxy service",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
ID: "test-proxy-id", ID: "test-proxy-id",
Name: "test-proxy", Name: "test-proxy",
@ -246,29 +237,26 @@ func TestGenerateConfigFromFlags(t *testing.T) {
Address: "1.1.1.1", Address: "1.1.1.1",
}, },
}, },
iptables.Config{}, expError: "service test-proxy-id is not a proxy service",
"service test-proxy-id is not a proxy service",
}, },
{ {
"only proxy inbound port is provided", name: "only proxy inbound port is provided",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyInboundPort = 15000 c.proxyInboundPort = 15000
return c return c
}, },
nil, expCfg: iptables.Config{
iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 15000, ProxyInboundPort: 15000,
ProxyOutboundPort: iptables.DefaultTProxyOutboundPort, ProxyOutboundPort: iptables.DefaultTProxyOutboundPort,
}, },
"",
}, },
{ {
"proxy inbound and outbound ports are provided", name: "proxy inbound and outbound ports are provided",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
@ -276,17 +264,15 @@ func TestGenerateConfigFromFlags(t *testing.T) {
c.proxyOutboundPort = 16000 c.proxyOutboundPort = 16000
return c return c
}, },
nil, expCfg: iptables.Config{
iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 15000, ProxyInboundPort: 15000,
ProxyOutboundPort: 16000, ProxyOutboundPort: 16000,
}, },
"",
}, },
{ {
"exclude inbound ports are provided", name: "exclude inbound ports are provided",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
@ -294,18 +280,16 @@ func TestGenerateConfigFromFlags(t *testing.T) {
c.excludeInboundPorts = []string{"8080", "21000"} c.excludeInboundPorts = []string{"8080", "21000"}
return c return c
}, },
nil, expCfg: iptables.Config{
iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 15000, ProxyInboundPort: 15000,
ProxyOutboundPort: 15001, ProxyOutboundPort: 15001,
ExcludeInboundPorts: []string{"8080", "21000"}, ExcludeInboundPorts: []string{"8080", "21000"},
}, },
"",
}, },
{ {
"exclude outbound ports are provided", name: "exclude outbound ports are provided",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
@ -313,18 +297,16 @@ func TestGenerateConfigFromFlags(t *testing.T) {
c.excludeOutboundPorts = []string{"8080", "21000"} c.excludeOutboundPorts = []string{"8080", "21000"}
return c return c
}, },
nil, expCfg: iptables.Config{
iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 15000, ProxyInboundPort: 15000,
ProxyOutboundPort: 15001, ProxyOutboundPort: 15001,
ExcludeOutboundPorts: []string{"8080", "21000"}, ExcludeOutboundPorts: []string{"8080", "21000"},
}, },
"",
}, },
{ {
"exclude outbound CIDRs are provided", name: "exclude outbound CIDRs are provided",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
@ -332,18 +314,16 @@ func TestGenerateConfigFromFlags(t *testing.T) {
c.excludeOutboundCIDRs = []string{"1.1.1.1", "2.2.2.2/24"} c.excludeOutboundCIDRs = []string{"1.1.1.1", "2.2.2.2/24"}
return c return c
}, },
nil, expCfg: iptables.Config{
iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 15000, ProxyInboundPort: 15000,
ProxyOutboundPort: 15001, ProxyOutboundPort: 15001,
ExcludeOutboundCIDRs: []string{"1.1.1.1", "2.2.2.2/24"}, ExcludeOutboundCIDRs: []string{"1.1.1.1", "2.2.2.2/24"},
}, },
"",
}, },
{ {
"exclude UIDs are provided", name: "exclude UIDs are provided",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
@ -351,25 +331,23 @@ func TestGenerateConfigFromFlags(t *testing.T) {
c.excludeUIDs = []string{"2345", "3456"} c.excludeUIDs = []string{"2345", "3456"}
return c return c
}, },
nil, expCfg: iptables.Config{
iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 15000, ProxyInboundPort: 15000,
ProxyOutboundPort: 15001, ProxyOutboundPort: 15001,
ExcludeUIDs: []string{"2345", "3456"}, ExcludeUIDs: []string{"2345", "3456"},
}, },
"",
}, },
{ {
"proxy config has envoy_prometheus_bind_addr set", name: "proxy config has envoy_prometheus_bind_addr set",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
Kind: api.ServiceKindConnectProxy, Kind: api.ServiceKindConnectProxy,
ID: "test-proxy-id", ID: "test-proxy-id",
@ -384,24 +362,23 @@ func TestGenerateConfigFromFlags(t *testing.T) {
}, },
}, },
}, },
iptables.Config{ expCfg: iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 20000, ProxyInboundPort: 20000,
ProxyOutboundPort: iptables.DefaultTProxyOutboundPort, ProxyOutboundPort: iptables.DefaultTProxyOutboundPort,
ExcludeInboundPorts: []string{"9000"}, ExcludeInboundPorts: []string{"9000"},
}, },
"",
}, },
{ {
"proxy config has an invalid envoy_prometheus_bind_addr set", name: "proxy config has an invalid envoy_prometheus_bind_addr set",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
Kind: api.ServiceKindConnectProxy, Kind: api.ServiceKindConnectProxy,
ID: "test-proxy-id", ID: "test-proxy-id",
@ -416,19 +393,18 @@ func TestGenerateConfigFromFlags(t *testing.T) {
}, },
}, },
}, },
iptables.Config{}, expError: "failed parsing host and port from envoy_prometheus_bind_addr: address 9000: missing port in address",
"failed parsing host and port from envoy_prometheus_bind_addr: address 9000: missing port in address",
}, },
{ {
"proxy config has envoy_stats_bind_addr set", name: "proxy config has envoy_stats_bind_addr set",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
Kind: api.ServiceKindConnectProxy, Kind: api.ServiceKindConnectProxy,
ID: "test-proxy-id", ID: "test-proxy-id",
@ -443,24 +419,23 @@ func TestGenerateConfigFromFlags(t *testing.T) {
}, },
}, },
}, },
iptables.Config{ expCfg: iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 20000, ProxyInboundPort: 20000,
ProxyOutboundPort: iptables.DefaultTProxyOutboundPort, ProxyOutboundPort: iptables.DefaultTProxyOutboundPort,
ExcludeInboundPorts: []string{"8000"}, ExcludeInboundPorts: []string{"8000"},
}, },
"",
}, },
{ {
"proxy config has an invalid envoy_stats_bind_addr set", name: "proxy config has an invalid envoy_stats_bind_addr set",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
Kind: api.ServiceKindConnectProxy, Kind: api.ServiceKindConnectProxy,
ID: "test-proxy-id", ID: "test-proxy-id",
@ -475,19 +450,18 @@ func TestGenerateConfigFromFlags(t *testing.T) {
}, },
}, },
}, },
iptables.Config{}, expError: "failed parsing host and port from envoy_stats_bind_addr: address 8000: missing port in address",
"failed parsing host and port from envoy_stats_bind_addr: address 8000: missing port in address",
}, },
{ {
"proxy config has expose paths with listener port set", name: "proxy config has expose paths with listener port set",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
Kind: api.ServiceKindConnectProxy, Kind: api.ServiceKindConnectProxy,
ID: "test-proxy-id", ID: "test-proxy-id",
@ -508,24 +482,23 @@ func TestGenerateConfigFromFlags(t *testing.T) {
}, },
}, },
}, },
iptables.Config{ expCfg: iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 20000, ProxyInboundPort: 20000,
ProxyOutboundPort: iptables.DefaultTProxyOutboundPort, ProxyOutboundPort: iptables.DefaultTProxyOutboundPort,
ExcludeInboundPorts: []string{"23000"}, ExcludeInboundPorts: []string{"23000"},
}, },
"",
}, },
{ {
"proxy config has expose paths with checks set to true", name: "proxy config has expose paths with checks set to true",
func() cmd { command: func() cmd {
var c cmd var c cmd
c.init() c.init()
c.proxyUID = "1234" c.proxyUID = "1234"
c.proxyID = "test-proxy-id" c.proxyID = "test-proxy-id"
return c return c
}, },
[]api.AgentServiceRegistration{ consulServices: []api.AgentServiceRegistration{
{ {
ID: "foo-id", ID: "foo-id",
Name: "foo", Name: "foo",
@ -559,13 +532,12 @@ func TestGenerateConfigFromFlags(t *testing.T) {
}, },
}, },
}, },
iptables.Config{ expCfg: iptables.Config{
ProxyUserID: "1234", ProxyUserID: "1234",
ProxyInboundPort: 20000, ProxyInboundPort: 20000,
ProxyOutboundPort: iptables.DefaultTProxyOutboundPort, ProxyOutboundPort: iptables.DefaultTProxyOutboundPort,
ExcludeInboundPorts: []string{"21500", "21501"}, ExcludeInboundPorts: []string{"21500", "21501"},
}, },
"",
}, },
} }
@ -596,7 +568,10 @@ func TestGenerateConfigFromFlags(t *testing.T) {
if c.expError == "" { if c.expError == "" {
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, c.expCfg, cfg)
sort.Strings(c.expCfg.ExcludeInboundPorts)
sort.Strings(cfg.ExcludeInboundPorts)
require.Equal(t, c.expCfg, cfg)
} else { } else {
require.Error(t, err) require.Error(t, err)
require.Contains(t, err.Error(), c.expError) require.Contains(t, err.Error(), c.expError)