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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package agent
import (
"bytes"
"context"
"sync"
"testing"
"time"
"github.com/netdata/netdata/go/go.d.plugin/agent/module"
"github.com/netdata/netdata/go/go.d.plugin/agent/safewriter"
"github.com/stretchr/testify/assert"
)
// TODO: tech debt
func TestNew(t *testing.T) {
}
func TestAgent_Run(t *testing.T) {
a := New(Config{Name: "nodyncfg"})
var buf bytes.Buffer
a.Out = safewriter.New(&buf)
var mux sync.Mutex
stats := make(map[string]int)
a.ModuleRegistry = prepareRegistry(&mux, stats, "module1", "module2")
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(1)
go func() { defer wg.Done(); a.run(ctx) }()
time.Sleep(time.Second * 2)
cancel()
wg.Wait()
assert.Equalf(t, 1, stats["module1_init"], "module1 init")
assert.Equalf(t, 1, stats["module2_init"], "module2 init")
assert.Equalf(t, 1, stats["module1_check"], "module1 check")
assert.Equalf(t, 1, stats["module2_check"], "module2 check")
assert.Equalf(t, 1, stats["module1_charts"], "module1 charts")
assert.Equalf(t, 1, stats["module2_charts"], "module2 charts")
assert.Truef(t, stats["module1_collect"] > 0, "module1 collect")
assert.Truef(t, stats["module2_collect"] > 0, "module2 collect")
assert.Equalf(t, 1, stats["module1_cleanup"], "module1 cleanup")
assert.Equalf(t, 1, stats["module2_cleanup"], "module2 cleanup")
assert.True(t, buf.String() != "")
}
func prepareRegistry(mux *sync.Mutex, stats map[string]int, names ...string) module.Registry {
reg := module.Registry{}
for _, name := range names {
name := name
reg.Register(name, module.Creator{
Create: func() module.Module {
return prepareMockModule(name, mux, stats)
},
})
}
return reg
}
func prepareMockModule(name string, mux *sync.Mutex, stats map[string]int) module.Module {
return &module.MockModule{
InitFunc: func() error {
mux.Lock()
defer mux.Unlock()
stats[name+"_init"]++
return nil
},
CheckFunc: func() error {
mux.Lock()
defer mux.Unlock()
stats[name+"_check"]++
return nil
},
ChartsFunc: func() *module.Charts {
mux.Lock()
defer mux.Unlock()
stats[name+"_charts"]++
return &module.Charts{
&module.Chart{ID: "id", Title: "title", Units: "units", Dims: module.Dims{{ID: "id1"}}},
}
},
CollectFunc: func() map[string]int64 {
mux.Lock()
defer mux.Unlock()
stats[name+"_collect"]++
return map[string]int64{"id1": 1}
},
CleanupFunc: func() {
mux.Lock()
defer mux.Unlock()
stats[name+"_cleanup"]++
},
}
}
|