github.com/netdata/go.d.plugin@v0.58.1/modules/freeradius/freeradius_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package freeradius 4 5 import ( 6 "errors" 7 "testing" 8 9 "github.com/netdata/go.d.plugin/modules/freeradius/api" 10 11 "github.com/netdata/go.d.plugin/agent/module" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestNew(t *testing.T) { 16 assert.Implements(t, (*module.Module)(nil), New()) 17 } 18 19 func TestFreeRADIUS_Init(t *testing.T) { 20 freeRADIUS := New() 21 22 assert.True(t, freeRADIUS.Init()) 23 } 24 25 func TestFreeRADIUS_Init_ReturnsFalseIfAddressNotSet(t *testing.T) { 26 freeRADIUS := New() 27 freeRADIUS.Address = "" 28 29 assert.False(t, freeRADIUS.Init()) 30 } 31 32 func TestFreeRADIUS_Init_ReturnsFalseIfPortNotSet(t *testing.T) { 33 freeRADIUS := New() 34 freeRADIUS.Port = 0 35 36 assert.False(t, freeRADIUS.Init()) 37 } 38 39 func TestFreeRADIUS_Init_ReturnsFalseIfSecretNotSet(t *testing.T) { 40 freeRADIUS := New() 41 freeRADIUS.Secret = "" 42 43 assert.False(t, freeRADIUS.Init()) 44 } 45 46 func TestFreeRADIUS_Check(t *testing.T) { 47 freeRADIUS := New() 48 freeRADIUS.client = newOKMockClient() 49 50 assert.True(t, freeRADIUS.Check()) 51 } 52 53 func TestFreeRADIUS_Check_ReturnsFalseIfClientStatusReturnsError(t *testing.T) { 54 freeRADIUS := New() 55 freeRADIUS.client = newErrorMockClient() 56 57 assert.False(t, freeRADIUS.Check()) 58 } 59 60 func TestFreeRADIUS_Charts(t *testing.T) { 61 assert.NotNil(t, New().Charts()) 62 } 63 64 func TestFreeRADIUS_Collect(t *testing.T) { 65 freeRADIUS := New() 66 freeRADIUS.client = newOKMockClient() 67 68 expected := map[string]int64{ 69 "access-requests": 1, 70 "access-accepts": 2, 71 "access-rejects": 3, 72 "access-challenges": 4, 73 "auth-responses": 5, 74 "auth-duplicate-requests": 6, 75 "auth-malformed-requests": 7, 76 "auth-invalid-requests": 8, 77 "auth-dropped-requests": 9, 78 "auth-unknown-types": 10, 79 "accounting-requests": 11, 80 "accounting-responses": 12, 81 "acct-duplicate-requests": 13, 82 "acct-malformed-requests": 14, 83 "acct-invalid-requests": 15, 84 "acct-dropped-requests": 16, 85 "acct-unknown-types": 17, 86 "proxy-access-requests": 18, 87 "proxy-access-accepts": 19, 88 "proxy-access-rejects": 20, 89 "proxy-access-challenges": 21, 90 "proxy-auth-responses": 22, 91 "proxy-auth-duplicate-requests": 23, 92 "proxy-auth-malformed-requests": 24, 93 "proxy-auth-invalid-requests": 25, 94 "proxy-auth-dropped-requests": 26, 95 "proxy-auth-unknown-types": 27, 96 "proxy-accounting-requests": 28, 97 "proxy-accounting-responses": 29, 98 "proxy-acct-duplicate-requests": 30, 99 "proxy-acct-malformed-requests": 31, 100 "proxy-acct-invalid-requests": 32, 101 "proxy-acct-dropped-requests": 33, 102 "proxy-acct-unknown-types": 34, 103 } 104 collected := freeRADIUS.Collect() 105 106 assert.Equal(t, expected, collected) 107 ensureCollectedHasAllChartsDimsVarsIDs(t, freeRADIUS, collected) 108 } 109 110 func TestFreeRADIUS_Collect_ReturnsNilIfClientStatusReturnsError(t *testing.T) { 111 freeRADIUS := New() 112 freeRADIUS.client = newErrorMockClient() 113 114 assert.Nil(t, freeRADIUS.Collect()) 115 } 116 117 func TestFreeRADIUS_Cleanup(t *testing.T) { 118 New().Cleanup() 119 } 120 121 func ensureCollectedHasAllChartsDimsVarsIDs(t *testing.T, f *FreeRADIUS, collected map[string]int64) { 122 for _, chart := range *f.Charts() { 123 for _, dim := range chart.Dims { 124 _, ok := collected[dim.ID] 125 assert.Truef(t, ok, "collected metrics has no data for dim '%s' chart '%s'", dim.ID, chart.ID) 126 } 127 for _, v := range chart.Vars { 128 _, ok := collected[v.ID] 129 assert.Truef(t, ok, "collected metrics has no data for var '%s' chart '%s'", v.ID, chart.ID) 130 } 131 } 132 } 133 134 func newOKMockClient() *mockClient { 135 return &mockClient{} 136 } 137 138 func newErrorMockClient() *mockClient { 139 return &mockClient{errOnStatus: true} 140 } 141 142 type mockClient struct { 143 errOnStatus bool 144 } 145 146 func (m mockClient) Status() (*api.Status, error) { 147 if m.errOnStatus { 148 return nil, errors.New("mock Status error") 149 } 150 151 status := &api.Status{ 152 AccessRequests: 1, 153 AccessAccepts: 2, 154 AccessRejects: 3, 155 AccessChallenges: 4, 156 AuthResponses: 5, 157 AuthDuplicateRequests: 6, 158 AuthMalformedRequests: 7, 159 AuthInvalidRequests: 8, 160 AuthDroppedRequests: 9, 161 AuthUnknownTypes: 10, 162 AccountingRequests: 11, 163 AccountingResponses: 12, 164 AcctDuplicateRequests: 13, 165 AcctMalformedRequests: 14, 166 AcctInvalidRequests: 15, 167 AcctDroppedRequests: 16, 168 AcctUnknownTypes: 17, 169 ProxyAccessRequests: 18, 170 ProxyAccessAccepts: 19, 171 ProxyAccessRejects: 20, 172 ProxyAccessChallenges: 21, 173 ProxyAuthResponses: 22, 174 ProxyAuthDuplicateRequests: 23, 175 ProxyAuthMalformedRequests: 24, 176 ProxyAuthInvalidRequests: 25, 177 ProxyAuthDroppedRequests: 26, 178 ProxyAuthUnknownTypes: 27, 179 ProxyAccountingRequests: 28, 180 ProxyAccountingResponses: 29, 181 ProxyAcctDuplicateRequests: 30, 182 ProxyAcctMalformedRequests: 31, 183 ProxyAcctInvalidRequests: 32, 184 ProxyAcctDroppedRequests: 33, 185 ProxyAcctUnknownTypes: 34, 186 } 187 return status, nil 188 }