...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/lifecyclehandler/handler_test.go

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

     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 lifecyclehandler
    16  
    17  import (
    18  	"testing"
    19  
    20  	corekccv1alpha1 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/apis/core/v1alpha1"
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
    22  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
    23  	testcontroller "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/controller"
    24  	testmain "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/main"
    25  	testvariable "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/resourcefixture/variable"
    26  
    27  	corev1 "k8s.io/api/core/v1"
    28  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    29  	"k8s.io/apimachinery/pkg/runtime/schema"
    30  	"sigs.k8s.io/controller-runtime/pkg/manager"
    31  )
    32  
    33  var (
    34  	mgr manager.Manager
    35  )
    36  
    37  func TestIsOrphaned(t *testing.T) {
    38  	tests := []struct {
    39  		name             string
    40  		resource         *k8s.Resource
    41  		parentObjectName string
    42  		parentConfigs    []corekccv1alpha1.TypeConfig
    43  		isOrphaned       bool
    44  	}{
    45  		{
    46  			name: "the parent k8s resource is gone",
    47  			resource: &k8s.Resource{
    48  				Spec: map[string]interface{}{
    49  					"barRef": map[string]interface{}{
    50  						"name": "bar-parent",
    51  					},
    52  				},
    53  			},
    54  			parentConfigs: []corekccv1alpha1.TypeConfig{
    55  				{
    56  					Key: "barRef",
    57  					GVK: schema.GroupVersionKind{
    58  						Group:   "test1.cnrm.cloud.google.com",
    59  						Version: "v1alpha1",
    60  						Kind:    "Test1Bar",
    61  					},
    62  					Parent: true,
    63  				},
    64  			},
    65  			isOrphaned: true,
    66  		},
    67  		{
    68  			name: "the parent k8s resource is around",
    69  			resource: &k8s.Resource{
    70  				Spec: map[string]interface{}{
    71  					"barRef": map[string]interface{}{
    72  						"name": "bar-parent",
    73  					},
    74  				},
    75  			},
    76  			parentConfigs: []corekccv1alpha1.TypeConfig{
    77  				{
    78  					Key: "barRef",
    79  					GVK: schema.GroupVersionKind{
    80  						Group:   "test1.cnrm.cloud.google.com",
    81  						Version: "v1alpha1",
    82  						Kind:    "Test1Bar",
    83  					},
    84  					Parent: true,
    85  				},
    86  			},
    87  			parentObjectName: "bar-parent",
    88  			isOrphaned:       false,
    89  		},
    90  		{
    91  			name: "external parent resource",
    92  			resource: &k8s.Resource{
    93  				Spec: map[string]interface{}{
    94  					"barRef": map[string]interface{}{
    95  						"external": "bar-parent",
    96  					},
    97  				},
    98  			},
    99  			parentConfigs: []corekccv1alpha1.TypeConfig{
   100  				{
   101  					Key: "barRef",
   102  					GVK: schema.GroupVersionKind{
   103  						Group:   "test1.cnrm.cloud.google.com",
   104  						Version: "v1alpha1",
   105  						Kind:    "Test1Bar",
   106  					},
   107  					Parent: true,
   108  				},
   109  			},
   110  			isOrphaned: false,
   111  		},
   112  		{
   113  			name: "some resource can have multiple parent resource types, but only one parent field is specified",
   114  			resource: &k8s.Resource{
   115  				Spec: map[string]interface{}{
   116  					"barRef": map[string]interface{}{
   117  						"name": "bar-parent",
   118  					},
   119  				},
   120  			},
   121  			parentConfigs: []corekccv1alpha1.TypeConfig{
   122  				{
   123  					Key: "fooRef",
   124  					GVK: schema.GroupVersionKind{
   125  						Group:   "test1.cnrm.cloud.google.com",
   126  						Version: "v1alpha1",
   127  						Kind:    "Test1Foo",
   128  					},
   129  					Parent: true,
   130  				},
   131  				{
   132  					Key: "barRef",
   133  					GVK: schema.GroupVersionKind{
   134  						Group:   "test1.cnrm.cloud.google.com",
   135  						Version: "v1alpha1",
   136  						Kind:    "Test1Bar",
   137  					},
   138  					Parent: true,
   139  				},
   140  			},
   141  			parentObjectName: "bar-parent",
   142  			isOrphaned:       false,
   143  		},
   144  		{
   145  			name: "resources have no parent",
   146  			resource: &k8s.Resource{
   147  				Spec: map[string]interface{}{},
   148  			},
   149  			parentConfigs: []corekccv1alpha1.TypeConfig{},
   150  			isOrphaned:    false,
   151  		},
   152  	}
   153  	c := mgr.GetClient()
   154  	for _, tc := range tests {
   155  		tc := tc
   156  		t.Run(tc.name, func(t *testing.T) {
   157  			t.Parallel()
   158  			testId := testvariable.NewUniqueId()
   159  			tc.resource.SetNamespace(testId)
   160  			testcontroller.EnsureNamespaceExists(c, testId)
   161  			if tc.parentObjectName != "" {
   162  				references := []*unstructured.Unstructured{
   163  					test.NewBarUnstructured(tc.parentObjectName, testId, corev1.ConditionTrue),
   164  				}
   165  				test.EnsureObjectsExist(t, references, c)
   166  			}
   167  			res, _, err := IsOrphaned(tc.resource, tc.parentConfigs, c)
   168  			if err != nil {
   169  				t.Fatalf("unexpected error: %v", err)
   170  			}
   171  			if res != tc.isOrphaned {
   172  				t.Fatalf("got resource isOrphaned as %v, want %v", res, tc.isOrphaned)
   173  			}
   174  		})
   175  	}
   176  }
   177  
   178  func TestMain(m *testing.M) {
   179  	testmain.TestMainForUnitTests(m, &mgr)
   180  }
   181  

View as plain text