-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
236 lines (195 loc) · 6.68 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/Masterminds/semver/v3"
"github.com/chelnak/ysmrr"
"github.com/chelnak/ysmrr/pkg/colors"
"github.com/fatih/color"
"github.com/rs/zerolog/log"
"github.com/verdexlab/verdex/verdex/core"
"github.com/verdexlab/verdex/verdex/detect"
"github.com/verdexlab/verdex/verdex/products"
"github.com/verdexlab/verdex/verdex/templates"
"github.com/verdexlab/verdex/verdex/tests"
)
var maxSimultaneousTestContainers = 3
func RunTests(execution *core.Execution, product *products.Product) {
err := product.Versions.ReloadList(&execution.Config, product.ID)
if err != nil {
log.Fatal().Err(err).Msg("Failed to load product versions list")
}
log.Info().Str("product", product.ID).Msg("Starting Verdex tests")
testCases := tests.GetProductTestCases(product.ID)
if len(testCases) == 0 {
log.Warn().Msgf("[%s] No test cases found!", product.ID)
return
}
spinnerManager := ysmrr.NewSpinnerManager(
ysmrr.WithSpinnerColor(colors.FgHiBlue),
)
spinnerManager.Start()
var wg sync.WaitGroup
for _, testCase := range testCases {
versionsConstraints := []*semver.Constraints{testCase.Constraint}
if execution.Config.TestVersion != "" {
constraint, err := semver.NewConstraint(execution.Config.TestVersion)
if err != nil {
log.Fatal().Err(err).Msg("Invalid -test-version argument (for syntax, see https://github.com/Masterminds/semver)")
}
versionsConstraints = append(versionsConstraints, constraint)
}
versionsToTest, _, err := product.Versions.GetVersionsMatchingConstraints(versionsConstraints, []*semver.Constraints{})
if err != nil {
spinnerManager.Stop()
log.Fatal().Err(err).Msg("Failed to get versions matching constraints")
}
if len(versionsToTest) == 0 {
log.Warn().Msgf("[%s/%s] No versions to test found", testCase.Info.Product, testCase.Name)
continue
}
rounds := len(versionsToTest) / maxSimultaneousTestContainers
if rounds*maxSimultaneousTestContainers < len(versionsToTest) {
rounds++
}
for round := 0; round < rounds; round++ {
for _, version := range versionsToTest[round*maxSimultaneousTestContainers : min(len(versionsToTest), (round+1)*maxSimultaneousTestContainers)] {
wg.Add(1)
go runTestCaseOnVersion(execution, testCase, version, spinnerManager, &wg)
time.Sleep(100 * time.Millisecond) // preserve order
}
wg.Wait()
}
}
spinnerManager.Stop()
}
func runTestCaseOnVersion(execution *core.Execution, testCase *tests.TestCase, version *semver.Version, spinnerManager ysmrr.SpinnerManager, wg *sync.WaitGroup) {
defer wg.Done()
spinner := spinnerManager.AddSpinner("initializing instance")
setPrefixColor := func(c color.Attribute) {
spinner.UpdatePrefixf("%s %s ", testCase.Info.Product, color.New(c).Sprint(version))
}
setPrefixColor(color.FgCyan)
instance, err := testCase.NewInstance(version.String())
if err != nil {
setPrefixColor(color.FgRed)
spinner.ErrorWithMessage("failed")
log.Error().Err(err).Msgf("[%s/%s/%s] Failed to initialize test instance", testCase.Info.Product, testCase.Name, version)
instance.Destroy()
return
}
spinner.UpdateMessage("pulling docker image")
err = instance.Up()
if err != nil {
setPrefixColor(color.FgRed)
spinner.ErrorWithMessage("failed")
log.Error().Err(err).Msgf("[%s/%s/%s] Failed to up test instance", testCase.Info.Product, testCase.Name, version)
instance.Destroy()
return
}
spinner.UpdateMessage("starting instance")
for {
time.Sleep(5 * time.Second)
healthy, err := instance.ServiceIsHealthy()
if err != nil {
setPrefixColor(color.FgRed)
spinner.ErrorWithMessage("failed")
log.Error().Err(err).Msgf("[%s/%s/%s] Failed to up test instance", testCase.Info.Product, testCase.Name, version)
instance.Destroy()
return
} else if healthy {
break
}
}
target, err := instance.ServiceOrigin()
if err != nil {
spinner.ErrorWithMessage("failed")
log.Error().Err(err).Msgf("[%s/%s/%s] Failed to determine service origin", testCase.Info.Product, testCase.Name, version)
instance.Destroy()
return
}
if execution.Config.TestSession {
for {
spinner.UpdateMessagef("at %s | [d]etect or [s]top", target)
var action []byte = make([]byte, 1)
os.Stdin.Read(action)
if string(action) == "d" {
if execution.Config.TemplatesSource == core.TemplatesSourceLocalDirectory {
spinner.UpdateMessage("reloading templates")
templates.LoadTemplatesFromDirRecursively(&execution.Config)
}
spinner.UpdateMessage("running version detection")
completeMessage, errorMessage, _ := versionDetectionOnVersion(execution, version, target)
if completeMessage != "" {
log.Info().Msgf("%s %s : %s", execution.Product, version, completeMessage)
} else {
log.Error().Msgf("%s %s : %s", execution.Product, version, errorMessage)
}
} else if string(action) == "s" {
spinner.UpdateMessage("destroying instance")
instance.Destroy()
spinner.CompleteWithMessage("session done")
return
}
}
}
spinner.UpdateMessage("running version detection")
completeMessage, errorMessage, messageColor := versionDetectionOnVersion(execution, version, target)
setPrefixColor(messageColor)
if completeMessage != "" {
spinner.CompleteWithMessage(completeMessage)
} else {
spinner.ErrorWithMessage(errorMessage)
}
instance.Destroy()
}
func versionDetectionOnVersion(execution *core.Execution, version *semver.Version, target string) (completeMessage string, errorMessage string, messageColor color.Attribute) {
detection := core.Detection{
Target: target,
Product: execution.Product,
Variables: make(map[string]string, 0),
}
versions, err := detect.DetectVersion(execution, &detection)
if err != nil {
messageColor = color.FgRed
errorMessage = fmt.Sprintf("error: %s", err)
return
}
if len(versions) == 1 && versions[0].Equal(version) {
messageColor = color.FgGreen
completeMessage = "OK"
return
}
if len(versions) == 0 {
messageColor = color.FgRed
errorMessage = "failed: no version"
return
}
versionsResults := make([]string, 0)
for _, detectedVersion := range versions {
if detectedVersion.Equal(version) {
versionsResults = append(versionsResults, color.New(color.FgGreen).Sprintf("%s ✓", detectedVersion))
} else {
versionsResults = append(versionsResults, color.New(color.FgRed).Sprintf("%s ✗", detectedVersion))
}
}
var versionIsInaccurate bool
for _, v := range versions {
if v.Equal(version) {
versionIsInaccurate = true
break
}
}
message := strings.Join(versionsResults, color.HiBlackString(" | "))
if versionIsInaccurate {
messageColor = color.FgYellow
completeMessage = "inaccurate: " + message
} else {
messageColor = color.FgRed
errorMessage = "failed: " + message
}
return
}