...

Source file src/k8s.io/kubectl/pkg/util/util_test.go

Documentation: k8s.io/kubectl/pkg/util

     1  /*
     2  Copyright 2016 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 "testing"
    20  
    21  func TestParseFileSource(t *testing.T) {
    22  	tests := []struct {
    23  		name     string
    24  		input    string
    25  		key      string
    26  		filepath string
    27  		err      bool
    28  	}{
    29  		{
    30  			name:     "success 1",
    31  			input:    "boo=zoo",
    32  			key:      "boo",
    33  			filepath: "zoo",
    34  			err:      false,
    35  		},
    36  		{
    37  			name:     "success 2",
    38  			input:    "boo=/path/to/zoo",
    39  			key:      "boo",
    40  			filepath: "/path/to/zoo",
    41  			err:      false,
    42  		},
    43  		{
    44  			name:     "success 3",
    45  			input:    "boo-2=/1/2/3/4/5/zab.txt",
    46  			key:      "boo-2",
    47  			filepath: "/1/2/3/4/5/zab.txt",
    48  			err:      false,
    49  		},
    50  		{
    51  			name:     "success 4",
    52  			input:    "boo-=this/seems/weird.txt",
    53  			key:      "boo-",
    54  			filepath: "this/seems/weird.txt",
    55  			err:      false,
    56  		},
    57  		{
    58  			name:     "success 5",
    59  			input:    "-key=some/path",
    60  			key:      "-key",
    61  			filepath: "some/path",
    62  			err:      false,
    63  		},
    64  		{
    65  			name:  "invalid 1",
    66  			input: "key==some/path",
    67  			err:   true,
    68  		},
    69  		{
    70  			name:  "invalid 2",
    71  			input: "=key=some/path",
    72  			err:   true,
    73  		},
    74  		{
    75  			name:  "invalid 3",
    76  			input: "==key=/some/other/path",
    77  			err:   true,
    78  		},
    79  		{
    80  			name:  "invalid 4",
    81  			input: "=key",
    82  			err:   true,
    83  		},
    84  		{
    85  			name:  "invalid 5",
    86  			input: "key=",
    87  			err:   true,
    88  		},
    89  	}
    90  
    91  	for _, tt := range tests {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			key, filepath, err := ParseFileSource(tt.input)
    94  			if err != nil {
    95  				if tt.err {
    96  					return
    97  				}
    98  
    99  				t.Errorf("%v: unexpected error: %v", tt.name, err)
   100  				return
   101  			}
   102  
   103  			if tt.err {
   104  				t.Errorf("%v: unexpected success", tt.name)
   105  				return
   106  			}
   107  
   108  			if e, a := tt.key, key; e != a {
   109  				t.Errorf("%v: expected key %v; got %v", tt.name, e, a)
   110  				return
   111  			}
   112  
   113  			if e, a := tt.filepath, filepath; e != a {
   114  				t.Errorf("%v: expected filepath %v; got %v", tt.name, e, a)
   115  			}
   116  		})
   117  	}
   118  }
   119  
   120  func TestParseLiteralSource(t *testing.T) {
   121  	tests := []struct {
   122  		name  string
   123  		input string
   124  		key   string
   125  		value string
   126  		err   bool
   127  	}{
   128  		{
   129  			name:  "success 1",
   130  			input: "key=value",
   131  			key:   "key",
   132  			value: "value",
   133  			err:   false,
   134  		},
   135  		{
   136  			name:  "success 2",
   137  			input: "key=value/with/slashes",
   138  			key:   "key",
   139  			value: "value/with/slashes",
   140  			err:   false,
   141  		},
   142  		{
   143  			name:  "err 1",
   144  			input: "key==value",
   145  			key:   "key",
   146  			value: "=value",
   147  			err:   false,
   148  		},
   149  		{
   150  			name:  "err 2",
   151  			input: "key=value=",
   152  			key:   "key",
   153  			value: "value=",
   154  			err:   false,
   155  		},
   156  		{
   157  			name:  "err 3",
   158  			input: "key2=value==",
   159  			key:   "key2",
   160  			value: "value==",
   161  			err:   false,
   162  		},
   163  		{
   164  			name:  "err 4",
   165  			input: "==key",
   166  			err:   true,
   167  		},
   168  		{
   169  			name:  "err 5",
   170  			input: "=key=",
   171  			err:   true,
   172  		},
   173  	}
   174  
   175  	for _, tt := range tests {
   176  		t.Run(tt.name, func(t *testing.T) {
   177  			key, value, err := ParseLiteralSource(tt.input)
   178  			if err != nil {
   179  				if tt.err {
   180  					return
   181  				}
   182  
   183  				t.Errorf("%v: unexpected error: %v", tt.name, err)
   184  				return
   185  			}
   186  
   187  			if tt.err {
   188  				t.Errorf("%v: unexpected success", tt.name)
   189  				return
   190  			}
   191  
   192  			if e, a := tt.key, key; e != a {
   193  				t.Errorf("%v: expected key %v; got %v", tt.name, e, a)
   194  				return
   195  			}
   196  
   197  			if e, a := tt.value, value; e != a {
   198  				t.Errorf("%v: expected value %v; got %v", tt.name, e, a)
   199  			}
   200  		})
   201  	}
   202  }
   203  

View as plain text