|
| 1 | +package provisiontest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/cenkalti/backoff" |
| 9 | + "github.com/rs/zerolog/log" |
| 10 | + "github.com/threefoldtech/zosbase/pkg/perf" |
| 11 | + "github.com/threefoldtech/zosbase/pkg/stubs" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + id = "provisiontest" |
| 16 | + schedule = "0 0 0 * * *" |
| 17 | + description = "daily provision test that deploys a test VM to verify the node can run virtual machines and sets flags for the power daemon" |
| 18 | +) |
| 19 | + |
| 20 | +type provisionTestTask struct{} |
| 21 | + |
| 22 | +var _ perf.Task = (*provisionTestTask)(nil) |
| 23 | + |
| 24 | +func NewTask() perf.Task { |
| 25 | + return &provisionTestTask{} |
| 26 | +} |
| 27 | + |
| 28 | +func (t *provisionTestTask) ID() string { |
| 29 | + return id |
| 30 | +} |
| 31 | + |
| 32 | +func (t *provisionTestTask) Cron() string { |
| 33 | + return schedule |
| 34 | +} |
| 35 | + |
| 36 | +func (t *provisionTestTask) Description() string { |
| 37 | + return description |
| 38 | +} |
| 39 | + |
| 40 | +func (t *provisionTestTask) Jitter() uint32 { |
| 41 | + return 30 * 60 |
| 42 | +} |
| 43 | + |
| 44 | +func (t *provisionTestTask) Run(ctx context.Context) (interface{}, error) { |
| 45 | + log.Debug().Msg("starting provision test task") |
| 46 | + |
| 47 | + cl := perf.MustGetZbusClient(ctx) |
| 48 | + zui := stubs.NewZUIStub(cl) |
| 49 | + |
| 50 | + var result []string |
| 51 | + |
| 52 | + op := func() error { |
| 53 | + errs := vmCheck(ctx) |
| 54 | + result = errorsToStrings(errs) |
| 55 | + |
| 56 | + if err := zui.PushErrors(ctx, "vm", result); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + if len(errs) != 0 { |
| 61 | + return fmt.Errorf("provision test failed: %s", result) |
| 62 | + } |
| 63 | + |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + notify := func(err error, t time.Duration) { |
| 68 | + log.Error().Err(err).Dur("retry-in", t).Msg("failed provision test. retrying") |
| 69 | + } |
| 70 | + |
| 71 | + bo := backoff.NewExponentialBackOff() |
| 72 | + bo.InitialInterval = 3 * time.Minute |
| 73 | + bo.MaxInterval = 30 * time.Second |
| 74 | + bo.MaxElapsedTime = 10 * time.Minute |
| 75 | + |
| 76 | + _ = backoff.RetryNotify(op, bo, notify) |
| 77 | + |
| 78 | + return result, nil |
| 79 | +} |
| 80 | + |
| 81 | +func errorsToStrings(errs []error) []string { |
| 82 | + s := make([]string, 0, len(errs)) |
| 83 | + for _, err := range errs { |
| 84 | + s = append(s, err.Error()) |
| 85 | + } |
| 86 | + return s |
| 87 | +} |
0 commit comments