istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/util/label_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 util
    16  
    17  import (
    18  	"testing"
    19  
    20  	"k8s.io/apimachinery/pkg/api/meta"
    21  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    22  )
    23  
    24  func TestSetLabel(t *testing.T) {
    25  	tests := []struct {
    26  		desc      string
    27  		wantLabel string
    28  		wantValue string
    29  		wantErr   error
    30  	}{
    31  		{
    32  			desc:      "AddMapLabelMapValue",
    33  			wantLabel: "foo",
    34  			wantValue: "bar",
    35  			wantErr:   nil,
    36  		},
    37  	}
    38  	for _, tt := range tests {
    39  		t.Run(tt.desc, func(t *testing.T) {
    40  			resource := &unstructured.Unstructured{Object: make(map[string]any)}
    41  			gotErr := SetLabel(resource, tt.wantLabel, tt.wantValue)
    42  			resourceAccessor, _ := meta.Accessor(resource)
    43  			labels := resourceAccessor.GetLabels()
    44  			if gotVal, ok := labels[tt.wantLabel]; !ok || gotVal != tt.wantValue || gotErr != tt.wantErr {
    45  				t.Errorf("%s: ok: %v, got value: %v, want value: %v, got error: %v, want error: %v", tt.desc, ok, gotVal, tt.wantValue, gotErr, tt.wantErr)
    46  			}
    47  		})
    48  	}
    49  }