...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/cluster/installation_identifier_test.go

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

     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 cluster_test
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/cluster"
    22  	testcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/controller"
    23  	testmain "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/main"
    24  	corev1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/types"
    27  
    28  	"sigs.k8s.io/controller-runtime/pkg/manager"
    29  )
    30  
    31  const (
    32  	namespaceName = "my-namespace"
    33  	setIDValue    = "my-specified-id"
    34  )
    35  
    36  var (
    37  	mgr manager.Manager
    38  
    39  	namespaceIDConfigMapNN = types.NamespacedName{
    40  		Namespace: "system-namespace-name",
    41  		Name:      "namespace-id",
    42  	}
    43  )
    44  
    45  func TestGetNamespaceId(t *testing.T) {
    46  	testcontroller.EnsureNamespaceExists(mgr.GetClient(), namespaceIDConfigMapNN.Namespace)
    47  	testGetNamespaceID(t, mgr)
    48  	testSetNamespaceID(t, mgr)
    49  	testDeleteNamespaceID(t, mgr)
    50  }
    51  
    52  func testGetNamespaceID(t *testing.T, mgr manager.Manager) {
    53  	id := getNamespaceID(t, mgr)
    54  	if id == setIDValue {
    55  		t.Fatalf("expected a generated value for id, instead got '%v'", id)
    56  	}
    57  }
    58  
    59  func testSetNamespaceID(t *testing.T, mgr manager.Manager) {
    60  	err := cluster.SetNamespaceID(namespaceIDConfigMapNN, mgr.GetClient(), context.TODO(), namespaceName, setIDValue)
    61  	if err != nil {
    62  		t.Fatalf("unexpected error when setting namespace id: %v", err)
    63  	}
    64  	id := getNamespaceID(t, mgr)
    65  	if id != setIDValue {
    66  		t.Fatalf("unexpected id value: got '%v', want '%v'", id, setIDValue)
    67  	}
    68  }
    69  
    70  func getNamespaceID(t *testing.T, mgr manager.Manager) string {
    71  	t.Helper()
    72  	id, err := cluster.GetNamespaceID(namespaceIDConfigMapNN, mgr.GetClient(), context.TODO(), namespaceName)
    73  	if err != nil {
    74  		t.Fatalf("unexpected error when getting namespace id: %v", err)
    75  	}
    76  	if id == "" {
    77  		t.Fatalf("expected valid id value, instead got empty string")
    78  	}
    79  	return id
    80  }
    81  
    82  func testDeleteNamespaceID(t *testing.T, mgr manager.Manager) {
    83  	if err := cluster.DeleteNamespaceID(namespaceIDConfigMapNN, mgr.GetClient(), context.TODO(), namespaceName); err != nil {
    84  		t.Fatalf("unexpected error when deleting namespace id: %v", err)
    85  	}
    86  
    87  	var configMap = corev1.ConfigMap{
    88  		ObjectMeta: metav1.ObjectMeta{
    89  			Name:      namespaceIDConfigMapNN.Name,
    90  			Namespace: namespaceIDConfigMapNN.Namespace,
    91  		},
    92  	}
    93  
    94  	if err := mgr.GetClient().Get(context.TODO(), namespaceIDConfigMapNN, &configMap); err != nil {
    95  		t.Fatalf("error getting configmap '%v': %v", namespaceIDConfigMapNN, err)
    96  	}
    97  
    98  	if val, ok := configMap.Data[namespaceName]; ok {
    99  		t.Fatalf("error checking deleted namespace, upexpected value from configmap '%v': %v", namespaceName, val)
   100  	}
   101  }
   102  
   103  func TestMain(m *testing.M) {
   104  	testmain.TestMainForUnitTests(m, &mgr)
   105  }
   106  

View as plain text