|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package browser |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "github.com/elastic/beats/v7/heartbeat/monitors/jobs" |
| 12 | + "github.com/elastic/beats/v7/heartbeat/monitors/plugin" |
| 13 | + "github.com/elastic/beats/v7/libbeat/beat" |
| 14 | + "github.com/elastic/beats/v7/libbeat/common" |
| 15 | + "github.com/elastic/beats/v7/x-pack/heartbeat/monitors/browser/synthexec" |
| 16 | +) |
| 17 | + |
| 18 | +type JourneyLister func(ctx context.Context, suitePath string, params common.MapStr) (journeyNames []string, err error) |
| 19 | + |
| 20 | +type Suite struct { |
| 21 | + rawCfg *common.Config |
| 22 | + suiteCfg *Config |
| 23 | +} |
| 24 | + |
| 25 | +func NewSuite(rawCfg *common.Config) (*Suite, error) { |
| 26 | + s := &Suite{ |
| 27 | + rawCfg: rawCfg, |
| 28 | + suiteCfg: DefaultConfig(), |
| 29 | + } |
| 30 | + err := rawCfg.Unpack(s.suiteCfg) |
| 31 | + if err != nil { |
| 32 | + return nil, ErrBadConfig(err) |
| 33 | + } |
| 34 | + |
| 35 | + return s, nil |
| 36 | +} |
| 37 | + |
| 38 | +func ErrBadConfig(err error) error { |
| 39 | + return fmt.Errorf("could not parse suite config: %w", err) |
| 40 | +} |
| 41 | + |
| 42 | +func (s *Suite) String() string { |
| 43 | + panic("implement me") |
| 44 | +} |
| 45 | + |
| 46 | +func (s *Suite) Fetch() error { |
| 47 | + return s.suiteCfg.Source.Active().Fetch() |
| 48 | +} |
| 49 | + |
| 50 | +func (s *Suite) Workdir() string { |
| 51 | + return s.suiteCfg.Source.Active().Workdir() |
| 52 | +} |
| 53 | + |
| 54 | +func (s *Suite) InlineSource() (string, bool) { |
| 55 | + if s.suiteCfg.Source.Inline != nil { |
| 56 | + return s.suiteCfg.Source.Inline.Script, true |
| 57 | + } |
| 58 | + return "", false |
| 59 | +} |
| 60 | + |
| 61 | +func (s *Suite) Params() map[string]interface{} { |
| 62 | + return s.suiteCfg.Params |
| 63 | +} |
| 64 | + |
| 65 | +func (s *Suite) Close() error { |
| 66 | + if s.suiteCfg.Source.ActiveMemo != nil { |
| 67 | + s.suiteCfg.Source.ActiveMemo.Close() |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func (s *Suite) extraArgs() []string { |
| 74 | + extraArgs := s.suiteCfg.SyntheticsArgs |
| 75 | + if s.suiteCfg.Sandbox { |
| 76 | + extraArgs = append(extraArgs, "--sandbox") |
| 77 | + } |
| 78 | + |
| 79 | + return extraArgs |
| 80 | +} |
| 81 | + |
| 82 | +func (s *Suite) jobs() []jobs.Job { |
| 83 | + var j jobs.Job |
| 84 | + if src, ok := s.InlineSource(); ok { |
| 85 | + j = synthexec.InlineJourneyJob(context.TODO(), src, s.Params(), s.extraArgs()...) |
| 86 | + } else { |
| 87 | + j = func(event *beat.Event) ([]jobs.Job, error) { |
| 88 | + err := s.Fetch() |
| 89 | + if err != nil { |
| 90 | + return nil, fmt.Errorf("could not fetch for suite job: %w", err) |
| 91 | + } |
| 92 | + sj, err := synthexec.SuiteJob(context.TODO(), s.Workdir(), s.Params(), s.extraArgs()...) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + return sj(event) |
| 97 | + } |
| 98 | + } |
| 99 | + return []jobs.Job{j} |
| 100 | +} |
| 101 | + |
| 102 | +func (s *Suite) plugin() plugin.Plugin { |
| 103 | + return plugin.Plugin{ |
| 104 | + Jobs: s.jobs(), |
| 105 | + Close: s.Close, |
| 106 | + Endpoints: 1, |
| 107 | + } |
| 108 | +} |
0 commit comments