...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/unstructured_test.go

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

     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 test
    16  
    17  import "testing"
    18  
    19  const exampleResource = `apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
    20  kind: Project
    21  metadata:
    22    name: test-project
    23  spec:
    24    billingAccountRef:
    25      external: 1234567890
    26    name: test-project
    27  `
    28  
    29  const exampleResourceWithLabels = `apiVersion: resourcemanager.cnrm.cloud.google.com/v1beta1
    30  kind: Project
    31  metadata:
    32    labels:
    33      foo: "bar"
    34    name: test-project
    35  spec:
    36    billingAccountRef:
    37      external: 1234567890
    38    name: test-project
    39  `
    40  
    41  // ToUnstruct should add the cnrm-test label,
    42  func TestToUnstructAddsTestLabel(t *testing.T) {
    43  	for _, example := range []string{exampleResourceWithLabels, exampleResource} {
    44  		b := []byte(example)
    45  		got := ToUnstruct(t, b)
    46  		labels := got.GetLabels()
    47  		if labels["cnrm-test"] != "true" {
    48  			t.Errorf("ToUnstruct(%v) label cnrm-test not set:  got '%v', want '%v'", example, labels["cnrm-test"], "true")
    49  		}
    50  	}
    51  }
    52  
    53  // ToUnstructWithNamespace should add the cnrm-test label,
    54  // and appropriately set the namespace.
    55  func TestToUnstructWithNamespaceAddsNamespace(t *testing.T) {
    56  	for _, example := range []string{exampleResourceWithLabels, exampleResource} {
    57  		b := []byte(example)
    58  		namespace := "foo"
    59  		got := ToUnstructWithNamespace(t, b, namespace)
    60  		if got.GetNamespace() != namespace {
    61  			t.Errorf("ToUnstructWithNamespace(%v) namespace not set:  got '%v', want '%v'", example, got.GetNamespace(), namespace)
    62  		}
    63  		labels := got.GetLabels()
    64  		if labels["cnrm-test"] != "true" {
    65  			t.Errorf("ToUnstructWithNamespace(%v) label cnrm-test not set:  got '%v', want '%v'", example, labels["cnrm-test"], "true")
    66  		}
    67  	}
    68  }
    69  

View as plain text