Skip to content

Commit

Permalink
integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kon-angelo committed Aug 23, 2022
1 parent 6117caf commit d5d13bf
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pkg/apis/gcp/types_infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type NetworkConfig struct {
PrivateServiceConnect *PrivateServiceConnectConfig
}

// PrivateServiceConnect holds the configuration for Private Service Connect endpoints.
// PrivateServiceConnectConfig holds the configuration for Private Service Connect endpoints.
type PrivateServiceConnectConfig struct {
// EndpointIP is the IP where the services will be accessible.
EndpointIP string
Expand Down
6 changes: 3 additions & 3 deletions pkg/internal/infrastructure/templates/main.tpl.tf
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ output "{{ .outputKeys.subnetInternal }}" {
}
{{- end }}

{{ if .networks.privateServiceConnect }}
output "{{ .outputKeys.privateServiceConnectIP }}" {
{{- if .networks.privateServiceConnect }}
output "{{ .outputKeys.privateServiceConnectName }}" {
value = google_compute_global_forwarding_rule.default.name
}
output "{{ .outputKeys.privateServiceConnectName }}" {
output "{{ .outputKeys.privateServiceConnectIP }}" {
value = google_compute_global_address.default.address
}
{{- end }}
2 changes: 1 addition & 1 deletion pkg/internal/infrastructure/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const (
TerraformOutputKeyCloudRouter = "cloud_router"
// TerraformOutputKeyPrivateServiceConnectIP is the name of the psc_ip terraform output variable.
TerraformOutputKeyPrivateServiceConnectIP = "psc_ip"
// TerraformOutputKeyPrivateServiceConnectIP is the name of the psc_name terraform output variable.
// TerraformOutputKeyPrivateServiceConnectName is the name of the psc_name terraform output variable.
TerraformOutputKeyPrivateServiceConnectName = "psc_name"
)

Expand Down
56 changes: 56 additions & 0 deletions pkg/internal/infrastructure/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strconv"

mockterraformer "github.com/gardener/gardener/extensions/pkg/terraformer/mock"
"k8s.io/utils/pointer"

api "github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp"
apiv1alpha1 "github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp/v1alpha1"
Expand Down Expand Up @@ -158,6 +159,7 @@ var _ = Describe("Terraform", func() {
CloudNATName: cloudNATName,
}))
})

It("should return correct state when cloudRouter name is NOT specified", func() {
var (
vpcWithoutCloudRouterConfig = &api.InfrastructureConfig{
Expand Down Expand Up @@ -452,6 +454,60 @@ var _ = Describe("Terraform", func() {
},
}))
})

It("should correctly compute the terraformer chart values with privateServiceConnect", func() {
var (
endpointIP = "10.0.0.0"
endpointName = "endpoint"
)
config.Networks.PrivateServiceConnect = &api.PrivateServiceConnectConfig{
EndpointIP: endpointIP,
EndpointName: pointer.String(endpointName),
}
values, err := ComputeTerraformerTemplateValues(infra, serviceAccount, config, &podCIDR, true)
Expect(err).To(BeNil())
Expect(values).To(Equal(map[string]interface{}{
"google": map[string]interface{}{
"region": infra.Spec.Region,
"project": projectID,
"enableBeta": true,
},
"create": map[string]interface{}{
"vpc": false,
"cloudRouter": false,
"serviceAccount": true,
},
"vpc": map[string]interface{}{
"name": strconv.Quote(config.Networks.VPC.Name),
"cloudRouter": map[string]interface{}{
"name": "cloudrouter",
},
},
"clusterName": infra.Namespace,
"networks": map[string]interface{}{
"workers": config.Networks.Workers,
"internal": config.Networks.Internal,
"cloudNAT": map[string]interface{}{
"minPortsPerVM": minPortsPerVM,
},
"privateServiceConnect": map[string]interface{}{
"endpointName": endpointName,
"address": endpointIP,
},
},
"podCIDR": podCIDR,
"outputKeys": map[string]interface{}{
"vpcName": TerraformerOutputKeyVPCName,
"cloudNAT": TerraformOutputKeyCloudNAT,
"cloudRouter": TerraformOutputKeyCloudRouter,
"serviceAccountEmail": TerraformerOutputKeyServiceAccountEmail,
"subnetNodes": TerraformerOutputKeySubnetNodes,
"subnetInternal": TerraformerOutputKeySubnetInternal,
"privateServiceConnectIP": TerraformOutputKeyPrivateServiceConnectIP,
"privateServiceConnectName": TerraformOutputKeyPrivateServiceConnectName,
},
}))
})
})

