|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +package e2e |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "k8s.io/client-go/kubernetes" |
| 9 | + "testing" |
| 10 | + |
| 11 | + operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller" |
| 12 | + gatewayapi_upgradeable "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/gatewayapi-upgradeable" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 15 | + |
| 16 | + "k8s.io/apimachinery/pkg/api/errors" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/client/config" |
| 18 | +) |
| 19 | + |
| 20 | +func TestGatewayAPIUpgradeable(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + |
| 23 | + t.Log("Starting test: Ensure the Gateway API CRDs exist") |
| 24 | + // Ensure the Gateway API CRDs exist |
| 25 | + ensureCRDs(t) |
| 26 | + |
| 27 | + t.Log("Starting test: Testing if admin gate works properly") |
| 28 | + // Test if admin gate works properly |
| 29 | + testAdminGate(t) |
| 30 | + |
| 31 | + t.Log("Starting test: Deleting Gateway API CRDs") |
| 32 | + // Delete the Gateway API CRDs |
| 33 | + deleteCRDs(t) |
| 34 | + |
| 35 | + t.Log("Starting test: Testing if admin gate works properly after CRDs are deleted") |
| 36 | + // Test if admin gate works properly after CRDs are deleted |
| 37 | + testAdminGate(t) |
| 38 | + |
| 39 | + // Defer the cleanup of the test gateway. |
| 40 | + t.Cleanup(func() { |
| 41 | + t.Log("Starting cleanup: Deleting test gateway") |
| 42 | + testGateway := gatewayapiv1.Gateway{ObjectMeta: metav1.ObjectMeta{Name: testGatewayName, Namespace: operatorcontroller.DefaultOperandNamespace}} |
| 43 | + if err := kclient.Delete(context.TODO(), &testGateway); err != nil { |
| 44 | + if errors.IsNotFound(err) { |
| 45 | + t.Logf("Gateway %q not found during cleanup", testGateway.Name) |
| 46 | + return |
| 47 | + } |
| 48 | + t.Errorf("failed to delete gateway %q: %v", testGateway.Name, err) |
| 49 | + } else { |
| 50 | + t.Logf("Successfully deleted gateway %q", testGateway.Name) |
| 51 | + } |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +func getClient() (*kubernetes.Clientset, error) { |
| 56 | + kubeConfig, err := config.GetConfig() |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + return kubernetes.NewForConfig(kubeConfig) |
| 61 | +} |
| 62 | + |
| 63 | +func testAdminGate(t *testing.T) { |
| 64 | + kclient, err := getClient() |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("failed to get kube client: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + t.Log("Ensuring CRDs exist") |
| 70 | + crdsExist := true |
| 71 | + |
| 72 | + // Correcting the namespace to "openshift-config-managed" |
| 73 | + t.Logf("Getting configmap admin-gates in namespace openshift-config-managed") |
| 74 | + configMap, err := kclient.CoreV1().ConfigMaps("openshift-config-managed"). |
| 75 | + Get(context.TODO(), "admin-gates", metav1.GetOptions{}) |
| 76 | + if err != nil { |
| 77 | + t.Errorf("failed to get configmap admin-gates: %v", err) |
| 78 | + // Additional logging for errors |
| 79 | + if errors.IsNotFound(err) { |
| 80 | + t.Logf("ConfigMap 'admin-gates' not found in namespace openshift-config-managed") |
| 81 | + } else { |
| 82 | + t.Logf("Error getting ConfigMap: %v", err) |
| 83 | + } |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + t.Log("Checking for admin gate key in the configmap") |
| 88 | + adminGateKeyExists := false |
| 89 | + if _, ok := configMap.Data[gatewayapi_upgradeable.GatewayAPIAdminKey]; ok { |
| 90 | + adminGateKeyExists = true |
| 91 | + } |
| 92 | + |
| 93 | + t.Logf("CRDs exist: %v, Admin Gate Key exists: %v", crdsExist, adminGateKeyExists) |
| 94 | + |
| 95 | + if crdsExist && !adminGateKeyExists { |
| 96 | + t.Errorf("expected admin gate key to exist in configmap, but it does not") |
| 97 | + } else if !crdsExist && adminGateKeyExists { |
| 98 | + t.Errorf("expected admin gate key to be removed from configmap, but it still exists") |
| 99 | + } |
| 100 | +} |
0 commit comments