From 4d39d545acee4b9a2d43c244ecc889be42559276 Mon Sep 17 00:00:00 2001 From: vojindjukic Date: Mon, 16 Sep 2024 16:07:55 +0200 Subject: [PATCH] fix ip_filter acc test - delete resource after creation to avoid conflict during next run --- sysdig/resource_sysdig_ip_filter_test.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/sysdig/resource_sysdig_ip_filter_test.go b/sysdig/resource_sysdig_ip_filter_test.go index 2acec57d..21d7e29e 100644 --- a/sysdig/resource_sysdig_ip_filter_test.go +++ b/sysdig/resource_sysdig_ip_filter_test.go @@ -4,14 +4,18 @@ package sysdig_test import ( "fmt" - "github.com/draios/terraform-provider-sysdig/sysdig" "testing" + "github.com/draios/terraform-provider-sysdig/sysdig" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func TestAccSysdigIpFilter_fullLifecycle(t *testing.T) { + ipRange1 := generateRandomIPRange() + ipRange2 := generateRandomIPRange() + resource.Test(t, resource.TestCase{ PreCheck: preCheckAnyEnv(t, SysdigMonitorApiTokenEnv), ProviderFactories: map[string]func() (*schema.Provider, error){ @@ -21,19 +25,19 @@ func TestAccSysdigIpFilter_fullLifecycle(t *testing.T) { }, Steps: []resource.TestStep{ { - // Create resource - Config: createIPFilter("192.168.1.0/24", "Initial note", true), + // Create resource with the first random IP range + Config: createIPFilter(ipRange1, "Initial note", true), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("sysdig_ip_filter.test", "ip_range", "192.168.1.0/24"), + resource.TestCheckResourceAttr("sysdig_ip_filter.test", "ip_range", ipRange1), resource.TestCheckResourceAttr("sysdig_ip_filter.test", "note", "Initial note"), resource.TestCheckResourceAttr("sysdig_ip_filter.test", "enabled", "true"), ), }, { - // Update resource - Config: createIPFilter("192.168.2.0/24", "Updated note", false), + // Update resource with the second random IP range + Config: createIPFilter(ipRange2, "Updated note", false), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("sysdig_ip_filter.test", "ip_range", "192.168.2.0/24"), + resource.TestCheckResourceAttr("sysdig_ip_filter.test", "ip_range", ipRange2), resource.TestCheckResourceAttr("sysdig_ip_filter.test", "note", "Updated note"), resource.TestCheckResourceAttr("sysdig_ip_filter.test", "enabled", "false"), ), @@ -42,6 +46,11 @@ func TestAccSysdigIpFilter_fullLifecycle(t *testing.T) { }) } +func generateRandomIPRange() string { + rand.Seed(time.Now().UnixNano()) + return fmt.Sprintf("%d.%d.%d.0/24", rand.Intn(256), rand.Intn(256), rand.Intn(256)) +} + func createIPFilter(ipRange, note string, enabled bool) string { return fmt.Sprintf(` resource "sysdig_ip_filter" "test" {