forked from flanksource/karina
/
test.go
198 lines (185 loc) · 5.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package cmd
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"sync"
"time"
"github.com/flanksource/commons/console"
"github.com/moshloop/platform-cli/pkg/phases/audit"
"github.com/moshloop/platform-cli/pkg/phases/base"
"github.com/moshloop/platform-cli/pkg/phases/configmapreloader"
"github.com/moshloop/platform-cli/pkg/phases/consul"
"github.com/moshloop/platform-cli/pkg/phases/dex"
"github.com/moshloop/platform-cli/pkg/phases/eck"
"github.com/moshloop/platform-cli/pkg/phases/elasticsearch"
"github.com/moshloop/platform-cli/pkg/phases/fluentdoperator"
"github.com/moshloop/platform-cli/pkg/phases/flux"
"github.com/moshloop/platform-cli/pkg/phases/harbor"
"github.com/moshloop/platform-cli/pkg/phases/monitoring"
"github.com/moshloop/platform-cli/pkg/phases/nsx"
"github.com/moshloop/platform-cli/pkg/phases/opa"
"github.com/moshloop/platform-cli/pkg/phases/postgresoperator"
"github.com/moshloop/platform-cli/pkg/phases/quack"
"github.com/moshloop/platform-cli/pkg/phases/registrycreds"
"github.com/moshloop/platform-cli/pkg/phases/sealedsecrets"
"github.com/moshloop/platform-cli/pkg/phases/stubs"
"github.com/moshloop/platform-cli/pkg/phases/vault"
"github.com/moshloop/platform-cli/pkg/phases/velero"
"github.com/moshloop/platform-cli/pkg/platform"
"github.com/spf13/cobra"
"github.com/vbauerster/mpb/v5"
)
var (
wait int
failOnError bool
waitInterval int
junitPath, suiteName string
p *platform.Platform
progress *mpb.Progress
test *console.TestResults
testE2E, showProgress bool
concurrency int
ch chan int
wg *sync.WaitGroup
stdout bytes.Buffer
)
var Test = &cobra.Command{
Use: "test",
}
func end(test *console.TestResults) {
if junitPath != "" {
if suiteName == "" {
test.SuiteName(p.Name)
} else {
test.SuiteName(suiteName)
}
xml, _ := test.ToXML()
os.MkdirAll(path.Dir(junitPath), 0755) // nolint: errcheck
ioutil.WriteFile(junitPath, []byte(xml), 0644) // nolint: errcheck
}
if test.FailCount > 0 && failOnError {
os.Exit(1)
}
}
type TestFn func(p *platform.Platform, test *console.TestResults)
func queue(name string, fn TestFn, wg *sync.WaitGroup, ch chan int) {
wg.Add(1)
go func() {
defer wg.Done()
localTest := &console.TestResults{Writer: &stdout}
var bar console.Progress
if showProgress {
bar = console.NewTerminalProgress(name, localTest, progress)
} else {
bar = console.NewTextProgress(name, localTest)
}
start := time.Now()
bar.Start()
for {
ch <- 1
fn(p.WithLogOutput(localTest.Writer).WithField("test", name), localTest)
elapsed := time.Since(start)
if localTest.FailCount == 0 || wait == 0 || int(elapsed.Seconds()) >= wait {
test.Append(localTest)
bar.Done()
<-ch
return
}
localTest.Retry()
bar.Status(fmt.Sprintf("Waiting to re-run tests, retry: %d", localTest.Retries))
time.Sleep(time.Duration(waitInterval) * time.Second)
bar.Status("Re-running tests")
<-ch
}
}()
}
func init() {
Test.PersistentPreRun = func(cmd *cobra.Command, args []string) {
p = getPlatform(cmd)
wg = &sync.WaitGroup{}
ch = make(chan int, concurrency)
progress = mpb.New(mpb.WithWaitGroup(wg), mpb.WithWidth(40))
test = &console.TestResults{
Writer: &stdout,
}
}
Test.PersistentPostRun = func(cmd *cobra.Command, args []string) {
progress.Wait()
wg.Wait()
fmt.Println(stdout.String())
end(test)
}
tests := map[string]TestFn{
"audit": audit.Test,
"base": base.Test,
"configmap-reloader": configmapreloader.Test,
"consul": consul.Test,
"dex": dex.Test,
"eck": eck.Test,
"elasticsearch": elasticsearch.Test,
"fluentd": fluentdoperator.Test,
"gitops": flux.Test,
"harbor": harbor.Test,
"monitoring": monitoring.Test,
"nsx": nsx.Test,
"opa": opa.Test,
"postgres-operator": postgresoperator.Test,
"promtheus": monitoring.TestPrometheus,
"quack": quack.Test,
"registry-creds": registrycreds.Test,
"sealed-secrets": sealedsecrets.Test,
"stubs": stubs.Test,
"thanos": monitoring.TestThanos,
"vault": vault.Test,
"velero": velero.Test,
}
var Phases = &cobra.Command{
Use: "phases",
Run: func(cmd *cobra.Command, args []string) {
for name, fn := range tests {
_name := name
_fn := fn
flag, _ := cmd.Flags().GetBool(name)
if !flag {
continue
}
queue(_name, _fn, wg, ch)
}
},
}
Test.AddCommand(Phases)
for name, fn := range tests {
_name := name
_fn := fn
Phases.Flags().Bool(name, false, "Test "+name)
Test.AddCommand(&cobra.Command{
Use: name,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
queue(_name, _fn, wg, ch)
},
})
}
testAllCmd := &cobra.Command{
Use: "all",
Short: "Test all components",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
for name, fn := range tests {
queue(name, fn, wg, ch)
}
},
}
Test.PersistentFlags().IntVar(&wait, "wait", 0, "Time in seconds to wait for tests to pass")
Test.PersistentFlags().IntVar(&waitInterval, "wait-interval", 5, "Time in seconds to wait between repeated tests")
Test.PersistentFlags().StringVar(&junitPath, "junit-path", "", "Path to export JUnit formatted test results")
Test.PersistentFlags().StringVar(&suiteName, "suite-name", "", "Name of the Test Suite, defaults to platform name")
Test.PersistentFlags().BoolVar(&failOnError, "fail-on-error", true, "Return an exit code of 1 if any tests fail")
Test.PersistentFlags().BoolVarP(&testE2E, "e2e", "", false, "Run e2e tests")
Test.PersistentFlags().BoolVar(&showProgress, "progress", true, "Display progress as tests run")
Test.PersistentFlags().IntVar(&concurrency, "concurrency", 8, "Number of tests to run concurrently")
Test.AddCommand(testAllCmd)
}