...

Source file src/sigs.k8s.io/kustomize/kyaml/utils/pathsplitter_test.go

Documentation: sigs.k8s.io/kustomize/kyaml/utils

     1  // Copyright 2021 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package utils_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	. "sigs.k8s.io/kustomize/kyaml/utils"
    11  )
    12  
    13  func TestPathSplitter(t *testing.T) {
    14  	for _, tc := range []struct {
    15  		exp  []string
    16  		path string
    17  	}{
    18  		{
    19  			path: "",
    20  			exp:  []string{""},
    21  		},
    22  		{
    23  			path: "s",
    24  			exp:  []string{"s"},
    25  		},
    26  		{
    27  			path: "a/b/c",
    28  			exp:  []string{"a", "b", "c"},
    29  		},
    30  		{
    31  			path: "/a/b/c",
    32  			exp:  []string{"a", "b", "c"},
    33  		},
    34  		{
    35  			path: `a/b[]/c`,
    36  			exp:  []string{"a", "b[]", "c"},
    37  		},
    38  		{
    39  			path: `a/b\/c/d\/e/f`,
    40  			exp:  []string{"a", "b/c", "d/e", "f"},
    41  		},
    42  		{
    43  			// The actual reason for this.
    44  			path: `metadata/annotations/nginx.ingress.kubernetes.io\/auth-secret`,
    45  			exp: []string{
    46  				"metadata",
    47  				"annotations",
    48  				"nginx.ingress.kubernetes.io/auth-secret"},
    49  		},
    50  	} {
    51  		assert.Equal(t, tc.exp, PathSplitter(tc.path, "/"))
    52  	}
    53  }
    54  
    55  func TestSmarterPathSplitter(t *testing.T) {
    56  	testCases := map[string]struct {
    57  		input    string
    58  		expected []string
    59  	}{
    60  		"simple": {
    61  			input:    "spec.replicas",
    62  			expected: []string{"spec", "replicas"},
    63  		},
    64  		"sequence": {
    65  			input:    "spec.data.[name=first].key",
    66  			expected: []string{"spec", "data", "[name=first]", "key"},
    67  		},
    68  		"key, value with . prefix": {
    69  			input:    "spec.data.[.name=.first].key",
    70  			expected: []string{"spec", "data", "[.name=.first]", "key"},
    71  		},
    72  		"key, value with . suffix": {
    73  			input:    "spec.data.[name.=first.].key",
    74  			expected: []string{"spec", "data", "[name.=first.]", "key"},
    75  		},
    76  		"multiple '.' in value": {
    77  			input:    "spec.data.[name=f.i.r.s.t.].key",
    78  			expected: []string{"spec", "data", "[name=f.i.r.s.t.]", "key"},
    79  		},
    80  		"with escaped delimiter": {
    81  			input:    `spec\.replicas`,
    82  			expected: []string{`spec.replicas`},
    83  		},
    84  		"unmatched bracket": {
    85  			input:    "spec.data.[name=f.i.[r.s.t..key",
    86  			expected: []string{"spec", "data", "[name=f.i.[r.s.t..key"},
    87  		},
    88  		"mapping value with .": {
    89  			input:    "metadata.annotations.[a.b.c/d.e.f-g.]",
    90  			expected: []string{"metadata", "annotations", "a.b.c/d.e.f-g."},
    91  		},
    92  	}
    93  	for tn, tc := range testCases {
    94  		t.Run(tn, func(t *testing.T) {
    95  			assert.Equal(t, tc.expected, SmarterPathSplitter(tc.input, "."))
    96  		})
    97  	}
    98  }
    99  

View as plain text