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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package jobmgr
import (
"bytes"
"context"
"errors"
"strings"
"testing"
"time"
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/confgroup"
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/netdataapi"
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/safewriter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type runSim struct {
do func(mgr *Manager, in chan []*confgroup.Group)
wantDiscovered []confgroup.Config
wantSeen []seenConfig
wantExposed []seenConfig
wantRunning []string
wantDyncfg string
}
func (s *runSim) run(t *testing.T) {
t.Helper()
require.NotNil(t, s.do, "s.do is nil")
var buf bytes.Buffer
mgr := New()
mgr.api = netdataapi.New(safewriter.New(&buf))
mgr.Modules = prepareMockRegistry()
done := make(chan struct{})
grpCh := make(chan []*confgroup.Group)
ctx, cancel := context.WithCancel(context.Background())
go func() { defer close(done); defer close(grpCh); mgr.Run(ctx, grpCh) }()
timeout := time.Second * 5
select {
case <-mgr.started:
case <-time.After(timeout):
t.Errorf("failed to start work in %s", timeout)
}
s.do(mgr, grpCh)
cancel()
select {
case <-done:
case <-time.After(timeout):
t.Errorf("failed to finish work in %s", timeout)
}
var lines []string
for _, s := range strings.Split(buf.String(), "\n") {
if strings.HasPrefix(s, "CONFIG") && strings.Contains(s, " template ") {
continue
}
if strings.HasPrefix(s, "FUNCTION_RESULT_BEGIN") {
parts := strings.Fields(s)
s = strings.Join(parts[:len(parts)-1], " ") // remove timestamp
}
lines = append(lines, s)
}
wantDyncfg, gotDyncfg := strings.TrimSpace(s.wantDyncfg), strings.TrimSpace(strings.Join(lines, "\n"))
//fmt.Println(gotDyncfg)
assert.Equal(t, wantDyncfg, gotDyncfg, "dyncfg commands")
var n int
for _, cfgs := range mgr.discoveredConfigs.items {
n += len(cfgs)
}
wantLen, gotLen := len(s.wantDiscovered), n
require.Equalf(t, wantLen, gotLen, "discoveredConfigs: different len (want %d got %d)", wantLen, gotLen)
for _, cfg := range s.wantDiscovered {
cfgs, ok := mgr.discoveredConfigs.items[cfg.Source()]
require.Truef(t, ok, "discoveredConfigs: source %s is not found", cfg.Source())
_, ok = cfgs[cfg.Hash()]
require.Truef(t, ok, "discoveredConfigs: source %s config %d is not found", cfg.Source(), cfg.Hash())
}
wantLen, gotLen = len(s.wantSeen), len(mgr.seenConfigs.items)
require.Equalf(t, wantLen, gotLen, "seenConfigs: different len (want %d got %d)", wantLen, gotLen)
for _, scfg := range s.wantSeen {
v, ok := mgr.seenConfigs.lookup(scfg.cfg)
require.Truef(t, ok, "seenConfigs: config '%s' is not found", scfg.cfg.UID())
require.Truef(t, scfg.status == v.status, "seenConfigs: wrong status, want %s got %s", scfg.status, v.status)
}
wantLen, gotLen = len(s.wantExposed), len(mgr.exposedConfigs.items)
require.Equalf(t, wantLen, gotLen, "exposedConfigs: different len (want %d got %d)", wantLen, gotLen)
for _, scfg := range s.wantExposed {
v, ok := mgr.exposedConfigs.lookup(scfg.cfg)
require.Truef(t, ok && scfg.cfg.UID() == v.cfg.UID(), "exposedConfigs: config '%s' is not found", scfg.cfg.UID())
require.Truef(t, scfg.status == v.status, "exposedConfigs: wrong status, want %s got %s", scfg.status, v.status)
}
wantLen, gotLen = len(s.wantRunning), len(mgr.runningJobs.items)
require.Equalf(t, wantLen, gotLen, "runningJobs: different len (want %d got %d)", wantLen, gotLen)
for _, name := range s.wantRunning {
_, ok := mgr.runningJobs.lookup(name)
require.Truef(t, ok, "runningJobs: job '%s' is not found", name)
}
}
func prepareMockRegistry() module.Registry {
reg := module.Registry{}
type config struct {
OptionOne string `yaml:"option_one" json:"option_one"`
OptionTwo int64 `yaml:"option_two" json:"option_two"`
}
reg.Register("success", module.Creator{
JobConfigSchema: module.MockConfigSchema,
Create: func() module.Module {
return &module.MockModule{
ChartsFunc: func() *module.Charts {
return &module.Charts{&module.Chart{ID: "id", Title: "title", Units: "units", Dims: module.Dims{{ID: "id1"}}}}
},
CollectFunc: func() map[string]int64 { return map[string]int64{"id1": 1} },
}
},
Config: func() any {
return &config{OptionOne: "one", OptionTwo: 2}
},
})
reg.Register("fail", module.Creator{
Create: func() module.Module {
return &module.MockModule{
InitFunc: func() error { return errors.New("mock failed init") },
}
},
})
return reg
}
|