...

Source file src/k8s.io/kubernetes/cluster/images/etcd/migrate/options_test.go

Documentation: k8s.io/kubernetes/cluster/images/etcd/migrate

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  )
    23  
    24  func setEnvVar(t *testing.T, env, val string, exists bool) {
    25  	if exists {
    26  		t.Setenv(env, val)
    27  	} else if prev, ok := os.LookupEnv(env); ok {
    28  		t.Cleanup(func() { os.Setenv(env, prev) })
    29  
    30  		if err := os.Unsetenv(env); err != nil {
    31  			t.Errorf("couldn't unset env %s: %v", env, err)
    32  		}
    33  	}
    34  }
    35  
    36  func TestFallbackToEnv(t *testing.T) {
    37  	testCases := []struct {
    38  		desc          string
    39  		env           string
    40  		value         string
    41  		valueSet      bool
    42  		expectedValue string
    43  		expectedError bool
    44  	}{
    45  		{
    46  			desc:          "value unset",
    47  			env:           "FOO",
    48  			valueSet:      false,
    49  			expectedValue: "",
    50  			expectedError: true,
    51  		},
    52  		{
    53  			desc:          "value set empty",
    54  			env:           "FOO",
    55  			value:         "",
    56  			valueSet:      true,
    57  			expectedValue: "",
    58  			expectedError: true,
    59  		},
    60  		{
    61  			desc:          "value set",
    62  			env:           "FOO",
    63  			value:         "foo",
    64  			valueSet:      true,
    65  			expectedValue: "foo",
    66  			expectedError: false,
    67  		},
    68  	}
    69  
    70  	for _, test := range testCases {
    71  		t.Run(test.desc, func(t *testing.T) {
    72  			setEnvVar(t, test.env, test.value, test.valueSet)
    73  			value, err := fallbackToEnv("some-flag", test.env)
    74  			if test.expectedError {
    75  				if err == nil {
    76  					t.Errorf("expected error, got: %v", err)
    77  				}
    78  			} else {
    79  				if err != nil {
    80  					t.Errorf("unexpected error: %v", err)
    81  				}
    82  				if value != test.expectedValue {
    83  					t.Errorf("unexpected result: %s, expected: %s", value, test.expectedValue)
    84  				}
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestFallbackToEnvWithDefault(t *testing.T) {
    91  	testCases := []struct {
    92  		desc          string
    93  		env           string
    94  		value         string
    95  		valueSet      bool
    96  		defaultValue  string
    97  		expectedValue string
    98  		expectedError bool
    99  	}{
   100  		{
   101  			desc:          "value unset",
   102  			env:           "FOO",
   103  			valueSet:      false,
   104  			defaultValue:  "default",
   105  			expectedValue: "default",
   106  		},
   107  		{
   108  			desc:          "value set empty",
   109  			env:           "FOO",
   110  			value:         "",
   111  			valueSet:      true,
   112  			defaultValue:  "default",
   113  			expectedValue: "default",
   114  		},
   115  		{
   116  			desc:          "value set",
   117  			env:           "FOO",
   118  			value:         "foo",
   119  			valueSet:      true,
   120  			defaultValue:  "default",
   121  			expectedValue: "foo",
   122  		},
   123  	}
   124  
   125  	for _, test := range testCases {
   126  		t.Run(test.desc, func(t *testing.T) {
   127  			setEnvVar(t, test.env, test.value, test.valueSet)
   128  			value := fallbackToEnvWithDefault("some-flag", test.env, test.defaultValue)
   129  			if value != test.expectedValue {
   130  				t.Errorf("unexpected result: %s, expected: %s", value, test.expectedValue)
   131  			}
   132  		})
   133  	}
   134  }
   135  

View as plain text