From 85a3ba382cb6501e2167af4c48b0b6a4be30bcd3 Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Sun, 25 Sep 2022 13:45:12 +1200 Subject: [PATCH] Test total_shards_per_node only when supported --- internal/elasticsearch/index/ilm_test.go | 88 +++++++++++++++++++- internal/elasticsearch/security/user_test.go | 10 ++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/internal/elasticsearch/index/ilm_test.go b/internal/elasticsearch/index/ilm_test.go index 27e6a3207..43948eca1 100644 --- a/internal/elasticsearch/index/ilm_test.go +++ b/internal/elasticsearch/index/ilm_test.go @@ -1,16 +1,21 @@ package index_test import ( + "encoding/json" "fmt" + "io" "testing" "github.com/elastic/terraform-provider-elasticstack/internal/acctest" "github.com/elastic/terraform-provider-elasticstack/internal/clients" + "github.com/hashicorp/go-version" sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) +var totalShardsPerNodeVersionLimit = version.Must(version.NewVersion("7.16.0")) + func TestAccResourceILM(t *testing.T) { // generate a random policy name policyName := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum) @@ -52,14 +57,56 @@ func TestAccResourceILM(t *testing.T) { resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "warm.0.readonly.#", "1"), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "warm.0.allocate.#", "1"), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "warm.0.allocate.0.number_of_replicas", "1"), - resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "warm.0.allocate.0.total_shards_per_node", "200"), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "cold.#", "0"), resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "frozen.#", "0"), ), }, + { + SkipFunc: serverVersionLessThanTotalShardsPerNodeLimit, + Config: testAccResourceILMTotalShardsPerNode(policyName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "name", policyName), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "warm.#", "1"), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "warm.0.min_age", "0ms"), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "warm.0.set_priority.0.priority", "60"), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "warm.0.readonly.#", "1"), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "warm.0.allocate.#", "1"), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "warm.0.allocate.0.number_of_replicas", "1"), + resource.TestCheckResourceAttr("elasticstack_elasticsearch_index_lifecycle.test", "warm.0.allocate.0.total_shards_per_node", "200"), + ), + }, }, }) } +func serverVersionLessThanTotalShardsPerNodeLimit() (bool, error) { + client := acctest.ApiClient() + res, err := client.GetESClient().Info() + + if err != nil { + return false, err + } + + defer res.Body.Close() + + if res.IsError() { + body, err := io.ReadAll(res.Body) + return false, fmt.Errorf("failed to check elasticsearch version %s %s", err, body) + } + + var body map[string]interface{} + // Deserialize the response into a map. + if err := json.NewDecoder(res.Body).Decode(&body); err != nil { + return false, fmt.Errorf("failed to parse the elasticsearch info body %w", err) + } + + rawVersion := body["version"].(map[string]interface{})["number"].(string) + serverVersion, err := version.NewVersion(rawVersion) + if err != nil { + return false, fmt.Errorf("failed to parse the elasticsearch version %w", err) + } + + return serverVersion.LessThan(totalShardsPerNodeVersionLimit), nil +} func testAccResourceILMCreate(name string) string { return fmt.Sprintf(` @@ -97,6 +144,45 @@ provider "elasticstack" { elasticsearch {} } +resource "elasticstack_elasticsearch_index_lifecycle" "test" { + name = "%s" + + hot { + min_age = "1h" + + set_priority { + priority = 0 + } + + rollover { + max_age = "2d" + } + } + + warm { + min_age = "0ms" + set_priority { + priority = 60 + } + readonly {} + allocate { + exclude = jsonencode({ + box_type = "hot" + }) + number_of_replicas = 1 + } + } + +} + `, name) +} + +func testAccResourceILMTotalShardsPerNode(name string) string { + return fmt.Sprintf(` +provider "elasticstack" { + elasticsearch {} +} + resource "elasticstack_elasticsearch_index_lifecycle" "test" { name = "%s" diff --git a/internal/elasticsearch/security/user_test.go b/internal/elasticsearch/security/user_test.go index 583bba74d..b5e5674b5 100644 --- a/internal/elasticsearch/security/user_test.go +++ b/internal/elasticsearch/security/user_test.go @@ -58,9 +58,11 @@ func TestAccImportedUserDoesNotResetPassword(t *testing.T) { resp, err := client.GetESClient().Security.PutUser(username, strings.NewReader(body)) if err != nil { - return false, nil + return false, err } + defer resp.Body.Close() + if resp.IsError() { body, err := io.ReadAll(resp.Body) return false, fmt.Errorf("failed to manually create import test user [%s] %s %s", username, body, err) @@ -121,6 +123,8 @@ func TestAccImportedUserDoesNotResetPassword(t *testing.T) { return false, nil } + defer resp.Body.Close() + if resp.IsError() { body, err := io.ReadAll(resp.Body) return false, fmt.Errorf("failed to manually change import test user password [%s] %s %s", username, body, err) @@ -151,6 +155,8 @@ func checkUserCanAuthenticate(username string, password string) func(*terraform. return err } + defer resp.Body.Close() + if resp.IsError() { body, err := io.ReadAll(resp.Body) @@ -220,6 +226,8 @@ func checkResourceSecurityUserDestroy(s *terraform.State) error { return err } + defer res.Body.Close() + if res.StatusCode != 404 { return fmt.Errorf("User (%s) still exists", compId.ResourceId) }