-
Notifications
You must be signed in to change notification settings - Fork 6
/
magefile.go
134 lines (114 loc) · 3.54 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
//+build mage
// iac-terraform 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(Test)
mg.Deps(TestSimpleWeb)
mg.Deps(TestWebData)
}
// Execute Integration Tests for the simpleweb sample. Only executes tests in 'simpleweb' directories.
func TestSimpleWeb() error {
mg.Deps(Check)
fmt.Println("INFO: Running sample tests...")
return FindAndRunTests("simpleweb")
}
// Execute Integration Tests for the webdata sample. Only executes tests in 'webdata' directories.
func TestWebData() error {
mg.Deps(Check)
fmt.Println("INFO: Running sample tests...")
return FindAndRunTests("webdata")
}
// Execute Module Tests and fail if a test fails. Only executes tests in 'test' directories.
func Test() error {
mg.Deps(Clean)
mg.Deps(Check)
fmt.Println("INFO: Running unit tests...")
return FindAndRunTests("test")
}
// 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(".", 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...)
}