Skip to content

Commit

Permalink
try adding some tests
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinav Tamaskar <abhinav@narmi.com>
  • Loading branch information
atammy-narmi committed Jan 8, 2024
1 parent 7f06459 commit 60209bd
Showing 1 changed file with 68 additions and 46 deletions.
114 changes: 68 additions & 46 deletions provider/resource_opensearch_monitor_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package provider

import (
"encoding/json"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

Expand Down Expand Up @@ -39,13 +41,29 @@ func testCheckOpensearchMonitorExists(name string) resource.TestCheckFunc {

meta := testAccOpendistroProvider.Meta()

var err error
_, err = resourceOpensearchOpenDistroGetMonitor(rs.Primary.ID, meta.(*ProviderConf))
resp, err := resourceOpensearchOpenDistroGetMonitor(rs.Primary.ID, meta.(*ProviderConf))

if err != nil {
return err
}

monitorJson, err := json.Marshal(resp.Monitor)

if err != nil {
return err
}

monitorJsonNormalized, err := structure.NormalizeJsonString(string(monitorJson))
if err != nil {
return err
}

diff := diffSuppressMonitor("", monitorJsonNormalized, testAccOpensearchOpenDistroMonitorJSON, nil)

if !diff {
return fmt.Errorf("Monitor does not match")
}

return nil
}
}
Expand All @@ -71,53 +89,57 @@ func testCheckOpensearchMonitorDestroy(s *terraform.State) error {
return nil
}

var testAccOpensearchOpenDistroMonitorJSON = `
{
"name": "test-monitor",
"type": "monitor",
"monitor_type": "query_level_monitor",
"enabled": true,
"schedule": {
"period": {
"interval": 1,
"unit": "MINUTES"
}
},
"inputs": [
{
"search": {
"indices": ["*"],
"query": {
"size": 0,
"aggregations": {},
"query": {
"bool": {
"adjust_pure_negative": true,
"boost": 1,
"filter": [
{
"range": {
"@timestamp": {
"boost": 1,
"from": "||-1h",
"to": "",
"include_lower": true,
"include_upper": true,
"format": "epoch_millis"
}
}
}
]
}
}
}
}
}
],
"triggers": []
}
`

var testAccOpensearchOpenDistroMonitor = `
resource "opensearch_monitor" "test_monitor" {
body = <<EOF
{
"name": "test-monitor",
"type": "monitor",
"monitor_type": "query_level_monitor",
"enabled": true,
"schedule": {
"period": {
"interval": 1,
"unit": "MINUTES"
}
},
"inputs": [
{
"search": {
"indices": ["*"],
"query": {
"size": 0,
"aggregations": {},
"query": {
"bool": {
"adjust_pure_negative": true,
"boost": 1,
"filter": [
{
"range": {
"@timestamp": {
"boost": 1,
"from": "||-1h",
"to": "",
"include_lower": true,
"include_upper": true,
"format": "epoch_millis"
}
}
}
]
}
}
}
}
}
],
"triggers": []
}
` + testAccOpensearchOpenDistroMonitorJSON + `
EOF
}
`

0 comments on commit 60209bd

Please sign in to comment.