github.com/mre-fog/trillianxx@v1.1.2-0.20180615153820-ae375a99d36a/util/flagsaver/flagsaver_test.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     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 flagsaver
    16  
    17  import (
    18  	"flag"
    19  	"testing"
    20  	"time"
    21  
    22  	_ "github.com/golang/glog"
    23  )
    24  
    25  var (
    26  	_ = flag.Int("int_flag", 123, "test integer flag")
    27  	_ = flag.String("str_flag", "foo", "test string flag")
    28  	_ = flag.Duration("duration_flag", 5*time.Second, "test duration flag")
    29  )
    30  
    31  // TestRestore checks that flags are saved and restore correctly.
    32  // Checks are performed on flags with both their default values and with explicit values set.
    33  // Only a subset of the possible flag types are currently tested.
    34  func TestRestore(t *testing.T) {
    35  	tests := []struct {
    36  		desc string
    37  		// flag is the name of the flag to save and restore.
    38  		flag string
    39  		// oldValue is the value the flag should have when saved. If empty, this indicates the flag should have its default value.
    40  		oldValue string
    41  		// newValue ist he value the flag should have just before being restored to oldValue.
    42  		newValue string
    43  	}{
    44  		{
    45  			desc:     "RestoreDefaultIntValue",
    46  			flag:     "int_flag",
    47  			newValue: "666",
    48  		},
    49  		{
    50  			desc:     "RestoreDefaultStrValue",
    51  			flag:     "str_flag",
    52  			newValue: "baz",
    53  		},
    54  		{
    55  			desc:     "RestoreDefaultDurationValue",
    56  			flag:     "duration_flag",
    57  			newValue: "1m0s",
    58  		},
    59  		{
    60  			desc:     "RestoreSetIntValue",
    61  			flag:     "int_flag",
    62  			oldValue: "555",
    63  			newValue: "666",
    64  		},
    65  		{
    66  			desc:     "RestoreSetStrValue",
    67  			flag:     "str_flag",
    68  			oldValue: "bar",
    69  			newValue: "baz",
    70  		},
    71  		{
    72  			desc:     "RestoreSetDurationValue",
    73  			flag:     "duration_flag",
    74  			oldValue: "10s",
    75  			newValue: "1m0s",
    76  		},
    77  	}
    78  
    79  	for _, test := range tests {
    80  		f := flag.Lookup(test.flag)
    81  		if f == nil {
    82  			t.Errorf("%v: flag.Lookup(%q) = nil, want not nil", test.desc, test.flag)
    83  			continue
    84  		}
    85  
    86  		if test.oldValue != "" {
    87  			if err := flag.Set(test.flag, test.oldValue); err != nil {
    88  				t.Errorf("%v: flag.Set(%q, %q) = %q, want nil", test.desc, test.flag, test.oldValue, err)
    89  				continue
    90  			}
    91  		} else {
    92  			// Use the default value of the flag as the oldValue if none was set.
    93  			test.oldValue = f.DefValue
    94  		}
    95  
    96  		func() {
    97  			defer Save().Restore()
    98  			flag.Set(test.flag, test.newValue)
    99  			if gotValue := f.Value.String(); gotValue != test.newValue {
   100  				t.Errorf("%v: flag.Lookup(%q).Value.String() = %q, want %q", test.desc, test.flag, gotValue, test.newValue)
   101  			}
   102  		}()
   103  
   104  		if gotValue := f.Value.String(); gotValue != test.oldValue {
   105  			t.Errorf("%v: flag.Lookup(%q).Value.String() = %q, want %q", test.desc, test.flag, gotValue, test.oldValue)
   106  		}
   107  	}
   108  }