2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-05-19 07:43:38 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/connect/proxy"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFlagUpstreams_impl(t *testing.T) {
|
|
|
|
var _ flag.Value = new(FlagUpstreams)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFlagUpstreams(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Input []string
|
|
|
|
Expected map[string]proxy.UpstreamConfig
|
|
|
|
Error string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"bad format",
|
|
|
|
[]string{"foo"},
|
|
|
|
nil,
|
|
|
|
"should be name:addr",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"port not int",
|
|
|
|
[]string{"db:hello"},
|
|
|
|
nil,
|
|
|
|
"invalid syntax",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"4 parts",
|
|
|
|
[]string{"db:127.0.0.1:8181:foo"},
|
|
|
|
nil,
|
|
|
|
"invalid syntax",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"single value",
|
|
|
|
[]string{"db:8181"},
|
|
|
|
map[string]proxy.UpstreamConfig{
|
2020-06-16 17:19:31 +00:00
|
|
|
"db": {
|
2018-05-19 07:43:38 +00:00
|
|
|
LocalBindPort: 8181,
|
|
|
|
DestinationName: "db",
|
|
|
|
DestinationType: "service",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
2018-06-06 03:27:30 +00:00
|
|
|
{
|
|
|
|
"single value prepared query",
|
|
|
|
[]string{"db.query:8181"},
|
|
|
|
map[string]proxy.UpstreamConfig{
|
2020-06-16 17:19:31 +00:00
|
|
|
"db": {
|
2018-06-06 03:27:30 +00:00
|
|
|
LocalBindPort: 8181,
|
|
|
|
DestinationName: "db",
|
|
|
|
DestinationType: "prepared_query",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"invalid type",
|
|
|
|
[]string{"db.bad:8181"},
|
|
|
|
nil,
|
|
|
|
"Upstream type",
|
|
|
|
},
|
|
|
|
|
2018-05-19 07:43:38 +00:00
|
|
|
{
|
|
|
|
"address specified",
|
|
|
|
[]string{"db:127.0.0.55:8181"},
|
|
|
|
map[string]proxy.UpstreamConfig{
|
2020-06-16 17:19:31 +00:00
|
|
|
"db": {
|
2018-05-19 07:43:38 +00:00
|
|
|
LocalBindAddress: "127.0.0.55",
|
|
|
|
LocalBindPort: 8181,
|
|
|
|
DestinationName: "db",
|
|
|
|
DestinationType: "service",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"repeat value, overwrite",
|
|
|
|
[]string{"db:8181", "db:8282"},
|
|
|
|
map[string]proxy.UpstreamConfig{
|
2020-06-16 17:19:31 +00:00
|
|
|
"db": {
|
2018-05-19 07:43:38 +00:00
|
|
|
LocalBindPort: 8282,
|
|
|
|
DestinationName: "db",
|
|
|
|
DestinationType: "service",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
|
|
|
|
var actual map[string]proxy.UpstreamConfig
|
|
|
|
f := (*FlagUpstreams)(&actual)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, input := range tc.Input {
|
|
|
|
err = f.Set(input)
|
|
|
|
// Note we only test the last error. This could make some
|
|
|
|
// test failures confusing but it shouldn't be too bad.
|
|
|
|
}
|
|
|
|
if tc.Error != "" {
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tc.Error)
|
2018-05-19 07:43:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.Equal(t, tc.Expected, actual)
|
2018-05-19 07:43:38 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|