github.com/cilium/cilium@v1.16.2/pkg/identity/key/global_identity_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package key
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/cilium/cilium/pkg/labels"
    10  	"github.com/cilium/cilium/pkg/labelsfilter"
    11  )
    12  
    13  func TestGetCIDKeyFromLabels(t *testing.T) {
    14  	labelsfilter.ParseLabelPrefixCfg(nil, nil, "")
    15  
    16  	tests := []struct {
    17  		name     string
    18  		labels   map[string]string
    19  		source   string
    20  		expected *GlobalIdentity
    21  	}{
    22  		{
    23  			name:   "Valid Labels",
    24  			labels: map[string]string{"source1:label1": "value1", "source2:label2": "value2", "irrelevant": "foo"},
    25  			source: "source1",
    26  			expected: &GlobalIdentity{LabelArray: []labels.Label{
    27  				{Key: "label1", Value: "value1", Source: "source1"},
    28  				{Key: "label2", Value: "value2", Source: "source1"},
    29  				{Key: "irrelevant", Value: "foo", Source: "source1"},
    30  			}},
    31  		},
    32  		{
    33  			name:     "Empty Labels",
    34  			labels:   map[string]string{},
    35  			source:   "source",
    36  			expected: &GlobalIdentity{LabelArray: []labels.Label{}},
    37  		},
    38  		{
    39  			name:   "Empty source",
    40  			labels: map[string]string{"source1:foo1": "value1", "source2:foo2": "value2", "foo3": "value3"},
    41  			source: "",
    42  			expected: &GlobalIdentity{LabelArray: []labels.Label{
    43  				{Key: "foo1", Value: "value1", Source: "source1"},
    44  				{Key: "foo2", Value: "value2", Source: "source2"},
    45  				{Key: "foo3", Value: "value3", Source: labels.LabelSourceUnspec},
    46  			}},
    47  		},
    48  	}
    49  
    50  	for _, tt := range tests {
    51  		t.Run(tt.name, func(t *testing.T) {
    52  
    53  			result := GetCIDKeyFromLabels(tt.labels, tt.source)
    54  
    55  			if result == nil {
    56  				t.Fatalf("Expected a GlobalIdentity result, but got nil")
    57  			}
    58  
    59  			result.LabelArray.Sort()
    60  			tt.expected.LabelArray.Sort()
    61  
    62  			if !tt.expected.LabelArray.Equals(result.LabelArray) {
    63  				t.Errorf("Unexpected result:\nGot: %v\nExpected: %v", result.LabelArray, tt.expected.LabelArray)
    64  			}
    65  
    66  		})
    67  	}
    68  }