master
go 165 lines 3.78 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package x509check
4
5 import (
6 "context"
7 "crypto/x509"
8 "errors"
9 "os"
10 "testing"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14
15 "github.com/netdata/netdata/go/plugins/pkg/tlscfg"
16 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest"
17 )
18
19 var (
20 dataConfigJSON, _ = os.ReadFile("testdata/config.json")
21 dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
22 )
23
24 func Test_testDataIsValid(t *testing.T) {
25 for name, data := range map[string][]byte{
26 "dataConfigJSON": dataConfigJSON,
27 "dataConfigYAML": dataConfigYAML,
28 } {
29 assert.NotNil(t, data, name)
30 }
31 }
32
33 func TestCollector_ConfigurationSerialize(t *testing.T) {
34 collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
35 }
36
37 func TestCollector_Cleanup(t *testing.T) {
38 assert.NotPanics(t, func() { New().Cleanup(context.Background()) })
39 }
40
41 func TestCollector_Charts(t *testing.T) {
42 collr := New()
43 collr.Source = "https://example.com"
44 require.NoError(t, collr.Init(context.Background()))
45 assert.NotNil(t, collr.Charts())
46 }
47
48 func TestCollector_Init(t *testing.T) {
49 const (
50 file = iota
51 net
52 smtp
53 )
54 tests := map[string]struct {
55 config Config
56 providerType int
57 err bool
58 }{
59 "ok from net https": {
60 config: Config{Source: "https://example.org"},
61 providerType: net,
62 },
63 "ok from net tcp": {
64 config: Config{Source: "tcp://example.org"},
65 providerType: net,
66 },
67 "ok from file": {
68 config: Config{Source: "file:///home/me/cert.pem"},
69 providerType: file,
70 },
71 "ok from smtp": {
72 config: Config{Source: "smtp://smtp.my_mail.org:587"},
73 providerType: smtp,
74 },
75 "empty source": {
76 config: Config{Source: ""},
77 err: true},
78 "unknown provider": {
79 config: Config{Source: "http://example.org"},
80 err: true,
81 },
82 "nonexistent TLSCA": {
83 config: Config{Source: "https://example.org", TLSConfig: tlscfg.TLSConfig{TLSCA: "testdata/tls"}},
84 err: true,
85 },
86 }
87
88 for name, test := range tests {
89 t.Run(name, func(t *testing.T) {
90 collr := New()
91 collr.Config = test.config
92
93 if test.err {
94 assert.Error(t, collr.Init(context.Background()))
95 } else {
96 require.NoError(t, collr.Init(context.Background()))
97
98 var typeOK bool
99 switch test.providerType {
100 case file:
101 _, typeOK = collr.prov.(*fromFile)
102 case net:
103 _, typeOK = collr.prov.(*fromNet)
104 case smtp:
105 _, typeOK = collr.prov.(*fromSMTP)
106 }
107
108 assert.True(t, typeOK)
109 }
110 })
111 }
112 }
113
114 func TestCollector_Check(t *testing.T) {
115 collr := New()
116 collr.prov = &mockProvider{certs: []*x509.Certificate{{}}}
117
118 assert.NoError(t, collr.Check(context.Background()))
119 }
120
121 func TestCollector_Check_ReturnsFalseOnProviderError(t *testing.T) {
122 collr := New()
123 collr.prov = &mockProvider{err: true}
124
125 assert.Error(t, collr.Check(context.Background()))
126 }
127
128 func TestCollector_Collect(t *testing.T) {
129 collr := New()
130 collr.Source = "https://example.com"
131 require.NoError(t, collr.Init(context.Background()))
132 collr.prov = &mockProvider{certs: []*x509.Certificate{{}}}
133
134 mx := collr.Collect(context.Background())
135
136 assert.NotZero(t, mx)
137 collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
138 }
139
140 func TestCollector_Collect_ReturnsNilOnProviderError(t *testing.T) {
141 collr := New()
142 collr.prov = &mockProvider{err: true}
143
144 assert.Nil(t, collr.Collect(context.Background()))
145 }
146
147 func TestCollector_Collect_ReturnsNilOnZeroCertificates(t *testing.T) {
148 collr := New()
149 collr.prov = &mockProvider{certs: []*x509.Certificate{}}
150 mx := collr.Collect(context.Background())
151
152 assert.Nil(t, mx)
153 }
154
155 type mockProvider struct {
156 certs []*x509.Certificate
157 err bool
158 }
159
160 func (m mockProvider) certificates() ([]*x509.Certificate, error) {
161 if m.err {
162 return nil, errors.New("mock certificates error")
163 }
164 return m.certs, nil
165 }