Describe("#StatusFromTerraformState", func() {
Expand Down
112 changes: 75 additions & 37 deletions test/integration/infrastructure/infrastructure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"

gcpinternal "github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp"
"github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp/helper"
gcpinstall "github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp/install"
gcpv1alpha1 "github.com/gardener/gardener-extension-provider-gcp/pkg/apis/gcp/v1alpha1"
"github.com/gardener/gardener-extension-provider-gcp/pkg/controller/infrastructure"
Expand All @@ -57,6 +59,7 @@ const (
workersSubnetCIDR = "10.250.0.0/19"
internalSubnetCIDR = "10.250.112.0/22"
podCIDR = "100.96.0.0/11"
pscIP = "10.249.0.0"
)

var (
Expand Down Expand Up @@ -169,8 +172,21 @@ var _ = Describe("Infrastructure tests", func() {
framework.RunCleanupActions()
})

It("should successfully create and delete", func() {
// It("should successfully create and delete", func() {
// providerConfig := newProviderConfig(nil)
//
// namespace, err := generateNamespaceName()
// Expect(err).NotTo(HaveOccurred())
//
// err = runTest(ctx, c, namespace, providerConfig, project, computeService, iamService)
// Expect(err).NotTo(HaveOccurred())
// })

It("should successfully create and delete with privateServiceConnect", func() {
providerConfig := newProviderConfig(nil)
providerConfig.Networks.PrivateServiceConnect = &gcpv1alpha1.PrivateServiceConnectConfig{
EndpointIP: pscIP,
}

namespace, err := generateNamespaceName()
Expect(err).NotTo(HaveOccurred())
Expand All @@ -180,40 +196,40 @@ var _ = Describe("Infrastructure tests", func() {
})
})

Context("with infrastructure that uses existing vpc", func() {
AfterEach(func() {
framework.RunCleanupActions()
})

It("should successfully create and delete", func() {
namespace, err := generateNamespaceName()
Expect(err).NotTo(HaveOccurred())

networkName := namespace
cloudRouterName := networkName + "-cloud-router"

err = prepareNewNetwork(ctx, log, project, computeService, networkName, cloudRouterName)
Expect(err).NotTo(HaveOccurred())

var cleanupHandle framework.CleanupActionHandle
cleanupHandle = framework.AddCleanupAction(func() {
err := teardownNetwork(ctx, log, project, computeService, networkName, cloudRouterName)
Expect(err).NotTo(HaveOccurred())

framework.RemoveCleanupAction(cleanupHandle)
})

providerConfig := newProviderConfig(&gcpv1alpha1.VPC{
Name: networkName,
CloudRouter: &gcpv1alpha1.CloudRouter{
Name: cloudRouterName,
},
})

err = runTest(ctx, c, namespace, providerConfig, project, computeService, iamService)
Expect(err).NotTo(HaveOccurred())
})
})
// Context("with infrastructure that uses existing vpc", func() {
// AfterEach(func() {
// framework.RunCleanupActions()
// })
//
// It("should successfully create and delete", func() {
// namespace, err := generateNamespaceName()
// Expect(err).NotTo(HaveOccurred())
//
// networkName := namespace
// cloudRouterName := networkName + "-cloud-router"
//
// err = prepareNewNetwork(ctx, log, project, computeService, networkName, cloudRouterName)
// Expect(err).NotTo(HaveOccurred())
//
// var cleanupHandle framework.CleanupActionHandle
// cleanupHandle = framework.AddCleanupAction(func() {
// err := teardownNetwork(ctx, log, project, computeService, networkName, cloudRouterName)
// Expect(err).NotTo(HaveOccurred())
//
// framework.RemoveCleanupAction(cleanupHandle)
// })
//
// providerConfig := newProviderConfig(&gcpv1alpha1.VPC{
// Name: networkName,
// CloudRouter: &gcpv1alpha1.CloudRouter{
// Name: cloudRouterName,
// },
// })
//
// err = runTest(ctx, c, namespace, providerConfig, project, computeService, iamService)
// Expect(err).NotTo(HaveOccurred())
// })
// })
})

func runTest(
Expand Down Expand Up @@ -337,7 +353,14 @@ func runTest(
}

By("verify infrastructure creation")
verifyCreation(ctx, project, computeService, iamService, infra, providerConfig)
err = c.Get(ctx, client.ObjectKey{
Namespace: infra.Namespace,
Name: infra.Name,
}, infra)
Expect(err).To(BeNil())
providerStatus, err := helper.InfrastructureStatusFromRaw(infra.Status.ProviderStatus)
Expect(err).To(BeNil())
verifyCreation(ctx, project, computeService, iamService, infra, providerConfig, providerStatus)

return nil
}
Expand Down Expand Up @@ -487,6 +510,7 @@ func verifyCreation(
iamService *iam.Service,
infra *extensionsv1alpha1.Infrastructure,
providerConfig *gcpv1alpha1.InfrastructureConfig,
providerStatus *gcpinternal.InfrastructureStatus,
) {
// service account
if !features.ExtensionFeatureGate.Enabled(features.DisableGardenerServiceAccountCreation) {
Expand All @@ -497,7 +521,6 @@ func verifyCreation(
}

// network

network, err := computeService.Networks.Get(project, infra.Namespace).Do()
Expect(err).NotTo(HaveOccurred())
Expect(network.AutoCreateSubnetworks).To(BeFalse())
Expand Down Expand Up @@ -594,6 +617,21 @@ func verifyCreation(
Ports: []string{"30000-32767"},
},
}))

if providerConfig.Networks.PrivateServiceConnect != nil {
Expect(providerStatus.Networks).NotTo(BeNil())
Expect(providerStatus.Networks.PrivateServiceConnectStatus).NotTo(BeNil())
Expect(providerStatus.Networks.PrivateServiceConnectStatus.EndpointName).NotTo(BeEmpty())
fwd, err := computeService.GlobalForwardingRules.Get(project, providerStatus.Networks.PrivateServiceConnectStatus.EndpointName).Do()
Expect(err).To(BeNil())
Expect(fwd.Network).To(Equal(network.SelfLink))

addr, err := computeService.GlobalAddresses.Get(project, infra.Namespace).Do()

Expect(err).To(BeNil())
Expect(addr.Address).To(Equal(providerConfig.Networks.PrivateServiceConnect.EndpointIP))
Expect(addr.AddressType).To(Equal("INTERNAL"))
}
}

func verifyDeletion(
Expand Down

0 comments on commit d5d13bf

Please sign in to comment.