diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 12:08:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 12:08:18 +0000 |
commit | 5da14042f70711ea5cf66e034699730335462f66 (patch) | |
tree | 0f6354ccac934ed87a2d555f45be4c831cf92f4a /src/go/collectors/go.d.plugin/modules/x509check/x509check_test.go | |
parent | Releasing debian version 1.44.3-2. (diff) | |
download | netdata-5da14042f70711ea5cf66e034699730335462f66.tar.xz netdata-5da14042f70711ea5cf66e034699730335462f66.zip |
Merging upstream version 1.45.3+dfsg.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/go/collectors/go.d.plugin/modules/x509check/x509check_test.go | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/x509check/x509check_test.go b/src/go/collectors/go.d.plugin/modules/x509check/x509check_test.go new file mode 100644 index 000000000..6d93bd3e4 --- /dev/null +++ b/src/go/collectors/go.d.plugin/modules/x509check/x509check_test.go @@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package x509check + +import ( + "crypto/x509" + "errors" + "os" + "testing" + + "github.com/netdata/netdata/go/go.d.plugin/agent/module" + "github.com/netdata/netdata/go/go.d.plugin/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 +} |