-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcli_test.go.erb
238 lines (212 loc) · 10.1 KB
/
cli_test.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<% autogen_exception -%>
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package test
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/GoogleCloudPlatform/terraform-validator/converters/google"
)
// TestCLI tests the "convert" and "validate" subcommand against a generated .tfplan file.
func TestCLI(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test in short mode.")
return
}
// Define the reusable constraints to be use for the test cases.
type constraint struct {
name string
wantViolation bool
wantOutputRegex string
}
// Currently, we only test one rule. Moving forward, resource specific rules
// should be added to increase the coverage.
alwaysViolate := constraint{name: "always_violate", wantViolation: true, wantOutputRegex: "Constraint GCPAlwaysViolatesConstraintV1.always_violates_all on resource"}
// Test cases for each type of resource is defined here.
cases := []struct {
name string
constraints []constraint
compareConvertOutput compareConvertOutputFunc
}{
// cli-only, the following tests are not in read_test or
// have unique paramters that separate them
{name: "bucket"},
{name: "disk"},
{name: "firewall"},
{name: "instance"},
{name: "sql"},
{name: "example_compute_forwarding_rule"},
{name: "example_compute_instance"},
{name: "example_folder_iam_member", compareConvertOutput: compareMergedIamMemberOutput},
{name: "example_project_create", constraints: []constraint{alwaysViolate, constraint{name: "project_match_target", wantViolation: false, wantOutputRegex: ""}}},
{name: "example_project_update", constraints: []constraint{alwaysViolate, constraint{name: "project_match_target", wantViolation: true, wantOutputRegex: "Constraint GCPAlwaysViolatesConstraintV1.always_violates_project_match_target on resource"}}},
{name: "example_project_iam_binding", compareConvertOutput: compareMergedIamBindingOutput},
{name: "example_project_iam_member", compareConvertOutput: compareMergedIamMemberOutput},
{name: "example_storage_bucket_iam_binding", compareConvertOutput: compareMergedIamBindingOutput},
{name: "example_storage_bucket_iam_member", compareConvertOutput: compareMergedIamMemberOutput},
// auto inserted tests that are not in list above or manually inserted in read_test.go
<% @non_defined_tests.each do |test| -%>
{name: "<%= test -%>"},
<% end -%>
}
// Map of cases to skip to reasons for the skip
skipCases := map[string]string{
"TestCLI/v=0.12/tf=example_compute_forwarding_rule/offline=true/cmd=convert": "temperarily skip because of the predictable drift in offline mode",
"TestCLI/v=0.12/tf=example_compute_forwarding_rule/offline=true/cmd=validate/constraint=always_violate": "temperarily skip because of the predictable drift in offline mode",
"TestCLI/v=0.12/tf=example_compute_instance/offline=true/cmd=convert": "compute_instance doesn't work in offline mode - github.com/hashicorp/terraform-provider-google/issues/8489",
"TestCLI/v=0.12/tf=example_compute_instance/offline=true/cmd=validate/constraint=always_violate": "compute_instance doesn't work in offline mode - github.com/hashicorp/terraform-provider-google/issues/8489",
"TestCLI/v=0.12/tf=example_organization_iam_binding/offline=false/cmd=convert": "skip because test runner doesn't have org permissions",
"TestCLI/v=0.12/tf=example_organization_iam_binding/offline=false/cmd=validate/constraint=always_violate": "skip because test runner doesn't have org permissions",
"TestCLI/v=0.12/tf=example_organization_iam_member/offline=false/cmd=convert": "skip because test runner doesn't have org permissions",
"TestCLI/v=0.12/tf=example_organization_iam_member/offline=false/cmd=validate/constraint=always_violate": "skip because test runner doesn't have org permissions",
"TestCLI/v=0.12/tf=example_project_iam/offline=false/cmd=convert": "example_project_iam is too complex to untangle merges with online data generically",
"TestCLI/v=0.12/tf=example_storage_bucket_iam_member_random_suffix/offline=false": "test produces inconsistent results based on randomized names - github.com/GoogleCloudPlatform/terraform-validator/issues/259",
"TestCLI/v=0.12/tf=example_storage_bucket_iam_member_random_suffix/offline=false/cmd=convert": "test produces inconsistent results based on randomized names - github.com/GoogleCloudPlatform/terraform-validator/issues/259",
}
for i := range cases {
// Allocate a variable to make sure test can run in parallel.
c := cases[i]
// Add default constraints if not set.
if len(c.constraints) == 0 {
c.constraints = []constraint{alwaysViolate}
}
// Add default convert comparison func if not set
if c.compareConvertOutput == nil {
c.compareConvertOutput = compareUnmergedConvertOutput
}
// Test both offline and online mode.
for _, offline := range []bool{true, false} {
offline := offline
t.Run(fmt.Sprintf("v=0.12/tf=%s/offline=%t", c.name, offline), func(t *testing.T) {
t.Parallel()
// Create a temporary directory for running terraform.
dir, err := ioutil.TempDir(tmpDir, "terraform")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
// Generate the <name>.tf and <name>_assets.json files into the temporary directory.
generateTestFiles(t, "../testdata/templates", dir, c.name+".tf")
generateTestFiles(t, "../testdata/templates", dir, c.name+".json")
// Uses glob matching to match generateTestFiles internals.
tfstateMatches, err := filepath.Glob(filepath.Join("../testdata/templates", c.name+".tfstate"))
if err != nil {
t.Fatalf("malformed glob: %v", err)
}
if tfstateMatches != nil {
generateTestFiles(t, "../testdata/templates", dir, c.name+".tfstate")
err = os.Rename(
filepath.Join(dir, c.name+".tfstate"),
filepath.Join(dir, "terraform.tfstate"),
)
if err != nil {
t.Fatalf("renaming tfstate: %v", err)
}
}
terraformWorkflow(t, dir, c.name)
t.Run("cmd=convert", func(t *testing.T) {
if reason, exists := skipCases[t.Name()]; exists {
t.Skip(reason)
}
testConvertCommand(t, dir, c.name, offline, c.compareConvertOutput)
})
for _, ct := range c.constraints {
t.Run(fmt.Sprintf("cmd=validate/constraint=%s", ct.name), func(t *testing.T) {
if reason, exists := skipCases[t.Name()]; exists {
t.Skip(reason)
}
testValidateCommand(t, ct.wantViolation, ct.wantOutputRegex, dir, c.name, offline, ct.name)
})
}
})
}
}
}
type compareConvertOutputFunc func(t *testing.T, expected []google.Asset, actual []google.Asset, offline bool)
func compareUnmergedConvertOutput(t *testing.T, expected []google.Asset, actual []google.Asset, offline bool) {
expectedAssets := normalizeAssets(t, expected, offline)
actualAssets := normalizeAssets(t, actual, offline)
require.ElementsMatch(t, expectedAssets, actualAssets)
}
// For merged IAM members, only consider whether the expected members are present.
func compareMergedIamMemberOutput(t *testing.T, expected []google.Asset, actual []google.Asset, offline bool) {
var normalizedActual []google.Asset
for i := range expected {
expectedAsset := expected[i]
actualAsset := actual[i]
// Copy actualAsset
normalizedActualAsset := actualAsset
expectedBindings := map[string]map[string]struct{}{}
for _, binding := range expectedAsset.IAMPolicy.Bindings {
expectedBindings[binding.Role] = map[string]struct{}{}
for _, member := range binding.Members {
expectedBindings[binding.Role][member] = struct{}{}
}
}
iamPolicy := google.IAMPolicy{}
for _, binding := range actualAsset.IAMPolicy.Bindings {
if expectedMembers, exists := expectedBindings[binding.Role]; exists {
iamBinding := google.IAMBinding{
Role: binding.Role,
}
for _, member := range binding.Members {
if _, exists := expectedMembers[member]; exists {
iamBinding.Members = append(iamBinding.Members, member)
}
}
iamPolicy.Bindings = append(iamPolicy.Bindings, iamBinding)
}
}
normalizedActualAsset.IAMPolicy = &iamPolicy
normalizedActual = append(normalizedActual, normalizedActualAsset)
}
expectedAssets := normalizeAssets(t, expected, offline)
actualAssets := normalizeAssets(t, normalizedActual, offline)
require.ElementsMatch(t, expectedAssets, actualAssets)
}
// For merged IAM bindings, only consider whether the expected bindings are as expected.
func compareMergedIamBindingOutput(t *testing.T, expected []google.Asset, actual []google.Asset, offline bool) {
var normalizedActual []google.Asset
for i := range expected {
expectedAsset := expected[i]
actualAsset := actual[i]
// Copy actualAsset
normalizedActualAsset := actualAsset
expectedBindings := map[string]struct{}{}
for _, binding := range expectedAsset.IAMPolicy.Bindings {
expectedBindings[binding.Role] = struct{}{}
}
iamPolicy := google.IAMPolicy{}
for _, binding := range actualAsset.IAMPolicy.Bindings {
if _, exists := expectedBindings[binding.Role]; exists {
iamPolicy.Bindings = append(iamPolicy.Bindings, binding)
}
}
normalizedActualAsset.IAMPolicy = &iamPolicy
normalizedActual = append(normalizedActual, normalizedActualAsset)
}
expectedAssets := normalizeAssets(t, expected, offline)
actualAssets := normalizeAssets(t, normalizedActual, offline)
require.ElementsMatch(t, expectedAssets, actualAssets)
}