| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package freeradius |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "os" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/freeradius/api" |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest" |
| 13 | |
| 14 | "github.com/stretchr/testify/assert" |
| 15 | "github.com/stretchr/testify/require" |
| 16 | ) |
| 17 | |
| 18 | var ( |
| 19 | dataConfigJSON, _ = os.ReadFile("testdata/config.json") |
| 20 | dataConfigYAML, _ = os.ReadFile("testdata/config.yaml") |
| 21 | ) |
| 22 | |
| 23 | func Test_testDataIsValid(t *testing.T) { |
| 24 | for name, data := range map[string][]byte{ |
| 25 | "dataConfigJSON": dataConfigJSON, |
| 26 | "dataConfigYAML": dataConfigYAML, |
| 27 | } { |
| 28 | require.NotNil(t, data, name) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestCollector_ConfigurationSerialize(t *testing.T) { |
| 33 | collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) |
| 34 | } |
| 35 | |
| 36 | func TestCollector_Init(t *testing.T) { |
| 37 | collr := New() |
| 38 | |
| 39 | assert.NoError(t, collr.Init(context.Background())) |
| 40 | } |
| 41 | |
| 42 | func TestCollector_Init_ReturnsFalseIfAddressNotSet(t *testing.T) { |
| 43 | collr := New() |
| 44 | collr.Address = "" |
| 45 | |
| 46 | assert.Error(t, collr.Init(context.Background())) |
| 47 | } |
| 48 | |
| 49 | func TestCollector_Init_ReturnsFalseIfPortNotSet(t *testing.T) { |
| 50 | collr := New() |
| 51 | collr.Port = 0 |
| 52 | |
| 53 | assert.Error(t, collr.Init(context.Background())) |
| 54 | } |
| 55 | |
| 56 | func TestCollector_Init_ReturnsFalseIfSecretNotSet(t *testing.T) { |
| 57 | collr := New() |
| 58 | collr.Secret = "" |
| 59 | |
| 60 | assert.Error(t, collr.Init(context.Background())) |
| 61 | } |
| 62 | |
| 63 | func TestCollector_Check(t *testing.T) { |
| 64 | collr := New() |
| 65 | collr.client = newOKMockClient() |
| 66 | |
| 67 | assert.NoError(t, collr.Check(context.Background())) |
| 68 | } |
| 69 | |
| 70 | func TestCollector_Check_ReturnsFalseIfClientStatusReturnsError(t *testing.T) { |
| 71 | collr := New() |
| 72 | collr.client = newErrorMockClient() |
| 73 | |
| 74 | assert.Error(t, collr.Check(context.Background())) |
| 75 | } |
| 76 | |
| 77 | func TestCollector_Charts(t *testing.T) { |
| 78 | assert.NotNil(t, New().Charts()) |
| 79 | } |
| 80 | |
| 81 | func TestCollector_Collect(t *testing.T) { |
| 82 | collr := New() |
| 83 | collr.client = newOKMockClient() |
| 84 | |
| 85 | expected := map[string]int64{ |
| 86 | "access-requests": 1, |
| 87 | "access-accepts": 2, |
| 88 | "access-rejects": 3, |
| 89 | "access-challenges": 4, |
| 90 | "auth-responses": 5, |
| 91 | "auth-duplicate-requests": 6, |
| 92 | "auth-malformed-requests": 7, |
| 93 | "auth-invalid-requests": 8, |
| 94 | "auth-dropped-requests": 9, |
| 95 | "auth-unknown-types": 10, |
| 96 | "accounting-requests": 11, |
| 97 | "accounting-responses": 12, |
| 98 | "acct-duplicate-requests": 13, |
| 99 | "acct-malformed-requests": 14, |
| 100 | "acct-invalid-requests": 15, |
| 101 | "acct-dropped-requests": 16, |
| 102 | "acct-unknown-types": 17, |
| 103 | "proxy-access-requests": 18, |
| 104 | "proxy-access-accepts": 19, |
| 105 | "proxy-access-rejects": 20, |
| 106 | "proxy-access-challenges": 21, |
| 107 | "proxy-auth-responses": 22, |
| 108 | "proxy-auth-duplicate-requests": 23, |
| 109 | "proxy-auth-malformed-requests": 24, |
| 110 | "proxy-auth-invalid-requests": 25, |
| 111 | "proxy-auth-dropped-requests": 26, |
| 112 | "proxy-auth-unknown-types": 27, |
| 113 | "proxy-accounting-requests": 28, |
| 114 | "proxy-accounting-responses": 29, |
| 115 | "proxy-acct-duplicate-requests": 30, |
| 116 | "proxy-acct-malformed-requests": 31, |
| 117 | "proxy-acct-invalid-requests": 32, |
| 118 | "proxy-acct-dropped-requests": 33, |
| 119 | "proxy-acct-unknown-types": 34, |
| 120 | } |
| 121 | mx := collr.Collect(context.Background()) |
| 122 | |
| 123 | assert.Equal(t, expected, mx) |
| 124 | collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) |
| 125 | } |
| 126 | |
| 127 | func TestCollector_Collect_ReturnsNilIfClientStatusReturnsError(t *testing.T) { |
| 128 | collr := New() |
| 129 | collr.client = newErrorMockClient() |
| 130 | |
| 131 | assert.Nil(t, collr.Collect(context.Background())) |
| 132 | } |
| 133 | |
| 134 | func TestCollector_Cleanup(t *testing.T) { |
| 135 | New().Cleanup(context.Background()) |
| 136 | } |
| 137 | |
| 138 | func newOKMockClient() *mockClient { |
| 139 | return &mockClient{} |
| 140 | } |
| 141 | |
| 142 | func newErrorMockClient() *mockClient { |
| 143 | return &mockClient{errOnStatus: true} |
| 144 | } |
| 145 | |
| 146 | type mockClient struct { |
| 147 | errOnStatus bool |
| 148 | } |
| 149 | |
| 150 | func (m mockClient) Status() (*api.Status, error) { |
| 151 | if m.errOnStatus { |
| 152 | return nil, errors.New("mock Status error") |
| 153 | } |
| 154 | |
| 155 | status := &api.Status{ |
| 156 | AccessRequests: 1, |
| 157 | AccessAccepts: 2, |
| 158 | AccessRejects: 3, |
| 159 | AccessChallenges: 4, |
| 160 | AuthResponses: 5, |
| 161 | AuthDuplicateRequests: 6, |
| 162 | AuthMalformedRequests: 7, |
| 163 | AuthInvalidRequests: 8, |
| 164 | AuthDroppedRequests: 9, |
| 165 | AuthUnknownTypes: 10, |
| 166 | AccountingRequests: 11, |
| 167 | AccountingResponses: 12, |
| 168 | AcctDuplicateRequests: 13, |
| 169 | AcctMalformedRequests: 14, |
| 170 | AcctInvalidRequests: 15, |
| 171 | AcctDroppedRequests: 16, |
| 172 | AcctUnknownTypes: 17, |
| 173 | ProxyAccessRequests: 18, |
| 174 | ProxyAccessAccepts: 19, |
| 175 | ProxyAccessRejects: 20, |
| 176 | ProxyAccessChallenges: 21, |
| 177 | ProxyAuthResponses: 22, |
| 178 | ProxyAuthDuplicateRequests: 23, |
| 179 | ProxyAuthMalformedRequests: 24, |
| 180 | ProxyAuthInvalidRequests: 25, |
| 181 | ProxyAuthDroppedRequests: 26, |
| 182 | ProxyAuthUnknownTypes: 27, |
| 183 | ProxyAccountingRequests: 28, |
| 184 | ProxyAccountingResponses: 29, |
| 185 | ProxyAcctDuplicateRequests: 30, |
| 186 | ProxyAcctMalformedRequests: 31, |
| 187 | ProxyAcctInvalidRequests: 32, |
| 188 | ProxyAcctDroppedRequests: 33, |
| 189 | ProxyAcctUnknownTypes: 34, |
| 190 | } |
| 191 | return status, nil |
| 192 | } |