-
Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathsample_test.go
166 lines (148 loc) · 5.37 KB
/
sample_test.go
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
// Copyright 2022 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 samples_test
import (
"fmt"
"io/fs"
"path/filepath"
"sort"
"strings"
"testing"
"time"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
"github.com/stretchr/testify/assert"
)
const (
setupPath = "../setup"
sampleDir = "../../"
)
// testGroups represents a collection of samples matched to a projectID.
type testGroups struct {
projectID string
group int
samples []string
}
// Retry if these errors are encountered.
var retryErrors = map[string]string{
// IAM for Eventarc service agent is eventually consistent
".*Permission denied while using the Eventarc Service Agent.*": "Eventarc Service Agent IAM is eventually consistent",
// API activation is eventually consistent.
".*SERVICE_DISABLED.*": "Service enablement is eventually consistent",
// Retry function GCS access
".*does not have storage.objects.get access.*": "GCS IAM is eventually consistent",
}
func TestSamples(t *testing.T) {
// common test env
testEnv := map[string]string{
"GOOGLE_REGION": "us-central1",
"GOOGLE_ZONE": "us-central1-a",
}
for k, v := range testEnv {
utils.SetEnv(t, k, v)
}
// This initial blueprint test is to extract output info
// so we only have to set the env vars once.
setup := tft.NewTFBlueprintTest(t,
tft.WithTFDir(sampleDir),
tft.WithSetupPath(setupPath),
)
testProjectIDs := setup.GetTFSetupOutputListVal("project_ids")
// number of groups is determined by length of testProjectIDs slice
testGroups := discoverTestCaseGroups(t, testProjectIDs)
for _, tg := range testGroups {
for _, samplePath := range tg.samples {
sampleName := filepath.Base(samplePath)
testName := fmt.Sprintf("%d/%s", tg.group, sampleName)
t.Run(testName, func(t *testing.T) {
utils.SetEnv(t, "GOOGLE_PROJECT", tg.projectID)
t.Logf("Test %s running %s project", sampleName, tg.projectID)
sampleTest := tft.NewTFBlueprintTest(t,
tft.WithTFDir(samplePath),
tft.WithSetupPath(setupPath),
tft.WithRetryableTerraformErrors(retryErrors, 10, time.Minute),
)
sampleTest.DefineVerify(func(a *assert.Assertions) {})
sampleTest.Test()
t.Logf("Test %s completed in %s project", sampleName, tg.projectID)
})
}
}
}
// skipDiscoverDirs are directories that are skipped when discovering test cases.
var skipDiscoverDirs = map[string]bool{
"test": true,
"build": true,
".git": true,
}
// discoverTestCaseGroups discovers individual sample directories in the parent directory
// and assigns them to a test group based on projects provided.
// It also skips known directories that are not samples.
func discoverTestCaseGroups(t *testing.T, projects []string) []*testGroups {
t.Helper()
// skip any dirs with root in skipDiscoverDirs
skipDirs := func(f fs.FileInfo) bool {
rootDir := strings.SplitN(f.Name(), "/", 2)[0]
return skipDiscoverDirs[rootDir]
}
samples, err := walkTerraformDirs(sampleDir, skipDirs)
if err != nil {
t.Fatal(err)
}
sort.Strings(samples)
// One test group is associated to one project.
groups := []*testGroups{}
for i, project := range projects {
groups = append(groups, &testGroups{projectID: project, samples: []string{}, group: i})
}
// Rather than chunking we assign them in a round robin fashion as some samples like sql takes more time.
// Chunking would result in all sql* assigned to a single project.
// We sort the sample slice beforehand so assignments should be stable for a given run.
for i, sample := range samples {
groupIndex := i % len(projects)
groups[groupIndex].samples = append(groups[groupIndex].samples, sample)
}
return groups
}
// replace with https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/blob/1554af3ba2093bb02301ccd7061606011c82d9bc/cli/util/file.go#L17
// after https://github.com/GoogleCloudPlatform/cloud-foundation-toolkit/pull/1279
const (
tfInternalDirPrefix = ".terraform"
)
// walkTerraformDirs traverses a provided path to return a list of directories
// that hold terraform configs while skiping internal folders that have a
// .terraform.* prefix
func walkTerraformDirs(topLevelPath string, skip func(fs.FileInfo) bool) ([]string, error) {
var tfDirs []string
err := filepath.Walk(topLevelPath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("failure in accessing the path %q: %v", path, err)
}
if skip(info) {
return filepath.SkipDir
}
if info.IsDir() && strings.HasPrefix(info.Name(), tfInternalDirPrefix) {
return filepath.SkipDir
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".tf") {
tfDirs = append(tfDirs, filepath.Dir(path))
return filepath.SkipDir
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error walking the path %q: %v", topLevelPath, err)
}
return tfDirs, nil
}