...

Source file src/k8s.io/kubectl/pkg/cmd/util/env_file_test.go

Documentation: k8s.io/kubectl/pkg/cmd/util

     1  /*
     2  Copyright 2017 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 util
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  )
    23  
    24  // Test the cases of proccessEnvFileLine that can be run without touching the
    25  // environment.
    26  func Test_processEnvFileLine(t *testing.T) {
    27  	testCases := []struct {
    28  		name          string
    29  		line          []byte
    30  		currentLine   int
    31  		expectedKey   string
    32  		expectedValue string
    33  		expectedErr   string
    34  	}{
    35  		{"the utf8bom is trimmed on the first line",
    36  			append(utf8bom, 'a', '=', 'c'), 0, "a", "c", ""},
    37  
    38  		{"the utf8bom is NOT trimmed on the second line",
    39  			append(utf8bom, 'a', '=', 'c'), 1, "", "", "not a valid key name"},
    40  
    41  		{"no key is returned on a comment line",
    42  			[]byte{' ', '#', 'c'}, 0, "", "", ""},
    43  
    44  		{"no key is returned on a blank line",
    45  			[]byte{' ', ' ', '\t'}, 0, "", "", ""},
    46  
    47  		{"key is returned even with no value",
    48  			[]byte{' ', 'x', '='}, 0, "x", "", ""},
    49  	}
    50  	for _, tt := range testCases {
    51  		t.Run(tt.name, func(t *testing.T) {
    52  			key, value, err := processEnvFileLine(tt.line, `filename`, tt.currentLine)
    53  			t.Logf("Testing that %s.", tt.name)
    54  			if tt.expectedKey != key {
    55  				t.Errorf("\texpected key %q, received %q", tt.expectedKey, key)
    56  			}
    57  			if tt.expectedValue != value {
    58  				t.Errorf("\texpected value %q, received %q", tt.expectedValue, value)
    59  			}
    60  			if len(tt.expectedErr) == 0 {
    61  				if err != nil {
    62  					t.Errorf("\tunexpected err %v", err)
    63  				}
    64  			} else {
    65  				if !strings.Contains(err.Error(), tt.expectedErr) {
    66  					t.Errorf("\terr %v doesn't match expected %q", err, tt.expectedErr)
    67  				}
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  // proccessEnvFileLine needs to fetch the value from the environment if no
    74  // equals sign is provided.
    75  // For example:
    76  //
    77  //	my_key1=alpha
    78  //	my_key2=beta
    79  //	my_key3
    80  //
    81  // In this file, my_key3 must be fetched from the environment.
    82  // Test this capability.
    83  func Test_processEnvFileLine_readEnvironment(t *testing.T) {
    84  	const realKey = "k8s_test_env_file_key"
    85  	const realValue = `my_value`
    86  
    87  	t.Setenv(realKey, `my_value`)
    88  
    89  	key, value, err := processEnvFileLine([]byte(realKey), `filename`, 3)
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	if key != realKey {
    94  		t.Errorf(`expected key %q, received %q`, realKey, key)
    95  	}
    96  	if value != realValue {
    97  		t.Errorf(`expected value %q, received %q`, realValue, value)
    98  	}
    99  }
   100  

View as plain text