Skip to content

Commit

Permalink
add indices_resource tests retry mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
RaynorChavez committed Jul 16, 2024
1 parent 5453690 commit 6021d4f
Showing 1 changed file with 73 additions and 9 deletions.
82 changes: 73 additions & 9 deletions internal/provider/indices_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package provider

import (
"fmt"
"os"
"terraform-provider-marqo/marqo"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAccResourceIndex(t *testing.T) {
Expand All @@ -14,9 +18,9 @@ func TestAccResourceIndex(t *testing.T) {
Steps: []resource.TestStep{
// Create and Read testing
{
Config: testAccResourceIndexConfig("example_index_3"),
Config: testAccResourceIndexConfig("example_index_6"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("marqo_index.test", "index_name", "example_index_3"),
resource.TestCheckResourceAttr("marqo_index.test", "index_name", "example_index_6"),
resource.TestCheckResourceAttr("marqo_index.test", "settings.type", "unstructured"),
resource.TestCheckResourceAttr("marqo_index.test", "settings.vector_numeric_type", "float"),
resource.TestCheckResourceAttr("marqo_index.test", "settings.treat_urls_and_pointers_as_images", "true"),
Expand All @@ -34,20 +38,36 @@ func TestAccResourceIndex(t *testing.T) {
resource.TestCheckResourceAttr("marqo_index.test", "settings.ann_parameters.parameters.ef_construction", "512"),
resource.TestCheckResourceAttr("marqo_index.test", "settings.ann_parameters.parameters.m", "16"),
resource.TestCheckResourceAttr("marqo_index.test", "settings.filter_string_max_length", "20"),
testAccCheckIndexIsReady("example_index_6"),
func(s *terraform.State) error {
fmt.Println("Create and Read testing completed")
return nil
},
),
},
// ImportState testing
{
ResourceName: "marqo_index.test",
ImportState: true,
ImportStateVerify: true,
},
/*
{
ResourceName: "marqo_index.test",
ImportState: true,
ImportStateVerify: true,
},
*/
// Update and Read testing
{
Config: testAccResourceIndexConfig("example_index_3_updated"),
Config: testAccResourceIndexConfig("example_index_6_updated"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("marqo_index.test", "index_name", "example_index_3_updated"),
func(s *terraform.State) error {
fmt.Println("Starting Update and Read testing")
return nil
},
resource.TestCheckResourceAttr("marqo_index.test", "index_name", "example_index_6_updated"),
resource.TestCheckResourceAttr("marqo_index.test", "settings.inference_type", "marqo.CPU.large"),
testAccCheckIndexIsReady("example_index_6_updated"),
func(s *terraform.State) error {
fmt.Println("Update and Read testing completed")
return nil
},
),
},
// Delete testing automatically occurs in TestCase
Expand Down Expand Up @@ -89,3 +109,47 @@ resource "marqo_index" "test" {
}
`, name)
}

func testAccCheckIndexIsReady(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Get environment variables
host := os.Getenv("MARQO_HOST")
apiKey := os.Getenv("MARQO_API_KEY")

// Create a new Marqo client
client, err := marqo.NewClient(&host, &apiKey)
if err != nil {
return fmt.Errorf("Error creating Marqo client: %s", err)
}

timeout := time.After(10 * time.Minute)
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()

fmt.Printf("Waiting for index %s to be ready...\n", name)

for {
select {
case <-timeout:
return fmt.Errorf("Index %s did not become ready within the timeout period", name)
case <-ticker.C:
indices, err := client.ListIndices()
if err != nil {
fmt.Printf("Error listing indices: %s\n", err)
continue
}
for _, index := range indices {
if index.IndexName == name {
fmt.Printf("Index %s status: %s\n", name, index.IndexStatus)
if index.IndexStatus == "READY" {
fmt.Printf("Index %s is now ready\n", name)
return nil
}
break
}
}
fmt.Printf("Index %s not ready yet, continuing to wait...\n", name)
}
}
}
}

0 comments on commit 6021d4f

Please sign in to comment.