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

     1  /*
     2  Copyright 2025 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 parser
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  type testConfig struct {
    27  	str    string
    28  	toggle bool
    29  	i16    int16
    30  	i32    int32
    31  	i64    int64
    32  	u16    uint16
    33  	u32    uint32
    34  	u64    uint64
    35  	i      int
    36  	ui     uint
    37  	f32    float32
    38  	f64    float64
    39  	dur    time.Duration
    40  }
    41  
    42  func TestParse(t *testing.T) {
    43  	tests := []struct {
    44  		name      string
    45  		conf      testConfig
    46  		data      map[string]string
    47  		want      testConfig
    48  		expectErr bool
    49  	}{{
    50  		name: "all good",
    51  		data: map[string]string{
    52  			"test-string":   "foo.bar",
    53  			"test-bool":     "true",
    54  			"test-int16":    "6",
    55  			"test-int32":    "1",
    56  			"test-int64":    "2",
    57  			"test-uint16":   "5",
    58  			"test-uint32":   "3",
    59  			"test-uint64":   "6",
    60  			"test-int":      "4",
    61  			"test-uint":     "5",
    62  			"test-float32":  "1.2",
    63  			"test-float64":  "1.0",
    64  			"test-duration": "1m",
    65  			"test-set":      "a,b,c, d",
    66  			"test-quantity": "500m",
    67  
    68  			"test-namespaced-name":          "some-namespace/some-name",
    69  			"test-optional-namespaced-name": "some-other-namespace/some-other-name",
    70  
    71  			"test-dict.k":  "v",
    72  			"test-dict.k1": "v1",
    73  		},
    74  		want: testConfig{
    75  			str:    "foo.bar",
    76  			toggle: true,
    77  			i16:    6,
    78  			i32:    1,
    79  			i64:    2,
    80  			u16:    5,
    81  			u32:    3,
    82  			u64:    6,
    83  			f32:    1.2,
    84  			f64:    1.0,
    85  			i:      4,
    86  			ui:     5,
    87  			dur:    time.Minute,
    88  		},
    89  	}, {
    90  		name: "respect defaults",
    91  		conf: testConfig{
    92  			str:    "foo.bar",
    93  			toggle: true,
    94  			i32:    1,
    95  			i64:    2,
    96  			f64:    1.0,
    97  			i:      4,
    98  			dur:    time.Minute,
    99  		},
   100  		want: testConfig{
   101  			str:    "foo.bar",
   102  			toggle: true,
   103  			i32:    1,
   104  			i64:    2,
   105  			f64:    1.0,
   106  			i:      4,
   107  			dur:    time.Minute,
   108  		},
   109  	}, {
   110  		name: "junk bool fails",
   111  		data: map[string]string{
   112  			"test-bool": "foo",
   113  		},
   114  		expectErr: true,
   115  	}, {
   116  		name: "int32 error",
   117  		data: map[string]string{
   118  			"test-int32": "foo",
   119  		},
   120  		expectErr: true,
   121  	}, {
   122  		name: "int64 error",
   123  		data: map[string]string{
   124  			"test-int64": "foo",
   125  		},
   126  		expectErr: true,
   127  	}, {
   128  		name: "uint32 error",
   129  		data: map[string]string{
   130  			"test-uint32": "foo",
   131  		},
   132  		expectErr: true,
   133  	}, {
   134  		name: "int error",
   135  		data: map[string]string{
   136  			"test-int": "foo",
   137  		},
   138  		expectErr: true,
   139  	}, {
   140  		name: "float64 error",
   141  		data: map[string]string{
   142  			"test-float64": "foo",
   143  		},
   144  		expectErr: true,
   145  	}, {
   146  		name: "duration error",
   147  		data: map[string]string{
   148  			"test-duration": "foo",
   149  		},
   150  		expectErr: true,
   151  	}}
   152  
   153  	for _, test := range tests {
   154  		t.Run(test.name, func(t *testing.T) {
   155  			if err := Parse(test.data,
   156  				As("test-string", &test.conf.str),
   157  				As("test-bool", &test.conf.toggle),
   158  				As("test-int16", &test.conf.i16),
   159  				As("test-int32", &test.conf.i32),
   160  				As("test-int64", &test.conf.i64),
   161  				As("test-uint16", &test.conf.u16),
   162  				As("test-uint32", &test.conf.u32),
   163  				As("test-uint64", &test.conf.u64),
   164  				As("test-int", &test.conf.i),
   165  				As("test-uint", &test.conf.ui),
   166  				As("test-float32", &test.conf.f32),
   167  				As("test-float64", &test.conf.f64),
   168  				As("test-duration", &test.conf.dur),
   169  			); (err == nil) == test.expectErr {
   170  				t.Fatal("Failed to parse data:", err)
   171  			}
   172  
   173  			if diff := cmp.Diff(test.want, test.conf, cmp.AllowUnexported(testConfig{})); diff != "" {
   174  				t.Fatal("(-want, +got)", diff)
   175  			}
   176  		})
   177  	}
   178  }