forked from goss-org/goss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
124 lines (110 loc) · 2.67 KB
/
validate.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
package goss
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sync"
"time"
"github.com/aelsabbahy/goss/outputs"
"github.com/aelsabbahy/goss/resource"
"github.com/aelsabbahy/goss/system"
"github.com/fatih/color"
"github.com/urfave/cli"
)
func getGossConfig(c *cli.Context) GossConfig {
// handle stdin
var fh *os.File
var path, source string
var gossConfig GossConfig
TemplateFilter = NewTemplateFilter(c.GlobalString("vars"))
specFile := c.GlobalString("gossfile")
if specFile == "-" {
source = "STDIN"
fh = os.Stdin
data, err := ioutil.ReadAll(fh)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
OutStoreFormat = getStoreFormatFromData(data)
gossConfig = ReadJSONData(data, true)
} else {
source = specFile
path = filepath.Dir(specFile)
OutStoreFormat = getStoreFormatFromFileName(specFile)
gossConfig = ReadJSON(specFile)
}
gossConfig = mergeJSONData(gossConfig, 0, path)
if len(gossConfig.Resources()) == 0 {
fmt.Printf("Error: found 0 tests, source: %v\n", source)
os.Exit(1)
}
return gossConfig
}
func getOutputer(c *cli.Context) outputs.Outputer {
if c.Bool("no-color") {
color.NoColor = true
}
if c.Bool("color") {
color.NoColor = false
}
return outputs.GetOutputer(c.String("format"))
}
func Validate(c *cli.Context, startTime time.Time) {
gossConfig := getGossConfig(c)
sys := system.New(c)
outputer := getOutputer(c)
sleep := c.Duration("sleep")
retryTimeout := c.Duration("retry-timeout")
i := 1
for {
iStartTime := time.Now()
out := validate(sys, gossConfig, c.Int("max-concurrent"))
exitCode := outputer.Output(os.Stdout, out, iStartTime)
if retryTimeout == 0 || exitCode == 0 {
os.Exit(exitCode)
}
elapsed := time.Since(startTime)
if elapsed+sleep > retryTimeout {
color.Red("\nERROR: Timeout of %s reached before tests entered a passing state", retryTimeout)
os.Exit(3)
}
color.Red("Retrying in %s (elapsed/timeout time: %.3fs/%s)\n\n\n", sleep, elapsed.Seconds(), retryTimeout)
// Reset cache
sys = system.New(c)
time.Sleep(sleep)
i++
fmt.Printf("Attempt #%d:\n", i)
}
}
func validate(sys *system.System, gossConfig GossConfig, maxConcurrent int) <-chan []resource.TestResult {
out := make(chan []resource.TestResult)
in := make(chan resource.Resource)
go func() {
for _, t := range gossConfig.Resources() {
in <- t
}
close(in)
}()
workerCount := runtime.NumCPU() * 5
if workerCount > maxConcurrent {
workerCount = maxConcurrent
}
var wg sync.WaitGroup
for i := 0; i < workerCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for f := range in {
out <- f.Validate(sys)
}
}()
}
go func() {
wg.Wait()
close(out)
}()
return out
}