2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2017-09-25 18:40:42 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2017-10-20 10:02:05 +00:00
|
|
|
"flag"
|
2017-09-25 18:40:42 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-06-02 16:41:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-09-25 18:40:42 +00:00
|
|
|
)
|
|
|
|
|
2020-05-01 22:17:28 +00:00
|
|
|
// TestAddFlags_WithParse tests whether command line flags are properly parsed
|
2017-09-25 18:40:42 +00:00
|
|
|
// into the Flags/File structure. It contains an example for every type
|
|
|
|
// that is parsed. It does not test the conversion into the final
|
|
|
|
// runtime configuration. See TestConfig for that.
|
2020-05-01 22:17:28 +00:00
|
|
|
func TestAddFlags_WithParse(t *testing.T) {
|
2017-09-25 18:40:42 +00:00
|
|
|
tests := []struct {
|
2020-05-01 22:17:28 +00:00
|
|
|
args []string
|
2020-12-21 18:25:32 +00:00
|
|
|
expected LoadOpts
|
2020-05-01 22:17:28 +00:00
|
|
|
extra []string
|
2017-09-25 18:40:42 +00:00
|
|
|
}{
|
|
|
|
{},
|
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-bind`, `a`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{BindAddr: pString("a")}}},
|
2017-09-25 18:40:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-bootstrap`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Bootstrap: pBool(true)}}},
|
2017-09-25 18:40:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-bootstrap=true`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Bootstrap: pBool(true)}}},
|
2017-09-25 18:40:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-bootstrap=false`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Bootstrap: pBool(false)}}},
|
2017-09-25 18:40:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-config-file`, `a`, `-config-dir`, `b`, `-config-file`, `c`, `-config-dir`, `d`},
|
2020-12-21 18:25:32 +00:00
|
|
|
expected: LoadOpts{ConfigFiles: []string{"a", "b", "c", "d"}},
|
2017-09-25 18:40:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-datacenter`, `a`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Datacenter: pString("a")}}},
|
2017-09-25 18:40:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-dns-port`, `1`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Ports: Ports{DNS: pInt(1)}}}},
|
2017-09-25 18:40:42 +00:00
|
|
|
},
|
2018-10-03 13:05:28 +00:00
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-grpc-port`, `1`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Ports: Ports{GRPC: pInt(1)}}}},
|
2018-10-03 13:05:28 +00:00
|
|
|
},
|
2020-01-22 10:32:17 +00:00
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-http-port`, `1`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Ports: Ports{HTTP: pInt(1)}}}},
|
2020-01-22 10:32:17 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-https-port`, `1`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Ports: Ports{HTTPS: pInt(1)}}}},
|
2020-01-22 10:32:17 +00:00
|
|
|
},
|
2018-07-06 18:05:06 +00:00
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-serf-lan-port`, `1`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Ports: Ports{SerfLAN: pInt(1)}}}},
|
2018-07-06 18:05:06 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-serf-wan-port`, `1`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Ports: Ports{SerfWAN: pInt(1)}}}},
|
2018-07-06 18:05:06 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-server-port`, `1`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Ports: Ports{Server: pInt(1)}}}},
|
2018-07-06 18:05:06 +00:00
|
|
|
},
|
2017-09-25 18:40:42 +00:00
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-join`, `a`, `-join`, `b`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{DeprecatedConfig: DeprecatedConfig{StartJoinAddrsLAN: []string{"a", "b"}}}},
|
2017-09-25 18:40:42 +00:00
|
|
|
},
|
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-node-meta`, `a:b`, `-node-meta`, `c:d`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{NodeMeta: map[string]string{"a": "b", "c": "d"}}}},
|
2017-09-25 18:40:42 +00:00
|
|
|
},
|
2017-10-20 10:02:05 +00:00
|
|
|
{
|
2020-05-01 22:17:28 +00:00
|
|
|
args: []string{`-bootstrap`, `true`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{FlagValues: FlagValuesTarget{Config: Config{Bootstrap: pBool(true)}}},
|
2020-05-01 22:17:28 +00:00
|
|
|
extra: []string{"true"},
|
2017-10-20 10:02:05 +00:00
|
|
|
},
|
2020-03-09 20:59:02 +00:00
|
|
|
{
|
|
|
|
args: []string{`-primary-gateway`, `foo.local`, `-primary-gateway`, `bar.local`},
|
2022-12-14 20:28:25 +00:00
|
|
|
expected: LoadOpts{
|
|
|
|
FlagValues: FlagValuesTarget{
|
|
|
|
Config: Config{
|
|
|
|
PrimaryGateways: []string{"foo.local", "bar.local"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-03-09 20:59:02 +00:00
|
|
|
},
|
2017-09-25 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
|
2020-12-21 18:25:32 +00:00
|
|
|
flags := LoadOpts{}
|
2017-10-20 10:02:05 +00:00
|
|
|
fs := flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
AddFlags(fs, &flags)
|
2020-05-01 22:17:28 +00:00
|
|
|
|
2017-10-20 10:02:05 +00:00
|
|
|
err := fs.Parse(tt.args)
|
2020-05-01 22:17:28 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-02 16:41:25 +00:00
|
|
|
|
|
|
|
// Normalize the expected value because require.Equal considers
|
|
|
|
// empty slices/maps and nil slices/maps to be different.
|
2020-05-01 22:17:28 +00:00
|
|
|
if tt.extra == nil && fs.Args() != nil {
|
|
|
|
tt.extra = []string{}
|
2020-06-02 16:41:25 +00:00
|
|
|
}
|
2020-12-21 18:25:32 +00:00
|
|
|
if len(tt.expected.FlagValues.NodeMeta) == 0 {
|
|
|
|
tt.expected.FlagValues.NodeMeta = map[string]string{}
|
2017-09-25 18:40:42 +00:00
|
|
|
}
|
2020-05-01 22:17:28 +00:00
|
|
|
require.Equal(t, tt.extra, fs.Args())
|
|
|
|
require.Equal(t, tt.expected, flags)
|
2017-09-25 18:40:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|