-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathtest_file.go.erb
143 lines (124 loc) · 4.23 KB
/
test_file.go.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<%= lines(autogen_notice(:go, pwd)) -%>
package google
import (
<% unless object.skip_delete || object.cgc_only -%>
<% unless object.custom_code.test_check_destroy -%>
"fmt"
<% end -%>
"strings"
<% end -%>
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
<% unless object.skip_delete || object.cgc_only -%>
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
<% end -%>
)
<%
resource_name = product_ns + object.name
tf_product = (@config.legacy_name || product_ns).underscore
tf_resource = object.name.underscore
terraform_name = object.legacy_name || "google_#{tf_product}_#{tf_resource}"
-%>
<%
# @api.version_obj_or_closest(version) is slightly wrong; we want the _object_ version or the generation version.
# Ultimately this will not matter though, since the API default should always be less than the object default.
object.examples
.reject(&:skip_test)
.reject { |e| @api.version_obj_or_closest(version) < @api.version_obj_or_closest(e.min_version) }
.each do |example|
# {Compute}{Address}_{addressBasic}
test_slug = "#{resource_name}_#{example.name.camelize(:lower)}Example"
ignore_read = object.all_user_properties
.select{|p| p.url_param_only || p.ignore_read || p.is_a?(Api::Type::ResourceRef) }
.map { |p| p.name.underscore }
.concat(example.ignore_read_extra)
# Use explicit version for the example if given.
# Otherwise, use object version.
example_version = example.min_version || object.min_version.name
# Use explicit resource type if given
resource_type = example.primary_resource_type || terraform_name
-%>
func TestAcc<%= test_slug -%>(t *testing.T) {
<% if example.skip_vcr -%>
skipIfVcr(t)
<% end -%>
t.Parallel()
context := map[string]interface{} {
<%= lines(indent(compile(pwd + '/templates/terraform/env_var_context.go.erb'), 4)) -%>
<% unless example.test_vars_overrides.nil? -%>
<% example.test_vars_overrides.each do |var_name, override| -%>
"<%= var_name %>": <%= override %>,
<% end -%>
<% end -%>
"random_suffix": randString(t, 10),
}
<% versioned_provider = !example_version.nil? && example_version != 'ga' -%>
vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
<% unless versioned_provider -%>
Providers: testAccProviders,
<% else -%>
Providers: testAccProvidersOiCS,
<% end -%>
<% if example.pull_external -%>
ExternalProviders: map[string]resource.ExternalProvider{
"random": {},
"time": {},
},
<% end -%>
<% unless object.skip_delete || object.cgc_only -%>
CheckDestroy: testAccCheck<%= "#{resource_name}" -%>DestroyProducer(t),
<% end -%>
Steps: []resource.TestStep{
{
Config: testAcc<%= test_slug -%>(context),
},
<% unless example.skip_import_test -%>
{
ResourceName: "<%= resource_type -%>.<%= example.primary_resource_id -%>",
ImportState: true,
ImportStateVerify: true,
<%- unless ignore_read.empty? -%>
ImportStateVerifyIgnore: <%= go_literal(ignore_read) %>,
<%- end -%>
},
<% end -%>
},
})
}
func testAcc<%= test_slug -%>(context map[string]interface{}) string {
<%= example.config_test(pwd) -%>
}
<%- end %>
<% unless object.skip_delete || object.cgc_only -%>
func testAccCheck<%= resource_name -%>DestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "<%= terraform_name -%>" {
continue
}
if strings.HasPrefix(name, "data.") {
continue
}
<% if object.custom_code.test_check_destroy -%>
<%= lines(compile(pwd + '/' + object.custom_code.test_check_destroy)) -%>
<% else -%>
config := googleProviderConfig(t)
url, err := replaceVarsForTest(config, rs, "<%= "{{#{object.__product.name}BasePath}}#{object.self_link_uri}" -%>")
if err != nil {
return err
}
billingProject := ""
if config.BillingProject != "" {
billingProject = config.BillingProject
}
_, err = sendRequest(config, "<%= object.read_verb.to_s.upcase -%>", billingProject, url, config.userAgent, nil<%= object.error_retry_predicates ? ", " + object.error_retry_predicates.join(',') : "" -%>)
if err == nil {
return fmt.Errorf("<%= resource_name -%> still exists at %s", url)
}
<% end -%>
}
return nil
}
}
<% end -%>