...

Source file src/sigs.k8s.io/gateway-api/conformance/tests/gateway-observed-generation-bump.go

Documentation: sigs.k8s.io/gateway-api/conformance/tests

     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  	"k8s.io/apimachinery/pkg/types"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	v1 "sigs.k8s.io/gateway-api/apis/v1"
    29  	"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
    30  	"sigs.k8s.io/gateway-api/conformance/utils/suite"
    31  )
    32  
    33  func init() {
    34  	ConformanceTests = append(ConformanceTests, GatewayObservedGenerationBump)
    35  }
    36  
    37  var GatewayObservedGenerationBump = suite.ConformanceTest{
    38  	ShortName:   "GatewayObservedGenerationBump",
    39  	Description: "A Gateway in the gateway-conformance-infra namespace should update the observedGeneration in all of its Status.Conditions after an update to the spec",
    40  	Features: []suite.SupportedFeature{
    41  		suite.SupportGateway,
    42  		suite.SupportGatewayPort8080,
    43  	},
    44  	Manifests: []string{"tests/gateway-observed-generation-bump.yaml"},
    45  	Test: func(t *testing.T, s *suite.ConformanceTestSuite) {
    46  		gwNN := types.NamespacedName{Name: "gateway-observed-generation-bump", Namespace: "gateway-conformance-infra"}
    47  
    48  		t.Run("observedGeneration should increment", func(t *testing.T) {
    49  			ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
    50  			defer cancel()
    51  
    52  			namespaces := []string{"gateway-conformance-infra"}
    53  			kubernetes.NamespacesMustBeReady(t, s.Client, s.TimeoutConfig, namespaces)
    54  
    55  			// Sanity check
    56  			kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwNN)
    57  
    58  			original := &v1.Gateway{}
    59  			err := s.Client.Get(ctx, gwNN, original)
    60  			require.NoErrorf(t, err, "error getting Gateway: %v", err)
    61  
    62  			all := v1.NamespacesFromAll
    63  
    64  			mutate := original.DeepCopy()
    65  
    66  			// mutate the Gateway Spec
    67  			mutate.Spec.Listeners = append(mutate.Spec.Listeners, v1.Listener{
    68  				Name:     "alternate",
    69  				Port:     8080,
    70  				Protocol: v1.HTTPProtocolType,
    71  				AllowedRoutes: &v1.AllowedRoutes{
    72  					Namespaces: &v1.RouteNamespaces{From: &all},
    73  				},
    74  			})
    75  
    76  			err = s.Client.Patch(ctx, mutate, client.MergeFrom(original))
    77  			require.NoErrorf(t, err, "error patching the Gateway: %v", err)
    78  
    79  			// Ensure the generation and observedGeneration sync up
    80  			kubernetes.NamespacesMustBeReady(t, s.Client, s.TimeoutConfig, namespaces)
    81  
    82  			// Sanity check
    83  			kubernetes.GatewayMustHaveLatestConditions(t, s.Client, s.TimeoutConfig, gwNN)
    84  
    85  			updated := &v1.Gateway{}
    86  			err = s.Client.Get(ctx, gwNN, updated)
    87  			require.NoErrorf(t, err, "error getting Gateway: %v", err)
    88  
    89  			require.NotEqual(t, original.Generation, updated.Generation, "generation should change after an update")
    90  		})
    91  	},
    92  }
    93  

View as plain text