forked from Azure/osdu-infrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magefile.go
181 lines (154 loc) · 4.79 KB
/
magefile.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//+build mage
// osdu-infrastructure task runner.
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// A build step that runs all tests.
func All() {
mg.Deps(TestModules)
mg.Deps(TestCommonResources)
mg.Deps(TestDataResources)
mg.Deps(TestServiceResources)
}
// Execute Module Tests and fail if a test fails. Only executes tests in 'test' directories.
func TestModules() error {
mg.Deps(Clean)
mg.Deps(Check)
fmt.Println("INFO: Running unit tests...")
return FindAndRunTests("testing")
}
// Execute Tests for R3 Common Resources.
func TestCommonResources() {
mg.Deps(CommonUnitTest)
mg.Deps(CommonIntegrationTest)
}
// Execute Unit Tests for OSDU R3 Common Resources.
func CommonUnitTest() error {
mg.Deps(Check)
fmt.Println("INFO: Running unit tests...")
return FindAndRunTests("common_resources/tests/unit")
}
// Execute Integration Tests for OSDU R3 Common Resources.
func CommonIntegrationTest() error {
mg.Deps(Check)
fmt.Println("INFO: Running integration tests...")
return FindAndRunTests("common_resources/tests/integration")
}
// Execute Tests for R3 Data Resources.
func TestDataResources() {
mg.Deps(DataUnitTest)
mg.Deps(DataIntegrationTest)
}
// Execute Unit Tests for OSDU R3 Data Resources.
func DataUnitTest() error {
mg.Deps(Check)
fmt.Println("INFO: Running unit tests...")
return FindAndRunTests("data_resources/tests/unit")
}
// Execute Integration Tests for OSDU R3 Data Resources.
func DataIntegrationTest() error {
mg.Deps(Check)
fmt.Println("INFO: Running integration tests...")
return FindAndRunTests("data_resources/tests/integration")
}
// Execute Tests for R3 Service Resources.
func TestServiceResources() {
mg.Deps(ServiceUnitTest)
mg.Deps(ServiceIntegrationTest)
}
// Execute Unit Tests for OSDU R3 Service Resources.
func ServiceUnitTest() error {
mg.Deps(Check)
fmt.Println("INFO: Running unit tests...")
return FindAndRunTests("service_resources/tests/unit")
}
// Execute Integration Tests for OSDU R3 Service Resources.
func ServiceIntegrationTest() error {
mg.Deps(Check)
fmt.Println("INFO: Running integration tests...")
return FindAndRunTests("service_resources/tests/integration")
}
// Validate both Terraform code and Go code.
func Check() {
mg.Deps(LintTF)
mg.Deps(LintGO)
}
// Lint check Go and fail if files are not not formatted properly.
func LintGO() error {
fmt.Println("INFO: Checking format for Go files...")
return verifyRunsQuietly("Run `go fmt ./...` to fix", "go", "fmt", "./...")
}
// Lint check Terraform and fail if files are not formatted properly.
func LintTF() error {
fmt.Println("INFO: Checking format for Terraform files...")
return verifyRunsQuietly("Run `terraform fmt --check --recursive` to fix the offending files", "terraform", "fmt")
}
// Remove temporary build and test files.
func Clean() error {
fmt.Println("INFO: Cleaning...")
return filepath.Walk("./infra/modules", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && info.Name() == "vendor" {
return filepath.SkipDir
}
if info.IsDir() && info.Name() == ".terraform" {
os.RemoveAll(path)
fmt.Printf("Removed \"%v\"\n", path)
return filepath.SkipDir
}
if info.IsDir() && info.Name() == "terraform.tfstate.d" {
os.RemoveAll(path)
fmt.Printf("Removed \"%v\"\n", path)
return filepath.SkipDir
}
if !info.IsDir() && (info.Name() == "terraform.tfstate" ||
info.Name() == "terraform.tfplan" ||
info.Name() == "terraform.tfstate.backup") {
os.Remove(path)
fmt.Printf("Removed \"%v\"\n", path)
}
return nil
})
}
//-------------------------------
// GO UTILITY FUNCTIONS
//-------------------------------
// runs a command and ensures that the exit code indicates success and that there is no output to stdout
func verifyRunsQuietly(instructionsToFix string, cmd string, args ...string) error {
output, err := sh.Output(cmd, args...)
if err != nil {
return err
}
if len(output) == 0 {
return nil
}
return fmt.Errorf("ERROR: command '%s' with arguments %s failed. Output was: '%s'. %s", cmd, args, output, instructionsToFix)
}
// FindAndRunTests finds all tests with a given path suffix and runs them using `go test`
func FindAndRunTests(pathSuffix string) error {
goModules, err := sh.Output("go", "list", "./...")
if err != nil {
return err
}
testTargetModules := make([]string, 0)
for _, module := range strings.Fields(goModules) {
if strings.HasSuffix(module, pathSuffix) {
testTargetModules = append(testTargetModules, module)
}
}
if len(testTargetModules) == 0 {
return fmt.Errorf("No modules found for testing prefix '%s'", pathSuffix)
}
cmdArgs := []string{"test"}
cmdArgs = append(cmdArgs, testTargetModules...)
cmdArgs = append(cmdArgs, "-v", "-timeout", "7200s")
return sh.RunV("go", cmdArgs...)
}