-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate.go
100 lines (85 loc) · 2.15 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
package goss
import (
"fmt"
"io"
"os"
"runtime"
"sync"
"time"
"github.com/SimonBaeumer/goss/outputs"
"github.com/SimonBaeumer/goss/resource"
"github.com/SimonBaeumer/goss/system"
"github.com/SimonBaeumer/goss/util"
"github.com/fatih/color"
)
type Validator struct {
GossConfig GossConfig
RetryTimeout time.Duration
Sleep time.Duration
FormatOptions []string
Outputer outputs.Outputer
MaxConcurrent int //Separating concurrency and validation, irritating atm...
OutputWriter io.Writer
}
// Validate validation runtime
func (v *Validator) Validate(startTime time.Time) int {
if v.OutputWriter == nil {
v.OutputWriter = os.Stdout
}
outputConfig := util.OutputConfig{
FormatOptions: v.FormatOptions,
}
sys := system.New()
i := 1
for {
iStartTime := time.Now()
out := validate(sys, v.GossConfig, v.MaxConcurrent)
exitCode := v.Outputer.Output(v.OutputWriter, out, iStartTime, outputConfig)
if v.RetryTimeout == 0 || exitCode == 0 {
return exitCode
}
elapsed := time.Since(startTime)
if elapsed+v.Sleep > v.RetryTimeout {
color.Red("\nERROR: Timeout of %s reached before tests entered a passing state", v.RetryTimeout)
return exitCode
}
color.Red("Retrying in %s (elapsed/timeout time: %.3fs/%s)\n\n\n", v.Sleep, elapsed.Seconds(), v.RetryTimeout)
// Reset Cache
sys = system.New()
time.Sleep(v.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)
// Send resources to input channel
go func() {
for _, res := range gossConfig.Resources() {
in <- res
}
close(in)
}()
// Read resources from input channel and validate
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 res := range in {
out <- res.Validate(sys)
}
}()
}
// Wait for the out channel to be finished, after that close it
go func() {
wg.Wait()
close(out)
}()
return out
}