Skip to content

Commit

Permalink
Update acceptance tests with new db client
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyiliev committed Nov 24, 2023
1 parent 0a9f67d commit 53ff098
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
16 changes: 12 additions & 4 deletions pkg/provider/acceptance_cluster_replica_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"testing"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/utils"
"github.com/hashicorp/terraform-plugin-testing/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) {
Expand Down Expand Up @@ -129,18 +129,26 @@ resource "materialize_cluster_replica" "test" {

func testAccCheckClusterReplicaExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
db := testAccProvider.Meta().(*sqlx.DB)
meta := testAccProvider.Meta()
db, err := utils.GetDBClientFromMeta(meta, nil)
if err != nil {
return fmt.Errorf("error getting DB client: %s", err)
}
r, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("cluster replica not found: %s", name)
}
_, err := materialize.ScanClusterReplica(db, r.Primary.ID)
_, err = materialize.ScanClusterReplica(db, r.Primary.ID)
return err
}
}

func testAccCheckAllClusterReplicaDestroyed(s *terraform.State) error {
db := testAccProvider.Meta().(*sqlx.DB)
meta := testAccProvider.Meta()
db, err := utils.GetDBClientFromMeta(meta, nil)
if err != nil {
return fmt.Errorf("error getting DB client: %s", err)
}

for _, r := range s.RootModule().Resources {
if r.Type != "materialize_cluster_replica" {
Expand Down
18 changes: 13 additions & 5 deletions pkg/provider/acceptance_grant_system_privilege_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"testing"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/utils"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/jmoiron/sqlx"
)

func TestAccGrantSystemPrivilege_basic(t *testing.T) {
Expand Down Expand Up @@ -70,7 +70,11 @@ resource "materialize_grant_system_privilege" "test" {

func testAccCheckGrantSystemPrivilegeExists(grantName, roleName, privilege string) resource.TestCheckFunc {
return func(s *terraform.State) error {
db := testAccProvider.Meta().(*sqlx.DB)
meta := testAccProvider.Meta()
db, err := utils.GetDBClientFromMeta(meta, nil)
if err != nil {
return fmt.Errorf("error getting DB client: %s", err)
}
_, ok := s.RootModule().Resources[grantName]
if !ok {
return fmt.Errorf("grant not found")
Expand All @@ -81,7 +85,7 @@ func testAccCheckGrantSystemPrivilegeExists(grantName, roleName, privilege strin
// return err
// }

_, err := materialize.ScanSystemPrivileges(db)
_, err = materialize.ScanSystemPrivileges(db)
if err != nil {
return err
}
Expand All @@ -92,8 +96,12 @@ func testAccCheckGrantSystemPrivilegeExists(grantName, roleName, privilege strin

func testAccCheckGrantSystemPrivilegeRevoked(roleName, privilege string) resource.TestCheckFunc {
return func(s *terraform.State) error {
db := testAccProvider.Meta().(*sqlx.DB)
_, err := db.Exec(fmt.Sprintf(`REVOKE %[1]s ON SYSTEM FROM %[2]s;`, roleName, privilege))
meta := testAccProvider.Meta()
db, err := utils.GetDBClientFromMeta(meta, nil)
if err != nil {
return fmt.Errorf("error getting DB client: %s", err)
}
_, err = db.Exec(fmt.Sprintf(`REVOKE %[1]s ON SYSTEM FROM %[2]s;`, roleName, privilege))
return err
}
}
16 changes: 12 additions & 4 deletions pkg/provider/acceptance_role_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"testing"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/utils"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/jmoiron/sqlx"
)

func TestAccGrantRole_basic(t *testing.T) {
Expand Down Expand Up @@ -84,7 +84,11 @@ resource "materialize_role_grant" "test" {

func testAccCheckGrantRoleExists(grantName, roleName, granteeName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
db := testAccProvider.Meta().(*sqlx.DB)
meta := testAccProvider.Meta()
db, err := utils.GetDBClientFromMeta(meta, nil)
if err != nil {
return fmt.Errorf("error getting DB client: %s", err)
}
_, ok := s.RootModule().Resources[grantName]
if !ok {
return fmt.Errorf("grant not found")
Expand All @@ -111,8 +115,12 @@ func testAccCheckGrantRoleExists(grantName, roleName, granteeName string) resour

func testAccCheckGrantRoleRevoked(roleName, granteeName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
db := testAccProvider.Meta().(*sqlx.DB)
_, err := db.Exec(fmt.Sprintf(`REVOKE %[1]s FROM %[2]s;`, roleName, granteeName))
meta := testAccProvider.Meta()
db, err := utils.GetDBClientFromMeta(meta, nil)
if err != nil {
return fmt.Errorf("error getting DB client: %s", err)
}
_, err = db.Exec(fmt.Sprintf(`REVOKE %[1]s FROM %[2]s;`, roleName, granteeName))
return err
}
}
16 changes: 12 additions & 4 deletions pkg/provider/acceptance_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"testing"

"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/utils"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/jmoiron/sqlx"
)

func TestAccRole_basic(t *testing.T) {
Expand Down Expand Up @@ -105,18 +105,26 @@ resource "materialize_role" "test" {

func testAccCheckRoleExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
db := testAccProvider.Meta().(*sqlx.DB)
meta := testAccProvider.Meta()
db, err := utils.GetDBClientFromMeta(meta, nil)
if err != nil {
return fmt.Errorf("error getting DB client: %s", err)
}
r, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("role not found: %s", name)
}
_, err := materialize.ScanRole(db, r.Primary.ID)
_, err = materialize.ScanRole(db, r.Primary.ID)
return err
}
}

func testAccCheckAllRolesDestroyed(s *terraform.State) error {
db := testAccProvider.Meta().(*sqlx.DB)
meta := testAccProvider.Meta()
db, err := utils.GetDBClientFromMeta(meta, nil)
if err != nil {
return fmt.Errorf("error getting DB client: %s", err)
}

for _, r := range s.RootModule().Resources {
if r.Type != "materialize_role" {
Expand Down

0 comments on commit 53ff098

Please sign in to comment.