github.com/verrazzano/verrazzano@v1.7.0/pkg/k8s/resource/resource_test.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package resource
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    12  	vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1"
    13  	adminv1 "k8s.io/api/admissionregistration/v1"
    14  	corev1 "k8s.io/api/core/v1"
    15  	"k8s.io/apimachinery/pkg/api/errors"
    16  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    17  	"k8s.io/apimachinery/pkg/types"
    18  	k8scheme "k8s.io/client-go/kubernetes/scheme"
    19  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    20  )
    21  
    22  // TestDeleteClusterResource tests the deletion of a cluster resource
    23  // GIVEN a Kubernetes resource
    24  // WHEN delete is called
    25  // THEN the resource should get deleted
    26  func TestDeleteClusterResource(t *testing.T) {
    27  	asserts := assert.New(t)
    28  
    29  	name := "test"
    30  	_ = vzapi.AddToScheme(k8scheme.Scheme)
    31  	c := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(
    32  		&adminv1.ValidatingWebhookConfiguration{
    33  			ObjectMeta: metav1.ObjectMeta{Name: name},
    34  		}).Build()
    35  
    36  	// Validate resource exists
    37  	wh := adminv1.ValidatingWebhookConfiguration{ObjectMeta: metav1.ObjectMeta{Name: name}}
    38  	err := c.Get(context.TODO(), types.NamespacedName{Name: name}, &wh)
    39  	asserts.NoError(err)
    40  
    41  	// Delete the resource
    42  	err = Resource{
    43  		Name:   name,
    44  		Client: c,
    45  		Object: &adminv1.ValidatingWebhookConfiguration{},
    46  		Log:    vzlog.DefaultLogger(),
    47  	}.Delete()
    48  
    49  	// Validate that resource is deleted
    50  	asserts.NoError(err)
    51  	err = c.Get(context.TODO(), types.NamespacedName{Name: name}, &wh)
    52  	asserts.Error(err)
    53  	asserts.True(errors.IsNotFound(err))
    54  }
    55  
    56  // TestDeleteClusterResourceNotExists tests the deletion of a cluster resource
    57  // GIVEN a resource that doesn't exist
    58  // WHEN delete is called
    59  // THEN the delete function should not return an error
    60  func TestDeleteClusterResourceNotExists(t *testing.T) {
    61  	asserts := assert.New(t)
    62  
    63  	name := "test"
    64  	_ = vzapi.AddToScheme(k8scheme.Scheme)
    65  	c := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).Build()
    66  
    67  	// Validate resource does not exist
    68  	wh := adminv1.ValidatingWebhookConfiguration{ObjectMeta: metav1.ObjectMeta{Name: name}}
    69  	err := c.Get(context.TODO(), types.NamespacedName{Name: name}, &wh)
    70  	asserts.True(errors.IsNotFound(err))
    71  
    72  	// Delete the resource
    73  	err = Resource{
    74  		Name:   name,
    75  		Client: c,
    76  		Object: &adminv1.ValidatingWebhookConfiguration{},
    77  		Log:    vzlog.DefaultLogger(),
    78  	}.Delete()
    79  	asserts.NoError(err)
    80  }
    81  
    82  // TestDelete tests the deletion of a resource
    83  // GIVEN a Kubernetes resource
    84  // WHEN delete is called
    85  // THEN the resource should get deleted
    86  func TestDelete(t *testing.T) {
    87  	asserts := assert.New(t)
    88  
    89  	name := "test"
    90  	namespace := "testns"
    91  
    92  	_ = vzapi.AddToScheme(k8scheme.Scheme)
    93  	c := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(
    94  		&corev1.Pod{
    95  			ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name},
    96  		}).Build()
    97  
    98  	// Validate resource exists
    99  	pod := corev1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name}}
   100  	err := c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, &pod)
   101  	asserts.NoError(err)
   102  
   103  	// Delete the resource
   104  	err = Resource{
   105  		Namespace: namespace,
   106  		Name:      name,
   107  		Client:    c,
   108  		Object:    &corev1.Pod{},
   109  		Log:       vzlog.DefaultLogger(),
   110  	}.Delete()
   111  
   112  	// Validate that resource is deleted
   113  	asserts.NoError(err)
   114  	err = c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, &pod)
   115  	asserts.Error(err)
   116  	asserts.True(errors.IsNotFound(err))
   117  }
   118  
   119  // TestDeleteNotExists tests the deletion of a resource
   120  // GIVEN a resource that doesn't exist
   121  // WHEN delete is called
   122  // THEN the delete function should not return an error
   123  func TestDeleteNotExists(t *testing.T) {
   124  	asserts := assert.New(t)
   125  
   126  	name := "test"
   127  	namespace := "testns"
   128  
   129  	_ = vzapi.AddToScheme(k8scheme.Scheme)
   130  	c := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).Build()
   131  
   132  	// Validate resource does not exist
   133  	pod := corev1.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name}}
   134  	err := c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, &pod)
   135  	asserts.True(errors.IsNotFound(err))
   136  
   137  	// Delete the resource
   138  	err = Resource{
   139  		Namespace: namespace,
   140  		Name:      name,
   141  		Client:    c,
   142  		Object:    &corev1.Pod{},
   143  		Log:       vzlog.DefaultLogger(),
   144  	}.Delete()
   145  	asserts.NoError(err)
   146  }
   147  
   148  // TestRemoveFinalizers tests the removing a finalizers from a resource
   149  // GIVEN a Kubernetes resource
   150  // WHEN RemoveFinalizers is called
   151  // THEN the resource will have finalizers removed
   152  func TestRemoveFinalizers(t *testing.T) {
   153  	asserts := assert.New(t)
   154  
   155  	name := "test"
   156  	_ = vzapi.AddToScheme(k8scheme.Scheme)
   157  	c := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(
   158  		&corev1.Namespace{
   159  			ObjectMeta: metav1.ObjectMeta{
   160  				Name: name,
   161  				Finalizers: []string{
   162  					"fake-finalizer",
   163  				},
   164  			},
   165  		}).Build()
   166  
   167  	// Verify the resource exists with the finalizer
   168  	ns := corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: name}}
   169  	err := c.Get(context.TODO(), types.NamespacedName{Name: name}, &ns)
   170  	asserts.NoError(err)
   171  	asserts.Equal(1, len(ns.Finalizers))
   172  
   173  	// Remove the finalizer from the resource
   174  	err = Resource{
   175  		Name:   name,
   176  		Client: c,
   177  		Object: &corev1.Namespace{},
   178  		Log:    vzlog.DefaultLogger(),
   179  	}.RemoveFinalizers()
   180  
   181  	// Validate that resource is updated with no finalizer
   182  	asserts.NoError(err)
   183  	err = c.Get(context.TODO(), types.NamespacedName{Name: name}, &ns)
   184  	asserts.NoError(err)
   185  	asserts.Equal(0, len(ns.Finalizers))
   186  }
   187  
   188  // TestRemoveFinalizersNotExists tests the removal of finalizer from a resource
   189  // GIVEN a resource that doesn't exist
   190  // WHEN RemoveFinalizers is called
   191  // THEN the RemoveFinalizers function should not return an error
   192  func TestRemoveFinalizersNotExists(t *testing.T) {
   193  	asserts := assert.New(t)
   194  
   195  	name := "test"
   196  
   197  	_ = vzapi.AddToScheme(k8scheme.Scheme)
   198  	c := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).Build()
   199  
   200  	// Validate resource does not exist
   201  	ns := corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: name}}
   202  	err := c.Get(context.TODO(), types.NamespacedName{Name: name}, &ns)
   203  	asserts.True(errors.IsNotFound(err))
   204  
   205  	// Remove the finalizer from the resource
   206  	err = Resource{
   207  		Name:   name,
   208  		Client: c,
   209  		Object: &corev1.Pod{},
   210  		Log:    vzlog.DefaultLogger(),
   211  	}.RemoveFinalizers()
   212  	asserts.NoError(err)
   213  }
   214  
   215  // TestRemoveFinalizersAndDelete tests the removing a finalizers from a resource and deleting the resource
   216  // GIVEN a Kubernetes resource
   217  // WHEN RemoveFinalizersAndDelete is called
   218  // THEN the resource will have finalizers removed and be deleted
   219  func TestRemoveFinalizersAndDelete(t *testing.T) {
   220  	asserts := assert.New(t)
   221  
   222  	name := "test"
   223  	_ = vzapi.AddToScheme(k8scheme.Scheme)
   224  	c := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(
   225  		&corev1.Namespace{
   226  			ObjectMeta: metav1.ObjectMeta{
   227  				Name: name,
   228  				Finalizers: []string{
   229  					"fake-finalizer",
   230  				},
   231  			},
   232  		}).Build()
   233  
   234  	// Verify the resource exists with the finalizer
   235  	ns := corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: name}}
   236  	err := c.Get(context.TODO(), types.NamespacedName{Name: name}, &ns)
   237  	asserts.NoError(err)
   238  	asserts.Equal(1, len(ns.Finalizers))
   239  
   240  	// Remove the finalizer from the resource and delete the resource
   241  	err = Resource{
   242  		Name:   name,
   243  		Client: c,
   244  		Object: &corev1.Namespace{},
   245  		Log:    vzlog.DefaultLogger(),
   246  	}.RemoveFinalizersAndDelete()
   247  
   248  	// Validate that resource is deleted
   249  	asserts.NoError(err)
   250  	err = c.Get(context.TODO(), types.NamespacedName{Name: name}, &ns)
   251  	asserts.True(errors.IsNotFound(err))
   252  }