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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
// SPDX-License-Identifier: GPL-3.0-or-later
package x509check
import (
"crypto/x509"
"errors"
"os"
"testing"
"github.com/netdata/netdata/go/plugins/plugin/go.d/agent/module"
"github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/tlscfg"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
dataConfigJSON, _ = os.ReadFile("testdata/config.json")
dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
)
func Test_testDataIsValid(t *testing.T) {
for name, data := range map[string][]byte{
"dataConfigJSON": dataConfigJSON,
"dataConfigYAML": dataConfigYAML,
} {
assert.NotNil(t, data, name)
}
}
func TestX509Check_ConfigurationSerialize(t *testing.T) {
module.TestConfigurationSerialize(t, &X509Check{}, dataConfigJSON, dataConfigYAML)
}
func TestX509Check_Cleanup(t *testing.T) {
assert.NotPanics(t, New().Cleanup)
}
func TestX509Check_Charts(t *testing.T) {
x509Check := New()
x509Check.Source = "https://example.com"
require.NoError(t, x509Check.Init())
assert.NotNil(t, x509Check.Charts())
}
func TestX509Check_Init(t *testing.T) {
const (
file = iota
net
smtp
)
tests := map[string]struct {
config Config
providerType int
err bool
}{
"ok from net https": {
config: Config{Source: "https://example.org"},
providerType: net,
},
"ok from net tcp": {
config: Config{Source: "tcp://example.org"},
providerType: net,
},
"ok from file": {
config: Config{Source: "file:///home/me/cert.pem"},
providerType: file,
},
"ok from smtp": {
config: Config{Source: "smtp://smtp.my_mail.org:587"},
providerType: smtp,
},
"empty source": {
config: Config{Source: ""},
err: true},
"unknown provider": {
config: Config{Source: "http://example.org"},
err: true,
},
"nonexistent TLSCA": {
config: Config{Source: "https://example.org", TLSConfig: tlscfg.TLSConfig{TLSCA: "testdata/tls"}},
err: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
x509Check := New()
x509Check.Config = test.config
if test.err {
assert.Error(t, x509Check.Init())
} else {
require.NoError(t, x509Check.Init())
var typeOK bool
switch test.providerType {
case file:
_, typeOK = x509Check.prov.(*fromFile)
case net:
_, typeOK = x509Check.prov.(*fromNet)
case smtp:
_, typeOK = x509Check.prov.(*fromSMTP)
}
assert.True(t, typeOK)
}
})
}
}
func TestX509Check_Check(t *testing.T) {
x509Check := New()
x509Check.prov = &mockProvider{certs: []*x509.Certificate{{}}}
assert.NoError(t, x509Check.Check())
}
func TestX509Check_Check_ReturnsFalseOnProviderError(t *testing.T) {
x509Check := New()
x509Check.prov = &mockProvider{err: true}
assert.Error(t, x509Check.Check())
}
func TestX509Check_Collect(t *testing.T) {
x509Check := New()
x509Check.Source = "https://example.com"
require.NoError(t, x509Check.Init())
x509Check.prov = &mockProvider{certs: []*x509.Certificate{{}}}
collected := x509Check.Collect()
assert.NotZero(t, collected)
ensureCollectedHasAllChartsDimsVarsIDs(t, x509Check, collected)
}
func TestX509Check_Collect_ReturnsNilOnProviderError(t *testing.T) {
x509Check := New()
x509Check.prov = &mockProvider{err: true}
assert.Nil(t, x509Check.Collect())
}
func TestX509Check_Collect_ReturnsNilOnZeroCertificates(t *testing.T) {
x509Check := New()
x509Check.prov = &mockProvider{certs: []*x509.Certificate{}}
mx := x509Check.Collect()
assert.Nil(t, mx)
}
func ensureCollectedHasAllChartsDimsVarsIDs(t *testing.T, x509Check *X509Check, collected map[string]int64) {
for _, chart := range *x509Check.Charts() {
for _, dim := range chart.Dims {
_, ok := collected[dim.ID]
assert.Truef(t, ok, "collected metrics has no data for dim '%s' chart '%s'", dim.ID, chart.ID)
}
for _, v := range chart.Vars {
_, ok := collected[v.ID]
assert.Truef(t, ok, "collected metrics has no data for var '%s' chart '%s'", v.ID, chart.ID)
}
}
}
type mockProvider struct {
certs []*x509.Certificate
err bool
}
func (m mockProvider) certificates() ([]*x509.Certificate, error) {
if m.err {
return nil, errors.New("mock certificates error")
}
return m.certs, nil
}
|