...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/label/label_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/label

     1  // Copyright 2022 Google LLC
     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 label_test
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/label"
    22  )
    23  
    24  func TestNewGcpFromK8sLabelsBasicMap(t *testing.T) {
    25  	labels := map[string]string{
    26  		"key1":        "val1",
    27  		"key2":        "val2",
    28  		"test.io/foo": "bar",
    29  	}
    30  	result := label.NewGcpFromK8sLabels(labels)
    31  	expectedResult := map[string]string{
    32  		"key1":            "val1",
    33  		"key2":            "val2",
    34  		"managed-by-cnrm": "true",
    35  	}
    36  	if !reflect.DeepEqual(result, expectedResult) {
    37  		t.Errorf("results mismatch: got '%v', want '%v'", result, expectedResult)
    38  	}
    39  }
    40  
    41  func TestNilMap(t *testing.T) {
    42  	result := label.NewGcpFromK8sLabels(nil)
    43  	expectedResult := map[string]string{
    44  		"managed-by-cnrm": "true",
    45  	}
    46  	if !reflect.DeepEqual(result, expectedResult) {
    47  		t.Errorf("results mismatch: got '%v', want '%v'", result, expectedResult)
    48  	}
    49  }
    50  
    51  func TestRemoveLabelsWithKRMPrefix(t *testing.T) {
    52  	labels := map[string]string{
    53  		"test.io/foo": "bar",
    54  		"key1":        "val1",
    55  	}
    56  	result := label.RemoveLabelsWithKRMPrefix(labels)
    57  	expectedResult := map[string]string{
    58  		"key1": "val1",
    59  	}
    60  	if !reflect.DeepEqual(result, expectedResult) {
    61  		t.Errorf("results mismatch: got '%v', want '%v'", result, expectedResult)
    62  	}
    63  }
    64  
    65  func TestNewGCPLabelsFromK8sLabelsBasicMap(t *testing.T) {
    66  	labels := map[string]string{
    67  		"key1":        "val1",
    68  		"key2":        "val2",
    69  		"test.io/foo": "bar",
    70  	}
    71  	result := label.NewGCPLabelsFromK8SLabels(labels, label.GetDefaultLabels())
    72  	expectedResult := map[string]interface{}{
    73  		"key1":            "val1",
    74  		"key2":            "val2",
    75  		"managed-by-cnrm": "true",
    76  	}
    77  	if !reflect.DeepEqual(result, expectedResult) {
    78  		t.Errorf("results mismatch: got '%v', want '%v'", result, expectedResult)
    79  	}
    80  	result = label.NewGCPLabelsFromK8SLabels(labels)
    81  	expectedResult = map[string]interface{}{
    82  		"key1": "val1",
    83  		"key2": "val2",
    84  	}
    85  	if !reflect.DeepEqual(result, expectedResult) {
    86  		t.Errorf("results mismatch: got '%v', want '%v'", result, expectedResult)
    87  	}
    88  	result = label.NewGCPLabelsFromK8SLabels(labels, nil)
    89  	expectedResult = map[string]interface{}{
    90  		"key1": "val1",
    91  		"key2": "val2",
    92  	}
    93  	if !reflect.DeepEqual(result, expectedResult) {
    94  		t.Errorf("results mismatch: got '%v', want '%v'", result, expectedResult)
    95  	}
    96  	result = label.NewGCPLabelsFromK8SLabels(labels, map[string]string{"foo": "bar"})
    97  	expectedResult = map[string]interface{}{
    98  		"key1": "val1",
    99  		"key2": "val2",
   100  		"foo":  "bar",
   101  	}
   102  	if !reflect.DeepEqual(result, expectedResult) {
   103  		t.Errorf("results mismatch: got '%v', want '%v'", result, expectedResult)
   104  	}
   105  }
   106  

View as plain text