github.com/verrazzano/verrazzano@v1.7.0/pkg/k8s/webhook/webhook_test.go (about)

     1  // Copyright (c) 2022, 2023, 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 webhook
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  
    10  	"k8s.io/apimachinery/pkg/api/errors"
    11  	"k8s.io/apimachinery/pkg/types"
    12  
    13  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    17  	vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1"
    18  	adminv1 "k8s.io/api/admissionregistration/v1"
    19  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    20  	k8scheme "k8s.io/client-go/kubernetes/scheme"
    21  )
    22  
    23  // TestDeleteValidating tests the deletion of a ValidatingWebhookConfiguration
    24  // GIVEN a ValidatingWebhookConfiguration
    25  // WHEN delete is called
    26  // THEN the ValidatingWebhookConfiguration should get deleted
    27  func TestDeleteValidating(t *testing.T) {
    28  	asserts := assert.New(t)
    29  
    30  	name := "test"
    31  	_ = vzapi.AddToScheme(k8scheme.Scheme)
    32  	c := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(
    33  		&adminv1.ValidatingWebhookConfiguration{
    34  			ObjectMeta: metav1.ObjectMeta{Name: name},
    35  		}).Build()
    36  
    37  	// Validate webhook exists
    38  	wh := adminv1.ValidatingWebhookConfiguration{
    39  		ObjectMeta: metav1.ObjectMeta{Name: name},
    40  	}
    41  	err := c.Get(context.TODO(), types.NamespacedName{Name: name}, &wh)
    42  	asserts.NoError(err)
    43  
    44  	// Delete the webhook
    45  	err = DeleteValidatingWebhookConfiguration(vzlog.DefaultLogger(), c, name)
    46  
    47  	// Validate that webhook is deleted
    48  	asserts.NoError(err)
    49  	err = c.Get(context.TODO(), types.NamespacedName{Name: name}, &wh)
    50  	asserts.Error(err)
    51  	asserts.True(errors.IsNotFound(err))
    52  }
    53  
    54  // TestDeleteValidatingNotExists tests the deletion of a ValidatingWebhookConfiguration
    55  // GIVEN a ValidatingWebhookConfiguration that doesn't exist
    56  // WHEN delete is called
    57  // THEN the ValidatingWebhookConfiguration should get deleted
    58  func TestDeleteValidatingNotExists(t *testing.T) {
    59  	asserts := assert.New(t)
    60  
    61  	name := "test"
    62  	_ = vzapi.AddToScheme(k8scheme.Scheme)
    63  	c := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).Build()
    64  
    65  	// Validate webhook exists
    66  	wh := adminv1.ValidatingWebhookConfiguration{
    67  		ObjectMeta: metav1.ObjectMeta{Name: name},
    68  	}
    69  	err := c.Get(context.TODO(), types.NamespacedName{Name: name}, &wh)
    70  	asserts.True(errors.IsNotFound(err))
    71  
    72  	// Delete the webhook
    73  	err = DeleteValidatingWebhookConfiguration(vzlog.DefaultLogger(), c, name)
    74  	asserts.NoError(err)
    75  }
    76  
    77  // TestDeleteMutating tests the deletion of a MutatingWebhookConfiguration
    78  // GIVEN a MutatingWebhookConfiguration
    79  // WHEN delete is called
    80  // THEN the MutatingWebhookConfiguration should not return an error
    81  func TestDeleteMutating(t *testing.T) {
    82  	asserts := assert.New(t)
    83  
    84  	name := "test"
    85  	_ = vzapi.AddToScheme(k8scheme.Scheme)
    86  	c := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(
    87  		&adminv1.MutatingWebhookConfiguration{
    88  			ObjectMeta: metav1.ObjectMeta{Name: name},
    89  		}).Build()
    90  
    91  	// Validate webhook exists
    92  	wh := adminv1.MutatingWebhookConfiguration{
    93  		ObjectMeta: metav1.ObjectMeta{Name: name},
    94  	}
    95  	err := c.Get(context.TODO(), types.NamespacedName{Name: name}, &wh)
    96  	asserts.NoError(err)
    97  
    98  	// Delete the webhook
    99  	err = DeleteMutatingWebhookConfiguration(vzlog.DefaultLogger(), c, []string{name})
   100  
   101  	// Validate that webhook is deleted
   102  	asserts.NoError(err)
   103  	err = c.Get(context.TODO(), types.NamespacedName{Name: name}, &wh)
   104  	asserts.Error(err)
   105  	asserts.True(errors.IsNotFound(err))
   106  }
   107  
   108  // TestDeleteMutatingNotExists tests the deletion of a MutatingWebhookConfiguration
   109  // GIVEN a MutatingWebhookConfiguration that doesn't exist
   110  // WHEN delete is called
   111  // THEN the MutatingWebhookConfiguration should not return an error
   112  func TestDeleteMutatingNotExists(t *testing.T) {
   113  	asserts := assert.New(t)
   114  
   115  	name := "test"
   116  	_ = vzapi.AddToScheme(k8scheme.Scheme)
   117  	c := fake.NewClientBuilder().WithScheme(k8scheme.Scheme).Build()
   118  
   119  	// Validate webhook exists
   120  	wh := adminv1.MutatingWebhookConfiguration{
   121  		ObjectMeta: metav1.ObjectMeta{Name: name},
   122  	}
   123  	err := c.Get(context.TODO(), types.NamespacedName{Name: name}, &wh)
   124  	asserts.True(errors.IsNotFound(err))
   125  
   126  	// Delete the webhook
   127  	err = DeleteMutatingWebhookConfiguration(vzlog.DefaultLogger(), c, []string{name})
   128  	asserts.NoError(err)
   129  }