github.com/netdata/go.d.plugin@v0.58.1/modules/whoisquery/whoisquery_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package whoisquery 4 5 import ( 6 "errors" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestWhoisQuery_Cleanup(t *testing.T) { 14 New().Cleanup() 15 } 16 17 func TestWhoisQuery_Charts(t *testing.T) { 18 whoisquery := New() 19 whoisquery.Source = "example.com" 20 require.True(t, whoisquery.Init()) 21 22 assert.NotNil(t, whoisquery.Charts()) 23 } 24 25 func TestWhoisQuery_Init(t *testing.T) { 26 const net = iota 27 tests := map[string]struct { 28 config Config 29 providerType int 30 err bool 31 }{ 32 "ok from net": { 33 config: Config{Source: "example.org"}, 34 providerType: net, 35 }, 36 "empty source": { 37 config: Config{Source: ""}, 38 err: true, 39 }, 40 } 41 42 for name, test := range tests { 43 t.Run(name, func(t *testing.T) { 44 whoisquery := New() 45 whoisquery.Config = test.config 46 47 if test.err { 48 assert.False(t, whoisquery.Init()) 49 } else { 50 require.True(t, whoisquery.Init()) 51 52 var typeOK bool 53 if test.providerType == net { 54 _, typeOK = whoisquery.prov.(*fromNet) 55 } 56 57 assert.True(t, typeOK) 58 } 59 }) 60 } 61 } 62 63 func TestWhoisQuery_Check(t *testing.T) { 64 whoisquery := New() 65 whoisquery.prov = &mockProvider{remTime: 12345.678} 66 67 assert.True(t, whoisquery.Check()) 68 } 69 70 func TestWhoisQuery_Check_ReturnsFalseOnProviderError(t *testing.T) { 71 whoisquery := New() 72 whoisquery.prov = &mockProvider{err: true} 73 74 assert.False(t, whoisquery.Check()) 75 } 76 77 func TestWhoisQuery_Collect(t *testing.T) { 78 whoisquery := New() 79 whoisquery.Source = "example.com" 80 require.True(t, whoisquery.Init()) 81 whoisquery.prov = &mockProvider{remTime: 12345} 82 83 collected := whoisquery.Collect() 84 85 expected := map[string]int64{ 86 "expiry": 12345, 87 "days_until_expiration_warning": 90, 88 "days_until_expiration_critical": 30, 89 } 90 91 assert.NotZero(t, collected) 92 assert.Equal(t, expected, collected) 93 ensureCollectedHasAllChartsDimsVarsIDs(t, whoisquery, collected) 94 } 95 96 func TestWhoisQuery_Collect_ReturnsNilOnProviderError(t *testing.T) { 97 whoisquery := New() 98 whoisquery.Source = "example.com" 99 require.True(t, whoisquery.Init()) 100 whoisquery.prov = &mockProvider{err: true} 101 102 assert.Nil(t, whoisquery.Collect()) 103 } 104 105 func ensureCollectedHasAllChartsDimsVarsIDs(t *testing.T, whoisquery *WhoisQuery, collected map[string]int64) { 106 for _, chart := range *whoisquery.Charts() { 107 for _, dim := range chart.Dims { 108 _, ok := collected[dim.ID] 109 assert.Truef(t, ok, "collected metrics has no data for dim '%s' chart '%s'", dim.ID, chart.ID) 110 } 111 for _, v := range chart.Vars { 112 _, ok := collected[v.ID] 113 assert.Truef(t, ok, "collected metrics has no data for var '%s' chart '%s'", v.ID, chart.ID) 114 } 115 } 116 } 117 118 type mockProvider struct { 119 remTime float64 120 err bool 121 } 122 123 func (m mockProvider) remainingTime() (float64, error) { 124 if m.err { 125 return 0, errors.New("mock remaining time error") 126 } 127 return m.remTime, nil 128 }