github.com/cilium/cilium@v1.16.2/operator/pkg/gateway-api/gatewayclass_reconcile_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package gateway_api
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  	"k8s.io/apimachinery/pkg/types"
    14  	ctrl "sigs.k8s.io/controller-runtime"
    15  	"sigs.k8s.io/controller-runtime/pkg/client"
    16  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    17  	gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
    18  	gwconformanceconfig "sigs.k8s.io/gateway-api/conformance/utils/config"
    19  	gwconformance "sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
    20  )
    21  
    22  var (
    23  	gwcFinalizer = "batch.gateway.io/finalizer"
    24  	gwcFixture   = []client.Object{
    25  		&gatewayv1.GatewayClass{
    26  			ObjectMeta: metav1.ObjectMeta{
    27  				Name: "dummy-gw-class",
    28  			},
    29  			Spec: gatewayv1.GatewayClassSpec{
    30  				ControllerName: "io.cilium/gateway-controller",
    31  			},
    32  		},
    33  		&gatewayv1.GatewayClass{
    34  			ObjectMeta: metav1.ObjectMeta{
    35  				Name:              "deleting-gw-class",
    36  				DeletionTimestamp: &metav1.Time{Time: time.Now()},
    37  				Finalizers:        []string{gwcFinalizer},
    38  			},
    39  			Spec: gatewayv1.GatewayClassSpec{
    40  				ControllerName: "io.cilium/gateway-controller",
    41  			},
    42  		},
    43  		&gatewayv1.GatewayClass{
    44  			ObjectMeta: metav1.ObjectMeta{
    45  				Name:              "non-matching-gw-class",
    46  				DeletionTimestamp: &metav1.Time{Time: time.Now()},
    47  				Finalizers:        []string{gwcFinalizer},
    48  			},
    49  			Spec: gatewayv1.GatewayClassSpec{
    50  				ControllerName: "not-cilium-controller-name",
    51  			},
    52  		},
    53  	}
    54  )
    55  
    56  func Test_gatewayClassReconciler_Reconcile(t *testing.T) {
    57  	c := fake.NewClientBuilder().
    58  		WithScheme(testScheme()).
    59  		WithObjects(gwcFixture...).
    60  		WithStatusSubresource(&gatewayv1.GatewayClass{}).
    61  		Build()
    62  	r := &gatewayClassReconciler{Client: c}
    63  
    64  	t.Run("no gateway class", func(t *testing.T) {
    65  		result, err := r.Reconcile(context.Background(), ctrl.Request{
    66  			NamespacedName: types.NamespacedName{
    67  				Name: "non-existing-gw-class",
    68  			},
    69  		})
    70  		require.NoError(t, err)
    71  		require.Equal(t, ctrl.Result{}, result)
    72  	})
    73  
    74  	t.Run("gateway class exists but being deleted", func(t *testing.T) {
    75  		result, err := r.Reconcile(context.Background(), ctrl.Request{
    76  			NamespacedName: types.NamespacedName{
    77  				Name: "deleting-gw-class",
    78  			},
    79  		})
    80  
    81  		require.NoError(t, err, "Error reconciling gateway class")
    82  		require.Equal(t, ctrl.Result{}, result, "Result should be empty")
    83  	})
    84  
    85  	t.Run("gateway class exists and active", func(t *testing.T) {
    86  		result, err := r.Reconcile(context.Background(), ctrl.Request{
    87  			NamespacedName: client.ObjectKey{
    88  				Name: "dummy-gw-class",
    89  			},
    90  		})
    91  
    92  		require.NoError(t, err, "Error reconciling gateway class")
    93  		require.Equal(t, ctrl.Result{}, result, "Result should be empty")
    94  		gwconformance.GWCMustHaveAcceptedConditionTrue(t, c, gwconformanceconfig.DefaultTimeoutConfig(), "dummy-gw-class")
    95  	})
    96  
    97  	t.Run("non-matching controller name", func(t *testing.T) {
    98  		result, err := r.Reconcile(context.Background(), ctrl.Request{
    99  			NamespacedName: types.NamespacedName{
   100  				Name: "non-matching-gw-class",
   101  			},
   102  		})
   103  
   104  		require.NoError(t, err, "Error reconciling gateway class")
   105  		require.Equal(t, ctrl.Result{}, result, "Result should be empty")
   106  
   107  		gwc := &gatewayv1.GatewayClass{}
   108  		err = c.Get(context.Background(), types.NamespacedName{Name: "non-matching-gw-class"}, gwc)
   109  
   110  		require.NoError(t, err, "Error getting gateway class")
   111  		require.Len(t, gwc.Status.Conditions, 0, "Gateway class should not have any conditions")
   112  	})
   113  }