| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package collectorapi |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | ) |
| 9 | |
| 10 | const MockConfigSchema = ` |
| 11 | { |
| 12 | "$schema": "http://json-schema.org/draft-07/schema#", |
| 13 | "type": "object", |
| 14 | "properties": { |
| 15 | "option_str": { |
| 16 | "type": "string", |
| 17 | "description": "Option string value" |
| 18 | }, |
| 19 | "option_int": { |
| 20 | "type": "integer", |
| 21 | "description": "Option integer value" |
| 22 | } |
| 23 | }, |
| 24 | "required": [ |
| 25 | "option_str", |
| 26 | "option_int" |
| 27 | ] |
| 28 | } |
| 29 | ` |
| 30 | |
| 31 | type MockConfiguration struct { |
| 32 | OptionStr string `yaml:"option_str" json:"option_str"` |
| 33 | OptionInt int `yaml:"option_int" json:"option_int"` |
| 34 | } |
| 35 | |
| 36 | // MockCollectorV1 MockCollectorV1. |
| 37 | type MockCollectorV1 struct { |
| 38 | Base |
| 39 | |
| 40 | Config MockConfiguration `yaml:",inline" json:""` |
| 41 | |
| 42 | FailOnInit bool |
| 43 | |
| 44 | InitFunc func(context.Context) error |
| 45 | CheckFunc func(context.Context) error |
| 46 | ChartsFunc func() *Charts |
| 47 | CollectFunc func(context.Context) map[string]int64 |
| 48 | CleanupFunc func(context.Context) |
| 49 | CleanupDone bool |
| 50 | } |
| 51 | |
| 52 | // Init invokes InitFunc. |
| 53 | func (m *MockCollectorV1) Init(ctx context.Context) error { |
| 54 | if m.FailOnInit { |
| 55 | return errors.New("mock init error") |
| 56 | } |
| 57 | if m.InitFunc == nil { |
| 58 | return nil |
| 59 | } |
| 60 | return m.InitFunc(ctx) |
| 61 | } |
| 62 | |
| 63 | // Check invokes CheckFunc. |
| 64 | func (m *MockCollectorV1) Check(ctx context.Context) error { |
| 65 | if m.CheckFunc == nil { |
| 66 | return nil |
| 67 | } |
| 68 | return m.CheckFunc(ctx) |
| 69 | } |
| 70 | |
| 71 | // Charts invokes ChartsFunc. |
| 72 | func (m *MockCollectorV1) Charts() *Charts { |
| 73 | if m.ChartsFunc == nil { |
| 74 | return nil |
| 75 | } |
| 76 | return m.ChartsFunc() |
| 77 | } |
| 78 | |
| 79 | // Collect invokes CollectDunc. |
| 80 | func (m *MockCollectorV1) Collect(ctx context.Context) map[string]int64 { |
| 81 | if m.CollectFunc == nil { |
| 82 | return nil |
| 83 | } |
| 84 | return m.CollectFunc(ctx) |
| 85 | } |
| 86 | |
| 87 | // Cleanup sets CleanupDone to true. |
| 88 | func (m *MockCollectorV1) Cleanup(ctx context.Context) { |
| 89 | if m.CleanupFunc != nil { |
| 90 | m.CleanupFunc(ctx) |
| 91 | } |
| 92 | m.CleanupDone = true |
| 93 | } |
| 94 | |
| 95 | func (m *MockCollectorV1) Configuration() any { |
| 96 | return m.Config |
| 97 | } |