Skip to content

Commit

Permalink
Add Go VCR test for peering and import
Browse files Browse the repository at this point in the history
  • Loading branch information
tbroden84 committed Dec 18, 2024
1 parent 1366ff2 commit 8187305
Show file tree
Hide file tree
Showing 4 changed files with 1,361 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cloudamqp/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ func cloudamqpResourceTest(t *testing.T, c resource.TestCase) {
fmt.Println("SKIP: PUT /api/instances/{id}/config", i.Request.URL, "error:", errStr)
i.DiscardOnSave = true
}
case i.Response.Code == 200 && i.Request.Method == "GET" &&
regexp.MustCompile(`/api/vpcs/\d+/vpc-peering$`).MatchString(i.Request.URL):
// Filter polling for vpc peering state, only store active response
state := gjson.Get(i.Response.Body, "rows.0.state").String()
if state == "INACTIVE" {
fmt.Println("SKIP: GET /api/vpcs/{id}/vpc-peering", i.Request.URL, "state:", state)
i.DiscardOnSave = true
}
}
return nil
}
Expand Down
73 changes: 73 additions & 0 deletions cloudamqp/resource_cloudamqp_vpc_gcp_peering_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package cloudamqp

import (
"fmt"
"os"
"testing"

"github.com/cloudamqp/terraform-provider-cloudamqp/cloudamqp/vcr-testing/configuration"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

// TestAccVpcGcpPeering_Basic: Create standalone VPC and instance, enable VPC Connect and import.
func TestAccVpcGcpPeering_Basic(t *testing.T) {
var (
fileNames = []string{"vpc_and_instance", "vpc_gcp_peering"}
vpcResourceName = "cloudamqp_vpc.vpc"
instanceResourceName = "cloudamqp_instance.instance"
vpcGcpPeeringResourceName = "cloudamqp_vpc_gcp_peering.vpc_peering"
region = "google-compute-engine::europe-west1"
peerNetworkUri = os.Getenv("PEER_NETWORK_URI")

params = map[string]string{
"VpcName": "TestAccVpcGcpPeering_Basic",
"VpcRegion": region,
"VpcSubnet": "10.56.72.0/24",
"InstanceName": "TestAccVpcGcpPeering_Basic",
"InstanceRegion": region,
"InstanceID": fmt.Sprintf("%s.id", instanceResourceName),
"InstancePlan": "bunny-1",
"PeerNetworkUri": peerNetworkUri,
"WaitOnPeeringStatus": "true",
}
)

cloudamqpResourceTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactory,
Steps: []resource.TestStep{
{
Config: configuration.GetTemplatedConfig(t, fileNames, params),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(vpcResourceName, "name", params["VpcName"]),
resource.TestCheckResourceAttr(instanceResourceName, "name", params["InstanceName"]),
resource.TestCheckResourceAttr(vpcGcpPeeringResourceName, "auto_create_routes", "true"),
resource.TestCheckResourceAttr(vpcGcpPeeringResourceName, "wait_on_peering_status", "true"),
resource.TestCheckResourceAttr(vpcGcpPeeringResourceName, "state", "ACTIVE"),
),
},
{
ResourceName: vpcGcpPeeringResourceName,
ImportStateIdFunc: testAccImportVpcPeeringStateIdFunc("vpc", vpcResourceName, peerNetworkUri),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"wait_on_peering_status"},
},
},
})
}

func testAccImportVpcPeeringStateIdFunc(importType, resourceName, peerNetworkUri string) resource.ImportStateIdFunc {
return func(state *terraform.State) (string, error) {
rs, ok := state.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Resource %s not found", resourceName)
}
if rs.Primary.ID == "" {
return "", fmt.Errorf("No resource id set")
}
resourceID := rs.Primary.ID
return fmt.Sprintf("%s,%s,%s", importType, resourceID, peerNetworkUri), nil
}
}
5 changes: 5 additions & 0 deletions test/configurations/vpc_gcp_peering.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
resource "cloudamqp_vpc_gcp_peering" "vpc_peering" {
vpc_id = {{or .VpcID `cloudamqp_vpc.vpc.id`}}
peer_network_uri = "{{.PeerNetworkUri}}"
wait_on_peering_status = {{or .WaitOnPeeringStatus false}}
}
Loading

0 comments on commit 8187305

Please sign in to comment.