github.com/netdata/go.d.plugin@v0.58.1/modules/x509check/x509check_test.go (about)

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