forked from influxdata/telegraf-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_data_test.go
110 lines (97 loc) · 2.46 KB
/
class_data_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
package main
import (
"os"
"testing"
logrTesting "github.com/go-logr/logr/testing"
)
const (
sampleClassData = `
[[outputs.file]]
files = ["stdout"]
`
)
func Test_classDataHandler_getData(t *testing.T) {
tests := []struct {
name string
classes map[string]string
secretName string
className string
namespace string
want string
wantErr bool
}{
{
name: "data does not contain class name",
className: "unknown",
classes: map[string]string{testTelegrafClass: sampleClassData},
wantErr: true,
},
{
name: "returns secret data",
className: testTelegrafClass,
classes: map[string]string{testTelegrafClass: sampleClassData},
want: sampleClassData,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger := &logrTesting.TestLogger{T: t}
dir := createTempClassesDirectory(t, tt.classes)
defer os.RemoveAll(dir)
testClassDataHandler := &directoryClassDataHandler{
Logger: logger,
TelegrafClassesDirectory: dir,
}
got, err := testClassDataHandler.getData(tt.className)
if (err != nil) != tt.wantErr {
t.Errorf("classDataHandler.getData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("classDataHandler.getData() = %v, want %v", got, tt.want)
}
})
}
}
func Test_classDataHandler_validateClassData(t *testing.T) {
tests := []struct {
name string
classes map[string]string
wantErr bool
}{
{
name: "returns error when no classes found",
classes: map[string]string{},
wantErr: true,
},
{
name: "returns no error when all elmeents are valid",
classes: map[string]string{testTelegrafClass: sampleClassData},
wantErr: false,
},
{
name: "returns error when at least one TOML parsing error found",
classes: map[string]string{testTelegrafClass: sampleClassData, "invalid": `
[invalid]
"invalid" = invalid
`},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger := &logrTesting.TestLogger{T: t}
dir := createTempClassesDirectory(t, tt.classes)
defer os.RemoveAll(dir)
testClassDataHandler := &directoryClassDataHandler{
Logger: logger,
TelegrafClassesDirectory: dir,
}
err := testClassDataHandler.validateClassData()
if (err != nil) != tt.wantErr {
t.Errorf("classDataHandler.validateClassData() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}