Skip to content

Commit 311afbc

Browse files
committed
test: create locations on demand instead of using a static ones
Instead of using a static geo location ID that may not exist, we now create one first if needed.
1 parent e823f3b commit 311afbc

File tree

13 files changed

+215
-80
lines changed

13 files changed

+215
-80
lines changed

dynatrace/api/v1/config/metrics/calculated/web/service_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,31 @@ import (
2323
"testing"
2424

2525
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/testing/api"
26+
"github.com/dynatrace-oss/terraform-provider-dynatrace/provider"
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
28+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2629
)
2730

2831
func TestAccCalculatedWebMetrics(t *testing.T) {
29-
api.TestAcc(t)
32+
if !api.AccEnvsGiven(t) {
33+
return
34+
}
35+
36+
// combine all and have the web application creation once (web application takes longer than 1m30s)
37+
configA, _ := api.ReadTfConfig(t, "testdata/terraform/example-a.tf")
38+
configB, _ := api.ReadTfConfig(t, "testdata/terraform/example-b.tf")
39+
configC, _ := api.ReadTfConfig(t, "testdata/terraform/example-c.tf")
40+
configD, _ := api.ReadTfConfig(t, "testdata/terraform/example-d.tf")
41+
configE, _ := api.ReadTfConfig(t, "testdata/terraform/example-e.tf")
42+
configF, _ := api.ReadTfConfig(t, "testdata/terraform/example-f.tf")
43+
44+
testCase := resource.TestCase{
45+
ProviderFactories: map[string]func() (*schema.Provider, error){
46+
"dynatrace": func() (*schema.Provider, error) {
47+
return provider.Provider(), nil
48+
},
49+
},
50+
Steps: []resource.TestStep{{Config: configA + configB + configC + configD + configE + configF}},
51+
}
52+
resource.Test(t, testCase)
3053
}

