|
| 1 | +/* |
| 2 | +Copyright 2025 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 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/types" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | + |
| 28 | + "sigs.k8s.io/gateway-api/apis/v1alpha2" |
| 29 | + "sigs.k8s.io/gateway-api/apis/v1alpha3" |
| 30 | + "sigs.k8s.io/gateway-api/conformance/utils/kubernetes" |
| 31 | + "sigs.k8s.io/gateway-api/conformance/utils/suite" |
| 32 | + "sigs.k8s.io/gateway-api/pkg/features" |
| 33 | +) |
| 34 | + |
| 35 | +func init() { |
| 36 | + ConformanceTests = append(ConformanceTests, BackendTLSPolicyObservedGenerationBump) |
| 37 | +} |
| 38 | + |
| 39 | +var BackendTLSPolicyObservedGenerationBump = suite.ConformanceTest{ |
| 40 | + ShortName: "BackendTLSPolicyObservedGenerationBump", |
| 41 | + Description: "A BackendTLSPolicy in the gateway-conformance-infra namespace should update the observedGeneration in all of it's Status.Conditions after an update to the spec", |
| 42 | + Features: []features.FeatureName{ |
| 43 | + features.SupportGateway, |
| 44 | + features.SupportHTTPRoute, |
| 45 | + features.SupportBackendTLSPolicy, |
| 46 | + }, |
| 47 | + Manifests: []string{"tests/backendtlspolicy-observed-generation-bump.yaml"}, |
| 48 | + Test: func(t *testing.T, suite *suite.ConformanceTestSuite) { |
| 49 | + ns := "gateway-conformance-infra" |
| 50 | + policyNN := types.NamespacedName{Name: "observed-generation-bump", Namespace: ns} |
| 51 | + gwNN := types.NamespacedName{Name: "same-namespace", Namespace: ns} |
| 52 | + |
| 53 | + t.Run("observedGeneration should increment", func(t *testing.T) { |
| 54 | + ctx, cancel := context.WithTimeout(context.Background(), suite.TimeoutConfig.LatestObservedGenerationSet) |
| 55 | + defer cancel() |
| 56 | + |
| 57 | + namespaces := []string{"gateway-conformance-infra"} |
| 58 | + kubernetes.NamespacesMustBeReady(t, suite.Client, suite.TimeoutConfig, namespaces) |
| 59 | + |
| 60 | + original := &v1alpha3.BackendTLSPolicy{} |
| 61 | + err := suite.Client.Get(ctx, policyNN, original) |
| 62 | + require.NoError(t, err, "error getting HTTPRoute") |
| 63 | + |
| 64 | + // Sanity check |
| 65 | + kubernetes.BackendTLSPolicyMustHaveLatestConditions(t, original) |
| 66 | + |
| 67 | + mutate := original.DeepCopy() |
| 68 | + mutate.Spec.Validation.Hostname = "foo.example.com" |
| 69 | + err = suite.Client.Patch(ctx, mutate, client.MergeFrom(original)) |
| 70 | + require.NoError(t, err, "error patching the BackendTLSPolicy") |
| 71 | + |
| 72 | + kubernetes.BackendTLSPolicyMustHaveCondition(t, suite.Client, suite.TimeoutConfig, policyNN, gwNN, metav1.Condition{ |
| 73 | + Type: string(v1alpha2.PolicyConditionAccepted), |
| 74 | + Status: metav1.ConditionTrue, |
| 75 | + Reason: "", // any reason |
| 76 | + }) |
| 77 | + |
| 78 | + updated := &v1alpha3.BackendTLSPolicy{} |
| 79 | + err = suite.Client.Get(ctx, policyNN, updated) |
| 80 | + require.NoError(t, err, "error getting BackendTLSPolicy") |
| 81 | + |
| 82 | + // Sanity check |
| 83 | + kubernetes.BackendTLSPolicyMustHaveLatestConditions(t, updated) |
| 84 | + |
| 85 | + require.NotEqual(t, original.Generation, updated.Generation, "generation should change after an update") |
| 86 | + }) |
| 87 | + }, |
| 88 | +} |
0 commit comments