github.com/cilium/cilium@v1.16.2/pkg/k8s/apis/cilium.io/v2/validator/unknown_fields_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package validator
     5  
     6  import (
     7  	"sort"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func Test_getFields(t *testing.T) {
    14  	tests := []struct {
    15  		name      string
    16  		structure map[string]interface{}
    17  		expected  []string
    18  		err       error
    19  	}{
    20  		{
    21  			name:      "nil structure",
    22  			structure: nil,
    23  			expected:  []string{},
    24  			err:       nil,
    25  		},
    26  		{
    27  			name: "empty structure",
    28  			structure: map[string]interface{}{
    29  				"": "",
    30  			},
    31  			expected: []string{""},
    32  			err:      nil,
    33  		},
    34  		{
    35  			name: "simple structure",
    36  			structure: map[string]interface{}{
    37  				"spec": "",
    38  			},
    39  			expected: []string{"spec"},
    40  			err:      nil,
    41  		},
    42  		{
    43  			name: "nested structure",
    44  			structure: map[string]interface{}{
    45  				"spec": map[string]interface{}{
    46  					"more":   "",
    47  					"fields": "",
    48  					"another": map[string]interface{}{
    49  						"field": "",
    50  					},
    51  				},
    52  			},
    53  			expected: []string{"spec.more", "spec.fields", "spec.another.field"},
    54  			err:      nil,
    55  		},
    56  		{
    57  			name: `contains "matchLabels"`,
    58  			structure: map[string]interface{}{
    59  				"spec": map[string]interface{}{
    60  					"endpointSelector": map[string]interface{}{
    61  						"matchLabels": map[string]interface{}{
    62  							"app": "",
    63  						},
    64  					},
    65  				},
    66  			},
    67  			expected: []string{"spec.endpointSelector.matchLabels"},
    68  			err:      nil,
    69  		},
    70  		{
    71  			name: `contains multiple labels inside multiple "matchLabels"`,
    72  			structure: map[string]interface{}{
    73  				"spec": map[string]interface{}{
    74  					"endpointSelector": map[string]interface{}{
    75  						"matchLabels": map[string]interface{}{
    76  							"app":      "",
    77  							"key":      "",
    78  							"operator": "",
    79  						},
    80  					},
    81  				},
    82  			},
    83  			expected: []string{"spec.endpointSelector.matchLabels"},
    84  			err:      nil,
    85  		},
    86  		{
    87  			name: `contains multiple labels inside "matchLabels" based on real policy`,
    88  			structure: map[string]interface{}{
    89  				"specs": []interface{}{
    90  					map[string]interface{}{
    91  						"description": "Policy to test multiple rules in a single file",
    92  						"endpointSelector": map[string]interface{}{
    93  							"matchLabels": map[string]interface{}{
    94  								"app":     "",
    95  								"version": "",
    96  							},
    97  						},
    98  						"ingress": []interface{}{
    99  							map[string]interface{}{
   100  								"fromEndpoints": []interface{}{
   101  									map[string]interface{}{
   102  										"matchLabels": map[string]interface{}{
   103  											"app":     "",
   104  											"track":   "",
   105  											"version": "",
   106  										},
   107  									},
   108  								},
   109  							},
   110  						},
   111  					},
   112  					map[string]interface{}{
   113  						"endpointSelector": map[string]interface{}{
   114  							"matchLabels": map[string]interface{}{
   115  								"app":     "details",
   116  								"track":   "stable",
   117  								"version": "v1",
   118  							},
   119  						},
   120  						"ingress": []interface{}{
   121  							map[string]interface{}{
   122  								"fromEndpoints": []interface{}{
   123  									map[string]interface{}{
   124  										"matchLabels": map[string]interface{}{
   125  											"app":     "productpage",
   126  											"track":   "stable",
   127  											"version": "v1",
   128  										},
   129  									},
   130  								},
   131  							},
   132  						},
   133  					},
   134  				},
   135  			},
   136  			expected: []string{"specs.0.description",
   137  				"specs.0.endpointSelector.matchLabels",
   138  				"specs.1.endpointSelector.matchLabels",
   139  				"specs.0.ingress.0.fromEndpoints.0.matchLabels",
   140  				"specs.1.ingress.0.fromEndpoints.0.matchLabels"},
   141  			err: nil,
   142  		},
   143  		{
   144  			name: `contains "matchLabels" and "matchExpressions" based on real policy`,
   145  			structure: map[string]interface{}{
   146  				"spec": map[string]interface{}{
   147  					"description": "Policy to test matchExpressions key",
   148  					"endpointSelector": map[string]interface{}{
   149  						"matchLabels": map[string]interface{}{
   150  							"id": "app1",
   151  						},
   152  					},
   153  					"ingress": []interface{}{
   154  						map[string]interface{}{
   155  							"fromEndpoints": []interface{}{
   156  								map[string]interface{}{
   157  									"matchExpressions": []interface{}{
   158  										map[string]interface{}{
   159  											"key":      "",
   160  											"operator": "Exists",
   161  										},
   162  									},
   163  								},
   164  							},
   165  						},
   166  					},
   167  				},
   168  			},
   169  			expected: []string{"spec.description", "spec.endpointSelector.matchLabels",
   170  				"spec.ingress.0.fromEndpoints.0.matchExpressions"},
   171  			err: nil,
   172  		},
   173  	}
   174  	for _, tt := range tests {
   175  		t.Log(tt.name)
   176  
   177  		got, err := getFields(tt.structure)
   178  		require.EqualValues(t, err, tt.err)
   179  
   180  		sort.Strings(tt.expected) // Must sort to check slice equality
   181  		sort.Strings(got)
   182  		require.EqualValues(t, got, tt.expected)
   183  	}
   184  }