/
magefile.go
122 lines (103 loc) · 3.44 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
// Build a script to format and run tests of a Terraform module project
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Default The default target when the command executes `mage` in Cloud Shell
var Default = RunAllTargets
func main() {
Default()
}
// RunAllTargets A build step that runs Clean, Format, Unit and Integration in sequence
func RunAllTargets() {
mg.Deps(CleanAll)
mg.Deps(LintCheckGo)
mg.Deps(LintCheckTerraform)
mg.Deps(RunTestHarnessUnitTests)
mg.Deps(RunUnitTests)
mg.Deps(RunIntegrationTests)
}
// RunTestHarnessUnitTests run unit test for the test harness itself
func RunTestHarnessUnitTests() error {
return sh.RunV("go", "test", "github.com/microsoft/cobalt/test-harness/infratests")
}
// RunUnitTests A build step that runs unit tests
func RunUnitTests() error {
fmt.Println("INFO: Running unit tests...")
return FindAndRunTests("unit")
}
// RunIntegrationTests A build step that runs integration tests
func RunIntegrationTests() error {
fmt.Println("INFO: Running integration tests...")
return FindAndRunTests("integration")
}
// 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...)
}
// LintCheckGo A build step that fails if go code is not formatted properly
func LintCheckGo() error {
fmt.Println("INFO: Checking format for Go files...")
return verifyRunsQuietly("Run `go fmt ./...` to fix", "go", "fmt", "./...")
}
// LintCheckTerraform a build step that fails if terraform files are not formatted properly
func LintCheckTerraform() error {
fmt.Println("INFO: Checking format for Terraform files...")
return verifyRunsQuietly("Run `terraform fmt` to fix the offending files", "terraform", "fmt")
}
// 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)
}
// CleanAll A build step that removes temporary build and test files
func CleanAll() 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" ||
info.Name() == "terraform.tfplan" ||
info.Name() == "terraform.tfstate.backup") {
os.Remove(path)
fmt.Printf("Removed \"%v\"\n", path)
}
return nil
})
}