|
| 1 | +// Copyright 2021 Splunk, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package synthetics |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strconv" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 23 | +) |
| 24 | + |
| 25 | +type maxWaitTimeTestValues struct { |
| 26 | + maxWaitTime int |
| 27 | + expectedMaxWaitTime string |
| 28 | + expectedMaxWaitTimeDefault string |
| 29 | +} |
| 30 | + |
| 31 | +func maxWaitTimeTestConfig(name string, maxWaitTime int) string { |
| 32 | + var maxWaitTimeStr string |
| 33 | + if maxWaitTime == 0 { |
| 34 | + maxWaitTimeStr = "null" |
| 35 | + } else { |
| 36 | + maxWaitTimeStr = strconv.Itoa(maxWaitTime) |
| 37 | + } |
| 38 | + |
| 39 | + return fmt.Sprintf(` |
| 40 | +resource "synthetics_create_browser_check_v2" "example" { |
| 41 | + provider = synthetics.synthetics |
| 42 | + test { |
| 43 | + device_id = 1 |
| 44 | + frequency = 5 |
| 45 | + location_ids = ["aws-us-east-1"] |
| 46 | + name = "%s" |
| 47 | + advanced_settings { |
| 48 | + verify_certificates = true |
| 49 | + } |
| 50 | + transactions { |
| 51 | + name = "First Synthetic transaction" |
| 52 | + steps { |
| 53 | + name = "01 Go to URL" |
| 54 | + type = "go_to_url" |
| 55 | + url = "https://www.splunk.com" |
| 56 | + } |
| 57 | + steps { |
| 58 | + name = "assert element visible" |
| 59 | + type = "assert_element_visible" |
| 60 | + selector = "beep" |
| 61 | + selector_type = "id" |
| 62 | + max_wait_time = %s |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | +`, name, maxWaitTimeStr) |
| 68 | +} |
| 69 | + |
| 70 | +func TestAccCreateUpdateBrowserCheckV2MaxWaitTime(t *testing.T) { |
| 71 | + |
| 72 | + maxWaitTimeTestCases := []struct { |
| 73 | + name string |
| 74 | + apply1 maxWaitTimeTestValues |
| 75 | + apply2 maxWaitTimeTestValues |
| 76 | + }{ |
| 77 | + { |
| 78 | + name: "default value", |
| 79 | + apply1: maxWaitTimeTestValues{ |
| 80 | + maxWaitTime: 0, |
| 81 | + expectedMaxWaitTime: "0", |
| 82 | + expectedMaxWaitTimeDefault: "true", |
| 83 | + }, |
| 84 | + apply2: maxWaitTimeTestValues{ |
| 85 | + maxWaitTime: 0, |
| 86 | + expectedMaxWaitTime: "0", |
| 87 | + expectedMaxWaitTimeDefault: "true", |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "custom value -> default", |
| 92 | + apply1: maxWaitTimeTestValues{ |
| 93 | + maxWaitTime: 5000, |
| 94 | + expectedMaxWaitTime: "5000", |
| 95 | + expectedMaxWaitTimeDefault: "false", |
| 96 | + }, |
| 97 | + apply2: maxWaitTimeTestValues{ |
| 98 | + maxWaitTime: 0, |
| 99 | + expectedMaxWaitTime: "0", |
| 100 | + expectedMaxWaitTimeDefault: "true", |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "custom value matching default -> default", |
| 105 | + apply1: maxWaitTimeTestValues{ |
| 106 | + maxWaitTime: 10000, |
| 107 | + expectedMaxWaitTime: "10000", |
| 108 | + expectedMaxWaitTimeDefault: "false", |
| 109 | + }, |
| 110 | + apply2: maxWaitTimeTestValues{ |
| 111 | + maxWaitTime: 0, |
| 112 | + expectedMaxWaitTime: "0", |
| 113 | + expectedMaxWaitTimeDefault: "true", |
| 114 | + }, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "default -> custom value matching default", |
| 118 | + apply1: maxWaitTimeTestValues{ |
| 119 | + maxWaitTime: 0, |
| 120 | + expectedMaxWaitTime: "0", |
| 121 | + expectedMaxWaitTimeDefault: "true", |
| 122 | + }, |
| 123 | + apply2: maxWaitTimeTestValues{ |
| 124 | + maxWaitTime: 10000, |
| 125 | + expectedMaxWaitTime: "10000", |
| 126 | + expectedMaxWaitTimeDefault: "false", |
| 127 | + }, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + for _, tc := range maxWaitTimeTestCases { |
| 132 | + t.Run(tc.name, func(t *testing.T) { |
| 133 | + resource.Test(t, resource.TestCase{ |
| 134 | + PreCheck: func() { testAccPreCheck(t) }, |
| 135 | + Providers: testAccProviders, |
| 136 | + Steps: []resource.TestStep{ |
| 137 | + { |
| 138 | + Config: providerConfig + maxWaitTimeTestConfig(tc.name, tc.apply1.maxWaitTime), |
| 139 | + Check: resource.ComposeTestCheckFunc( |
| 140 | + resource.TestCheckResourceAttr("synthetics_create_browser_check_v2.example", "test.0.transactions.0.steps.1.max_wait_time", tc.apply1.expectedMaxWaitTime), |
| 141 | + resource.TestCheckResourceAttr("synthetics_create_browser_check_v2.example", "test.0.transactions.0.steps.1.max_wait_time_default", tc.apply1.expectedMaxWaitTimeDefault), |
| 142 | + ), |
| 143 | + }, |
| 144 | + { |
| 145 | + Config: providerConfig + maxWaitTimeTestConfig(tc.name, tc.apply2.maxWaitTime), |
| 146 | + Check: resource.ComposeTestCheckFunc( |
| 147 | + resource.TestCheckResourceAttr("synthetics_create_browser_check_v2.example", "test.0.transactions.0.steps.1.max_wait_time", tc.apply2.expectedMaxWaitTime), |
| 148 | + resource.TestCheckResourceAttr("synthetics_create_browser_check_v2.example", "test.0.transactions.0.steps.1.max_wait_time_default", tc.apply2.expectedMaxWaitTimeDefault), |
| 149 | + ), |
| 150 | + }, |
| 151 | + }, |
| 152 | + }) |
| 153 | + }) |
| 154 | + } |
| 155 | +} |
0 commit comments