From 666b8ead0c2c49ac4775ac4ec237c6b46100563f Mon Sep 17 00:00:00 2001 From: SkyScrapr Date: Tue, 16 Apr 2024 10:18:35 +1200 Subject: [PATCH] Add disappears acceptance test for index resource. (#21) * add disappears test --- pinecone/provider/common_test.go | 14 +++- pinecone/provider/index_resource.go | 6 +- pinecone/provider/index_resource_test.go | 84 ++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 4 deletions(-) diff --git a/pinecone/provider/common_test.go b/pinecone/provider/common_test.go index a6f6f06..4f79fc1 100644 --- a/pinecone/provider/common_test.go +++ b/pinecone/provider/common_test.go @@ -5,12 +5,24 @@ package provider import ( "context" + "os" + "testing" + "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/pinecone-io/go-pinecone/pinecone" - "testing" ) +// NewTestClient returns a new Pinecone API client instance +// to be used in acceptance tests. +func NewTestClient() (*pinecone.Client, error) { + apiKey := os.Getenv("PINECONE_API_KEY") + + return pinecone.NewClient(pinecone.NewClientParams{ + ApiKey: apiKey, + }) +} + func TestDatasource_Configure(t *testing.T) { // Create a test *pinecone.Client testClient := &pinecone.Client{} diff --git a/pinecone/provider/index_resource.go b/pinecone/provider/index_resource.go index dbe56c6..38111cc 100644 --- a/pinecone/provider/index_resource.go +++ b/pinecone/provider/index_resource.go @@ -310,8 +310,6 @@ func (r *IndexResource) Create(ctx context.Context, req resource.CreateRequest, return } - // resp.Diagnostics.Append(data.Read(ctx, index)...) - // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } @@ -358,7 +356,9 @@ func (r *IndexResource) Delete(ctx context.Context, req resource.DeleteRequest, err := r.client.DeleteIndex(ctx, data.Name.ValueString()) if err != nil { - resp.Diagnostics.AddError("Failed to delete index", err.Error()) + if !strings.Contains(err.Error(), "not found") { + resp.Diagnostics.AddError("Failed to delete index", err.Error()) + } return } diff --git a/pinecone/provider/index_resource_test.go b/pinecone/provider/index_resource_test.go index 76e4509..a7bc456 100644 --- a/pinecone/provider/index_resource_test.go +++ b/pinecone/provider/index_resource_test.go @@ -4,11 +4,16 @@ package provider import ( + "context" "fmt" + "strings" "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "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/pinecone-io/go-pinecone/pinecone" ) func TestAccIndexResource_serverless(t *testing.T) { @@ -117,6 +122,85 @@ func TestAccIndexResource_dimension(t *testing.T) { }) } +func TestAccIndexResource_disappears(t *testing.T) { + rName := acctest.RandomWithPrefix("tftest") + resourceName := "pinecone_index.test" + + var index pinecone.Index + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + // CheckDestroy: testAccCheckIndexDestroy(ctx), + Steps: []resource.TestStep{ + // Create and Read testing + { + Config: testAccIndexResourceConfig_serverless(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckIndexExists(resourceName, &index), + testAccDeleteIndex(resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func testAccCheckIndexExists(resourceName string, index *pinecone.Index) resource.TestCheckFunc { + return func(state *terraform.State) error { + indexResource, found := state.RootModule().Resources[resourceName] + if !found { + return fmt.Errorf("Resource not found in state: %s", resourceName) + } + + // Create a new client, and use the default configurations from the environment + c, _ := NewTestClient() + + fetchedIndex, err := c.DescribeIndex(context.Background(), indexResource.Primary.ID) + if err != nil { + return fmt.Errorf("Error describing index: %w", err) + } + if fetchedIndex == nil { + return fmt.Errorf("Index not found for ID: %s", indexResource.Primary.ID) + } + + *index = *fetchedIndex + + return nil + } +} + +func testAccDeleteIndex(resourceName string) resource.TestCheckFunc { + return func(state *terraform.State) error { + indexResource, found := state.RootModule().Resources[resourceName] + if !found { + return fmt.Errorf("Resource not found in state: %s", resourceName) + } + + // Create a new client, and use the default configurations from the environment + c, _ := NewTestClient() + + err := c.DeleteIndex(context.Background(), indexResource.Primary.ID) + if err != nil { + return fmt.Errorf("Error deleting index: %w", err) + } + + ctx := context.TODO() + deleteTimeout := defaultIndexDeleteTimeout + err = retry.RetryContext(ctx, deleteTimeout, func() *retry.RetryError { + index, err := c.DescribeIndex(ctx, indexResource.Primary.ID) + if err != nil { + if strings.Contains(err.Error(), "not found") { + return nil + } + return retry.NonRetryableError(err) + } + return retry.RetryableError(fmt.Errorf("index not deleted. State: %s", index.Status.State)) + }) + return err + } +} + func testAccIndexResourceConfig_serverless(name string) string { return fmt.Sprintf(` provider "pinecone" {