Skip to content

Commit

Permalink
Add disappears acceptance test for index resource. (#21)
Browse files Browse the repository at this point in the history
* add disappears test
  • Loading branch information
skyscrapr authored Apr 15, 2024
1 parent 65f6076 commit 666b8ea
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 4 deletions.
14 changes: 13 additions & 1 deletion pinecone/provider/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
6 changes: 3 additions & 3 deletions pinecone/provider/index_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
}
Expand Down Expand Up @@ -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
}

Expand Down
84 changes: 84 additions & 0 deletions pinecone/provider/index_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 666b8ea

Please sign in to comment.