From 3f2109f0589a100ea9377fe9e10ac6445245562e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Mon, 30 Sep 2024 17:26:51 +0200 Subject: [PATCH] tests: refactor deploy helpers --- test/envtest/consts.go | 75 --- .../kongconsumercredential_acl_test.go | 23 +- .../kongconsumercredential_basicauth_test.go | 23 +- .../envtest/kongpluginbinding_managed_test.go | 23 +- .../kongpluginbinding_unmanaged_test.go | 44 +- .../kongplugincleanupfinalizer_test.go | 9 +- .../konnect_entities_cacertificate_test.go | 9 +- .../konnect_entities_certificate_test.go | 9 +- test/envtest/konnect_entities_key_test.go | 7 +- test/envtest/konnect_entities_keyset_test.go | 7 +- .../konnect_entities_kongconsumer_test.go | 16 +- .../konnect_entities_kongvault_test.go | 7 +- test/envtest/konnect_entities_sni_test.go | 26 +- test/helpers/deploy/consts.go | 78 +++ .../deploy}/deploy_resources.go | 223 +++++++-- test/integration/test_konnect_entities.go | 465 ++++-------------- 16 files changed, 470 insertions(+), 574 deletions(-) create mode 100644 test/helpers/deploy/consts.go rename test/{envtest => helpers/deploy}/deploy_resources.go (72%) diff --git a/test/envtest/consts.go b/test/envtest/consts.go index 75a0b2140..d063cf5b6 100644 --- a/test/envtest/consts.go +++ b/test/envtest/consts.go @@ -15,79 +15,4 @@ const ( // tickTime is a generic tick time for the tests' eventual conditions. tickTime = 500 * time.Millisecond - - // dummyValidCACertPEM is a dummy valid CA certificate PEM to be used in tests. - dummyValidCACertPEM = `-----BEGIN CERTIFICATE----- -MIIDPTCCAiWgAwIBAgIUcNKAk2icWRJGwZ5QDpdSkkeF5kUwDQYJKoZIhvcNAQEL -BQAwLjELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRIwEAYDVQQKDAlLb25nIElu -Yy4wHhcNMjQwOTE5MDkwODEzWhcNMjkwOTE4MDkwODEzWjAuMQswCQYDVQQGEwJV -UzELMAkGA1UECAwCQ0ExEjAQBgNVBAoMCUtvbmcgSW5jLjCCASIwDQYJKoZIhvcN -AQEBBQADggEPADCCAQoCggEBAMvDhLM0vTw0QXmgE+sB6gvKx2PUWzvd2tRZoamH -h4RAxYRjgJsJe6WEeAk0tjWQqwAq0Y2MQioMCC4X+L13kpdtomI+4PKjBozg+iTd -ThyV0oQSVHHWzayUzcSODnGR524H9YxmkXV5ImrXwbEqXwiUESPVtjnf/ZzWS01v -gtbu4x3YW+z8kRoXOTpJHKcEoI90SU9F4yeuQsCtbJHeJZRqPr6Kz84ZuHsZ2MeU -os4j1GdMaH3dSysqFv6o1hJ2+6bsrE/ONiGtBb4+tyhivgf+u+ixQwqIERlEJzhI -z/csoAAnfMBY401j2NNUgPpwx5sTQdCz5aFDmanol5152M8CAwEAAaNTMFEwHQYD -VR0OBBYEFK2qd3oRF37acVvgfDeLakx66ioTMB8GA1UdIwQYMBaAFK2qd3oRF37a -cVvgfDeLakx66ioTMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEB -AAuul+rAztaueTpPIM63nrS4bSZsIatCgAQ5Pihm0+rZ+13BJk4K2GxkS+T0qkB5 -34+F3eVhUB4cC+kVkWZrlEzD9BsJwWjnoJK+848znTg+ufTeaOQWslYNqFKjmy2k -K6NE7E6r+JLdNvafJzeDybSTXI1tCzDRWUdj5m+bgruX07B13KIJKrAweCTD1927 -WvvfJYxsg8P7dYD9DPlcuOm22ggAaPPu4P/MsnApiq3kJEI/nSGSsboKyjBO2hcz -VF1CYr6Epfyw/47kwuJLCVHjlTgT4haOChW1S8rZILCLXfb8ukM/g3XVYIeEwzsr -KU74cm8lTFCdxlcXePbMdHc= ------END CERTIFICATE----- -` - // dummyValidCertPEM is a dummy valid certificate PEM to be used in tests. - dummyValidCertPEM = `-----BEGIN CERTIFICATE----- -MIIDPTCCAiUCFG5IolqRiKPMfzTI8peXlaF6cZODMA0GCSqGSIb3DQEBCwUAMFsx -CzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTEVMBMGA1UEBwwMRGVmYXVsdCBDaXR5 -MRIwEAYDVQQKDAlLb25nIEluYy4xFDASBgNVBAMMC2tvbmdocS50ZWNoMB4XDTI0 -MDkyNTA3MjIzOFoXDTM0MDkyMzA3MjIzOFowWzELMAkGA1UEBhMCVVMxCzAJBgNV -BAgMAkNBMRUwEwYDVQQHDAxEZWZhdWx0IENpdHkxEjAQBgNVBAoMCUtvbmcgSW5j -LjEUMBIGA1UEAwwLa29uZ2hxLnRlY2gwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw -ggEKAoIBAQDXmNBzpWyJ0YUdfCamZpJiwRQMn5vVY8iKQrd3dD03DWyPHu/fXlrL -+QPTRip5d1SrxjzQ4S3fgme442BTlElF9d1w1rhg+DIg6NsW1jd+3IZaICnq7BZH -rJGlW+IWJSKHmNQ39nfVQwgL/QdylrYpbB7uwdEDMa78GfXteiXTcuNobCr7VWVz -rY6rQXo/dImWE1PtMp/EZEMsEbgbQpK5+fUnKTmFncVlDAZ2Q3s2MPikV5UhMVyQ -dKQydU0Ev0LRtpsjW8pQdshMG1ilMq6Yg6YU95gakLVjRXMoDlIJOu08mdped+2Y -VIUSXhRyRt1hbkFP0fXG0THfZ3DjH7jRAgMBAAEwDQYJKoZIhvcNAQELBQADggEB -ANEXlNaQKLrB+jsnNjybsTRkvRRmwjnXaQV0jHzjseGsTJoKY5ABBsSRDiHtqB+9 -LPTpHhLYJWsHSLwawIJ3aWDDpF4MNTRsvO12v7wM8Q42OSgkP23O6a5ESkyHRBAb -dLVEp+0Z3kjYwPIglIK37PcgDci6Zim73GOfapDEASNbnCu8js2g/ucYPPXkGMxl -PSUER7MTNf9wRbXrroCE+tZw4kUyUh+6taNlU4ialAJLO1x6UGVRHvPgEx0fAAxA -seBH+A9QMvVl2cKcvrOgZ0CWY01aFRO9ROQ7PrYXqRFvOZu8K3QzLw7xYoK1DTp+ -kkO/oPy+WIbqEvj7QrhUXpo= ------END CERTIFICATE----- -` - // dummyValidCertKeyPEM is a dummy valid certificate key PEM to be used in tests. - dummyValidCertKeyPEM = `-----BEGIN PRIVATE KEY----- -MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDXmNBzpWyJ0YUd -fCamZpJiwRQMn5vVY8iKQrd3dD03DWyPHu/fXlrL+QPTRip5d1SrxjzQ4S3fgme4 -42BTlElF9d1w1rhg+DIg6NsW1jd+3IZaICnq7BZHrJGlW+IWJSKHmNQ39nfVQwgL -/QdylrYpbB7uwdEDMa78GfXteiXTcuNobCr7VWVzrY6rQXo/dImWE1PtMp/EZEMs -EbgbQpK5+fUnKTmFncVlDAZ2Q3s2MPikV5UhMVyQdKQydU0Ev0LRtpsjW8pQdshM -G1ilMq6Yg6YU95gakLVjRXMoDlIJOu08mdped+2YVIUSXhRyRt1hbkFP0fXG0THf -Z3DjH7jRAgMBAAECggEAOSZ4h1dgDK5+H2FEK5MAFe6BnpEGsYu4YrIpySAGhBvq -XYwBYRA1eGFjmrM8WiOATeKIR4SRcPC0BwY7CBzESafRkfJRQN86BpBDV2vknRve -/3AMPIplo41CtHdFWMJyQ0iHZOhQPrd8oBTsTvtVgWh4UKkO+05FyO0mzFM3SLPs -pqRwMZjLlKVZhbI1l0Ur787tzWpMQQHmd8csAvlak+GIciQWELbVK+5kr/FDpJbq -joIeHX7DCmIqrD/Okwa8SfJu1sutmRX+nrxkDi7trPYcpqriDoWs2jMcnS2GHq9M -lsy2XHn+qLjCpl3/XU61xenWs+Rmmj6J/oIs1zYXCwKBgQDywRS/MNgwMst703Wh -ERJO0rNSR0XVzzoKOqc/ghPkeA8mVNwfNkbgWks+83tuAb6IunMIeyQJ3g/jRhjz -KImsqJmO+DoZCioeaR3PeIWibi9I9Irg6dtoNMwxSmmOtCKD0rccxM1V9OnYkn5a -0Fb+irQSgJYiHrF2SLAT0NoWEwKBgQDjXGLHCu/WEy49vROdkTia133Wc7m71/D5 -RDUqvSmAEHJyhTlzCbTO+JcNhC6cx3s102GNcVYHlAq3WoE5EV1YykUNJwUg4XPn -AggNkYOiXs6tf+uePmT8MddixFFgFxZ2bIqFhvnY+WqypHuxtwIepqKJjq5xZTiB -+lfp7SziCwKBgAivofdpXwLyfldy7I2T18zcOzBhfn01CgWdrahXFjEhnqEnfizb -u1OBx5l8CtmX1GJ+EWmnRlXYDUd7lZ71v19fNQdpmGKW+4TVDA0Fafqy6Jw6q9F6 -bLBg20GUQQyrI2UGICk2XYaK2ec27rB/Le2zttfGpBiaco0h8rLy0SrjAoGBAM4/ -UY/UOQsOrUTuT2wBf8LfNtUid9uSIZRNrplNrebxhJCkkB/uLyoN0iE9xncMcpW6 -YmVH6c3IGwyHOnBFc1OHcapjukBApL5rVljQpwPVU1GKmHgdi8hHgmajRlqPtx3I -isRkVCPi5kqV8WueY3rgmNOGLnLJasBmE/gt4ihPAoGAG3v93R5tAeSrn7DMHaZt -p+udsNw9mDPYHAzlYtnw1OE/I0ceR5UyCFSzCd00Q8ZYBLf9jRvsO/GUA4F51isE -8/7xyqSxJqDwzv9N8EGkqf/SfMKA3kK3Sc8u+ovhzJu8OxcY+qrpo4+vYWYeW42n -5XBwvWV2ovRMx7Ntw7FUc24= ------END PRIVATE KEY----- -` ) diff --git a/test/envtest/kongconsumercredential_acl_test.go b/test/envtest/kongconsumercredential_acl_test.go index e4406a4d4..663fc57e2 100644 --- a/test/envtest/kongconsumercredential_acl_test.go +++ b/test/envtest/kongconsumercredential_acl_test.go @@ -18,6 +18,7 @@ import ( "github.com/kong/gateway-operator/controller/konnect/ops" "github.com/kong/gateway-operator/modules/manager" "github.com/kong/gateway-operator/modules/manager/scheme" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -40,11 +41,11 @@ func TestKongConsumerCredential_ACL(t *testing.T) { require.NoError(t, err) clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) consumerID := uuid.NewString() - consumer := deployKongConsumerWithProgrammed(t, ctx, clientNamespaced, &configurationv1.KongConsumer{ + consumer := deploy.KongConsumerWithProgrammed(t, ctx, clientNamespaced, &configurationv1.KongConsumer{ Username: "username1", Spec: configurationv1.KongConsumerSpec{ ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ @@ -66,15 +67,15 @@ func TestKongConsumerCredential_ACL(t *testing.T) { require.NoError(t, clientNamespaced.Status().Update(ctx, consumer)) aclGroup := "acl-group1" - KongCredentialACL := deployKongCredentialACL(t, ctx, clientNamespaced, consumer.Name, aclGroup) + kongCredentialACL := deploy.KongCredentialACL(t, ctx, clientNamespaced, consumer.Name, aclGroup) aclID := uuid.NewString() tags := []string{ "k8s-generation:1", "k8s-group:configuration.konghq.com", "k8s-kind:KongCredentialACL", - "k8s-name:" + KongCredentialACL.Name, + "k8s-name:" + kongCredentialACL.Name, "k8s-namespace:" + ns.Name, - "k8s-uid:" + string(KongCredentialACL.GetUID()), + "k8s-uid:" + string(kongCredentialACL.GetUID()), "k8s-version:v1alpha1", } @@ -138,7 +139,7 @@ func TestKongConsumerCredential_ACL(t *testing.T) { }, nil, ) - require.NoError(t, clientNamespaced.Delete(ctx, KongCredentialACL)) + require.NoError(t, clientNamespaced.Delete(ctx, kongCredentialACL)) assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.KongCredentialsACLSDK.AssertExpectations(t)) @@ -146,17 +147,17 @@ func TestKongConsumerCredential_ACL(t *testing.T) { w := setupWatch[configurationv1alpha1.KongCredentialACLList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - KongCredentialACL = deployKongCredentialACL(t, ctx, clientNamespaced, consumer.Name, aclGroup) - t.Logf("redeployed %s KongCredentialACL resource", client.ObjectKeyFromObject(KongCredentialACL)) + kongCredentialACL = deploy.KongCredentialACL(t, ctx, clientNamespaced, consumer.Name, aclGroup) + t.Logf("redeployed %s KongCredentialACL resource", client.ObjectKeyFromObject(kongCredentialACL)) t.Logf("checking if KongConsumer %s removal will delete the associated credentials %s", client.ObjectKeyFromObject(consumer), - client.ObjectKeyFromObject(KongCredentialACL), + client.ObjectKeyFromObject(kongCredentialACL), ) require.NoError(t, clientNamespaced.Delete(ctx, consumer)) _ = watchFor(t, ctx, w, watch.Modified, func(c *configurationv1alpha1.KongCredentialACL) bool { - return c.Name == KongCredentialACL.Name + return c.Name == kongCredentialACL.Name }, "KongCredentialACL wasn't deleted but it should have been", ) diff --git a/test/envtest/kongconsumercredential_basicauth_test.go b/test/envtest/kongconsumercredential_basicauth_test.go index 3906e400b..b009db5bc 100644 --- a/test/envtest/kongconsumercredential_basicauth_test.go +++ b/test/envtest/kongconsumercredential_basicauth_test.go @@ -18,6 +18,7 @@ import ( "github.com/kong/gateway-operator/controller/konnect/ops" "github.com/kong/gateway-operator/modules/manager" "github.com/kong/gateway-operator/modules/manager/scheme" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -40,11 +41,11 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) { require.NoError(t, err) clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) consumerID := uuid.NewString() - consumer := deployKongConsumerWithProgrammed(t, ctx, clientNamespaced, &configurationv1.KongConsumer{ + consumer := deploy.KongConsumerWithProgrammed(t, ctx, clientNamespaced, &configurationv1.KongConsumer{ Username: "username1", Spec: configurationv1.KongConsumerSpec{ ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ @@ -67,15 +68,15 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) { password := "password" username := "username" - KongCredentialBasicAuth := deployKongCredentialBasicAuth(t, ctx, clientNamespaced, consumer.Name, username, password) + kongCredentialBasicAuth := deploy.KongCredentialBasicAuth(t, ctx, clientNamespaced, consumer.Name, username, password) basicAuthID := uuid.NewString() tags := []string{ "k8s-generation:1", "k8s-group:configuration.konghq.com", "k8s-kind:KongCredentialBasicAuth", - "k8s-name:" + KongCredentialBasicAuth.Name, + "k8s-name:" + kongCredentialBasicAuth.Name, "k8s-namespace:" + ns.Name, - "k8s-uid:" + string(KongCredentialBasicAuth.GetUID()), + "k8s-uid:" + string(kongCredentialBasicAuth.GetUID()), "k8s-version:v1alpha1", } @@ -140,7 +141,7 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) { }, nil, ) - require.NoError(t, clientNamespaced.Delete(ctx, KongCredentialBasicAuth)) + require.NoError(t, clientNamespaced.Delete(ctx, kongCredentialBasicAuth)) assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.KongCredentialsBasicAuthSDK.AssertExpectations(t)) @@ -148,17 +149,17 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) { w := setupWatch[configurationv1alpha1.KongCredentialBasicAuthList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - KongCredentialBasicAuth = deployKongCredentialBasicAuth(t, ctx, clientNamespaced, consumer.Name, username, password) - t.Logf("redeployed %s KongCredentialBasicAuth resource", client.ObjectKeyFromObject(KongCredentialBasicAuth)) + kongCredentialBasicAuth = deploy.KongCredentialBasicAuth(t, ctx, clientNamespaced, consumer.Name, username, password) + t.Logf("redeployed %s KongCredentialBasicAuth resource", client.ObjectKeyFromObject(kongCredentialBasicAuth)) t.Logf("checking if KongConsumer %s removal will delete the associated credentials %s", client.ObjectKeyFromObject(consumer), - client.ObjectKeyFromObject(KongCredentialBasicAuth), + client.ObjectKeyFromObject(kongCredentialBasicAuth), ) require.NoError(t, clientNamespaced.Delete(ctx, consumer)) _ = watchFor(t, ctx, w, watch.Modified, func(c *configurationv1alpha1.KongCredentialBasicAuth) bool { - return c.Name == KongCredentialBasicAuth.Name + return c.Name == kongCredentialBasicAuth.Name }, "KongCredentialBasicAuth wasn't deleted but it should have been", ) diff --git a/test/envtest/kongpluginbinding_managed_test.go b/test/envtest/kongpluginbinding_managed_test.go index e27641c83..e8d8b2cdd 100644 --- a/test/envtest/kongpluginbinding_managed_test.go +++ b/test/envtest/kongpluginbinding_managed_test.go @@ -23,6 +23,7 @@ import ( "github.com/kong/gateway-operator/modules/manager" "github.com/kong/gateway-operator/modules/manager/scheme" "github.com/kong/gateway-operator/pkg/consts" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -50,8 +51,8 @@ func TestKongPluginBindingManaged(t *testing.T) { require.NoError(t, err) clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) factory := ops.NewMockSDKFactory(t) sdk := factory.SDK @@ -96,8 +97,8 @@ func TestKongPluginBindingManaged(t *testing.T) { wKongPluginBinding := setupWatch[configurationv1alpha1.KongPluginBindingList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) wKongPlugin := setupWatch[configurationv1.KongPluginList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - kongService := deployKongServiceAttachedToCP(t, ctx, clientNamespaced, cp, - WithAnnotation(consts.PluginsAnnotationKey, rateLimitingkongPlugin.Name), + kongService := deploy.KongServiceAttachedToCP(t, ctx, clientNamespaced, cp, + deploy.WithAnnotation(consts.PluginsAnnotationKey, rateLimitingkongPlugin.Name), ) t.Cleanup(func() { require.NoError(t, clientNamespaced.Delete(ctx, kongService)) @@ -196,13 +197,13 @@ func TestKongPluginBindingManaged(t *testing.T) { wKongPluginBinding := setupWatch[configurationv1alpha1.KongPluginBindingList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) wKongPlugin := setupWatch[configurationv1.KongPluginList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - kongService := deployKongServiceAttachedToCP(t, ctx, clientNamespaced, cp) + kongService := deploy.KongServiceAttachedToCP(t, ctx, clientNamespaced, cp) t.Cleanup(func() { require.NoError(t, clientNamespaced.Delete(ctx, kongService)) }) updateKongServiceStatusWithProgrammed(t, ctx, clientNamespaced, kongService, serviceID, cp.GetKonnectStatus().GetKonnectID()) - kongRoute := deployKongRouteAttachedToService(t, ctx, clientNamespaced, kongService, - WithAnnotation(consts.PluginsAnnotationKey, rateLimitingkongPlugin.Name), + kongRoute := deploy.KongRouteAttachedToService(t, ctx, clientNamespaced, kongService, + deploy.WithAnnotation(consts.PluginsAnnotationKey, rateLimitingkongPlugin.Name), ) t.Cleanup(func() { require.NoError(t, clientNamespaced.Delete(ctx, kongRoute)) @@ -300,15 +301,15 @@ func TestKongPluginBindingManaged(t *testing.T) { wKongPluginBinding := setupWatch[configurationv1alpha1.KongPluginBindingList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) wKongPlugin := setupWatch[configurationv1.KongPluginList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - kongService := deployKongServiceAttachedToCP(t, ctx, clientNamespaced, cp, - WithAnnotation(consts.PluginsAnnotationKey, rateLimitingkongPlugin.Name), + kongService := deploy.KongServiceAttachedToCP(t, ctx, clientNamespaced, cp, + deploy.WithAnnotation(consts.PluginsAnnotationKey, rateLimitingkongPlugin.Name), ) t.Cleanup(func() { require.NoError(t, clientNamespaced.Delete(ctx, kongService)) }) updateKongServiceStatusWithProgrammed(t, ctx, clientNamespaced, kongService, serviceID, cp.GetKonnectStatus().GetKonnectID()) - kongRoute := deployKongRouteAttachedToService(t, ctx, clientNamespaced, kongService, - WithAnnotation(consts.PluginsAnnotationKey, rateLimitingkongPlugin.Name), + kongRoute := deploy.KongRouteAttachedToService(t, ctx, clientNamespaced, kongService, + deploy.WithAnnotation(consts.PluginsAnnotationKey, rateLimitingkongPlugin.Name), ) t.Cleanup(func() { require.NoError(t, clientNamespaced.Delete(ctx, kongRoute)) diff --git a/test/envtest/kongpluginbinding_unmanaged_test.go b/test/envtest/kongpluginbinding_unmanaged_test.go index b0efd9a49..d25e6e424 100644 --- a/test/envtest/kongpluginbinding_unmanaged_test.go +++ b/test/envtest/kongpluginbinding_unmanaged_test.go @@ -22,6 +22,7 @@ import ( "github.com/kong/gateway-operator/modules/manager" "github.com/kong/gateway-operator/modules/manager/scheme" "github.com/kong/gateway-operator/pkg/consts" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -43,8 +44,8 @@ func TestKongPluginBindingUnmanaged(t *testing.T) { require.NoError(t, err) clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) factory := ops.NewMockSDKFactory(t) sdk := factory.SDK @@ -60,7 +61,7 @@ func TestKongPluginBindingUnmanaged(t *testing.T) { StartReconcilers(ctx, t, mgr, logs, reconcilers...) t.Run("binding to KongService", func(t *testing.T) { - proxyCacheKongPlugin := deployProxyCachePlugin(t, ctx, clientNamespaced) + proxyCacheKongPlugin := deploy.ProxyCachePlugin(t, ctx, clientNamespaced) serviceID := uuid.NewString() pluginID := uuid.NewString() @@ -77,11 +78,11 @@ func TestKongPluginBindingUnmanaged(t *testing.T) { ) defer createCall.Unset() - kongService := deployKongServiceAttachedToCP(t, ctx, clientNamespaced, cp) + kongService := deploy.KongServiceAttachedToCP(t, ctx, clientNamespaced, cp) updateKongServiceStatusWithProgrammed(t, ctx, clientNamespaced, kongService, serviceID, cp.GetKonnectStatus().GetKonnectID()) wKongPlugin := setupWatch[configurationv1.KongPluginList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - kpb := deployKongPluginBinding(t, ctx, clientNamespaced, + kpb := deploy.KongPluginBinding(t, ctx, clientNamespaced, konnect.NewKongPluginBindingBuilder(). WithControlPlaneRefKonnectNamespaced(cp.Name). WithPluginRef(proxyCacheKongPlugin.Name). @@ -152,7 +153,7 @@ func TestKongPluginBindingUnmanaged(t *testing.T) { }, waitTime, tickTime) }) t.Run("binding to KongRoute", func(t *testing.T) { - proxyCacheKongPlugin := deployProxyCachePlugin(t, ctx, clientNamespaced) + proxyCacheKongPlugin := deploy.ProxyCachePlugin(t, ctx, clientNamespaced) serviceID := uuid.NewString() routeID := uuid.NewString() @@ -170,13 +171,13 @@ func TestKongPluginBindingUnmanaged(t *testing.T) { ) defer createCall.Unset() - kongService := deployKongServiceAttachedToCP(t, ctx, clientNamespaced, cp) + kongService := deploy.KongServiceAttachedToCP(t, ctx, clientNamespaced, cp) updateKongServiceStatusWithProgrammed(t, ctx, clientNamespaced, kongService, serviceID, cp.GetKonnectStatus().GetKonnectID()) - kongRoute := deployKongRouteAttachedToService(t, ctx, clientNamespaced, kongService) + kongRoute := deploy.KongRouteAttachedToService(t, ctx, clientNamespaced, kongService) updateKongRouteStatusWithProgrammed(t, ctx, clientNamespaced, kongRoute, routeID, cp.GetKonnectStatus().GetKonnectID(), serviceID) wKongPlugin := setupWatch[configurationv1.KongPluginList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - kpb := deployKongPluginBinding(t, ctx, clientNamespaced, + kpb := deploy.KongPluginBinding(t, ctx, clientNamespaced, konnect.NewKongPluginBindingBuilder(). WithControlPlaneRefKonnectNamespaced(cp.Name). WithPluginRef(proxyCacheKongPlugin.Name). @@ -249,15 +250,15 @@ func TestKongPluginBindingUnmanaged(t *testing.T) { }) t.Run("binding to KongService and KongRoute", func(t *testing.T) { - proxyCacheKongPlugin := deployProxyCachePlugin(t, ctx, clientNamespaced) + proxyCacheKongPlugin := deploy.ProxyCachePlugin(t, ctx, clientNamespaced) serviceID := uuid.NewString() routeID := uuid.NewString() pluginID := uuid.NewString() - kongService := deployKongServiceAttachedToCP(t, ctx, clientNamespaced, cp) + kongService := deploy.KongServiceAttachedToCP(t, ctx, clientNamespaced, cp) updateKongServiceStatusWithProgrammed(t, ctx, clientNamespaced, kongService, serviceID, cp.GetKonnectStatus().GetKonnectID()) - kongRoute := deployKongRouteAttachedToService(t, ctx, clientNamespaced, kongService) + kongRoute := deploy.KongRouteAttachedToService(t, ctx, clientNamespaced, kongService) updateKongRouteStatusWithProgrammed(t, ctx, clientNamespaced, kongRoute, routeID, cp.GetKonnectStatus().GetKonnectID(), serviceID) wKongPlugin := setupWatch[configurationv1.KongPluginList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) @@ -277,7 +278,7 @@ func TestKongPluginBindingUnmanaged(t *testing.T) { }, nil, ) - kpb := deployKongPluginBinding(t, ctx, clientNamespaced, + kpb := deploy.KongPluginBinding(t, ctx, clientNamespaced, konnect.NewKongPluginBindingBuilder(). WithControlPlaneRefKonnectNamespaced(cp.Name). WithPluginRef(proxyCacheKongPlugin.Name). @@ -351,19 +352,19 @@ func TestKongPluginBindingUnmanaged(t *testing.T) { }) t.Run("binding to KongService and KongConsumer", func(t *testing.T) { - proxyCacheKongPlugin := deployProxyCachePlugin(t, ctx, clientNamespaced) + proxyCacheKongPlugin := deploy.ProxyCachePlugin(t, ctx, clientNamespaced) serviceID := uuid.NewString() consumerID := uuid.NewString() pluginID := uuid.NewString() username := "test-user" + uuid.NewString() - kongService := deployKongServiceAttachedToCP(t, ctx, clientNamespaced, cp) + kongService := deploy.KongServiceAttachedToCP(t, ctx, clientNamespaced, cp) t.Cleanup(func() { require.NoError(t, client.IgnoreNotFound(clientNamespaced.Delete(ctx, kongService))) }) updateKongServiceStatusWithProgrammed(t, ctx, clientNamespaced, kongService, serviceID, cp.GetKonnectStatus().GetKonnectID()) - kongConsumer := deployKongConsumerAttachedToCP(t, ctx, clientNamespaced, username, cp) + kongConsumer := deploy.KongConsumerAttachedToCP(t, ctx, clientNamespaced, username, cp) t.Cleanup(func() { require.NoError(t, client.IgnoreNotFound(clientNamespaced.Delete(ctx, kongConsumer))) }) @@ -386,7 +387,7 @@ func TestKongPluginBindingUnmanaged(t *testing.T) { }, nil, ) - kpb := deployKongPluginBinding(t, ctx, clientNamespaced, + kpb := deploy.KongPluginBinding(t, ctx, clientNamespaced, konnect.NewKongPluginBindingBuilder(). WithControlPlaneRefKonnectNamespaced(cp.Name). WithPluginRef(proxyCacheKongPlugin.Name). @@ -460,16 +461,15 @@ func TestKongPluginBindingUnmanaged(t *testing.T) { }) t.Run("binding to KongService and KongConsumerGroup", func(t *testing.T) { - proxyCacheKongPlugin := deployProxyCachePlugin(t, ctx, clientNamespaced) + proxyCacheKongPlugin := deploy.ProxyCachePlugin(t, ctx, clientNamespaced) serviceID := uuid.NewString() consumerGroupID := uuid.NewString() pluginID := uuid.NewString() - cgName := "test-group-" + uuid.NewString() - kongService := deployKongServiceAttachedToCP(t, ctx, clientNamespaced, cp) + kongService := deploy.KongServiceAttachedToCP(t, ctx, clientNamespaced, cp) updateKongServiceStatusWithProgrammed(t, ctx, clientNamespaced, kongService, serviceID, cp.GetKonnectStatus().GetKonnectID()) - kongConsumerGroup := deployKongConsumerGroupAttachedToCP(t, ctx, clientNamespaced, cgName, cp) + kongConsumerGroup := deploy.KongConsumerGroupAttachedToCP(t, ctx, clientNamespaced, cp) updateKongConsumerGroupStatusWithKonnectID(t, ctx, clientNamespaced, kongConsumerGroup, consumerGroupID, cp.GetKonnectStatus().GetKonnectID()) wKongPlugin := setupWatch[configurationv1.KongPluginList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) @@ -489,7 +489,7 @@ func TestKongPluginBindingUnmanaged(t *testing.T) { }, nil, ) - kpb := deployKongPluginBinding(t, ctx, clientNamespaced, + kpb := deploy.KongPluginBinding(t, ctx, clientNamespaced, konnect.NewKongPluginBindingBuilder(). WithControlPlaneRefKonnectNamespaced(cp.Name). WithPluginRef(proxyCacheKongPlugin.Name). diff --git a/test/envtest/kongplugincleanupfinalizer_test.go b/test/envtest/kongplugincleanupfinalizer_test.go index 45b0589f8..f308b4f9e 100644 --- a/test/envtest/kongplugincleanupfinalizer_test.go +++ b/test/envtest/kongplugincleanupfinalizer_test.go @@ -16,6 +16,7 @@ import ( "github.com/kong/gateway-operator/modules/manager" "github.com/kong/gateway-operator/modules/manager/scheme" "github.com/kong/gateway-operator/pkg/consts" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" @@ -37,8 +38,8 @@ func TestKongPluginFinalizer(t *testing.T) { require.NoError(t, err) clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) require.NoError(t, manager.SetupCacheIndicesForKonnectTypes(ctx, mgr, false)) reconcilers := []Reconciler{ @@ -60,8 +61,8 @@ func TestKongPluginFinalizer(t *testing.T) { t.Logf("deployed %s KongPlugin (%s) resource", client.ObjectKeyFromObject(rateLimitingkongPlugin), rateLimitingkongPlugin.PluginName) wKongService := setupWatch[configurationv1alpha1.KongServiceList](t, ctx, clientWithWatch, client.InNamespace(ns.Name)) - kongService := deployKongServiceAttachedToCP(t, ctx, clientNamespaced, cp) - kpb := deployKongPluginBinding(t, ctx, clientNamespaced, + kongService := deploy.KongServiceAttachedToCP(t, ctx, clientNamespaced, cp) + kpb := deploy.KongPluginBinding(t, ctx, clientNamespaced, &configurationv1alpha1.KongPluginBinding{ Spec: configurationv1alpha1.KongPluginBindingSpec{ ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ diff --git a/test/envtest/konnect_entities_cacertificate_test.go b/test/envtest/konnect_entities_cacertificate_test.go index 705400a3d..e2f683e7e 100644 --- a/test/envtest/konnect_entities_cacertificate_test.go +++ b/test/envtest/konnect_entities_cacertificate_test.go @@ -18,6 +18,7 @@ import ( "github.com/kong/gateway-operator/controller/konnect/conditions" "github.com/kong/gateway-operator/controller/konnect/ops" "github.com/kong/gateway-operator/modules/manager/scheme" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" ) @@ -46,13 +47,13 @@ func TestKongCACertificate(t *testing.T) { clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) t.Log("Creating KonnectAPIAuthConfiguration and KonnectGatewayControlPlane") - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) t.Log("Setting up SDK expectations on KongCACertificate creation") sdk.CACertificatesSDK.EXPECT().CreateCaCertificate(mock.Anything, cp.GetKonnectStatus().GetKonnectID(), mock.MatchedBy(func(input sdkkonnectcomp.CACertificateInput) bool { - return input.Cert == dummyValidCACertPEM + return input.Cert == deploy.TestValidCACertPEM }), ).Return(&sdkkonnectops.CreateCaCertificateResponse{ CACertificate: &sdkkonnectcomp.CACertificate{ @@ -64,7 +65,7 @@ func TestKongCACertificate(t *testing.T) { w := setupWatch[configurationv1alpha1.KongCACertificateList](t, ctx, cl, client.InNamespace(ns.Name)) t.Log("Creating KongCACertificate") - createdCert := deployKongCACertificateAttachedToCP(t, ctx, clientNamespaced, cp) + createdCert := deploy.KongCACertificateAttachedToCP(t, ctx, clientNamespaced, cp) t.Log("Waiting for KongCACertificate to be programmed") watchFor(t, ctx, w, watch.Modified, func(c *configurationv1alpha1.KongCACertificate) bool { diff --git a/test/envtest/konnect_entities_certificate_test.go b/test/envtest/konnect_entities_certificate_test.go index 9948dce47..4fd327051 100644 --- a/test/envtest/konnect_entities_certificate_test.go +++ b/test/envtest/konnect_entities_certificate_test.go @@ -18,6 +18,7 @@ import ( "github.com/kong/gateway-operator/controller/konnect/conditions" "github.com/kong/gateway-operator/controller/konnect/ops" "github.com/kong/gateway-operator/modules/manager/scheme" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" ) @@ -46,13 +47,13 @@ func TestKongCertificate(t *testing.T) { clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) t.Log("Creating KonnectAPIAuthConfiguration and KonnectGatewayControlPlane") - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) t.Log("Setting up SDK expectations on KongCertificate creation") sdk.CertificatesSDK.EXPECT().CreateCertificate(mock.Anything, cp.GetKonnectStatus().GetKonnectID(), mock.MatchedBy(func(input sdkkonnectcomp.CertificateInput) bool { - return input.Cert == dummyValidCertPEM && input.Key == dummyValidCertKeyPEM + return input.Cert == deploy.TestValidCertPEM && input.Key == deploy.TestValidCertKeyPEM }), ).Return(&sdkkonnectops.CreateCertificateResponse{ Certificate: &sdkkonnectcomp.Certificate{ @@ -64,7 +65,7 @@ func TestKongCertificate(t *testing.T) { w := setupWatch[configurationv1alpha1.KongCertificateList](t, ctx, cl, client.InNamespace(ns.Name)) t.Log("Creating KongCertificate") - createdCert := deployKongCertificateAttachedToCP(t, ctx, clientNamespaced, cp) + createdCert := deploy.KongCertificateAttachedToCP(t, ctx, clientNamespaced, cp) t.Log("Waiting for KongCertificate to be programmed") watchFor(t, ctx, w, watch.Modified, func(c *configurationv1alpha1.KongCertificate) bool { diff --git a/test/envtest/konnect_entities_key_test.go b/test/envtest/konnect_entities_key_test.go index 87996deca..1e0635c8f 100644 --- a/test/envtest/konnect_entities_key_test.go +++ b/test/envtest/konnect_entities_key_test.go @@ -18,6 +18,7 @@ import ( "github.com/kong/gateway-operator/controller/konnect/conditions" "github.com/kong/gateway-operator/controller/konnect/ops" "github.com/kong/gateway-operator/modules/manager/scheme" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" ) @@ -52,8 +53,8 @@ func TestKongKey(t *testing.T) { clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) t.Log("Creating KonnectAPIAuthConfiguration and KonnectGatewayControlPlane") - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) t.Log("Setting up SDK expectations on KongKey creation") sdk.KeysSDK.EXPECT().CreateKey(mock.Anything, cp.GetKonnectStatus().GetKonnectID(), @@ -71,7 +72,7 @@ func TestKongKey(t *testing.T) { w := setupWatch[configurationv1alpha1.KongKeyList](t, ctx, cl, client.InNamespace(ns.Name)) t.Log("Creating KongKey") - createdKey := deployKongKeyAttachedToCP(t, ctx, clientNamespaced, keyKid, keyName, cp) + createdKey := deploy.KongKeyAttachedToCP(t, ctx, clientNamespaced, keyKid, keyName, cp) t.Log("Waiting for KongKey to be programmed") watchFor(t, ctx, w, watch.Modified, func(c *configurationv1alpha1.KongKey) bool { diff --git a/test/envtest/konnect_entities_keyset_test.go b/test/envtest/konnect_entities_keyset_test.go index 8e173941b..1e9c82c66 100644 --- a/test/envtest/konnect_entities_keyset_test.go +++ b/test/envtest/konnect_entities_keyset_test.go @@ -18,6 +18,7 @@ import ( "github.com/kong/gateway-operator/controller/konnect/conditions" "github.com/kong/gateway-operator/controller/konnect/ops" "github.com/kong/gateway-operator/modules/manager/scheme" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" ) @@ -51,8 +52,8 @@ func TestKongKeySet(t *testing.T) { clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) t.Log("Creating KonnectAPIAuthConfiguration and KonnectGatewayControlPlane") - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) t.Log("Setting up SDK expectations on KongKeySet creation") sdk.KeySetsSDK.EXPECT().CreateKeySet(mock.Anything, cp.GetKonnectStatus().GetKonnectID(), @@ -69,7 +70,7 @@ func TestKongKeySet(t *testing.T) { w := setupWatch[configurationv1alpha1.KongKeySetList](t, ctx, cl, client.InNamespace(ns.Name)) t.Log("Creating KongKeySet") - createdKeySet := deployKongKeySetAttachedToCP(t, ctx, clientNamespaced, keySetName, cp) + createdKeySet := deploy.KongKeySetAttachedToCP(t, ctx, clientNamespaced, keySetName, cp) t.Log("Waiting for KongKeySet to be programmed") watchFor(t, ctx, w, watch.Modified, func(c *configurationv1alpha1.KongKeySet) bool { diff --git a/test/envtest/konnect_entities_kongconsumer_test.go b/test/envtest/konnect_entities_kongconsumer_test.go index fe196543f..9b18cdd53 100644 --- a/test/envtest/konnect_entities_kongconsumer_test.go +++ b/test/envtest/konnect_entities_kongconsumer_test.go @@ -18,6 +18,7 @@ import ( "github.com/kong/gateway-operator/controller/konnect/conditions" konnectops "github.com/kong/gateway-operator/controller/konnect/ops" "github.com/kong/gateway-operator/modules/manager/scheme" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" configurationv1beta1 "github.com/kong/kubernetes-configuration/api/configuration/v1beta1" @@ -51,8 +52,8 @@ func TestKongConsumer(t *testing.T) { clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) t.Log("Creating KonnectAPIAuthConfiguration and KonnectGatewayControlPlane") - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) t.Log("Setting up a watch for KongConsumer events") cWatch := setupWatch[configurationv1.KongConsumerList](t, ctx, cl, client.InNamespace(ns.Name)) @@ -90,7 +91,7 @@ func TestKongConsumer(t *testing.T) { }).Return(&sdkkonnectops.ListConsumerGroupsForConsumerResponse{}, nil) t.Log("Creating KongConsumer") - createdConsumer := deployKongConsumerAttachedToCP(t, ctx, clientNamespaced, username, cp) + createdConsumer := deploy.KongConsumerAttachedToCP(t, ctx, clientNamespaced, username, cp) t.Log("Waiting for KongConsumer to be programmed") watchFor(t, ctx, cWatch, watch.Modified, func(c *configurationv1.KongConsumer) bool { @@ -202,10 +203,15 @@ func TestKongConsumer(t *testing.T) { }).Return(&sdkkonnectops.AddConsumerToGroupResponse{}, nil) t.Log("Creating KongConsumerGroup") - createdConsumerGroup := deployKongConsumerGroupAttachedToCP(t, ctx, clientNamespaced, consumerGroupName, cp) + createdConsumerGroup := deploy.KongConsumerGroupAttachedToCP(t, ctx, clientNamespaced, cp, + func(obj client.Object) { + cg := obj.(*configurationv1beta1.KongConsumerGroup) + cg.Spec.Name = consumerGroupName + }, + ) t.Log("Creating KongConsumer and patching it with ConsumerGroup") - createdConsumer := deployKongConsumerAttachedToCP(t, ctx, clientNamespaced, username, cp) + createdConsumer := deploy.KongConsumerAttachedToCP(t, ctx, clientNamespaced, username, cp) consumer := createdConsumer.DeepCopy() consumer.ConsumerGroups = []string{createdConsumerGroup.GetName()} require.NoError(t, clientNamespaced.Patch(ctx, consumer, client.MergeFrom(createdConsumer))) diff --git a/test/envtest/konnect_entities_kongvault_test.go b/test/envtest/konnect_entities_kongvault_test.go index 418d5afd7..f25743552 100644 --- a/test/envtest/konnect_entities_kongvault_test.go +++ b/test/envtest/konnect_entities_kongvault_test.go @@ -18,6 +18,7 @@ import ( "github.com/kong/gateway-operator/controller/konnect/conditions" konnectops "github.com/kong/gateway-operator/controller/konnect/ops" "github.com/kong/gateway-operator/modules/manager/scheme" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" ) @@ -47,8 +48,8 @@ func TestKongVault(t *testing.T) { clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) t.Log("Creating KonnectAPIAuthConfiguration and KonnectGatewayControlPlane") - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) t.Log("Setting up a watch for KongVault events") vaultWatch := setupWatch[configurationv1alpha1.KongVaultList](t, ctx, cl) @@ -72,7 +73,7 @@ func TestKongVault(t *testing.T) { }, }, nil) - vault := deployKongVaultAttachedToCP(t, ctx, cl, vaultBackend, vaultPrefix, []byte(vaultRawConfig), cp) + vault := deploy.KongVaultAttachedToCP(t, ctx, cl, vaultBackend, vaultPrefix, []byte(vaultRawConfig), cp) t.Log("Waiting for KongVault to be programmed") watchFor(t, ctx, vaultWatch, watch.Modified, func(v *configurationv1alpha1.KongVault) bool { diff --git a/test/envtest/konnect_entities_sni_test.go b/test/envtest/konnect_entities_sni_test.go index 526098946..42d2bcf28 100644 --- a/test/envtest/konnect_entities_sni_test.go +++ b/test/envtest/konnect_entities_sni_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" + k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/watch" "sigs.k8s.io/controller-runtime/pkg/client" @@ -18,6 +19,7 @@ import ( "github.com/kong/gateway-operator/controller/konnect/conditions" "github.com/kong/gateway-operator/controller/konnect/ops" "github.com/kong/gateway-operator/modules/manager/scheme" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" konnectalpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1" @@ -47,12 +49,12 @@ func TestKongSNI(t *testing.T) { clientNamespaced := client.NewNamespacedClient(mgr.GetClient(), ns.Name) t.Log("Creating KonnectAPIAuthConfiguration and KonnectGatewayControlPlane") - apiAuth := deployKonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) - cp := deployKonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) + apiAuth := deploy.KonnectAPIAuthConfigurationWithProgrammed(t, ctx, clientNamespaced) + cp := deploy.KonnectGatewayControlPlaneWithID(t, ctx, clientNamespaced, apiAuth) t.Run("adding, patching and deleting KongSNI", func(t *testing.T) { t.Log("Creating KongCertificate and setting it to Programmed") - createdCert := deployKongCertificateAttachedToCP(t, ctx, clientNamespaced, cp) + createdCert := deploy.KongCertificateAttachedToCP(t, ctx, clientNamespaced, cp) createdCert.Status = configurationv1alpha1.KongCertificateStatus{ Konnect: &konnectalpha1.KonnectEntityStatusWithControlPlaneRef{ KonnectEntityStatus: konnectEntityStatus("cert-12345"), @@ -87,11 +89,11 @@ func TestKongSNI(t *testing.T) { }, }, nil) - t.Log("Creating KongSNI") - createdSNI := deploySNIAttachedToCertificate(t, ctx, - clientNamespaced, - "test.kong-sni.example.com", nil, - createdCert, + createdSNI := deploy.KongSNIAttachedToCertificate(t, ctx, clientNamespaced, createdCert, + func(obj client.Object) { + sni := obj.(*configurationv1alpha1.KongSNI) + sni.Spec.KongSNIAPISpec.Name = "test.kong-sni.example.com" + }, ) t.Log("Waiting for SNI to be programmed and get Konnect ID") @@ -135,6 +137,14 @@ func TestKongSNI(t *testing.T) { t.Log("Deleting KongSNI") require.NoError(t, clientNamespaced.Delete(ctx, createdSNI)) + assert.EventuallyWithT(t, func(c *assert.CollectT) { + assert.True(c, k8serrors.IsNotFound( + clientNamespaced.Get(ctx, client.ObjectKeyFromObject(createdSNI), createdSNI), + )) + }, waitTime, tickTime, + "KongSNI was not deleted", + ) + t.Log("Waiting for SNI to be deleted in SDK") assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.SNIsSDK.AssertExpectations(t)) diff --git a/test/helpers/deploy/consts.go b/test/helpers/deploy/consts.go new file mode 100644 index 000000000..44c0407e1 --- /dev/null +++ b/test/helpers/deploy/consts.go @@ -0,0 +1,78 @@ +package deploy + +const ( + // TestValidCACertPEM is a valid CA certificate PEM to be used in tests. + TestValidCACertPEM = `-----BEGIN CERTIFICATE----- +MIIDPTCCAiWgAwIBAgIUcNKAk2icWRJGwZ5QDpdSkkeF5kUwDQYJKoZIhvcNAQEL +BQAwLjELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRIwEAYDVQQKDAlLb25nIElu +Yy4wHhcNMjQwOTE5MDkwODEzWhcNMjkwOTE4MDkwODEzWjAuMQswCQYDVQQGEwJV +UzELMAkGA1UECAwCQ0ExEjAQBgNVBAoMCUtvbmcgSW5jLjCCASIwDQYJKoZIhvcN +AQEBBQADggEPADCCAQoCggEBAMvDhLM0vTw0QXmgE+sB6gvKx2PUWzvd2tRZoamH +h4RAxYRjgJsJe6WEeAk0tjWQqwAq0Y2MQioMCC4X+L13kpdtomI+4PKjBozg+iTd +ThyV0oQSVHHWzayUzcSODnGR524H9YxmkXV5ImrXwbEqXwiUESPVtjnf/ZzWS01v +gtbu4x3YW+z8kRoXOTpJHKcEoI90SU9F4yeuQsCtbJHeJZRqPr6Kz84ZuHsZ2MeU +os4j1GdMaH3dSysqFv6o1hJ2+6bsrE/ONiGtBb4+tyhivgf+u+ixQwqIERlEJzhI +z/csoAAnfMBY401j2NNUgPpwx5sTQdCz5aFDmanol5152M8CAwEAAaNTMFEwHQYD +VR0OBBYEFK2qd3oRF37acVvgfDeLakx66ioTMB8GA1UdIwQYMBaAFK2qd3oRF37a +cVvgfDeLakx66ioTMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEB +AAuul+rAztaueTpPIM63nrS4bSZsIatCgAQ5Pihm0+rZ+13BJk4K2GxkS+T0qkB5 +34+F3eVhUB4cC+kVkWZrlEzD9BsJwWjnoJK+848znTg+ufTeaOQWslYNqFKjmy2k +K6NE7E6r+JLdNvafJzeDybSTXI1tCzDRWUdj5m+bgruX07B13KIJKrAweCTD1927 +WvvfJYxsg8P7dYD9DPlcuOm22ggAaPPu4P/MsnApiq3kJEI/nSGSsboKyjBO2hcz +VF1CYr6Epfyw/47kwuJLCVHjlTgT4haOChW1S8rZILCLXfb8ukM/g3XVYIeEwzsr +KU74cm8lTFCdxlcXePbMdHc= +-----END CERTIFICATE----- +` + // TestValidCertPEM is a valid certificate PEM to be used in tests. + TestValidCertPEM = `-----BEGIN CERTIFICATE----- +MIIDPTCCAiUCFG5IolqRiKPMfzTI8peXlaF6cZODMA0GCSqGSIb3DQEBCwUAMFsx +CzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTEVMBMGA1UEBwwMRGVmYXVsdCBDaXR5 +MRIwEAYDVQQKDAlLb25nIEluYy4xFDASBgNVBAMMC2tvbmdocS50ZWNoMB4XDTI0 +MDkyNTA3MjIzOFoXDTM0MDkyMzA3MjIzOFowWzELMAkGA1UEBhMCVVMxCzAJBgNV +BAgMAkNBMRUwEwYDVQQHDAxEZWZhdWx0IENpdHkxEjAQBgNVBAoMCUtvbmcgSW5j +LjEUMBIGA1UEAwwLa29uZ2hxLnRlY2gwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw +ggEKAoIBAQDXmNBzpWyJ0YUdfCamZpJiwRQMn5vVY8iKQrd3dD03DWyPHu/fXlrL ++QPTRip5d1SrxjzQ4S3fgme442BTlElF9d1w1rhg+DIg6NsW1jd+3IZaICnq7BZH +rJGlW+IWJSKHmNQ39nfVQwgL/QdylrYpbB7uwdEDMa78GfXteiXTcuNobCr7VWVz +rY6rQXo/dImWE1PtMp/EZEMsEbgbQpK5+fUnKTmFncVlDAZ2Q3s2MPikV5UhMVyQ +dKQydU0Ev0LRtpsjW8pQdshMG1ilMq6Yg6YU95gakLVjRXMoDlIJOu08mdped+2Y +VIUSXhRyRt1hbkFP0fXG0THfZ3DjH7jRAgMBAAEwDQYJKoZIhvcNAQELBQADggEB +ANEXlNaQKLrB+jsnNjybsTRkvRRmwjnXaQV0jHzjseGsTJoKY5ABBsSRDiHtqB+9 +LPTpHhLYJWsHSLwawIJ3aWDDpF4MNTRsvO12v7wM8Q42OSgkP23O6a5ESkyHRBAb +dLVEp+0Z3kjYwPIglIK37PcgDci6Zim73GOfapDEASNbnCu8js2g/ucYPPXkGMxl +PSUER7MTNf9wRbXrroCE+tZw4kUyUh+6taNlU4ialAJLO1x6UGVRHvPgEx0fAAxA +seBH+A9QMvVl2cKcvrOgZ0CWY01aFRO9ROQ7PrYXqRFvOZu8K3QzLw7xYoK1DTp+ +kkO/oPy+WIbqEvj7QrhUXpo= +-----END CERTIFICATE----- +` + // TestValidCertKeyPEM is a valid certificate key PEM to be used in tests. + TestValidCertKeyPEM = `-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDXmNBzpWyJ0YUd +fCamZpJiwRQMn5vVY8iKQrd3dD03DWyPHu/fXlrL+QPTRip5d1SrxjzQ4S3fgme4 +42BTlElF9d1w1rhg+DIg6NsW1jd+3IZaICnq7BZHrJGlW+IWJSKHmNQ39nfVQwgL +/QdylrYpbB7uwdEDMa78GfXteiXTcuNobCr7VWVzrY6rQXo/dImWE1PtMp/EZEMs +EbgbQpK5+fUnKTmFncVlDAZ2Q3s2MPikV5UhMVyQdKQydU0Ev0LRtpsjW8pQdshM +G1ilMq6Yg6YU95gakLVjRXMoDlIJOu08mdped+2YVIUSXhRyRt1hbkFP0fXG0THf +Z3DjH7jRAgMBAAECggEAOSZ4h1dgDK5+H2FEK5MAFe6BnpEGsYu4YrIpySAGhBvq +XYwBYRA1eGFjmrM8WiOATeKIR4SRcPC0BwY7CBzESafRkfJRQN86BpBDV2vknRve +/3AMPIplo41CtHdFWMJyQ0iHZOhQPrd8oBTsTvtVgWh4UKkO+05FyO0mzFM3SLPs +pqRwMZjLlKVZhbI1l0Ur787tzWpMQQHmd8csAvlak+GIciQWELbVK+5kr/FDpJbq +joIeHX7DCmIqrD/Okwa8SfJu1sutmRX+nrxkDi7trPYcpqriDoWs2jMcnS2GHq9M +lsy2XHn+qLjCpl3/XU61xenWs+Rmmj6J/oIs1zYXCwKBgQDywRS/MNgwMst703Wh +ERJO0rNSR0XVzzoKOqc/ghPkeA8mVNwfNkbgWks+83tuAb6IunMIeyQJ3g/jRhjz +KImsqJmO+DoZCioeaR3PeIWibi9I9Irg6dtoNMwxSmmOtCKD0rccxM1V9OnYkn5a +0Fb+irQSgJYiHrF2SLAT0NoWEwKBgQDjXGLHCu/WEy49vROdkTia133Wc7m71/D5 +RDUqvSmAEHJyhTlzCbTO+JcNhC6cx3s102GNcVYHlAq3WoE5EV1YykUNJwUg4XPn +AggNkYOiXs6tf+uePmT8MddixFFgFxZ2bIqFhvnY+WqypHuxtwIepqKJjq5xZTiB ++lfp7SziCwKBgAivofdpXwLyfldy7I2T18zcOzBhfn01CgWdrahXFjEhnqEnfizb +u1OBx5l8CtmX1GJ+EWmnRlXYDUd7lZ71v19fNQdpmGKW+4TVDA0Fafqy6Jw6q9F6 +bLBg20GUQQyrI2UGICk2XYaK2ec27rB/Le2zttfGpBiaco0h8rLy0SrjAoGBAM4/ +UY/UOQsOrUTuT2wBf8LfNtUid9uSIZRNrplNrebxhJCkkB/uLyoN0iE9xncMcpW6 +YmVH6c3IGwyHOnBFc1OHcapjukBApL5rVljQpwPVU1GKmHgdi8hHgmajRlqPtx3I +isRkVCPi5kqV8WueY3rgmNOGLnLJasBmE/gt4ihPAoGAG3v93R5tAeSrn7DMHaZt +p+udsNw9mDPYHAzlYtnw1OE/I0ceR5UyCFSzCd00Q8ZYBLf9jRvsO/GUA4F51isE +8/7xyqSxJqDwzv9N8EGkqf/SfMKA3kK3Sc8u+ovhzJu8OxcY+qrpo4+vYWYeW42n +5XBwvWV2ovRMx7Ntw7FUc24= +-----END PRIVATE KEY----- +` +) diff --git a/test/envtest/deploy_resources.go b/test/helpers/deploy/deploy_resources.go similarity index 72% rename from test/envtest/deploy_resources.go rename to test/helpers/deploy/deploy_resources.go index 92ea69b6f..3410146c9 100644 --- a/test/envtest/deploy_resources.go +++ b/test/helpers/deploy/deploy_resources.go @@ -1,9 +1,10 @@ -package envtest +package deploy import ( "context" "testing" + sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components" "github.com/google/uuid" "github.com/samber/lo" "github.com/stretchr/testify/require" @@ -20,6 +21,11 @@ import ( konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1" ) +const ( + // TestIDLabel is the label key used to identify resources created by the test suite. + TestIDLabel = "konghq.com/test-id" +) + type objOption func(obj client.Object) // WithAnnotation returns an objOption that sets the given key-value pair as an annotation on the object. @@ -34,12 +40,36 @@ func WithAnnotation(key, value string) objOption { } } -// deployKonnectAPIAuthConfiguration deploys a KonnectAPIAuthConfiguration resource +// WithTestIDLabel returns an objOption that sets the test ID label on the object. +func WithTestIDLabel(testID string) func(obj client.Object) { + return func(obj client.Object) { + labels := obj.GetLabels() + if labels == nil { + labels = map[string]string{} + } + labels[TestIDLabel] = testID + obj.SetLabels(labels) + } +} + +// WithLabels returns an objOption that sets the given key-value pairs as labels on the object. +func WithLabels[ + T client.Object, +](labels map[string]string) func(obj T) { + return func(obj T) { + for k, v := range labels { + obj.GetLabels()[k] = v + } + } +} + +// KonnectAPIAuthConfiguration deploys a KonnectAPIAuthConfiguration resource // and returns the resource. -func deployKonnectAPIAuthConfiguration( +func KonnectAPIAuthConfiguration( t *testing.T, ctx context.Context, cl client.Client, + opts ...objOption, ) *konnectv1alpha1.KonnectAPIAuthConfiguration { t.Helper() @@ -53,25 +83,28 @@ func deployKonnectAPIAuthConfiguration( ServerURL: "https://api.us.konghq.com", }, } + for _, opt := range opts { + opt(apiAuth) + } require.NoError(t, cl.Create(ctx, apiAuth)) t.Logf("deployed new %s KonnectAPIAuthConfiguration", client.ObjectKeyFromObject(apiAuth)) return apiAuth } -// deployKonnectAPIAuthConfigurationWithProgrammed deploys a KonnectAPIAuthConfiguration +// KonnectAPIAuthConfigurationWithProgrammed deploys a KonnectAPIAuthConfiguration // resource and returns the resource. // The Programmed condition is set on the returned resource using status Update() call. // It can be useful where the reconciler for KonnectAPIAuthConfiguration is not started // and hence the status has to be filled manually. -func deployKonnectAPIAuthConfigurationWithProgrammed( +func KonnectAPIAuthConfigurationWithProgrammed( t *testing.T, ctx context.Context, cl client.Client, ) *konnectv1alpha1.KonnectAPIAuthConfiguration { t.Helper() - apiAuth := deployKonnectAPIAuthConfiguration(t, ctx, cl) + apiAuth := KonnectAPIAuthConfiguration(t, ctx, cl) apiAuth.Status.Conditions = []metav1.Condition{ { Type: conditions.KonnectEntityAPIAuthConfigurationValidConditionType, @@ -85,18 +118,20 @@ func deployKonnectAPIAuthConfigurationWithProgrammed( return apiAuth } -// deployKonnectGatewayControlPlane deploys a KonnectGatewayControlPlane resource and returns the resource. -func deployKonnectGatewayControlPlane( +// KonnectGatewayControlPlane deploys a KonnectGatewayControlPlane resource and returns the resource. +func KonnectGatewayControlPlane( t *testing.T, ctx context.Context, cl client.Client, apiAuth *konnectv1alpha1.KonnectAPIAuthConfiguration, + opts ...objOption, ) *konnectv1alpha1.KonnectGatewayControlPlane { t.Helper() + name := "cp-" + uuid.NewString()[:8] cp := &konnectv1alpha1.KonnectGatewayControlPlane{ ObjectMeta: metav1.ObjectMeta{ - GenerateName: "cp-", + Name: name, }, Spec: konnectv1alpha1.KonnectGatewayControlPlaneSpec{ KonnectConfiguration: konnectv1alpha1.KonnectConfiguration{ @@ -104,19 +139,25 @@ func deployKonnectGatewayControlPlane( Name: apiAuth.Name, }, }, + CreateControlPlaneRequest: sdkkonnectcomp.CreateControlPlaneRequest{ + Name: name, + }, }, } + for _, opt := range opts { + opt(cp) + } require.NoError(t, cl.Create(ctx, cp)) t.Logf("deployed new %s KonnectGatewayControlPlane", client.ObjectKeyFromObject(cp)) return cp } -// deployKonnectGatewayControlPlaneWithID deploys a KonnectGatewayControlPlane resource and returns the resource. +// deploy.KonnectGatewayControlPlaneWithID deploys a KonnectGatewayControlPlane resource and returns the resource. // The Status ID and Programmed condition are set on the CP using status Update() call. // It can be useful where the reconciler for KonnectGatewayControlPlane is not started // and hence the status has to be filled manually. -func deployKonnectGatewayControlPlaneWithID( +func KonnectGatewayControlPlaneWithID( t *testing.T, ctx context.Context, cl client.Client, @@ -124,7 +165,7 @@ func deployKonnectGatewayControlPlaneWithID( ) *konnectv1alpha1.KonnectGatewayControlPlane { t.Helper() - cp := deployKonnectGatewayControlPlane(t, ctx, cl, apiAuth) + cp := KonnectGatewayControlPlane(t, ctx, cl, apiAuth) cp.Status.Conditions = []metav1.Condition{ { Type: conditions.KonnectEntityProgrammedConditionType, @@ -139,8 +180,8 @@ func deployKonnectGatewayControlPlaneWithID( return cp } -// deployKongServiceAttachedToCP deploys a KongService resource and returns the resource. -func deployKongServiceAttachedToCP( +// KongServiceAttachedToCP deploys a KongService resource and returns the resource. +func KongServiceAttachedToCP( t *testing.T, ctx context.Context, cl client.Client, @@ -157,6 +198,8 @@ func deployKongServiceAttachedToCP( Spec: configurationv1alpha1.KongServiceSpec{ KongServiceAPISpec: configurationv1alpha1.KongServiceAPISpec{ Name: lo.ToPtr(name), + URL: lo.ToPtr("http://example.com"), + Host: "example.com", }, ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, @@ -176,8 +219,8 @@ func deployKongServiceAttachedToCP( return &kongService } -// deployKongRouteAttachedToService deploys a KongRoute resource and returns the resource. -func deployKongRouteAttachedToService( +// KongRouteAttachedToService deploys a KongRoute resource and returns the resource. +func KongRouteAttachedToService( t *testing.T, ctx context.Context, cl client.Client, @@ -212,8 +255,8 @@ func deployKongRouteAttachedToService( return &kongRoute } -// deployKongConsumerWithProgrammed deploys a KongConsumer resource and returns the resource. -func deployKongConsumerWithProgrammed( +// KongConsumerWithProgrammed deploys a KongConsumer resource and returns the resource. +func KongConsumerWithProgrammed( t *testing.T, ctx context.Context, cl client.Client, @@ -239,24 +282,28 @@ func deployKongConsumerWithProgrammed( return consumer } -// deployKongPluginBinding deploys a KongPluginBinding resource and returns the resource. -func deployKongPluginBinding( +// KongPluginBinding deploys a KongPluginBinding resource and returns the resource. +func KongPluginBinding( t *testing.T, ctx context.Context, cl client.Client, kpb *configurationv1alpha1.KongPluginBinding, + opts ...objOption, ) *configurationv1alpha1.KongPluginBinding { t.Helper() kpb.GenerateName = "kongpluginbinding-" + for _, opt := range opts { + opt(kpb) + } require.NoError(t, cl.Create(ctx, kpb)) t.Logf("deployed new unmanaged KongPluginBinding %s", client.ObjectKeyFromObject(kpb)) return kpb } -// deployKongCredentialBasicAuth deploys a KongCredentialBasicAuth resource and returns the resource. -func deployKongCredentialBasicAuth( +// KongCredentialBasicAuth deploys a KongCredentialBasicAuth resource and returns the resource. +func KongCredentialBasicAuth( t *testing.T, ctx context.Context, cl client.Client, @@ -287,8 +334,8 @@ func deployKongCredentialBasicAuth( return c } -// deployKongCredentialACL deploys a KongCredentialACL resource and returns the resource. -func deployKongCredentialACL( +// KongCredentialACL deploys a KongCredentialACL resource and returns the resource. +func KongCredentialACL( t *testing.T, ctx context.Context, cl client.Client, @@ -317,8 +364,8 @@ func deployKongCredentialACL( return c } -// deployKongCACertificateAttachedToCP deploys a KongCACertificate resource attached to a CP and returns the resource. -func deployKongCACertificateAttachedToCP( +// KongCACertificateAttachedToCP deploys a KongCACertificate resource attached to a CP and returns the resource. +func KongCACertificateAttachedToCP( t *testing.T, ctx context.Context, cl client.Client, @@ -338,7 +385,7 @@ func deployKongCACertificateAttachedToCP( }, }, KongCACertificateAPISpec: configurationv1alpha1.KongCACertificateAPISpec{ - Cert: dummyValidCACertPEM, + Cert: TestValidCACertPEM, }, }, } @@ -348,8 +395,8 @@ func deployKongCACertificateAttachedToCP( return cert } -// deployKongCertificateAttachedToCP deploys a KongCertificate resource attached to a CP and returns the resource. -func deployKongCertificateAttachedToCP( +// KongCertificateAttachedToCP deploys a KongCertificate resource attached to a CP and returns the resource. +func KongCertificateAttachedToCP( t *testing.T, ctx context.Context, cl client.Client, @@ -369,8 +416,8 @@ func deployKongCertificateAttachedToCP( }, }, KongCertificateAPISpec: configurationv1alpha1.KongCertificateAPISpec{ - Cert: dummyValidCertPEM, - Key: dummyValidCertKeyPEM, + Cert: TestValidCertPEM, + Key: TestValidCertKeyPEM, }, }, } @@ -380,13 +427,77 @@ func deployKongCertificateAttachedToCP( return cert } -// deployKongConsumer deploys a KongConsumer resource attached to a Control Plane and returns the resource. -func deployKongConsumerAttachedToCP( +// KongUpstreamAttachedToCP deploys a KongUpstream resource attached to a Control Plane and returns the resource. +func KongUpstreamAttachedToCP( + t *testing.T, + ctx context.Context, + cl client.Client, + cp *konnectv1alpha1.KonnectGatewayControlPlane, + opts ...objOption, +) *configurationv1alpha1.KongUpstream { + t.Helper() + + u := &configurationv1alpha1.KongUpstream{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: "upstream-", + }, + Spec: configurationv1alpha1.KongUpstreamSpec{ + ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ + Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, + KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ + Name: cp.Name, + }, + }, + }, + } + for _, opt := range opts { + opt(u) + } + + require.NoError(t, cl.Create(ctx, u)) + t.Logf("deployed new KongUpstream %s", client.ObjectKeyFromObject(u)) + + return u +} + +// KongTargetAttachedToUpstream deploys a KongTarget resource attached to a Control Plane and returns the resource. +func KongTargetAttachedToUpstream( + t *testing.T, + ctx context.Context, + cl client.Client, + upstream *configurationv1alpha1.KongUpstream, + opts ...objOption, +) *configurationv1alpha1.KongTarget { + t.Helper() + + u := &configurationv1alpha1.KongTarget{ + ObjectMeta: metav1.ObjectMeta{ + GenerateName: "upstream-", + }, + Spec: configurationv1alpha1.KongTargetSpec{ + UpstreamRef: configurationv1alpha1.TargetRef{ + Name: upstream.Name, + }, + }, + } + for _, opt := range opts { + opt(u) + } + + require.NoError(t, cl.Create(ctx, u)) + t.Logf("deployed new KongTarget %s", client.ObjectKeyFromObject(u)) + + return u +} + +// KongConsumerAttachedToCP deploys a KongConsumer resource attached to a Control Plane and returns the resource. +func KongConsumerAttachedToCP( t *testing.T, ctx context.Context, cl client.Client, username string, cp *konnectv1alpha1.KonnectGatewayControlPlane, + opts ...objOption, ) *configurationv1.KongConsumer { t.Helper() @@ -404,6 +515,9 @@ func deployKongConsumerAttachedToCP( }, Username: username, } + for _, opt := range opts { + opt(c) + } require.NoError(t, cl.Create(ctx, c)) t.Logf("deployed new KongConsumer %s", client.ObjectKeyFromObject(c)) @@ -411,19 +525,20 @@ func deployKongConsumerAttachedToCP( return c } -// deployKongConsumerGroupAttachedToCP deploys a KongConsumerGroup resource attached to a Control Plane and returns the resource. -func deployKongConsumerGroupAttachedToCP( +// KongConsumerGroupAttachedToCP deploys a KongConsumerGroup resource attached to a Control Plane and returns the resource. +func KongConsumerGroupAttachedToCP( t *testing.T, ctx context.Context, cl client.Client, - cgName string, cp *konnectv1alpha1.KonnectGatewayControlPlane, + opts ...objOption, ) *configurationv1beta1.KongConsumerGroup { t.Helper() + name := "consumer-group-" + uuid.NewString()[:8] cg := configurationv1beta1.KongConsumerGroup{ ObjectMeta: metav1.ObjectMeta{ - GenerateName: "consumer-group-", + Name: name, }, Spec: configurationv1beta1.KongConsumerGroupSpec{ ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ @@ -432,9 +547,12 @@ func deployKongConsumerGroupAttachedToCP( Name: cp.Name, }, }, - Name: cgName, + Name: name, }, } + for _, opt := range opts { + opt(&cg) + } require.NoError(t, cl.Create(ctx, &cg)) t.Logf("deployed new KongConsumerGroup %s", client.ObjectKeyFromObject(&cg)) @@ -442,7 +560,8 @@ func deployKongConsumerGroupAttachedToCP( return &cg } -func deployKongVaultAttachedToCP( +// KongVaultAttachedToCP deploys a KongVault resource attached to a CP and returns the resource. +func KongVaultAttachedToCP( t *testing.T, ctx context.Context, cl client.Client, @@ -479,8 +598,8 @@ func deployKongVaultAttachedToCP( return vault } -// deployKongKeyAttachedToCP deploys a KongKey resource attached to a CP and returns the resource. -func deployKongKeyAttachedToCP( +// KongKeyAttachedToCP deploys a KongKey resource attached to a CP and returns the resource. +func KongKeyAttachedToCP( t *testing.T, ctx context.Context, cl client.Client, @@ -512,9 +631,9 @@ func deployKongKeyAttachedToCP( return key } -// deployProxyCachePlugin deploys the proxy-cache KongPlugin resource and returns the resource. +// ProxyCachePlugin deploys the proxy-cache KongPlugin resource and returns the resource. // The provided client should be namespaced, i.e. created with `client.NewNamespacedClient(client, ns)` -func deployProxyCachePlugin( +func ProxyCachePlugin( t *testing.T, ctx context.Context, cl client.Client, @@ -535,8 +654,8 @@ func deployProxyCachePlugin( return plugin } -// deployKongKeySetAttachedToCP deploys a KongKeySet resource attached to a CP and returns the resource. -func deployKongKeySetAttachedToCP( +// KongKeySetAttachedToCP deploys a KongKeySet resource attached to a CP and returns the resource. +func KongKeySetAttachedToCP( t *testing.T, ctx context.Context, cl client.Client, @@ -567,18 +686,20 @@ func deployKongKeySetAttachedToCP( return keySet } -func deploySNIAttachedToCertificate( +// KongSNIAttachedToCertificate deploys a KongSNI resource attached to a KongCertificate and returns the resource. +func KongSNIAttachedToCertificate( t *testing.T, ctx context.Context, cl client.Client, - name string, tags []string, cert *configurationv1alpha1.KongCertificate, + opts ...objOption, ) *configurationv1alpha1.KongSNI { t.Helper() + name := "sni-" + uuid.NewString()[:8] sni := &configurationv1alpha1.KongSNI{ ObjectMeta: metav1.ObjectMeta{ - GenerateName: "sni-", + Name: name, }, Spec: configurationv1alpha1.KongSNISpec{ CertificateRef: configurationv1alpha1.KongObjectRef{ @@ -586,10 +707,14 @@ func deploySNIAttachedToCertificate( }, KongSNIAPISpec: configurationv1alpha1.KongSNIAPISpec{ Name: name, - Tags: tags, }, }, } + + for _, opt := range opts { + opt(sni) + } + require.NoError(t, cl.Create(ctx, sni)) t.Logf("deployed KongSNI %s/%s", sni.Namespace, sni.Name) return sni diff --git a/test/integration/test_konnect_entities.go b/test/integration/test_konnect_entities.go index 4efb4884d..f7d41ec35 100644 --- a/test/integration/test_konnect_entities.go +++ b/test/integration/test_konnect_entities.go @@ -9,78 +9,23 @@ import ( "github.com/samber/lo" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/kong/gateway-operator/controller/konnect" "github.com/kong/gateway-operator/controller/konnect/conditions" testutils "github.com/kong/gateway-operator/pkg/utils/test" "github.com/kong/gateway-operator/test" "github.com/kong/gateway-operator/test/helpers" + "github.com/kong/gateway-operator/test/helpers/deploy" configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1" configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" - configurationv1beta1 "github.com/kong/kubernetes-configuration/api/configuration/v1beta1" konnectv1alpha1 "github.com/kong/kubernetes-configuration/api/konnect/v1alpha1" ) -const ( - // dummyValidCertPEM is a dummy valid certificate PEM to be used in tests. - dummyValidCertPEM = `-----BEGIN CERTIFICATE----- -MIIDPTCCAiUCFG5IolqRiKPMfzTI8peXlaF6cZODMA0GCSqGSIb3DQEBCwUAMFsx -CzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTEVMBMGA1UEBwwMRGVmYXVsdCBDaXR5 -MRIwEAYDVQQKDAlLb25nIEluYy4xFDASBgNVBAMMC2tvbmdocS50ZWNoMB4XDTI0 -MDkyNTA3MjIzOFoXDTM0MDkyMzA3MjIzOFowWzELMAkGA1UEBhMCVVMxCzAJBgNV -BAgMAkNBMRUwEwYDVQQHDAxEZWZhdWx0IENpdHkxEjAQBgNVBAoMCUtvbmcgSW5j -LjEUMBIGA1UEAwwLa29uZ2hxLnRlY2gwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw -ggEKAoIBAQDXmNBzpWyJ0YUdfCamZpJiwRQMn5vVY8iKQrd3dD03DWyPHu/fXlrL -+QPTRip5d1SrxjzQ4S3fgme442BTlElF9d1w1rhg+DIg6NsW1jd+3IZaICnq7BZH -rJGlW+IWJSKHmNQ39nfVQwgL/QdylrYpbB7uwdEDMa78GfXteiXTcuNobCr7VWVz -rY6rQXo/dImWE1PtMp/EZEMsEbgbQpK5+fUnKTmFncVlDAZ2Q3s2MPikV5UhMVyQ -dKQydU0Ev0LRtpsjW8pQdshMG1ilMq6Yg6YU95gakLVjRXMoDlIJOu08mdped+2Y -VIUSXhRyRt1hbkFP0fXG0THfZ3DjH7jRAgMBAAEwDQYJKoZIhvcNAQELBQADggEB -ANEXlNaQKLrB+jsnNjybsTRkvRRmwjnXaQV0jHzjseGsTJoKY5ABBsSRDiHtqB+9 -LPTpHhLYJWsHSLwawIJ3aWDDpF4MNTRsvO12v7wM8Q42OSgkP23O6a5ESkyHRBAb -dLVEp+0Z3kjYwPIglIK37PcgDci6Zim73GOfapDEASNbnCu8js2g/ucYPPXkGMxl -PSUER7MTNf9wRbXrroCE+tZw4kUyUh+6taNlU4ialAJLO1x6UGVRHvPgEx0fAAxA -seBH+A9QMvVl2cKcvrOgZ0CWY01aFRO9ROQ7PrYXqRFvOZu8K3QzLw7xYoK1DTp+ -kkO/oPy+WIbqEvj7QrhUXpo= ------END CERTIFICATE----- -` - // dummyValidCertKeyPEM is a dummy valid certificate key PEM to be used in tests. - dummyValidCertKeyPEM = `-----BEGIN PRIVATE KEY----- -MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDXmNBzpWyJ0YUd -fCamZpJiwRQMn5vVY8iKQrd3dD03DWyPHu/fXlrL+QPTRip5d1SrxjzQ4S3fgme4 -42BTlElF9d1w1rhg+DIg6NsW1jd+3IZaICnq7BZHrJGlW+IWJSKHmNQ39nfVQwgL -/QdylrYpbB7uwdEDMa78GfXteiXTcuNobCr7VWVzrY6rQXo/dImWE1PtMp/EZEMs -EbgbQpK5+fUnKTmFncVlDAZ2Q3s2MPikV5UhMVyQdKQydU0Ev0LRtpsjW8pQdshM -G1ilMq6Yg6YU95gakLVjRXMoDlIJOu08mdped+2YVIUSXhRyRt1hbkFP0fXG0THf -Z3DjH7jRAgMBAAECggEAOSZ4h1dgDK5+H2FEK5MAFe6BnpEGsYu4YrIpySAGhBvq -XYwBYRA1eGFjmrM8WiOATeKIR4SRcPC0BwY7CBzESafRkfJRQN86BpBDV2vknRve -/3AMPIplo41CtHdFWMJyQ0iHZOhQPrd8oBTsTvtVgWh4UKkO+05FyO0mzFM3SLPs -pqRwMZjLlKVZhbI1l0Ur787tzWpMQQHmd8csAvlak+GIciQWELbVK+5kr/FDpJbq -joIeHX7DCmIqrD/Okwa8SfJu1sutmRX+nrxkDi7trPYcpqriDoWs2jMcnS2GHq9M -lsy2XHn+qLjCpl3/XU61xenWs+Rmmj6J/oIs1zYXCwKBgQDywRS/MNgwMst703Wh -ERJO0rNSR0XVzzoKOqc/ghPkeA8mVNwfNkbgWks+83tuAb6IunMIeyQJ3g/jRhjz -KImsqJmO+DoZCioeaR3PeIWibi9I9Irg6dtoNMwxSmmOtCKD0rccxM1V9OnYkn5a -0Fb+irQSgJYiHrF2SLAT0NoWEwKBgQDjXGLHCu/WEy49vROdkTia133Wc7m71/D5 -RDUqvSmAEHJyhTlzCbTO+JcNhC6cx3s102GNcVYHlAq3WoE5EV1YykUNJwUg4XPn -AggNkYOiXs6tf+uePmT8MddixFFgFxZ2bIqFhvnY+WqypHuxtwIepqKJjq5xZTiB -+lfp7SziCwKBgAivofdpXwLyfldy7I2T18zcOzBhfn01CgWdrahXFjEhnqEnfizb -u1OBx5l8CtmX1GJ+EWmnRlXYDUd7lZ71v19fNQdpmGKW+4TVDA0Fafqy6Jw6q9F6 -bLBg20GUQQyrI2UGICk2XYaK2ec27rB/Le2zttfGpBiaco0h8rLy0SrjAoGBAM4/ -UY/UOQsOrUTuT2wBf8LfNtUid9uSIZRNrplNrebxhJCkkB/uLyoN0iE9xncMcpW6 -YmVH6c3IGwyHOnBFc1OHcapjukBApL5rVljQpwPVU1GKmHgdi8hHgmajRlqPtx3I -isRkVCPi5kqV8WueY3rgmNOGLnLJasBmE/gt4ihPAoGAG3v93R5tAeSrn7DMHaZt -p+udsNw9mDPYHAzlYtnw1OE/I0ceR5UyCFSzCd00Q8ZYBLf9jRvsO/GUA4F51isE -8/7xyqSxJqDwzv9N8EGkqf/SfMKA3kK3Sc8u+ovhzJu8OxcY+qrpo4+vYWYeW42n -5XBwvWV2ovRMx7Ntw7FUc24= ------END PRIVATE KEY----- -` -) - func TestKonnectEntities(t *testing.T) { // A cleaner is created underneath anyway, and a whole namespace is deleted eventually. // We can't use a cleaner to delete objects because it handles deletes in FIFO order and that won't work in this @@ -97,397 +42,188 @@ func TestKonnectEntities(t *testing.T) { testID := uuid.NewString()[:8] t.Logf("Running Konnect entities test with ID: %s", testID) - t.Logf("Creating KonnectAPIAuthConfiguration") - authCfg := &konnectv1alpha1.KonnectAPIAuthConfiguration{ - ObjectMeta: metav1.ObjectMeta{ - Name: "auth-" + testID, - Namespace: ns.Name, - }, - Spec: konnectv1alpha1.KonnectAPIAuthConfigurationSpec{ - Type: konnectv1alpha1.KonnectAPIAuthTypeToken, - Token: test.KonnectAccessToken(), - ServerURL: test.KonnectServerURL(), - }, - } - err := GetClients().MgrClient.Create(GetCtx(), authCfg) - require.NoError(t, err) - - cpName := "cp-" + testID - t.Logf("Creating KonnectGatewayControlPlane %s", cpName) - cp := &konnectv1alpha1.KonnectGatewayControlPlane{ - ObjectMeta: metav1.ObjectMeta{ - Name: cpName, - Namespace: ns.Name, - }, - Spec: konnectv1alpha1.KonnectGatewayControlPlaneSpec{ - CreateControlPlaneRequest: sdkkonnectcomp.CreateControlPlaneRequest{ - Name: cpName, - ClusterType: lo.ToPtr(sdkkonnectcomp.ClusterTypeClusterTypeControlPlane), - Labels: map[string]string{"test_id": testID}, - }, - KonnectConfiguration: konnectv1alpha1.KonnectConfiguration{ - APIAuthConfigurationRef: konnectv1alpha1.KonnectAPIAuthConfigurationRef{ - Name: authCfg.Name, - }, - }, + clientNamespaced := client.NewNamespacedClient(GetClients().MgrClient, ns.Name) + + authCfg := deploy.KonnectAPIAuthConfiguration(t, GetCtx(), clientNamespaced, + deploy.WithTestIDLabel(testID), + func(obj client.Object) { + authCfg := obj.(*konnectv1alpha1.KonnectAPIAuthConfiguration) + authCfg.Spec.Type = konnectv1alpha1.KonnectAPIAuthTypeToken + authCfg.Spec.Token = test.KonnectAccessToken() + authCfg.Spec.ServerURL = test.KonnectServerURL() }, - } - err = GetClients().MgrClient.Create(GetCtx(), cp) - require.NoError(t, err) + ) + + cp := deploy.KonnectGatewayControlPlane(t, GetCtx(), clientNamespaced, authCfg, + deploy.WithTestIDLabel(testID), + ) + t.Cleanup(deleteObjectAndWaitForDeletionFn(t, cp)) t.Logf("Waiting for Konnect ID to be assigned to ControlPlane %s/%s", cp.Namespace, cp.Name) require.EventuallyWithT(t, func(t *assert.CollectT) { err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: cp.Name, Namespace: cp.Namespace}, cp) require.NoError(t, err) - assertKonnectEntityProgrammed(t, cp.GetConditions(), cp.GetKonnectStatus()) + assertKonnectEntityProgrammed(t, cp) }, testutils.ObjectUpdateTimeout, time.Second) - t.Logf("Creating KongService") - ksName := "ks-" + testID - ks := &configurationv1alpha1.KongService{ - ObjectMeta: metav1.ObjectMeta{ - Name: "ks-" + testID, - Namespace: ns.Name, - }, - Spec: configurationv1alpha1.KongServiceSpec{ - ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ - Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, - KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{Name: cp.Name}, - }, - KongServiceAPISpec: configurationv1alpha1.KongServiceAPISpec{ - Name: lo.ToPtr(ksName), - URL: lo.ToPtr("http://example.com"), - Host: "example.com", - }, - }, - } - err = GetClients().MgrClient.Create(GetCtx(), ks) - require.NoError(t, err) + ks := deploy.KongServiceAttachedToCP(t, ctx, clientNamespaced, cp, + deploy.WithTestIDLabel(testID), + ) t.Logf("Waiting for KongService to be updated with Konnect ID") require.EventuallyWithT(t, func(t *assert.CollectT) { err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: ks.Name, Namespace: ks.Namespace}, ks) require.NoError(t, err) - assertKonnectEntityProgrammed(t, ks.GetConditions(), ks.GetKonnectStatus()) + assertKonnectEntityProgrammed(t, ks) }, testutils.ObjectUpdateTimeout, time.Second) - t.Logf("Creating KongRoute") - krName := "kr-" + testID - kr := configurationv1alpha1.KongRoute{ - ObjectMeta: metav1.ObjectMeta{ - Name: krName, - Namespace: ns.Name, + kr := deploy.KongRouteAttachedToService(t, ctx, clientNamespaced, ks, + deploy.WithTestIDLabel(testID), + func(obj client.Object) { + kr := obj.(*configurationv1alpha1.KongRoute) + kr.Spec.KongRouteAPISpec.Paths = []string{"/kr-" + testID} }, - Spec: configurationv1alpha1.KongRouteSpec{ - ServiceRef: &configurationv1alpha1.ServiceRef{ - Type: configurationv1alpha1.ServiceRefNamespacedRef, - NamespacedRef: &configurationv1alpha1.NamespacedServiceRef{ - Name: ks.Name, - }, - }, - KongRouteAPISpec: configurationv1alpha1.KongRouteAPISpec{ - Name: lo.ToPtr(krName), - Paths: []string{"/kr-" + testID}, - }, - }, - } - err = GetClients().MgrClient.Create(GetCtx(), &kr) - require.NoError(t, err) - t.Cleanup(deleteObjectAndWaitForDeletionFn(t, &kr)) + ) + t.Cleanup(deleteObjectAndWaitForDeletionFn(t, kr)) t.Logf("Waiting for KongRoute to be updated with Konnect ID") require.EventuallyWithT(t, func(t *assert.CollectT) { - err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kr.Name, Namespace: kr.Namespace}, &kr) + err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kr.Name, Namespace: kr.Namespace}, kr) require.NoError(t, err) - assertKonnectEntityProgrammed(t, kr.GetConditions(), kr.GetKonnectStatus()) + + assertKonnectEntityProgrammed(t, kr) }, testutils.ObjectUpdateTimeout, time.Second) - t.Logf("Creating KongConsumerGroup") - kcgName := "kcg-" + testID - kcg := configurationv1beta1.KongConsumerGroup{ - ObjectMeta: metav1.ObjectMeta{ - Name: kcgName, - Namespace: ns.Name, - }, - Spec: configurationv1beta1.KongConsumerGroupSpec{ - Name: kcgName, - ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ - Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, - KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ - Name: cp.Name, - }, - }, - }, - } - err = GetClients().MgrClient.Create(GetCtx(), &kcg) - require.NoError(t, err) + kcg := deploy.KongConsumerGroupAttachedToCP(t, ctx, clientNamespaced, cp, + deploy.WithTestIDLabel(testID), + ) t.Logf("Waiting for KongConsumerGroup to be updated with Konnect ID") require.EventuallyWithT(t, func(t *assert.CollectT) { - err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kcg.Name, Namespace: ns.Name}, &kcg) + err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kcg.Name, Namespace: ns.Name}, kcg) require.NoError(t, err) - assertKonnectEntityProgrammed(t, kcg.GetConditions(), kcg.GetKonnectStatus()) + + assertKonnectEntityProgrammed(t, kcg) }, testutils.ObjectUpdateTimeout, time.Second) - t.Logf("Creating KongConsumer") - kcName := "kc-" + testID - kc := configurationv1.KongConsumer{ - ObjectMeta: metav1.ObjectMeta{ - Name: kcName, - Namespace: ns.Name, - }, - Username: kcName, - ConsumerGroups: []string{ - kcg.Name, - }, - Spec: configurationv1.KongConsumerSpec{ - ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ + kc := deploy.KongConsumerAttachedToCP(t, ctx, clientNamespaced, "kc-"+testID, cp, + deploy.WithTestIDLabel(testID), + func(obj client.Object) { + kc := obj.(*configurationv1.KongConsumer) + kc.ConsumerGroups = []string{kcg.Name} + kc.Spec.ControlPlaneRef = &configurationv1alpha1.ControlPlaneRef{ Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{Name: cp.Name}, - }, + } }, - } - require.NoError(t, GetClients().MgrClient.Create(GetCtx(), &kc)) + ) t.Logf("Waiting for KongConsumer to be updated with Konnect ID and Programmed") require.EventuallyWithT(t, func(t *assert.CollectT) { - err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kc.Name, Namespace: ns.Name}, &kc) + err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kc.Name, Namespace: ns.Name}, kc) require.NoError(t, err) - assertKonnectEntityProgrammed(t, kc.GetConditions(), kc.GetKonnectStatus()) + + assertKonnectEntityProgrammed(t, kc) }, testutils.ObjectUpdateTimeout, time.Second) - t.Logf("Creating KongPlugin and KongPluginBinding") - kpName := "kp-" + testID - kp := configurationv1.KongPlugin{ - ObjectMeta: metav1.ObjectMeta{ - Name: kpName, - Namespace: ns.Name, - }, - PluginName: "key-auth", - } - err = GetClients().MgrClient.Create(GetCtx(), &kp) - require.NoError(t, err) - - kpbName := "kpb-" + testID - kpb := configurationv1alpha1.KongPluginBinding{ - ObjectMeta: metav1.ObjectMeta{ - Name: kpbName, - Namespace: ns.Name, - }, - Spec: configurationv1alpha1.KongPluginBindingSpec{ - PluginReference: configurationv1alpha1.PluginRef{ - Name: kp.Name, - Kind: lo.ToPtr("KongPlugin"), - }, - Targets: configurationv1alpha1.KongPluginBindingTargets{ - ServiceReference: &configurationv1alpha1.TargetRefWithGroupKind{ - Name: ks.Name, - Kind: "KongService", - Group: "configuration.konghq.com", - }, - }, - ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ - Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, - KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ - Name: cp.Name, - }, - }, - }, - } - err = GetClients().MgrClient.Create(GetCtx(), &kpb) - require.NoError(t, err) + kp := deploy.ProxyCachePlugin(t, ctx, clientNamespaced) + kpb := deploy.KongPluginBinding(t, ctx, clientNamespaced, + konnect.NewKongPluginBindingBuilder(). + WithServiceTarget(ks.Name). + WithPluginRef(kp.Name). + WithControlPlaneRefKonnectNamespaced(cp.Name). + Build(), + deploy.WithTestIDLabel(testID), + ) t.Logf("Waiting for KongPluginBinding to be updated with Konnect ID") require.EventuallyWithT(t, func(t *assert.CollectT) { - err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kpb.Name, Namespace: ns.Name}, &kpb) + err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kpb.Name, Namespace: ns.Name}, kpb) require.NoError(t, err) - assertKonnectEntityProgrammed(t, kpb.GetConditions(), kpb.GetKonnectStatus()) + + assertKonnectEntityProgrammed(t, kpb) }, testutils.ObjectUpdateTimeout, time.Second) - t.Log("Creating KongUpstream") - kupName := "kup-" + testID - kup := &configurationv1alpha1.KongUpstream{ - ObjectMeta: metav1.ObjectMeta{ - Name: kupName, - Namespace: ns.Name, + kup := deploy.KongUpstreamAttachedToCP(t, ctx, clientNamespaced, cp, + deploy.WithTestIDLabel(testID), + func(obj client.Object) { + kup := obj.(*configurationv1alpha1.KongUpstream) + kup.Spec.KongUpstreamAPISpec.Name = ks.Spec.Host + kup.Spec.KongUpstreamAPISpec.Slots = lo.ToPtr(int64(16384)) + kup.Spec.KongUpstreamAPISpec.Algorithm = sdkkonnectcomp.UpstreamAlgorithmConsistentHashing.ToPointer() }, - Spec: configurationv1alpha1.KongUpstreamSpec{ - ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ - Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, - KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ - Name: cp.Name, - }, - }, - KongUpstreamAPISpec: configurationv1alpha1.KongUpstreamAPISpec{ - Name: ks.Spec.Host, - Slots: lo.ToPtr(int64(16384)), - Algorithm: sdkkonnectcomp.UpstreamAlgorithmConsistentHashing.ToPointer(), - }, - }, - } - err = GetClients().MgrClient.Create(GetCtx(), kup) - require.NoError(t, err) + ) t.Log("Waiting for KongUpstream to be updated with Konnect ID") require.EventuallyWithT(t, func(t *assert.CollectT) { err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kup.Name, Namespace: ns.Name}, kup) require.NoError(t, err) - if !assert.NotNil(t, kup.Status.Konnect) { - return - } - assert.NotEmpty(t, kup.Status.Konnect.KonnectEntityStatus.GetKonnectID()) - assert.NotEmpty(t, kup.Status.Konnect.KonnectEntityStatus.GetOrgID()) - assert.NotEmpty(t, kup.Status.Konnect.KonnectEntityStatus.GetServerURL()) + assertKonnectEntityProgrammed(t, kup) }, testutils.ObjectUpdateTimeout, time.Second) - t.Log("Creating KongTarget") - ktName := "kt-" + testID - kt := &configurationv1alpha1.KongTarget{ - ObjectMeta: metav1.ObjectMeta{ - Name: ktName, - Namespace: ns.Name, + kt := deploy.KongTargetAttachedToUpstream(t, ctx, clientNamespaced, kup, + deploy.WithTestIDLabel(testID), + func(obj client.Object) { + kt := obj.(*configurationv1alpha1.KongTarget) + kt.Spec.KongTargetAPISpec.Target = "example.com" + kt.Spec.KongTargetAPISpec.Weight = 100 }, - Spec: configurationv1alpha1.KongTargetSpec{ - UpstreamRef: configurationv1alpha1.TargetRef{ - Name: kupName, - }, - KongTargetAPISpec: configurationv1alpha1.KongTargetAPISpec{ - Target: "example.com", - Weight: 100, - }, - }, - } - err = GetClients().MgrClient.Create(GetCtx(), kt) - require.NoError(t, err) + ) t.Log("Waiting for KongTarget to be updated with Konnect ID") require.EventuallyWithT(t, func(t *assert.CollectT) { err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kt.Name, Namespace: ns.Name}, kt) require.NoError(t, err) - if !assert.NotNil(t, kt.Status.Konnect) { - return - } - assert.NotEmpty(t, kt.Status.Konnect.KonnectEntityStatus.GetKonnectID()) - assert.NotEmpty(t, kt.Status.Konnect.KonnectEntityStatus.GetOrgID()) - assert.NotEmpty(t, kt.Status.Konnect.KonnectEntityStatus.GetServerURL()) + + assertKonnectEntityProgrammed(t, kt) }, testutils.ObjectUpdateTimeout, time.Second) // Should delete KongTarget because it will block deletion of KongUpstream owning it. t.Cleanup(deleteObjectAndWaitForDeletionFn(t, kt)) - t.Logf("Creating KongVault") - kvName := "kv-" + testID - kv := configurationv1alpha1.KongVault{ - ObjectMeta: metav1.ObjectMeta{ - Name: kvName, - }, - Spec: configurationv1alpha1.KongVaultSpec{ - Config: apiextensionsv1.JSON{ - Raw: []byte(`{"prefix":"env-vault"}`), - }, - Backend: "env", - Prefix: "env-vault", - ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ - Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, - KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ - Name: cp.Name, - Namespace: ns.Name, - }, - }, - }, - } - err = GetClients().MgrClient.Create(GetCtx(), &kv) - require.NoError(t, err) - + kv := deploy.KongVaultAttachedToCP(t, ctx, clientNamespaced, "env", "env-vault", []byte(`{"prefix":"env-vault"}`), cp) t.Logf("Waiting for KongVault to be updated with Konnect ID") require.EventuallyWithT(t, func(t *assert.CollectT) { - err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kv.Name}, &kv) + err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{Name: kv.Name}, kv) require.NoError(t, err) - if !assert.NotNil(t, kv.Status.Konnect) { - return - } - assert.NotEmpty(t, kv.Status.Konnect.KonnectEntityStatus.GetKonnectID()) - assert.NotEmpty(t, kv.Status.Konnect.KonnectEntityStatus.GetOrgID()) - assert.NotEmpty(t, kv.Status.Konnect.KonnectEntityStatus.GetServerURL()) + assertKonnectEntityProgrammed(t, kv) }, testutils.ObjectUpdateTimeout, time.Second) - t.Logf("Creating KongCertificate") - kcertName := "kcert-" + testID - kcert := configurationv1alpha1.KongCertificate{ - ObjectMeta: metav1.ObjectMeta{ - Name: kcertName, - Namespace: ns.Name, - }, - Spec: configurationv1alpha1.KongCertificateSpec{ - ControlPlaneRef: &configurationv1alpha1.ControlPlaneRef{ - Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef, - KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{ - Name: cp.Name, - Namespace: ns.Name, - }, - }, - KongCertificateAPISpec: configurationv1alpha1.KongCertificateAPISpec{ - Cert: dummyValidCertPEM, - Key: dummyValidCertKeyPEM, - }, - }, - } - require.NoError(t, GetClients().MgrClient.Create(GetCtx(), &kcert)) + kcert := deploy.KongCertificateAttachedToCP(t, ctx, clientNamespaced, cp) t.Logf("Waiting for KongCertificate to get Konnect ID") require.EventuallyWithT(t, func(t *assert.CollectT) { err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{ - Name: kcertName, + Name: kcert.Name, Namespace: ns.Name, - }, &kcert) + }, kcert) require.NoError(t, err) - if !assert.NotNil(t, kcert.Status.Konnect) { - return - } - assert.NotEmpty(t, kcert.Status.Konnect.KonnectEntityStatus.GetKonnectID()) - assert.NotEmpty(t, kcert.Status.Konnect.KonnectEntityStatus.GetOrgID()) - assert.NotEmpty(t, kcert.Status.Konnect.KonnectEntityStatus.GetServerURL()) + assertKonnectEntityProgrammed(t, kcert) }, testutils.ObjectUpdateTimeout, time.Second) - t.Log("Creating a KongSNI attached to KongCertificate") - ksniName := "ksni-" + testID - ksni := configurationv1alpha1.KongSNI{ - ObjectMeta: metav1.ObjectMeta{ - Name: ksniName, - Namespace: ns.Name, + ksni := deploy.KongSNIAttachedToCertificate(t, ctx, clientNamespaced, kcert, + deploy.WithTestIDLabel(testID), + func(obj client.Object) { + ksni := obj.(*configurationv1alpha1.KongSNI) + ksni.Spec.KongSNIAPISpec.Name = "test.kong-sni.example.com" }, - Spec: configurationv1alpha1.KongSNISpec{ - CertificateRef: configurationv1alpha1.KongObjectRef{ - Name: kcertName, - }, - KongSNIAPISpec: configurationv1alpha1.KongSNIAPISpec{ - Name: "test.kong-sni.example.com", - }, - }, - } - require.NoError(t, GetClients().MgrClient.Create(GetCtx(), &ksni)) + ) t.Logf("Waiting for KongSNI to get Konnect ID") require.EventuallyWithT(t, func(t *assert.CollectT) { err := GetClients().MgrClient.Get(GetCtx(), types.NamespacedName{ - Name: ksniName, + Name: ksni.Name, Namespace: ns.Name, - }, &ksni) + }, ksni) require.NoError(t, err) - if !assert.NotNil(t, ksni.Status.Konnect) { - return - } - assert.NotEmpty(t, ksni.Status.Konnect.KonnectEntityStatus.GetKonnectID()) - assert.NotEmpty(t, ksni.Status.Konnect.KonnectEntityStatus.GetOrgID()) - assert.NotEmpty(t, ksni.Status.Konnect.KonnectEntityStatus.GetServerURL()) + assertKonnectEntityProgrammed(t, ksni) assert.Equal(t, kcert.GetKonnectID(), ksni.Status.Konnect.CertificateID) }, testutils.ObjectUpdateTimeout, time.Second) - } // deleteObjectAndWaitForDeletionFn returns a function that deletes the given object and waits for it to be gone. @@ -506,7 +242,14 @@ func deleteObjectAndWaitForDeletionFn(t *testing.T, obj client.Object) func() { // assertKonnectEntityProgrammed asserts that the KonnectEntityProgrammed condition is set to true and the Konnect // status fields are populated. -func assertKonnectEntityProgrammed(t assert.TestingT, cs []metav1.Condition, konnectStatus *konnectv1alpha1.KonnectEntityStatus) { +func assertKonnectEntityProgrammed( + t assert.TestingT, + obj interface { + GetKonnectStatus() *konnectv1alpha1.KonnectEntityStatus + GetConditions() []metav1.Condition + }, +) { + konnectStatus := obj.GetKonnectStatus() if !assert.NotNil(t, konnectStatus) { return } @@ -514,7 +257,7 @@ func assertKonnectEntityProgrammed(t assert.TestingT, cs []metav1.Condition, kon assert.NotEmpty(t, konnectStatus.GetOrgID()) assert.NotEmpty(t, konnectStatus.GetServerURL()) - assert.True(t, lo.ContainsBy(cs, func(condition metav1.Condition) bool { + assert.True(t, lo.ContainsBy(obj.GetConditions(), func(condition metav1.Condition) bool { return condition.Type == conditions.KonnectEntityProgrammedConditionType && condition.Status == metav1.ConditionTrue }))