summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/agent/module/mock.go
blob: f83c7dbccd267c2cf183939ec0f8232aea1f5ba9 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package module

import "errors"

const MockConfigSchema = `
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "option_str": {
      "type": "string",
      "description": "Option string value"
    },
    "option_int": {
      "type": "integer",
      "description": "Option integer value"
    }
  },
  "required": [
    "option_str",
    "option_int"
  ]
}
`

type MockConfiguration struct {
	OptionStr string `yaml:"option_str" json:"option_str"`
	OptionInt int    `yaml:"option_int" json:"option_int"`
}

// MockModule MockModule.
type MockModule struct {
	Base

	Config MockConfiguration `yaml:",inline" json:""`

	FailOnInit bool

	InitFunc    func() error
	CheckFunc   func() error
	ChartsFunc  func() *Charts
	CollectFunc func() map[string]int64
	CleanupFunc func()
	CleanupDone bool
}

// Init invokes InitFunc.
func (m *MockModule) Init() error {
	if m.FailOnInit {
		return errors.New("mock init error")
	}
	if m.InitFunc == nil {
		return nil
	}
	return m.InitFunc()
}

// Check invokes CheckFunc.
func (m *MockModule) Check() error {
	if m.CheckFunc == nil {
		return nil
	}
	return m.CheckFunc()
}

// Charts invokes ChartsFunc.
func (m *MockModule) Charts() *Charts {
	if m.ChartsFunc == nil {
		return nil
	}
	return m.ChartsFunc()
}

// Collect invokes CollectDunc.
func (m *MockModule) Collect() map[string]int64 {
	if m.CollectFunc == nil {
		return nil
	}
	return m.CollectFunc()
}

// Cleanup sets CleanupDone to true.
func (m *MockModule) Cleanup() {
	if m.CleanupFunc != nil {
		m.CleanupFunc()
	}
	m.CleanupDone = true
}

func (m *MockModule) Configuration() any {
	return m.Config
}