Skip to content

Commit e04cff0

Browse files
Fix SM scripted check test (#1632)
* Fix SM scripted check test It was broken in the last commit here: #1456 * go generate * More fixes
1 parent 0a07747 commit e04cff0

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

docs/resources/synthetic_monitoring_check.md

+3-11
Original file line numberDiff line numberDiff line change
@@ -838,16 +838,6 @@ resource "grafana_synthetic_monitoring_check" "multihttp" {
838838
```terraform
839839
data "grafana_synthetic_monitoring_probes" "main" {}
840840
841-
data "local_file" "script" {
842-
// `script.js` is a file in the same directory as this file and contains the
843-
// script to be executed.
844-
//
845-
// The content of the file will be read and stored in the content attribute.
846-
// The content attribute is a string, so it can be used directly in the
847-
// settings block below.
848-
filename = "${path.module}/script.js"
849-
}
850-
851841
resource "grafana_synthetic_monitoring_check" "scripted" {
852842
job = "Validate homepage"
853843
target = "https://grafana.com/"
@@ -860,7 +850,9 @@ resource "grafana_synthetic_monitoring_check" "scripted" {
860850
}
861851
settings {
862852
scripted {
863-
script = data.local_file.script.content
853+
// `script.js` is a file in the same directory as this file and contains the
854+
// script to be executed.
855+
script = file("${path.module}/script.js")
864856
}
865857
}
866858
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
data "grafana_synthetic_monitoring_probes" "main" {}
22

3-
data "local_file" "script" {
4-
// `script.js` is a file in the same directory as this file and contains the
5-
// script to be executed.
6-
//
7-
// The content of the file will be read and stored in the content attribute.
8-
// The content attribute is a string, so it can be used directly in the
9-
// settings block below.
10-
filename = "${path.module}/script.js"
11-
}
12-
133
resource "grafana_synthetic_monitoring_check" "scripted" {
144
job = "Validate homepage"
155
target = "https://grafana.com/"
@@ -22,7 +12,9 @@ resource "grafana_synthetic_monitoring_check" "scripted" {
2212
}
2313
settings {
2414
scripted {
25-
script = data.local_file.script.content
15+
// `script.js` is a file in the same directory as this file and contains the
16+
// script to be executed.
17+
script = file("${path.module}/script.js")
2618
}
2719
}
2820
}

internal/resources/syntheticmonitoring/resource_check_test.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package syntheticmonitoring_test
22

33
import (
44
"context"
5+
"os"
6+
"path/filepath"
57
"regexp"
68
"strconv"
79
"testing"
@@ -11,6 +13,7 @@ import (
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1214
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
16+
"github.com/stretchr/testify/require"
1417
)
1518

1619
func TestAccResourceCheck_dns(t *testing.T) {
@@ -368,10 +371,17 @@ func TestAccResourceCheck_multihttp(t *testing.T) {
368371
func TestAccResourceCheck_scripted(t *testing.T) {
369372
testutils.CheckCloudInstanceTestsEnabled(t)
370373

374+
// Find and replace the path.module since it's not available in the test environment
375+
scriptFilepathAbs, err := filepath.Abs("../../../examples/resources/grafana_synthetic_monitoring_check")
376+
require.NoError(t, err)
377+
scriptFileContent, err := os.ReadFile(filepath.Join(scriptFilepathAbs, "script.js"))
378+
require.NoError(t, err)
379+
371380
// Inject random job names to avoid conflicts with other tests
372381
jobName := acctest.RandomWithPrefix("scripted")
373382
nameReplaceMap := map[string]string{
374-
`"Scripted defaults"`: strconv.Quote(jobName),
383+
`"Validate homepage"`: strconv.Quote(jobName),
384+
"${path.module}": scriptFilepathAbs,
375385
}
376386
resource.ParallelTest(t, resource.TestCase{
377387
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
@@ -382,11 +392,11 @@ func TestAccResourceCheck_scripted(t *testing.T) {
382392
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_check.scripted", "id"),
383393
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_check.scripted", "tenant_id"),
384394
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.scripted", "job", jobName),
385-
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.scripted", "target", "scripted target"),
395+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.scripted", "target", "https://grafana.com/"),
386396
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.scripted", "timeout", "5000"), // scripted has a default timeout of 5000
387397
resource.TestCheckResourceAttrSet("grafana_synthetic_monitoring_check.scripted", "probes.0"),
388-
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.scripted", "labels.foo", "bar"),
389-
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.scripted", "settings.0.scripted.0.script", "console.log('Hello, world!')"),
398+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.scripted", "labels.environment", "production"),
399+
resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.scripted", "settings.0.scripted.0.script", string(scriptFileContent)),
390400
),
391401
},
392402
{

0 commit comments

Comments
 (0)