Skip to content

Commit

Permalink
Test total_shards_per_node only when supported
Browse files Browse the repository at this point in the history
  • Loading branch information
tobio committed Nov 10, 2022
1 parent 00f886c commit 85a3ba3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
88 changes: 87 additions & 1 deletion internal/elasticsearch/index/ilm_test.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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(`
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 9 additions & 1 deletion internal/elasticsearch/security/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 85a3ba3

Please sign in to comment.