github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/agent/config/flags_test.go (about)

     1  package config
     2  
     3  import (
     4  	"flag"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/pascaldekloe/goe/verify"
    10  )
    11  
    12  // TestParseFlags tests whether command line flags are properly parsed
    13  // into the Flags/File structure. It contains an example for every type
    14  // that is parsed. It does not test the conversion into the final
    15  // runtime configuration. See TestConfig for that.
    16  func TestParseFlags(t *testing.T) {
    17  	tests := []struct {
    18  		args  []string
    19  		flags Flags
    20  		err   error
    21  	}{
    22  		{},
    23  		{
    24  			args:  []string{`-bind`, `a`},
    25  			flags: Flags{Config: Config{BindAddr: pString("a")}},
    26  		},
    27  		{
    28  			args:  []string{`-bootstrap`},
    29  			flags: Flags{Config: Config{Bootstrap: pBool(true)}},
    30  		},
    31  		{
    32  			args:  []string{`-bootstrap=true`},
    33  			flags: Flags{Config: Config{Bootstrap: pBool(true)}},
    34  		},
    35  		{
    36  			args:  []string{`-bootstrap=false`},
    37  			flags: Flags{Config: Config{Bootstrap: pBool(false)}},
    38  		},
    39  		{
    40  			args:  []string{`-config-file`, `a`, `-config-dir`, `b`, `-config-file`, `c`, `-config-dir`, `d`},
    41  			flags: Flags{ConfigFiles: []string{"a", "b", "c", "d"}},
    42  		},
    43  		{
    44  			args:  []string{`-datacenter`, `a`},
    45  			flags: Flags{Config: Config{Datacenter: pString("a")}},
    46  		},
    47  		{
    48  			args:  []string{`-dns-port`, `1`},
    49  			flags: Flags{Config: Config{Ports: Ports{DNS: pInt(1)}}},
    50  		},
    51  		{
    52  			args:  []string{`-grpc-port`, `1`},
    53  			flags: Flags{Config: Config{Ports: Ports{GRPC: pInt(1)}}},
    54  		},
    55  		{
    56  			args:  []string{`-serf-lan-port`, `1`},
    57  			flags: Flags{Config: Config{Ports: Ports{SerfLAN: pInt(1)}}},
    58  		},
    59  		{
    60  			args:  []string{`-serf-wan-port`, `1`},
    61  			flags: Flags{Config: Config{Ports: Ports{SerfWAN: pInt(1)}}},
    62  		},
    63  		{
    64  			args:  []string{`-server-port`, `1`},
    65  			flags: Flags{Config: Config{Ports: Ports{Server: pInt(1)}}},
    66  		},
    67  		{
    68  			args:  []string{`-join`, `a`, `-join`, `b`},
    69  			flags: Flags{Config: Config{StartJoinAddrsLAN: []string{"a", "b"}}},
    70  		},
    71  		{
    72  			args:  []string{`-node-meta`, `a:b`, `-node-meta`, `c:d`},
    73  			flags: Flags{Config: Config{NodeMeta: map[string]string{"a": "b", "c": "d"}}},
    74  		},
    75  		{
    76  			args:  []string{`-bootstrap`, `true`},
    77  			flags: Flags{Config: Config{Bootstrap: pBool(true)}, Args: []string{"true"}},
    78  		},
    79  	}
    80  
    81  	for _, tt := range tests {
    82  		t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
    83  			flags := Flags{}
    84  			fs := flag.NewFlagSet("", flag.ContinueOnError)
    85  			AddFlags(fs, &flags)
    86  			err := fs.Parse(tt.args)
    87  			if got, want := err, tt.err; !reflect.DeepEqual(got, want) {
    88  				t.Fatalf("got error %v want %v", got, want)
    89  			}
    90  			flags.Args = fs.Args()
    91  			if !verify.Values(t, "flag", flags, tt.flags) {
    92  				t.FailNow()
    93  			}
    94  		})
    95  	}
    96  }