master
go 137 lines 2.99 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package whoisquery
4
5 import (
6 "context"
7 "errors"
8 "os"
9 "testing"
10
11 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 )
16
17 var (
18 dataConfigJSON, _ = os.ReadFile("testdata/config.json")
19 dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
20 )
21
22 func Test_testDataIsValid(t *testing.T) {
23 for name, data := range map[string][]byte{
24 "dataConfigJSON": dataConfigJSON,
25 "dataConfigYAML": dataConfigYAML,
26 } {
27 require.NotNil(t, data, name)
28 }
29 }
30
31 func TestCollector_ConfigurationSerialize(t *testing.T) {
32 collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
33 }
34
35 func TestCollector_Cleanup(t *testing.T) {
36 New().Cleanup(context.Background())
37 }
38
39 func TestCollector_Charts(t *testing.T) {
40 collr := New()
41 collr.Source = "example.com"
42 require.NoError(t, collr.Init(context.Background()))
43
44 assert.NotNil(t, collr.Charts())
45 }
46
47 func TestCollector_Init(t *testing.T) {
48 const net = iota
49 tests := map[string]struct {
50 config Config
51 providerType int
52 err bool
53 }{
54 "ok from net": {
55 config: Config{Source: "example.org"},
56 providerType: net,
57 },
58 "empty source": {
59 config: Config{Source: ""},
60 err: true,
61 },
62 }
63
64 for name, test := range tests {
65 t.Run(name, func(t *testing.T) {
66 collr := New()
67 collr.Config = test.config
68
69 if test.err {
70 assert.Error(t, collr.Init(context.Background()))
71 } else {
72 require.NoError(t, collr.Init(context.Background()))
73
74 var typeOK bool
75 if test.providerType == net {
76 _, typeOK = collr.prov.(*whoisClient)
77 }
78
79 assert.True(t, typeOK)
80 }
81 })
82 }
83 }
84
85 func TestCollector_Check(t *testing.T) {
86 collr := New()
87 collr.prov = &mockProvider{remTime: 12345.678}
88
89 assert.NoError(t, collr.Check(context.Background()))
90 }
91
92 func TestCollector_Check_ReturnsFalseOnProviderError(t *testing.T) {
93 collr := New()
94 collr.prov = &mockProvider{err: true}
95
96 assert.Error(t, collr.Check(context.Background()))
97 }
98
99 func TestCollector_Collect(t *testing.T) {
100 collr := New()
101 collr.Source = "example.com"
102 require.NoError(t, collr.Init(context.Background()))
103 collr.prov = &mockProvider{remTime: 12345}
104
105 mx := collr.Collect(context.Background())
106
107 expected := map[string]int64{
108 "expiry": 12345,
109 "days_until_expiration_warning": 30,
110 "days_until_expiration_critical": 15,
111 }
112
113 assert.NotZero(t, mx)
114 assert.Equal(t, expected, mx)
115 collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx)
116 }
117
118 func TestCollector_Collect_ReturnsNilOnProviderError(t *testing.T) {
119 collr := New()
120 collr.Source = "example.com"
121 require.NoError(t, collr.Init(context.Background()))
122 collr.prov = &mockProvider{err: true}
123
124 assert.Nil(t, collr.Collect(context.Background()))
125 }
126
127 type mockProvider struct {
128 remTime float64
129 err bool
130 }
131
132 func (m mockProvider) remainingTime() (float64, error) {
133 if m.err {
134 return 0, errors.New("mock remaining time error")
135 }
136 return m.remTime, nil
137 }