consul/command/connect/proxy/flag_upstreams_test.go

129 lines
2.2 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
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)
})
}
}