sigs.k8s.io/gateway-api@v1.0.0/conformance/tests/httproute-observed-generation-bump.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tests
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/require"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/types"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  
    29  	v1 "sigs.k8s.io/gateway-api/apis/v1"
    30  	"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
    31  	"sigs.k8s.io/gateway-api/conformance/utils/suite"
    32  )
    33  
    34  func init() {
    35  	ConformanceTests = append(ConformanceTests, HTTPRouteObservedGenerationBump)
    36  }
    37  
    38  var HTTPRouteObservedGenerationBump = suite.ConformanceTest{
    39  	ShortName:   "HTTPRouteObservedGenerationBump",
    40  	Description: "A HTTPRoute in the gateway-conformance-infra namespace should update the observedGeneration in all of it's Status.Conditions after an update to the spec",
    41  	Features: []suite.SupportedFeature{
    42  		suite.SupportGateway,
    43  		suite.SupportHTTPRoute,
    44  	},
    45  	Manifests: []string{"tests/httproute-observed-generation-bump.yaml"},
    46  	Test: func(t *testing.T, suite *suite.ConformanceTestSuite) {
    47  		routeNN := types.NamespacedName{Name: "observed-generation-bump", Namespace: "gateway-conformance-infra"}
    48  		gwNN := types.NamespacedName{Name: "same-namespace", Namespace: "gateway-conformance-infra"}
    49  
    50  		t.Run("observedGeneration should increment", func(t *testing.T) {
    51  			ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
    52  			defer cancel()
    53  
    54  			namespaces := []string{"gateway-conformance-infra"}
    55  			kubernetes.NamespacesMustBeReady(t, suite.Client, suite.TimeoutConfig, namespaces)
    56  
    57  			original := &v1.HTTPRoute{}
    58  			err := suite.Client.Get(ctx, routeNN, original)
    59  			require.NoErrorf(t, err, "error getting HTTPRoute: %v", err)
    60  
    61  			// Sanity check
    62  			kubernetes.HTTPRouteMustHaveLatestConditions(t, original)
    63  
    64  			mutate := original.DeepCopy()
    65  			mutate.Spec.Rules[0].BackendRefs[0].Name = "infra-backend-v2"
    66  			err = suite.Client.Patch(ctx, mutate, client.MergeFrom(original))
    67  			require.NoErrorf(t, err, "error patching the HTTPRoute: %v", err)
    68  
    69  			kubernetes.HTTPRouteMustHaveCondition(t, suite.Client, suite.TimeoutConfig, routeNN, gwNN, metav1.Condition{
    70  				Type:   string(v1.RouteConditionAccepted),
    71  				Status: metav1.ConditionTrue,
    72  				Reason: "", // any reason
    73  			})
    74  			kubernetes.HTTPRouteMustHaveResolvedRefsConditionsTrue(t, suite.Client, suite.TimeoutConfig, routeNN, gwNN)
    75  
    76  			updated := &v1.HTTPRoute{}
    77  			err = suite.Client.Get(ctx, routeNN, updated)
    78  			require.NoErrorf(t, err, "error getting Gateway: %v", err)
    79  
    80  			// Sanity check
    81  			kubernetes.HTTPRouteMustHaveLatestConditions(t, updated)
    82  
    83  			require.NotEqual(t, original.Generation, updated.Generation, "generation should change after an update")
    84  		})
    85  	},
    86  }