/
ancientt.go
435 lines (372 loc) · 11.8 KB
/
ancientt.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
Copyright 2019 Cloudical Deutschland GmbH. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bufio"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/galexrt/ancientt/outputs"
"github.com/galexrt/ancientt/parsers"
"github.com/galexrt/ancientt/pkg/config"
"github.com/galexrt/ancientt/runners"
"github.com/galexrt/ancientt/testers"
au "github.com/logrusorgru/aurora"
"github.com/mattn/go-isatty"
"github.com/prometheus/common/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
outputSeparator = aurora.Red("===================")
aurora = au.NewAurora(isatty.IsTerminal(os.Stdout.Fd()))
rootCmd = &cobra.Command{
Use: "ancientt",
Short: "Ancientt is a tool to automate network testing tools, like iperf3, in dynamic environments such as Kubernetes and more to come dynamic environments.",
RunE: run,
}
cfg *config.Config
logLevel string
logger *zap.Logger
)
func init() {
// Set flags, viper binds for flags and viper bind default values
rootCmd.PersistentFlags().Bool("version", false, "Print version info and exit.")
rootCmd.PersistentFlags().BoolP("only-print-plan", "p", false, "Only print plan for the testdefinitions to console and exit.")
rootCmd.PersistentFlags().Bool("no-cleanup", false, "If runners should run cleanup routines after the tests.")
rootCmd.PersistentFlags().BoolP("yes", "y", false, "Ask for user confirmation for each test before executing it.")
rootCmd.PersistentFlags().StringP("testdefinition", "c", "testdefinition.yaml", "Path to the testdefinitions to read for the tests.")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "INFO", "Log level (DEBUG, INFO, WARN, ERROR, default: INFO).")
viper.BindPFlag("version", rootCmd.PersistentFlags().Lookup("version"))
viper.BindPFlag("only-print-plan", rootCmd.PersistentFlags().Lookup("only-print-plan"))
viper.BindPFlag("no-cleanup", rootCmd.PersistentFlags().Lookup("no-cleanup"))
viper.BindPFlag("yes", rootCmd.PersistentFlags().Lookup("yes"))
viper.BindPFlag("testdefinition", rootCmd.PersistentFlags().Lookup("testdefinition"))
viper.SetDefault("version", false)
viper.SetDefault("only-print-plan", false)
viper.SetDefault("no-cleanup", false)
viper.SetDefault("yes", false)
viper.SetDefault("testdefinition", "testdefinition.yaml")
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// loadConfig load the given config file
func loadConfig() error {
cfgFile := viper.GetString("testdefinition")
if cfgFile == "" {
return fmt.Errorf("empty testdefinition flag given")
}
var err error
cfg, err = config.Load(cfgFile)
return err
}
func newLogger() (*zap.Logger, error) {
// Logger Setup
loggerConfig := zap.NewProductionConfig()
level, err := zapcore.ParseLevel(logLevel)
if err != nil {
return nil, fmt.Errorf("failed to parse log level. %w", err)
}
loggerConfig.Level.SetLevel(level)
logger, err := loggerConfig.Build()
if err != nil {
return nil, fmt.Errorf("failed to configure logger. %w", err)
}
return logger, nil
}
func run(cmd *cobra.Command, args []string) error {
if viper.GetBool("version") {
fmt.Print(version.Print(os.Args[0]))
return nil
}
var err error
logger, err = newLogger()
if err != nil {
return err
}
logger.With(
zap.String("program", os.Args[0]),
zap.String("version", version.Version),
zap.String("branch", version.Branch),
zap.String("revision", version.Revision),
zap.String("go", version.GoVersion),
zap.String("buildUser", version.BuildUser),
zap.String("buildDate", version.BuildDate),
).Info("starting ancientt")
if err := loadConfig(); err != nil {
return err
}
var runner runners.Runner
runnerName := strings.ToLower(cfg.Runner.Name)
runnerNewFunc, ok := runners.Factories[runnerName]
if !ok {
return fmt.Errorf("runner with name %s not found", runnerName)
}
runner, err = runnerNewFunc(logger, cfg)
if err != nil {
return err
}
for i, test := range cfg.Tests {
logger.With(zap.String("runner", runnerName)).Info(fmt.Sprintf("doing test '%s', %d of %d", test.Name, i+1, len(cfg.Tests)))
logger, tester, parser, outputsAssembled, err := prepare(logger, test, runnerName)
if err != nil {
logger.Error("error preparing test run", zap.Error(err))
if !*test.RunOptions.ContinueOnError {
return err
}
logger.Warn(fmt.Sprintf("skipping test %d of %d due to error in initial prepare step", i+1, len(cfg.Tests)))
continue
}
// Get hosts for the test
hosts, err := runner.GetHostsForTest(test)
if err != nil {
return err
}
// Create testers.Environment with the hosts
env := &testers.Environment{
Hosts: hosts,
}
// Get plan from testers.Plan()
plan, err := tester.Plan(env, test)
if err != nil {
return err
}
// Set TestStartTime for usage in output / results later on
plan.TestStartTime = time.Now()
fmt.Println(outputSeparator)
// Pretty print the plan of the test to the shell
fmt.Println("--> BEGIN PLAN")
plan.PrettyPrint()
fmt.Println("--> END PLAN")
if viper.GetBool("only-print-plan") {
return nil
}
if !viper.GetBool("yes") {
// Ask user if we can continue or not
if err := askUserForYes(); err != nil {
return err
}
}
logger.Info("preparing test")
// Prepare the runner for the plan
if err = runner.Prepare(test.RunOptions, plan); err != nil {
return err
}
var wg sync.WaitGroup
doneCh := make(chan struct{})
inCh := make(chan parsers.Input)
dataCh := make(chan outputs.Data)
errCh := make(chan error)
go func() {
select {
case erro := <-errCh:
logger.Error(erro.Error())
case <-doneCh:
return
}
}()
wg.Add(1)
go func() {
defer wg.Done()
// Close dataCh as there won't be anything else coming through
defer close(dataCh)
if err := parser.Parse(doneCh, inCh, dataCh); err != nil {
errCh <- err
return
}
}()
// Start each output
wg.Add(1)
go func() {
defer wg.Done()
if err := doOutputs(outputsAssembled, test, doneCh, dataCh); err != nil {
errCh <- err
return
}
}()
logger.Info("executing test")
// Execute the plan
if err := runner.Execute(plan, inCh); err != nil {
errCh <- err
}
logger.Debug("runner execute returned, closing inCh and wg.Wait()")
close(inCh)
if err := checkForErrors(plan); err != nil {
logger.Error("found error during run", zap.Error(err))
if !*test.RunOptions.ContinueOnError {
return err
}
logger.Warn("continue on error run option given for test, continuing")
}
wg.Wait()
close(doneCh)
fmt.Println(outputSeparator)
fmt.Println(aurora.Magenta("Following files have been created / used:"))
for outName, output := range outputsAssembled {
for _, file := range output.OutputFiles() {
fmt.Printf("%s (output: %s)\n", file, outName)
}
}
fmt.Println(outputSeparator)
// Run runners.Cleanup() func if wanted by the user
if !viper.GetBool("no-cleanup") {
if err := runnerCleanup(runner, plan); err != nil {
return err
}
}
}
logger.Info("done with tests")
return nil
}
func askUserForYes() error {
fmt.Println(outputSeparator)
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print(aurora.Underline("Are you sure you want to continue with above test plan? ('Yes' or 'No'):"), " ")
userInput, err := reader.ReadString('\n')
if err != nil {
return err
}
userInput = strings.TrimSpace(strings.ToLower(userInput))
fmt.Println(outputSeparator)
if userInput == "yes" || userInput == "y" {
break
} else if userInput == "no" || userInput == "n" {
return fmt.Errorf("aborted by user")
}
}
return nil
}
func prepare(logger *zap.Logger, test *config.Test, runnerName string) (*zap.Logger, testers.Tester, parsers.Parser, map[string]outputs.Output, error) {
var tester testers.Tester
var parser parsers.Parser
outputsAssembled := map[string]outputs.Output{}
// Get tester for the test
testerName := strings.ToLower(test.Type)
logger = logger.With(zap.String("tester", testerName), zap.String("parser", testerName), zap.String("runner", runnerName))
testerNewFunc, ok := testers.Factories[testerName]
if !ok {
return logger, nil, nil, nil, fmt.Errorf("tester with name %s not found", testerName)
}
var err error
if tester, err = testerNewFunc(logger, cfg, test); err != nil {
return logger, nil, nil, nil, err
}
// Get parser for the tester output
parserNewFunc, ok := parsers.Factories[testerName]
if !ok {
return logger, nil, nil, nil, fmt.Errorf("parser with name %s not found", testerName)
}
if parser, err = parserNewFunc(logger, cfg, test); err != nil {
return logger, nil, nil, nil, err
}
for _, outputItem := range test.Outputs {
outputName := outputItem.Name
outputNewFunc, ok := outputs.Factories[outputName]
if !ok {
return logger, nil, nil, nil, fmt.Errorf("output with name %s not found", outputName)
}
var err error
outputsAssembled[outputName], err = outputNewFunc(logger, cfg, &outputItem)
if err != nil {
return logger, nil, nil, nil, err
}
}
return logger, tester, parser, outputsAssembled, err
}
func doOutputs(outputsAssembled map[string]outputs.Output, test *config.Test, doneCh chan struct{}, dataCh chan outputs.Data) error {
for {
select {
case data, ok := <-dataCh:
if !ok {
logger.Debug("dataCh closed, in doOutputs()")
return nil
}
for _, outputItem := range test.Outputs {
outputName := outputItem.Name
if err := outputsAssembled[outputName].Do(data); err != nil {
// TODO Run all ouputs and concat errors
return fmt.Errorf("error in output Do() func. %+v", err)
}
}
case <-doneCh:
return nil
}
}
}
func checkForErrors(plan *testers.Plan) error {
errorOccured := false
fmt.Println(outputSeparator)
for _, command := range plan.Commands {
for _, task := range command {
if task.Status == nil || task.Sleep != 0 {
logger.Debug("task status is empty or is sleep task, continuing")
continue
}
// FailedHosts
if len(task.Status.FailedHosts.Servers) > 0 {
errorOccured = true
fmt.Println(aurora.Yellow("-> Failed Server Hosts"))
for host, count := range task.Status.FailedHosts.Servers {
fmt.Printf("%s - %d\n", host, count)
for _, err := range task.Status.Errors[host] {
fmt.Println(err)
}
}
}
if len(task.Status.FailedHosts.Clients) > 0 {
errorOccured = true
fmt.Println(aurora.Yellow("-> Failed Client Hosts"))
for host, count := range task.Status.FailedHosts.Clients {
fmt.Printf("%s - %d\n", host, count)
for _, err := range task.Status.Errors[host] {
fmt.Println(err)
}
}
}
// SuccessfulHosts
if len(task.Status.SuccessfulHosts.Servers) > 0 {
fmt.Println(aurora.Green("-> Successful Server Hosts"))
for host, count := range task.Status.SuccessfulHosts.Servers {
fmt.Printf("%s - %d\n", host, count)
}
}
if len(task.Status.SuccessfulHosts.Clients) > 0 {
fmt.Println(aurora.Green("-> Successful Client Hosts"))
for host, count := range task.Status.SuccessfulHosts.Clients {
fmt.Printf("%s - %d\n", host, count)
}
}
}
}
fmt.Println(outputSeparator)
if errorOccured {
return fmt.Errorf("errors occured during task")
}
return nil
}
func runnerCleanup(runner runners.Runner, plan *testers.Plan) error {
logger.Info("running runner cleanup func for test")
if err := runner.Cleanup(plan); err != nil {
return err
}
return nil
}