...

Source file src/go.etcd.io/etcd/pkg/v3/flags/flag_test.go

Documentation: go.etcd.io/etcd/pkg/v3/flags

     1  // Copyright 2015 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package flags
    16  
    17  import (
    18  	"flag"
    19  	"os"
    20  	"strings"
    21  	"testing"
    22  
    23  	"go.uber.org/zap"
    24  )
    25  
    26  func TestSetFlagsFromEnv(t *testing.T) {
    27  	fs := flag.NewFlagSet("testing", flag.ExitOnError)
    28  	fs.String("a", "", "")
    29  	fs.String("b", "", "")
    30  	fs.String("c", "", "")
    31  	fs.Parse([]string{})
    32  
    33  	os.Clearenv()
    34  	// flags should be settable using env vars
    35  	os.Setenv("ETCD_A", "foo")
    36  	// and command-line flags
    37  	if err := fs.Set("b", "bar"); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	// first verify that flags are as expected before reading the env
    42  	for f, want := range map[string]string{
    43  		"a": "",
    44  		"b": "bar",
    45  	} {
    46  		if got := fs.Lookup(f).Value.String(); got != want {
    47  			t.Fatalf("flag %q=%q, want %q", f, got, want)
    48  		}
    49  	}
    50  
    51  	// now read the env and verify flags were updated as expected
    52  	err := SetFlagsFromEnv(zap.NewExample(), "ETCD", fs)
    53  	if err != nil {
    54  		t.Errorf("err=%v, want nil", err)
    55  	}
    56  	for f, want := range map[string]string{
    57  		"a": "foo",
    58  		"b": "bar",
    59  	} {
    60  		if got := fs.Lookup(f).Value.String(); got != want {
    61  			t.Errorf("flag %q=%q, want %q", f, got, want)
    62  		}
    63  	}
    64  }
    65  
    66  func TestSetFlagsFromEnvBad(t *testing.T) {
    67  	// now verify that an error is propagated
    68  	fs := flag.NewFlagSet("testing", flag.ExitOnError)
    69  	fs.Int("x", 0, "")
    70  	os.Setenv("ETCD_X", "not_a_number")
    71  	if err := SetFlagsFromEnv(zap.NewExample(), "ETCD", fs); err == nil {
    72  		t.Errorf("err=nil, want != nil")
    73  	}
    74  }
    75  
    76  func TestSetFlagsFromEnvParsingError(t *testing.T) {
    77  	fs := flag.NewFlagSet("etcd", flag.ContinueOnError)
    78  	var tickMs uint
    79  	fs.UintVar(&tickMs, "heartbeat-interval", 0, "Time (in milliseconds) of a heartbeat interval.")
    80  
    81  	if oerr := os.Setenv("ETCD_HEARTBEAT_INTERVAL", "100 # ms"); oerr != nil {
    82  		t.Fatal(oerr)
    83  	}
    84  	defer os.Unsetenv("ETCD_HEARTBEAT_INTERVAL")
    85  
    86  	err := SetFlagsFromEnv(zap.NewExample(), "ETCD", fs)
    87  	for _, v := range []string{"invalid syntax", "parse error"} {
    88  		if strings.Contains(err.Error(), v) {
    89  			err = nil
    90  			break
    91  		}
    92  	}
    93  	if err != nil {
    94  		t.Fatalf("unexpected error %v", err)
    95  	}
    96  }
    97  

View as plain text