This repository has been archived by the owner on Oct 28, 2024. It is now read-only.
forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler_test.go
121 lines (100 loc) · 3.4 KB
/
scheduler_test.go
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package collector
import (
"fmt"
"testing"
"github.com/DataDog/datadog-agent/pkg/aggregator/sender"
core "github.com/DataDog/datadog-agent/pkg/collector/corechecks"
"github.com/stretchr/testify/assert"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/integration"
"github.com/DataDog/datadog-agent/pkg/collector/check"
)
type MockCheck struct {
core.CheckBase
Name string
Loader string
}
func (m MockCheck) Run() error {
// not used in test
panic("implement me")
}
func (m MockCheck) String() string {
return fmt.Sprintf("Loader: %s, Check: %s", m.Loader, m.Name)
}
type MockCoreLoader struct{}
func (l *MockCoreLoader) Name() string {
return "core"
}
//nolint:revive // TODO(AML) Fix revive linter
func (l *MockCoreLoader) Load(_ sender.SenderManager, config integration.Config, _ integration.Data) (check.Check, error) {
mockCheck := MockCheck{Name: config.Name, Loader: l.Name()}
return &mockCheck, nil
}
type MockPythonLoader struct{}
func (l *MockPythonLoader) Name() string {
return "python"
}
//nolint:revive // TODO(AML) Fix revive linter
func (l *MockPythonLoader) Load(_ sender.SenderManager, config integration.Config, _ integration.Data) (check.Check, error) {
mockCheck := MockCheck{Name: config.Name, Loader: l.Name()}
return &mockCheck, nil
}
func TestAddLoader(t *testing.T) {
s := CheckScheduler{}
assert.Len(t, s.loaders, 0)
s.AddLoader(&MockCoreLoader{})
s.AddLoader(&MockCoreLoader{}) // noop
assert.Len(t, s.loaders, 1)
}
func TestGetChecksFromConfigs(t *testing.T) {
s := CheckScheduler{}
assert.Len(t, s.loaders, 0)
s.AddLoader(&MockCoreLoader{})
s.AddLoader(&MockPythonLoader{})
// test instance level loader selection
conf1 := integration.Config{
Name: "check_a",
Instances: []integration.Data{
integration.Data("{\"loader\": \"python\"}"),
integration.Data("{\"loader\": \"core\"}"),
integration.Data("{\"loader\": \"wrong\"}"),
integration.Data("{}"), // default to init config loader
},
InitConfig: integration.Data("{\"loader\": \"core\"}"),
}
// test init config level loader selection
conf2 := integration.Config{
Name: "check_b",
Instances: []integration.Data{integration.Data("{\"value\": 1}")},
InitConfig: integration.Data("{\"loader\": \"python\"}"),
}
// test that wrong loader will be skipped
conf3 := integration.Config{
Name: "check_wrong",
Instances: []integration.Data{integration.Data("{\"value\": 1}")},
InitConfig: integration.Data("{\"loader\": \"wrong_loader\"}"),
}
// test that first loader is selected when no loader is selected
// this is the current behaviour
conf4 := integration.Config{
Name: "check_c",
Instances: []integration.Data{integration.Data("{}")},
InitConfig: integration.Data("{}"),
}
checks := s.GetChecksFromConfigs([]integration.Config{conf1, conf2, conf3, conf4}, false)
assert.Len(t, s.loaders, 2)
var actualChecks []string
for _, c := range checks {
actualChecks = append(actualChecks, c.String())
}
assert.Equal(t, []string{
"Loader: python, Check: check_a",
"Loader: core, Check: check_a",
"Loader: core, Check: check_a",
"Loader: python, Check: check_b",
"Loader: core, Check: check_c",
}, actualChecks)
}