...

Source file src/k8s.io/kubectl/pkg/cmd/config/navigation_step_parser_test.go

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

     1  /*
     2  Copyright 2014 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 config
    18  
    19  import (
    20  	"reflect"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
    26  )
    27  
    28  type stepParserTest struct {
    29  	path                    string
    30  	expectedNavigationSteps navigationSteps
    31  	expectedError           string
    32  }
    33  
    34  func TestParseWithDots(t *testing.T) {
    35  	test := stepParserTest{
    36  		path: "clusters.my.dot.delimited.name.server",
    37  		expectedNavigationSteps: navigationSteps{
    38  			steps: []navigationStep{
    39  				{"clusters", reflect.TypeOf(make(map[string]*clientcmdapi.Cluster))},
    40  				{"my.dot.delimited.name", reflect.TypeOf(clientcmdapi.Cluster{})},
    41  				{"server", reflect.TypeOf("")},
    42  			},
    43  		},
    44  	}
    45  
    46  	test.run(t)
    47  }
    48  
    49  func TestParseWithDotsEndingWithName(t *testing.T) {
    50  	test := stepParserTest{
    51  		path: "contexts.10.12.12.12",
    52  		expectedNavigationSteps: navigationSteps{
    53  			steps: []navigationStep{
    54  				{"contexts", reflect.TypeOf(make(map[string]*clientcmdapi.Context))},
    55  				{"10.12.12.12", reflect.TypeOf(clientcmdapi.Context{})},
    56  			},
    57  		},
    58  	}
    59  
    60  	test.run(t)
    61  }
    62  
    63  func TestParseWithBadValue(t *testing.T) {
    64  	test := stepParserTest{
    65  		path: "user.bad",
    66  		expectedNavigationSteps: navigationSteps{
    67  			steps: []navigationStep{},
    68  		},
    69  		expectedError: "unable to parse user.bad after [] at api.Config",
    70  	}
    71  
    72  	test.run(t)
    73  }
    74  
    75  func TestParseWithNoMatchingValue(t *testing.T) {
    76  	test := stepParserTest{
    77  		path: "users.jheiss.exec.command",
    78  		expectedNavigationSteps: navigationSteps{
    79  			steps: []navigationStep{},
    80  		},
    81  		expectedError: "unable to parse one or more field values of users.jheiss.exec",
    82  	}
    83  
    84  	test.run(t)
    85  }
    86  
    87  func (test stepParserTest) run(t *testing.T) {
    88  	actualSteps, err := newNavigationSteps(test.path)
    89  	if len(test.expectedError) != 0 {
    90  		if err == nil {
    91  			t.Errorf("Did not get %v", test.expectedError)
    92  		} else {
    93  			if !strings.Contains(err.Error(), test.expectedError) {
    94  				t.Errorf("Expected %v, but got %v", test.expectedError, err)
    95  			}
    96  		}
    97  		return
    98  	}
    99  
   100  	if err != nil {
   101  		t.Errorf("Unexpected error: %v", err)
   102  	}
   103  
   104  	if !reflect.DeepEqual(test.expectedNavigationSteps, *actualSteps) {
   105  		t.Errorf("diff: %v", cmp.Diff(test.expectedNavigationSteps, *actualSteps))
   106  		t.Errorf("expected: %#v\n actual:   %#v", test.expectedNavigationSteps, *actualSteps)
   107  	}
   108  }
   109  

View as plain text