istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/tpath/struct_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tpath
    16  
    17  import (
    18  	"testing"
    19  
    20  	"sigs.k8s.io/yaml"
    21  
    22  	"istio.io/istio/operator/pkg/util"
    23  )
    24  
    25  func TestGetFromStructPath(t *testing.T) {
    26  	tests := []struct {
    27  		desc      string
    28  		nodeYAML  string
    29  		path      string
    30  		wantYAML  string
    31  		wantFound bool
    32  		wantErr   string
    33  	}{
    34  		{
    35  			desc: "GetStructItem",
    36  			nodeYAML: `
    37  a: va
    38  b: vb
    39  c:
    40    d: vd
    41    e:
    42      f: vf
    43  g:
    44    h:
    45    - i: vi
    46      j: vj
    47      k:
    48        l:
    49          m: vm
    50          n: vn
    51  `,
    52  			path: "c",
    53  			wantYAML: `
    54  d: vd
    55  e:
    56    f: vf
    57  `,
    58  			wantFound: true,
    59  		},
    60  		{
    61  			desc: "GetSliceEntryItem",
    62  			nodeYAML: `
    63  a: va
    64  b: vb
    65  c:
    66    d: vd
    67    e:
    68      f: vf
    69  g:
    70    h:
    71    - i: vi
    72      j: vj
    73      k:
    74        l:
    75          m: vm
    76          n: vm
    77  `,
    78  			path: "g.h.0",
    79  			wantYAML: `
    80  i: vi
    81  j: vj
    82  k:
    83    l:
    84      m: vm
    85      n: vm
    86  `,
    87  			wantFound: true,
    88  		},
    89  		{
    90  			desc: "GetMapEntryItem",
    91  			nodeYAML: `
    92  a: va
    93  b: vb
    94  c:
    95    d: vd
    96    e:
    97      f: vf
    98  g:
    99    h:
   100    - i: vi
   101      j: vj
   102      k:
   103        l:
   104          m: vm
   105          n: vm
   106  `,
   107  			path: "g.h.0.k",
   108  			wantYAML: `
   109  l:
   110    m: vm
   111    n: vm
   112  `,
   113  			wantFound: true,
   114  		},
   115  		{
   116  			desc: "GetPathNotExists",
   117  			nodeYAML: `
   118  a: va
   119  b: vb
   120  c:
   121    d: vd
   122    e:
   123      f: vf
   124  g:
   125    h:
   126    - i: vi
   127      j: vj
   128      k:
   129        l:
   130          m: vm
   131          n: vm
   132  `,
   133  			path:      "c.d.e",
   134  			wantFound: false,
   135  			wantErr:   "getFromStructPath path e, unsupported type string",
   136  		},
   137  	}
   138  
   139  	for _, tt := range tests {
   140  		t.Run(tt.desc, func(t *testing.T) {
   141  			rnode := make(map[string]any)
   142  			if err := yaml.Unmarshal([]byte(tt.nodeYAML), &rnode); err != nil {
   143  				t.Fatal(err)
   144  			}
   145  			GotOut, GotFound, gotErr := GetFromStructPath(rnode, tt.path)
   146  			if GotFound != tt.wantFound {
   147  				t.Fatalf("GetFromStructPath(%s): gotFound:%v, wantFound:%v", tt.desc, GotFound, tt.wantFound)
   148  			}
   149  			if gotErr, wantErr := errToString(gotErr), tt.wantErr; gotErr != wantErr {
   150  				t.Fatalf("GetFromStructPath(%s): gotErr:%s, wantErr:%s", tt.desc, gotErr, wantErr)
   151  			}
   152  			if tt.wantErr != "" || !tt.wantFound {
   153  				return
   154  			}
   155  			gotYAML := util.ToYAML(GotOut)
   156  			diff := util.YAMLDiff(gotYAML, tt.wantYAML)
   157  			if diff != "" {
   158  				t.Errorf("GetFromStructPath(%s): YAML of gotOut:\n%s\n, YAML of wantOut:\n%s\n, diff:\n%s\n", tt.desc, gotYAML, tt.wantYAML, diff)
   159  			}
   160  		})
   161  	}
   162  }