dynatrace/api/v1/config/metrics/calculated/web/testdata/terraform/example-a.tf

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,74 @@
1-
resource "dynatrace_calculated_web_metric" "#name#" {
1+
data "dynatrace_synthetic_location" "location" {
2+
type = "PUBLIC"
3+
name = "Sydney"
4+
}
5+
6+
resource "dynatrace_web_application" "application" {
7+
name = "#name#"
8+
type = "AUTO_INJECTED"
9+
cost_control_user_session_percentage = 100
10+
load_action_key_performance_metric = "VISUALLY_COMPLETE"
11+
real_user_monitoring_enabled = true
12+
xhr_action_key_performance_metric = "VISUALLY_COMPLETE"
13+
custom_action_apdex_settings {
14+
frustrating_fallback_threshold = 12000
15+
frustrating_threshold = 12000
16+
tolerated_fallback_threshold = 3000
17+
tolerated_threshold = 3000
18+
}
19+
load_action_apdex_settings {
20+
frustrating_fallback_threshold = 12000
21+
frustrating_threshold = 12000
22+
tolerated_fallback_threshold = 3000
23+
tolerated_threshold = 3000
24+
}
25+
monitoring_settings {
26+
add_cross_origin_anonymous_attribute = true
27+
cache_control_header_optimizations = true
28+
injection_mode = "JAVASCRIPT_TAG"
29+
script_tag_cache_duration_in_hours = 1
30+
advanced_javascript_tag_settings {
31+
max_action_name_length = 100
32+
max_errors_to_capture = 10
33+
additional_event_handlers {
34+
max_dom_nodes = 5000
35+
}
36+
}
37+
content_capture {
38+
resource_timing_settings {
39+
instrumentation_delay = 53
40+
non_w3c_resource_timings = true
41+
w3c_resource_timings = true
42+
}
43+
timeout_settings {
44+
temporary_action_limit = 3
45+
temporary_action_total_timeout = 100
46+
timed_action_support = true
47+
}
48+
}
49+
}
50+
user_action_naming_settings {}
51+
waterfall_settings {
52+
resource_browser_caching_threshold = 50
53+
resources_threshold = 100000
54+
slow_cnd_resources_threshold = 200000
55+
slow_first_party_resources_threshold = 200000
56+
slow_third_party_resources_threshold = 200000
57+
speed_index_visually_complete_ratio_threshold = 50
58+
uncompressed_resources_threshold = 860
59+
}
60+
xhr_action_apdex_settings {
61+
frustrating_fallback_threshold = 12000
62+
frustrating_threshold = 12000
63+
tolerated_fallback_threshold = 3000
64+
tolerated_threshold = 3000
65+
}
66+
}
67+
68+
resource "dynatrace_calculated_web_metric" "metric" {
269
name = "#name#"
370
enabled = true
4-
app_identifier = "APPLICATION-EA7C4B59F27D43EB"
71+
app_identifier = dynatrace_web_application.application.id
572
metric_key = "calc:apps.web.#name#"
673
dimensions {
774
dimension {
@@ -14,7 +81,7 @@ resource "dynatrace_calculated_web_metric" "#name#" {
1481
metric = "VisuallyComplete"
1582
}
1683
user_action_filter {
17-
continent = "GEOLOCATION-970B6D0A98F55995"
84+
continent = data.dynatrace_synthetic_location.location.geo_location_id
1885
target_view_group_name_match_type = "Equals"
1986
target_view_name_match_type = "Equals"
2087
}

dynatrace/api/v1/config/metrics/calculated/web/testdata/terraform/example-b.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
resource "dynatrace_calculated_web_metric" "#name#" {
1+
resource "dynatrace_calculated_web_metric" "apdex" {
22
name = "#name#"
33
enabled = true
4-
app_identifier = "APPLICATION-EA7C4B59F27D43EB"
4+
app_identifier = dynatrace_web_application.application.id
55
metric_key = "calc:apps.web.#name#"
66
metric_definition {
77
metric = "Apdex"
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
resource "dynatrace_calculated_web_metric" "#name#" {
1+
resource "dynatrace_calculated_web_metric" "user_action_duration" {
22
name = "#name#"
33
enabled = true
4-
app_identifier = "APPLICATION-EA7C4B59F27D43EB"
4+
app_identifier = dynatrace_web_application.application.id
55
metric_key = "calc:apps.web.#name#"
66
metric_definition {
77
metric = "UserActionDuration"
@@ -11,4 +11,4 @@ resource "dynatrace_calculated_web_metric" "#name#" {
1111
target_view_name_match_type = "Equals"
1212
user_action_name = "Loading of page /easytravel/login"
1313
}
14-
}
14+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
resource "dynatrace_calculated_web_metric" "#name#" {
1+
resource "dynatrace_calculated_web_metric" "application_cache" {
22
name = "#name#"
33
enabled = true
4-
app_identifier = "APPLICATION-EA7C4B59F27D43EB"
4+
app_identifier = dynatrace_web_application.application.id
55
metric_key = "calc:apps.web.#name#"
66
metric_definition {
77
metric = "ApplicationCache"
@@ -12,4 +12,4 @@ resource "dynatrace_calculated_web_metric" "#name#" {
1212
target_view_group_name_match_type = "Equals"
1313
target_view_name_match_type = "Equals"
1414
}
15-
}
15+
}

dynatrace/api/v1/config/metrics/calculated/web/testdata/terraform/example-e.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
resource "dynatrace_calculated_web_metric" "#name#" {
1+
resource "dynatrace_calculated_web_metric" "error_count" {
22
name = "#name#"
33
enabled = true
4-
app_identifier = "APPLICATION-EA7C4B59F27D43EB"
4+
app_identifier = dynatrace_web_application.application.id
55
metric_key = "calc:apps.web.#name#"
66
metric_definition {
77
metric = "ErrorCount"

dynatrace/api/v1/config/metrics/calculated/web/testdata/terraform/example-f.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
resource "dynatrace_calculated_web_metric" "#name#" {
1+
resource "dynatrace_calculated_web_metric" "dimensions" {
22
name = "#name#"
33
enabled = true
4-
app_identifier = "APPLICATION-EA7C4B59F27D43EB"
4+
app_identifier = dynatrace_web_application.application.id
55
metric_key = "calc:apps.web.#name#"
66
dimensions {
77
dimension {
@@ -17,4 +17,4 @@ resource "dynatrace_calculated_web_metric" "#name#" {
1717
metric_definition {
1818
metric = "UserActionDuration"
1919
}
20-
}
20+
}

dynatrace/api/v1/config/synthetic/locations/service_test.go

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@
2020
package locations_test
2121

2222
import (
23-
"context"
24-
"encoding/json"
25-
"log"
2623
"os"
2724
"testing"
2825

2926
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api"
3027
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v1/config/synthetic/locations"
3128
locsettings "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v1/config/synthetic/locations/settings"
3229
"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/rest"
30+
api2 "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/testing/api"
31+
"github.com/dynatrace-oss/terraform-provider-dynatrace/provider"
32+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
33+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
34+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
35+
"github.com/stretchr/testify/assert"
36+
"github.com/stretchr/testify/require"
3337
)
3438

3539
func TestSyntheticLocations(t *testing.T) {
@@ -41,42 +45,39 @@ func TestSyntheticLocations(t *testing.T) {
4145
}
4246
credentials := &rest.Credentials{URL: envURL, Token: apiToken}
4347
service := locations.Service(credentials)
48+
config, _ := api2.ReadTfConfig(t, "testdata/location.tf")
4449
var stubs api.Stubs
45-
var err error
46-
if stubs, err = service.List(context.Background()); err != nil {
47-
t.Error(err)
48-
return
49-
}
50+
resource.Test(t, resource.TestCase{
51+
ProviderFactories: map[string]func() (*schema.Provider, error){
52+
"dynatrace": func() (*schema.Provider, error) {
53+
return provider.Provider(), nil
54+
},
55+
},
56+
Steps: []resource.TestStep{
57+
{
58+
Config: config,
59+
// fetch locations before private location is destroyed
60+
Check: func(state *terraform.State) error {
61+
var err error
62+
stubs, err = service.List(t.Context())
63+
return err
64+
},
65+
},
66+
},
67+
})
68+
5069
foundPublic := false
5170
foundPrivate := false
5271
for _, stub := range stubs {
53-
if stub.Value != nil {
54-
loc := stub.Value.(*locsettings.SyntheticLocation)
55-
if loc.Type == locsettings.LocationTypes.Public {
56-
foundPublic = true
57-
} else if loc.Type == locsettings.LocationTypes.Private {
58-
foundPrivate = true
59-
}
60-
}
61-
if stub.Value == nil {
62-
t.Error("Stubs were expected to contain values, but didn't")
63-
return
64-
}
65-
if stub.Value.(*locsettings.SyntheticLocation).ID != stub.ID {
66-
data, _ := json.Marshal(stub)
67-
log.Println("stub: " + string(data))
68-
data, _ = json.Marshal(stub.Value)
69-
log.Println("value: " + string(data))
70-
t.Error("ID of Stubs don't match ID of values")
71-
return
72+
require.NotNil(t, stub.Value, "Stubs were expected to contain values, but didn't")
73+
loc := stub.Value.(*locsettings.SyntheticLocation)
74+
if loc.Type == locsettings.LocationTypes.Public {
75+
foundPublic = true
76+
} else if loc.Type == locsettings.LocationTypes.Private {
77+
foundPrivate = true
7278
}
79+
require.Equal(t, stub.ID, stub.Value.(*locsettings.SyntheticLocation).ID, "ID of Stubs don't match ID of values. stub: %v", stub)
7380
}
74-
if !foundPublic {
75-
t.Error("Expected to find public synthetic locations - found none")
76-
return
77-
}
78-
if !foundPrivate {
79-
t.Error("Expected to find private synthetic locations - found none")
80-
return
81-
}
81+
assert.True(t, foundPublic, "Expected to find public synthetic locations - found none")
82+
assert.True(t, foundPrivate, "Expected to find private synthetic locations - found none")
8283
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "dynatrace_synthetic_location" "location" {
2+
name = "#name#"
3+
city = "San Francisco de Asis"
4+
country_code = "VE"
5+
region_code = "04"
6+
deployment_type = "STANDARD"
7+
latitude = 10.0756
8+
location_node_outage_delay_in_minutes = 3
9+
longitude = -67.5442
10+
}
Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
resource "dynatrace_http_monitor_script" "#name#" {
2-
http_id = "${dynatrace_http_monitor.monitor.id}"
3-
script {
4-
request {
5-
description = "request1"
6-
method = "GET"
7-
url = "http://httpstat.us/200"
8-
configuration {
9-
accept_any_certificate = true
10-
}
11-
}
12-
request {
13-
description = "request2"
14-
method = "GET"
15-
url = "http://httpstat.us/400"
16-
configuration {
17-
accept_any_certificate = true
18-
}
19-
}
20-
}
1+
resource "dynatrace_synthetic_location" "location" {
2+
name = "#name#"
3+
city = "San Francisco de Asis"
4+
country_code = "VE"
5+
region_code = "04"
6+
deployment_type = "STANDARD"
7+
latitude = 10.0756
8+
location_node_outage_delay_in_minutes = 3
9+
longitude = -67.5442
10+
}
11+
12+
resource "time_sleep" "wait_for_location" {
13+
depends_on = [dynatrace_synthetic_location.location]
14+
create_duration = "5s"
2115
}
2216

2317
resource "dynatrace_http_monitor" "monitor" {
18+
depends_on = [time_sleep.wait_for_location]
2419
name = "#name#"
2520
frequency = 1
26-
locations = ["GEOLOCATION-F3E06A526BE3B4C4"]
21+
locations = [dynatrace_synthetic_location.location.id]
2722
anomaly_detection {
2823
loading_time_thresholds {
2924
}
@@ -36,3 +31,25 @@ resource "dynatrace_http_monitor" "monitor" {
3631
}
3732
no_script = true
3833
}
34+
35+
resource "dynatrace_http_monitor_script" "script" {
36+
http_id = dynatrace_http_monitor.monitor.id
37+
script {
38+
request {
39+
description = "request1"
40+
method = "GET"
41+
url = "https://example.com"
42+
configuration {
43+
accept_any_certificate = true
44+
}
45+
}
46+
request {
47+
description = "request2"
48+
method = "GET"
49+
url = "https://example.com"
50+
configuration {
51+
accept_any_certificate = true
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)