-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Terraform has a standard acceptance testing framework [0] that allows for testing the complete lifecyle of provider resources: apply, plan, refresh, destroy, etc. In particular, this makes it possible to test things like a resource going missing due to a manual drop (#157). This commit wires up the acceptance test framework, and then adds some basic acceptance tests for creating secrets, creating cluster replicas, and refreshing state for disappearing secrets. Ultimately I think we should move all of today's integration tests to this acceptance test framework. Work towards #165. [0]: https://developer.hashicorp.com/terraform/plugin/testing/acceptance-tests
- Loading branch information
Showing
7 changed files
with
571 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: acceptance | ||
on: | ||
pull_request: | ||
paths: | ||
- pkg/** | ||
schedule: | ||
- cron: 0 11 * * 0 | ||
|
||
jobs: | ||
integration: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Docker Compose Up | ||
run: docker compose up --build -d | ||
|
||
- name: make testacc | ||
run: make testacc | ||
env: | ||
MZ_HOST: localhost | ||
MZ_USER: materialize | ||
MZ_TESTING: "true" | ||
|
||
- name: Docker Compose Down | ||
run: docker compose down |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package provider | ||
|
||
import ( | ||
"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 TestAccClusterReplica_basic(t *testing.T) { | ||
clusterName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
replicaName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviderFactories, | ||
CheckDestroy: testAccCheckAllSecretsDestroyed, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccClusterReplicaResource(clusterName, replicaName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckClusterReplicaExists("materialize_cluster_replica.test"), | ||
resource.TestCheckResourceAttr("materialize_cluster_replica.test", "cluster_name", clusterName), | ||
resource.TestCheckResourceAttr("materialize_cluster_replica.test", "name", replicaName), | ||
resource.TestCheckResourceAttr("materialize_cluster_replica.test", "size", "1"), | ||
resource.TestCheckResourceAttr("materialize_cluster_replica.test", "introspection_interval", "1s"), | ||
resource.TestCheckResourceAttr("materialize_cluster_replica.test", "introspection_debugging", "false"), | ||
resource.TestCheckNoResourceAttr("materialize_cluster_replica.test", "idle_arrangement_merge_effort"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccClusterReplicaResource(clusterName, replicaName string) string { | ||
return fmt.Sprintf(` | ||
resource "materialize_cluster" "test" { | ||
name = "%[1]s" | ||
} | ||
resource "materialize_cluster_replica" "test" { | ||
cluster_name = "%[1]s" | ||
name = "%[2]s" | ||
size = "1" | ||
} | ||
`, clusterName, replicaName) | ||
} | ||
|
||
func testAccCheckClusterReplicaExists(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("cluster replica not found: %s", name) | ||
} | ||
_, err := materialize.ScanClusterReplica(db, r.Primary.ID) | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
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 TestAccSecret_basic(t *testing.T) { | ||
secretName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviderFactories, | ||
CheckDestroy: testAccCheckAllSecretsDestroyed, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSecretResource(secretName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckSecretExists("materialize_secret.test"), | ||
resource.TestCheckResourceAttr("materialize_secret.test", "name", secretName), | ||
resource.TestCheckResourceAttr("materialize_secret.test", "value", "sekret"), | ||
resource.TestCheckResourceAttr("materialize_secret.test", "database_name", "materialize"), | ||
resource.TestCheckResourceAttr("materialize_secret.test", "schema_name", "public"), | ||
resource.TestCheckResourceAttr("materialize_secret.test", "qualified_sql_name", fmt.Sprintf(`"materialize"."public"."%s"`, secretName)), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccSecret_disappears(t *testing.T) { | ||
secretName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviderFactories, | ||
CheckDestroy: testAccCheckAllSecretsDestroyed, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccSecretResource(secretName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckSecretExists("materialize_secret.test"), | ||
testAccCheckSecretDisappears(secretName), | ||
), | ||
ExpectNonEmptyPlan: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccSecretResource(name string) string { | ||
return fmt.Sprintf(` | ||
resource "materialize_secret" "test" { | ||
name = "%s" | ||
value = "sekret" | ||
} | ||
`, name) | ||
} | ||
|
||
func testAccCheckSecretExists(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("secret not found: %s", name) | ||
} | ||
_, err := materialize.ScanSecret(db, r.Primary.ID) | ||
return err | ||
} | ||
} | ||
|
||
func testAccCheckSecretDisappears(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
db := testAccProvider.Meta().(*sqlx.DB) | ||
_, err := db.Exec(fmt.Sprintf("DROP SECRET %s", name)) | ||
return err | ||
} | ||
} | ||
|
||
func testAccCheckAllSecretsDestroyed(s *terraform.State) error { | ||
db := testAccProvider.Meta().(*sqlx.DB) | ||
|
||
for _, r := range s.RootModule().Resources { | ||
if r.Type != "materialize_secret" { | ||
continue | ||
} | ||
|
||
_, err := materialize.ScanSecret(db, r.Primary.ID) | ||
if err == nil { | ||
return fmt.Errorf("secret %v still exists", r.Primary.ID) | ||
} else if err != sql.ErrNoRows { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters