master
go 109 lines 2.43 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package dummy
4
5 import (
6 "context"
7 "testing"
8 "time"
9
10 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
11 "github.com/netdata/netdata/go/plugins/plugin/framework/confgroup"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 )
16
17 func TestNewDiscovery(t *testing.T) {
18 tests := map[string]struct {
19 cfg Config
20 wantErr bool
21 }{
22 "valid config": {
23 cfg: Config{
24 Registry: confgroup.Registry{"module1": confgroup.Default{}},
25 Names: []string{"module1", "module2"},
26 },
27 },
28 "invalid config, registry not set": {
29 cfg: Config{
30 Names: []string{"module1", "module2"},
31 },
32 wantErr: true,
33 },
34 "invalid config, names not set": {
35 cfg: Config{
36 Names: []string{"module1", "module2"},
37 },
38 wantErr: true,
39 },
40 }
41
42 for name, test := range tests {
43 t.Run(name, func(t *testing.T) {
44 d, err := NewDiscovery(test.cfg)
45
46 if test.wantErr {
47 assert.Error(t, err)
48 } else {
49 require.NoError(t, err)
50 assert.NotNil(t, d)
51 }
52 })
53 }
54 }
55
56 func TestDiscovery_Run(t *testing.T) {
57 expected := []*confgroup.Group{
58 {
59 Source: "internal",
60 Configs: []confgroup.Config{
61 {
62 "name": "module1",
63 "module": "module1",
64 "update_every": collectorapi.UpdateEvery,
65 "autodetection_retry": collectorapi.AutoDetectionRetry,
66 "priority": collectorapi.Priority,
67 "__provider__": "dummy",
68 "__source_type__": confgroup.TypeStock,
69 "__source__": "internal",
70 },
71 {
72 "name": "module2",
73 "module": "module2",
74 "update_every": collectorapi.UpdateEvery,
75 "autodetection_retry": collectorapi.AutoDetectionRetry,
76 "priority": collectorapi.Priority,
77 "__provider__": "dummy",
78 "__source_type__": confgroup.TypeStock,
79 "__source__": "internal",
80 },
81 },
82 },
83 }
84
85 reg := confgroup.Registry{
86 "module1": {},
87 "module2": {},
88 }
89 cfg := Config{
90 Registry: reg,
91 Names: []string{"module1", "module2"},
92 }
93
94 discovery, err := NewDiscovery(cfg)
95 require.NoError(t, err)
96
97 in := make(chan []*confgroup.Group)
98 timeout := time.Second * 2
99
100 go discovery.Run(context.Background(), in)
101
102 var actual []*confgroup.Group
103 select {
104 case actual = <-in:
105 case <-time.After(timeout):
106 t.Logf("discovery timed out after %s", timeout)
107 }
108 assert.Equal(t, expected, actual)
109 }