open-consul/command/connect/proxy/flag_upstreams_test.go
R.B. Boyer 05c7373a28 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 10:46:23 -06:00

126 lines
2.1 KiB
Go

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{
"db": {
LocalBindPort: 8181,
DestinationName: "db",
DestinationType: "service",
},
},
"",
},
{
"single value prepared query",
[]string{"db.query:8181"},
map[string]proxy.UpstreamConfig{
"db": {
LocalBindPort: 8181,
DestinationName: "db",
DestinationType: "prepared_query",
},
},
"",
},
{
"invalid type",
[]string{"db.bad:8181"},
nil,
"Upstream type",
},
{
"address specified",
[]string{"db:127.0.0.55:8181"},
map[string]proxy.UpstreamConfig{
"db": {
LocalBindAddress: "127.0.0.55",
LocalBindPort: 8181,
DestinationName: "db",
DestinationType: "service",
},
},
"",
},
{
"repeat value, overwrite",
[]string{"db:8181", "db:8282"},
map[string]proxy.UpstreamConfig{
"db": {
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 != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.Error)
return
}
require.Equal(t, tc.Expected, actual)
})
}
}