forked from monzo/orchestra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
76 lines (70 loc) · 1.99 KB
/
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
package commands
import (
"errors"
"os"
"os/exec"
"strings"
"github.com/urfave/cli/v2"
"github.com/wsxiaoys/terminal"
"github.com/tifo/orchestra/services"
)
var TestCommand = &cli.Command{
Name: "test",
Usage: "Runs go test ./... for every service",
Action: BeforeAfterWrapper(TestAction),
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose, v",
},
&cli.BoolFlag{
Name: "race, r",
},
},
}
// StartAction starts all the services (or the specified ones)
func TestAction(c *cli.Context) error {
svcs := services.Sort(FilterServices(c))
for _, service := range svcs {
spacing := strings.Repeat(" ", services.MaxServiceNameLength+2-len(service.Name))
success, err := testService(c, service)
if err != nil {
appendError(err)
terminal.Stdout.Colorf("%s%s| @{r} error: @{|}%s\n", service.Name, spacing, err.Error())
} else if !success {
appendError(errors.New("Test Failed"))
terminal.Stdout.Colorf("%s%s| @{r} FAILED\n", service.Name, spacing)
} else {
terminal.Stdout.Colorf("%s%s| @{g} PASS\n", service.Name, spacing)
}
}
return nil
}
// startService takes a Service struct as input, creates a new log file in .orchestra,
// redirects the command stdout and stderr to the log file, configures the environment
// variables for the command and starts it. If cmd.Start() doesn't return any
// error, it will write a service.pid file in .orchestra
func testService(c *cli.Context, service *services.Service) (bool, error) {
cmdArgs := []string{"test"}
if c.Bool("verbose") {
cmdArgs = append(cmdArgs, "-v")
}
if c.Bool("race") {
cmdArgs = append(cmdArgs, "--race")
}
cmdArgs = append(cmdArgs, "./...")
cmdArgs = append([]string{"-n", niceness, "go"}, cmdArgs...)
cmd := exec.Command("nice", cmdArgs...)
cmd.Dir = service.Path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = GetEnvForService(c, service)
err := cmd.Start()
if err != nil {
return false, err
}
_ = cmd.Wait()
if !cmd.ProcessState.Success() {
return false, nil
}
return true, nil
}