...

Source file src/k8s.io/kubernetes/test/conformance/image/go-runner/env_test.go

Documentation: k8s.io/kubernetes/test/conformance/image/go-runner

     1  /*
     2  Copyright 2019 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 TestEnv(t *testing.T) {
    25  	testCases := []struct {
    26  		desc    string
    27  		preHook func()
    28  		env     Getenver
    29  		expect  map[string]string
    30  	}{
    31  		{
    32  			desc: "OS env",
    33  			env:  &osEnv{},
    34  			preHook: func() {
    35  				t.Setenv("key1", "1")
    36  			},
    37  			expect: map[string]string{"key1": "1"},
    38  		}, {
    39  			desc: "OS env falls defaults to empty",
    40  			env:  &osEnv{},
    41  			preHook: func() {
    42  				os.Unsetenv("key1")
    43  			},
    44  			expect: map[string]string{"key1": ""},
    45  		}, {
    46  			desc: "First choice of env respected",
    47  			env: &defaultEnver{
    48  				firstChoice: &explicitEnv{
    49  					vals: map[string]string{
    50  						"key1": "1",
    51  					},
    52  				},
    53  				defaults: map[string]string{
    54  					"key1": "default1",
    55  					"key2": "default2",
    56  				},
    57  			},
    58  			expect: map[string]string{
    59  				"key1": "1",
    60  				"key2": "default2",
    61  			},
    62  		},
    63  	}
    64  	for _, tc := range testCases {
    65  		t.Run(tc.desc, func(t *testing.T) {
    66  			for k, expectVal := range tc.expect {
    67  				if tc.preHook != nil {
    68  					tc.preHook()
    69  				}
    70  				val := tc.env.Getenv(k)
    71  				if val != expectVal {
    72  					t.Errorf("Expected %q but got %q", expectVal, val)
    73  				}
    74  			}
    75  		})
    76  	}
    77  }
    78  

View as plain text