knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/configmap/parse_test.go (about)

     1  /*
     2  Copyright 2020 The Knative 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 configmap
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  	"k8s.io/apimachinery/pkg/api/resource"
    24  	"k8s.io/apimachinery/pkg/types"
    25  	"k8s.io/apimachinery/pkg/util/sets"
    26  )
    27  
    28  type testConfig struct {
    29  	set sets.Set[string]
    30  	qua *resource.Quantity
    31  
    32  	nsn  types.NamespacedName
    33  	onsn *types.NamespacedName
    34  
    35  	dict map[string]string
    36  }
    37  
    38  func TestParse(t *testing.T) {
    39  	fiveHundredM := resource.MustParse("500m")
    40  	tests := []struct {
    41  		name      string
    42  		conf      testConfig
    43  		data      map[string]string
    44  		want      testConfig
    45  		expectErr bool
    46  	}{{
    47  		name: "all good",
    48  		data: map[string]string{
    49  			"test-set":      "a,b,c, d",
    50  			"test-quantity": "500m",
    51  
    52  			"test-namespaced-name":          "some-namespace/some-name",
    53  			"test-optional-namespaced-name": "some-other-namespace/some-other-name",
    54  
    55  			"test-dict.k":  "v",
    56  			"test-dict.k1": "v1",
    57  		},
    58  		want: testConfig{
    59  			set: sets.New("a", "b", "c", "d"),
    60  			qua: &fiveHundredM,
    61  			nsn: types.NamespacedName{
    62  				Name:      "some-name",
    63  				Namespace: "some-namespace",
    64  			},
    65  			onsn: &types.NamespacedName{
    66  				Name:      "some-other-name",
    67  				Namespace: "some-other-namespace",
    68  			},
    69  			dict: map[string]string{
    70  				"k":  "v",
    71  				"k1": "v1",
    72  			},
    73  		},
    74  	}, {
    75  		name: "respect defaults",
    76  		conf: testConfig{
    77  			qua: &fiveHundredM,
    78  		},
    79  		want: testConfig{
    80  			qua: &fiveHundredM,
    81  		},
    82  	}, {
    83  		name: "quantity error",
    84  		data: map[string]string{
    85  			"test-quantity": "foo",
    86  		},
    87  		expectErr: true,
    88  	}, {
    89  		name: "types.NamespacedName bad dns name error",
    90  		data: map[string]string{
    91  			"test-namespaced-name": "some.bad.name/blah.bad.name",
    92  		},
    93  		expectErr: true,
    94  	}, {
    95  		name: "types.NamespacedName bad segment count error",
    96  		data: map[string]string{
    97  			"test-namespaced-name": "default/resource/whut",
    98  		},
    99  		expectErr: true,
   100  	}, {
   101  		name: "dict without key and dot",
   102  		data: map[string]string{
   103  			"test-dict": "v",
   104  		},
   105  		expectErr: false,
   106  	}, {
   107  		name: "dict without key",
   108  		data: map[string]string{
   109  			"test-dict.": "v",
   110  		},
   111  		expectErr: false,
   112  	}}
   113  
   114  	for _, test := range tests {
   115  		t.Run(test.name, func(t *testing.T) {
   116  			if err := Parse(test.data,
   117  				AsStringSet("test-set", &test.conf.set),
   118  				AsQuantity("test-quantity", &test.conf.qua),
   119  				AsNamespacedName("test-namespaced-name", &test.conf.nsn),
   120  				AsOptionalNamespacedName("test-optional-namespaced-name", &test.conf.onsn),
   121  				CollectMapEntriesWithPrefix("test-dict", &test.conf.dict),
   122  			); (err == nil) == test.expectErr {
   123  				t.Fatal("Failed to parse data:", err)
   124  			}
   125  
   126  			if diff := cmp.Diff(test.want, test.conf, cmp.AllowUnexported(testConfig{})); diff != "" {
   127  				t.Fatal("(-want, +got)", diff)
   128  			}
   129  		})
   130  	}
   131  }