diff --git a/examples/resources/materialize_source_load_generator/resource.tf b/examples/resources/materialize_source_load_generator/resource.tf index 4a78d8f4..9e1b04a1 100644 --- a/examples/resources/materialize_source_load_generator/resource.tf +++ b/examples/resources/materialize_source_load_generator/resource.tf @@ -3,14 +3,15 @@ resource "materialize_source_load_generator" "example_source_load_generator" { schema_name = "schema" size = "3xsmall" + load_generator_type = "COUNTER" + counter_options { - load_generator_type = "COUNTER" - tick_interval = "500ms" - scale_factor = 0.01 + tick_interval = "500ms" + scale_factor = 0.01 } } # CREATE SOURCE schema.source_load_generator # FROM LOAD GENERATOR COUNTER # (TICK INTERVAL '500ms' SCALE FACTOR 0.01) -# WITH (SIZE = '3xsmall'); \ No newline at end of file +# WITH (SIZE = '3xsmall'); diff --git a/pkg/provider/acceptance_source_load_generator_test.go b/pkg/provider/acceptance_source_load_generator_test.go new file mode 100644 index 00000000..94ba5f62 --- /dev/null +++ b/pkg/provider/acceptance_source_load_generator_test.go @@ -0,0 +1,105 @@ +package provider + +import ( + "database/sql" + "fmt" + "testing" + + "github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/jmoiron/sqlx" +) + +func TestAccSourceLoadGenerator_basic(t *testing.T) { + sourceLoadGeneratorName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: nil, + Steps: []resource.TestStep{ + { + Config: testAccSourceLoadGeneratorResource(sourceLoadGeneratorName), + Check: resource.ComposeTestCheckFunc( + testAccCheckSourceLoadGeneratorExists("materialize_source_load_generator.test"), + resource.TestCheckResourceAttr("materialize_source_load_generator.test", "name", sourceLoadGeneratorName), + ), + }, + }, + }) +} + +func TestAccSourceLoadGenerator_disappears(t *testing.T) { + sourceLoadGeneratorName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccCheckAllSourceLoadGeneratorsDestroyed, + Steps: []resource.TestStep{ + { + Config: testAccSourceLoadGeneratorResource(sourceLoadGeneratorName), + Check: resource.ComposeTestCheckFunc( + testAccCheckSourceLoadGeneratorExists("materialize_source_load_generator.test"), + resource.TestCheckResourceAttr("materialize_source_load_generator.test", "name", sourceLoadGeneratorName), + testAccCheckSourceLoadGeneratorDisappears(sourceLoadGeneratorName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccSourceLoadGeneratorResource(name string) string { + return fmt.Sprintf(` +resource "materialize_source_load_generator" "test" { + name = "%s" + schema_name = "public" + size = "1" + load_generator_type = "COUNTER" + counter_options { + tick_interval = "500ms" + scale_factor = 0.01 + } +} +`, name) +} + +func testAccCheckSourceLoadGeneratorExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + db := testAccProvider.Meta().(*sqlx.DB) + r, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("SourceLoadGenerator not found: %s", name) + } + _, err := materialize.ScanSource(db, r.Primary.ID) + return err + } +} + +func testAccCheckSourceLoadGeneratorDisappears(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + db := testAccProvider.Meta().(*sqlx.DB) + _, err := db.Exec(fmt.Sprintf(`DROP SOURCE "%s";`, name)) + return err + } +} + +func testAccCheckAllSourceLoadGeneratorsDestroyed(s *terraform.State) error { + db := testAccProvider.Meta().(*sqlx.DB) + + for _, r := range s.RootModule().Resources { + if r.Type != "materialize_source_load_generator" { + continue + } + + _, err := materialize.ScanSource(db, r.Primary.ID) + if err == nil { + return fmt.Errorf("SourceLoadGenerator %v still exists", r.Primary.ID) + } else if err != sql.ErrNoRows { + return err + } + } + + return nil +}