|
5 | 5 | "testing" |
6 | 6 |
|
7 | 7 | "github.com/elastic/terraform-provider-elasticstack/internal/acctest" |
| 8 | + "github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics" |
8 | 9 | "github.com/elastic/terraform-provider-elasticstack/internal/versionutils" |
9 | 10 | "github.com/hashicorp/go-version" |
10 | 11 | sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" |
@@ -339,6 +340,46 @@ resource "elasticstack_kibana_synthetics_monitor" "%s" { |
339 | 340 | playwright_options = jsonencode({"httpCredentials":{"password":"test","username":"test"},"ignoreHTTPSErrors":false}) |
340 | 341 | } |
341 | 342 | } |
| 343 | +` |
| 344 | + |
| 345 | + httpMonitorLabelsConfig = ` |
| 346 | +resource "elasticstack_kibana_synthetics_monitor" "%s" { |
| 347 | + name = "TestHttpMonitorLabels - %s" |
| 348 | + private_locations = [elasticstack_kibana_synthetics_private_location.%s.label] |
| 349 | + labels = { |
| 350 | + environment = "production" |
| 351 | + team = "platform" |
| 352 | + service = "web-app" |
| 353 | + } |
| 354 | + http = { |
| 355 | + url = "http://localhost:5601" |
| 356 | + } |
| 357 | +} |
| 358 | +` |
| 359 | + |
| 360 | + httpMonitorLabelsUpdated = ` |
| 361 | +resource "elasticstack_kibana_synthetics_monitor" "%s" { |
| 362 | + name = "TestHttpMonitorLabels Updated - %s" |
| 363 | + private_locations = [elasticstack_kibana_synthetics_private_location.%s.label] |
| 364 | + labels = { |
| 365 | + environment = "staging" |
| 366 | + team = "platform-updated" |
| 367 | + service = "web-app-v2" |
| 368 | + } |
| 369 | + http = { |
| 370 | + url = "http://localhost:5601" |
| 371 | + } |
| 372 | +} |
| 373 | +` |
| 374 | + |
| 375 | + httpMonitorLabelsRemoved = ` |
| 376 | +resource "elasticstack_kibana_synthetics_monitor" "%s" { |
| 377 | + name = "TestHttpMonitorLabels Removed - %s" |
| 378 | + private_locations = [elasticstack_kibana_synthetics_private_location.%s.label] |
| 379 | + http = { |
| 380 | + url = "http://localhost:5601" |
| 381 | + } |
| 382 | +} |
342 | 383 | ` |
343 | 384 | ) |
344 | 385 |
|
@@ -828,6 +869,71 @@ func TestSyntheticMonitorBrowserResource(t *testing.T) { |
828 | 869 | }) |
829 | 870 | } |
830 | 871 |
|
| 872 | +func TestSyntheticMonitorLabelsResource(t *testing.T) { |
| 873 | + name := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlphaNum) |
| 874 | + id := "http-monitor-labels" |
| 875 | + labelsMonitorId, labelsConfig := testMonitorConfig(id, httpMonitorLabelsConfig, name) |
| 876 | + _, labelsConfigUpdated := testMonitorConfig(id, httpMonitorLabelsUpdated, name) |
| 877 | + _, labelsConfigRemoved := testMonitorConfig(id, httpMonitorLabelsRemoved, name) |
| 878 | + |
| 879 | + resource.Test(t, resource.TestCase{ |
| 880 | + PreCheck: func() { acctest.PreCheck(t) }, |
| 881 | + ProtoV6ProviderFactories: acctest.Providers, |
| 882 | + Steps: []resource.TestStep{ |
| 883 | + // Create and Read monitor with labels |
| 884 | + { |
| 885 | + SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion), |
| 886 | + Config: labelsConfig, |
| 887 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 888 | + resource.TestCheckResourceAttrSet(labelsMonitorId, "id"), |
| 889 | + resource.TestCheckResourceAttr(labelsMonitorId, "name", "TestHttpMonitorLabels - "+name), |
| 890 | + resource.TestCheckResourceAttr(labelsMonitorId, "labels.%", "3"), |
| 891 | + resource.TestCheckResourceAttr(labelsMonitorId, "labels.environment", "production"), |
| 892 | + resource.TestCheckResourceAttr(labelsMonitorId, "labels.team", "platform"), |
| 893 | + resource.TestCheckResourceAttr(labelsMonitorId, "labels.service", "web-app"), |
| 894 | + resource.TestCheckResourceAttr(labelsMonitorId, "http.url", "http://localhost:5601"), |
| 895 | + ), |
| 896 | + }, |
| 897 | + // ImportState testing |
| 898 | + { |
| 899 | + SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion), |
| 900 | + ResourceName: labelsMonitorId, |
| 901 | + ImportState: true, |
| 902 | + ImportStateVerify: true, |
| 903 | + Config: labelsConfig, |
| 904 | + }, |
| 905 | + // Update labels - change values but keep same keys |
| 906 | + { |
| 907 | + SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion), |
| 908 | + Config: labelsConfigUpdated, |
| 909 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 910 | + resource.TestCheckResourceAttrSet(labelsMonitorId, "id"), |
| 911 | + resource.TestCheckResourceAttr(labelsMonitorId, "name", "TestHttpMonitorLabels Updated - "+name), |
| 912 | + resource.TestCheckResourceAttr(labelsMonitorId, "labels.%", "3"), |
| 913 | + resource.TestCheckResourceAttr(labelsMonitorId, "labels.environment", "staging"), |
| 914 | + resource.TestCheckResourceAttr(labelsMonitorId, "labels.team", "platform-updated"), |
| 915 | + resource.TestCheckResourceAttr(labelsMonitorId, "labels.service", "web-app-v2"), |
| 916 | + ), |
| 917 | + }, |
| 918 | + // Remove all labels - this tests the round-trip consistency fix |
| 919 | + { |
| 920 | + SkipFunc: versionutils.CheckIfVersionIsUnsupported(synthetics.MinLabelsVersion), |
| 921 | + Config: labelsConfigRemoved, |
| 922 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 923 | + resource.TestCheckResourceAttrSet(labelsMonitorId, "id"), |
| 924 | + resource.TestCheckResourceAttr(labelsMonitorId, "name", "TestHttpMonitorLabels Removed - "+name), |
| 925 | + resource.TestCheckNoResourceAttr(labelsMonitorId, "labels.%"), |
| 926 | + resource.TestCheckNoResourceAttr(labelsMonitorId, "labels.environment"), |
| 927 | + resource.TestCheckNoResourceAttr(labelsMonitorId, "labels.team"), |
| 928 | + resource.TestCheckNoResourceAttr(labelsMonitorId, "labels.service"), |
| 929 | + resource.TestCheckNoResourceAttr(labelsMonitorId, "labels.version"), |
| 930 | + ), |
| 931 | + }, |
| 932 | + // Delete testing automatically occurs in TestCase |
| 933 | + }, |
| 934 | + }) |
| 935 | +} |
| 936 | + |
831 | 937 | func testMonitorConfig(id, cfg, name string) (string, string) { |
832 | 938 |
|
833 | 939 | resourceId := "elasticstack_kibana_synthetics_monitor." + id |
|
0 commit comments