-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle 404s during Read() correctly
re: #53 When a resource in the statefile has been deleted out of band, such as someone on the console or using pscale cli deleting it, terraform should remove it from state and plan to recreate it. However, we were treating 404s as fatal. To solve this, the `Read()` method in each of our four resources now checks for 404 and removes the resource from state so that terraform will offer to recreate the resource This PR also adds initial acceptance test coverage for `database`, `password`, and `branch` resources, including tests for the out-of-band deletion handling fixed by this PR. The `backup` resource is currently not working and so tests for it will be added in later PRs.
- Loading branch information
Showing
15 changed files
with
413 additions
and
34 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
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
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
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,64 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestAccBranchResource(t *testing.T) { | ||
// TODO: This test currently fails because the provider returns immediately | ||
// after DB creation but the DB is still pending and so the branch creation | ||
// will fail. Unblock and finish this test once this issue is resolved. | ||
t.Skip() | ||
|
||
dbName := acctest.RandomWithPrefix("testacc-branch") | ||
branchName := "two" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Create and Read testing | ||
{ | ||
Config: testAccBranchResourceConfig(dbName, branchName), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("planetscale_branch.two", "name", branchName), | ||
resource.TestCheckResourceAttr("planetscale_branch.two", "parent_branch", "main"), | ||
resource.TestCheckResourceAttr("planetscale_branch.two", "sharded", "false"), | ||
), | ||
}, | ||
// ImportState testing | ||
{ | ||
ResourceName: "planetscale_branch.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
// Update and Read testing | ||
// TODO: Implement an update test. | ||
// Delete testing automatically occurs in TestCase | ||
}, | ||
}) | ||
} | ||
|
||
// TODO: implement an out of bound deletion test like we have in the password and database tests. | ||
|
||
func testAccBranchResourceConfig(dbName, branchName string) string { | ||
return fmt.Sprintf(` | ||
resource "planetscale_database" "test" { | ||
organization = "%s" | ||
name = "%s" | ||
cluster_size = "PS-10" | ||
default_branch = "main" | ||
} | ||
resource "planetscale_branch" "two" { | ||
organization = "%s" | ||
database = planetscale_database.test.name | ||
name = "%s" | ||
parent_branch = planetscale_database.test.default_branch | ||
} | ||
`, testAccOrg, dbName, testAccOrg, branchName) | ||
} |
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,98 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestAccDatabaseResource(t *testing.T) { | ||
dbName := acctest.RandomWithPrefix("testacc-db") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Create and Read testing | ||
{ | ||
Config: testAccDatabaseResourceConfig(dbName, "PS-10"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// resource.TestCheckResourceAttr("planetscale_database.test", "id", "todo"), | ||
resource.TestCheckResourceAttr("planetscale_database.test", "production_branches_count", "1"), | ||
resource.TestCheckResourceAttr("planetscale_database.test", "default_branch", "main"), | ||
resource.TestCheckResourceAttr("planetscale_database.test", "cluster_size", "PS-10"), | ||
), | ||
}, | ||
// ImportState testing | ||
{ | ||
ResourceName: "planetscale_database.test", | ||
ImportStateId: fmt.Sprintf("%s,%s", testAccOrg, dbName), | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
// TODO: API does not return cluster_size which causes a diff on import. When fixed, remove this: | ||
ImportStateVerifyIgnore: []string{"cluster_size"}, | ||
}, | ||
// Update and Read testing | ||
{ | ||
Config: testAccDatabaseResourceConfig(dbName, "PS-20"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
// resource.TestCheckResourceAttr("planetscale_database.test", "id", "todo"), | ||
resource.TestCheckResourceAttr("planetscale_database.test", "production_branches_count", "1"), | ||
resource.TestCheckResourceAttr("planetscale_database.test", "default_branch", "main"), | ||
resource.TestCheckResourceAttr("planetscale_database.test", "cluster_size", "PS-20"), | ||
), | ||
}, | ||
// Delete testing automatically occurs in TestCase | ||
}, | ||
}) | ||
} | ||
|
||
// TestAccDatabaseResource_outOfBandDelete tests the out-of-band deletion of the database. | ||
// In this test we simulate the remote database has been deleted out of band, perhaps by | ||
// a user on the console or using pscale CLI. | ||
// https://github.com/planetscale/terraform-provider-planetscale/issues/53 | ||
func TestAccDatabaseResource_OutOfBandDelete(t *testing.T) { | ||
dbName := acctest.RandomWithPrefix("testacc-db-oob") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Create and Read testing | ||
{ | ||
Config: testAccDatabaseResourceConfig(dbName, "PS-10"), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("planetscale_database.test", "production_branches_count", "1"), | ||
resource.TestCheckResourceAttr("planetscale_database.test", "default_branch", "main"), | ||
resource.TestCheckResourceAttr("planetscale_database.test", "cluster_size", "PS-10"), | ||
), | ||
}, | ||
// Test out-of-bands deletion of the database should produce a plan to recreate, not error. | ||
{ | ||
ResourceName: "planetscale_database.test", | ||
RefreshState: true, | ||
ExpectNonEmptyPlan: true, | ||
PreConfig: func() { | ||
ctx := context.Background() | ||
_, err := testAccAPIClient.DeleteDatabase(ctx, testAccOrg, dbName) | ||
if err != nil { | ||
t.Fatalf("PreConfig: failed to delete database: %s", err) | ||
} | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDatabaseResourceConfig(dbName string, clusterSize string) string { | ||
return fmt.Sprintf(` | ||
resource "planetscale_database" "test" { | ||
organization = "%s" | ||
name = "%s" | ||
cluster_size = "%s" | ||
} | ||
`, testAccOrg, dbName, clusterSize) | ||
} |
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
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
Oops, something went wrong.