| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package openvpn |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/netdata/netdata/go/plugins/pkg/matcher" |
| 11 | "github.com/netdata/netdata/go/plugins/plugin/go.d/collector/openvpn/client" |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest" |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/socket" |
| 14 | |
| 15 | "github.com/stretchr/testify/assert" |
| 16 | "github.com/stretchr/testify/require" |
| 17 | ) |
| 18 | |
| 19 | var ( |
| 20 | dataConfigJSON, _ = os.ReadFile("testdata/config.json") |
| 21 | dataConfigYAML, _ = os.ReadFile("testdata/config.yaml") |
| 22 | ) |
| 23 | |
| 24 | func Test_testDataIsValid(t *testing.T) { |
| 25 | for name, data := range map[string][]byte{ |
| 26 | "dataConfigJSON": dataConfigJSON, |
| 27 | "dataConfigYAML": dataConfigYAML, |
| 28 | } { |
| 29 | require.NotNil(t, data, name) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestCollector_ConfigurationSerialize(t *testing.T) { |
| 34 | collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) |
| 35 | } |
| 36 | |
| 37 | func TestCollector_Init(t *testing.T) { |
| 38 | assert.NoError(t, New().Init(context.Background())) |
| 39 | } |
| 40 | |
| 41 | func TestCollector_Check(t *testing.T) { |
| 42 | collr := New() |
| 43 | |
| 44 | require.NoError(t, collr.Init(context.Background())) |
| 45 | collr.client = prepareMockOpenVPNClient() |
| 46 | require.NoError(t, collr.Check(context.Background())) |
| 47 | } |
| 48 | |
| 49 | func TestCollector_Charts(t *testing.T) { |
| 50 | assert.NotNil(t, New().Charts()) |
| 51 | } |
| 52 | |
| 53 | func TestCollector_Cleanup(t *testing.T) { |
| 54 | collr := New() |
| 55 | |
| 56 | assert.NotPanics(t, func() { collr.Cleanup(context.Background()) }) |
| 57 | require.NoError(t, collr.Init(context.Background())) |
| 58 | collr.client = prepareMockOpenVPNClient() |
| 59 | require.NoError(t, collr.Check(context.Background())) |
| 60 | collr.Cleanup(context.Background()) |
| 61 | } |
| 62 | |
| 63 | func TestCollector_Collect(t *testing.T) { |
| 64 | collr := New() |
| 65 | |
| 66 | require.NoError(t, collr.Init(context.Background())) |
| 67 | collr.perUserMatcher = matcher.TRUE() |
| 68 | collr.client = prepareMockOpenVPNClient() |
| 69 | require.NoError(t, collr.Check(context.Background())) |
| 70 | |
| 71 | expected := map[string]int64{ |
| 72 | "bytes_in": 1, |
| 73 | "bytes_out": 1, |
| 74 | "clients": 1, |
| 75 | "name_bytes_received": 1, |
| 76 | "name_bytes_sent": 2, |
| 77 | } |
| 78 | |
| 79 | mx := collr.Collect(context.Background()) |
| 80 | require.NotNil(t, mx) |
| 81 | delete(mx, "name_connection_time") |
| 82 | assert.Equal(t, expected, mx) |
| 83 | } |
| 84 | |
| 85 | func TestCollector_Collect_UNDEFUsername(t *testing.T) { |
| 86 | collr := New() |
| 87 | |
| 88 | require.NoError(t, collr.Init(context.Background())) |
| 89 | collr.perUserMatcher = matcher.TRUE() |
| 90 | cl := prepareMockOpenVPNClient() |
| 91 | cl.users = testUsersUNDEF |
| 92 | collr.client = cl |
| 93 | require.NoError(t, collr.Check(context.Background())) |
| 94 | |
| 95 | expected := map[string]int64{ |
| 96 | "bytes_in": 1, |
| 97 | "bytes_out": 1, |
| 98 | "clients": 1, |
| 99 | "common_name_bytes_received": 1, |
| 100 | "common_name_bytes_sent": 2, |
| 101 | } |
| 102 | |
| 103 | mx := collr.Collect(context.Background()) |
| 104 | require.NotNil(t, mx) |
| 105 | |
| 106 | delete(mx, "common_name_connection_time") |
| 107 | assert.Equal(t, expected, mx) |
| 108 | } |
| 109 | |
| 110 | func prepareMockOpenVPNClient() *mockOpenVPNClient { |
| 111 | return &mockOpenVPNClient{ |
| 112 | version: testVersion, |
| 113 | loadStats: testLoadStats, |
| 114 | users: testUsers, |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | type mockOpenVPNClient struct { |
| 119 | version client.Version |
| 120 | loadStats client.LoadStats |
| 121 | users client.Users |
| 122 | } |
| 123 | |
| 124 | func (m *mockOpenVPNClient) Connect() error { return nil } |
| 125 | func (m *mockOpenVPNClient) Disconnect() error { return nil } |
| 126 | func (m *mockOpenVPNClient) Version() (*client.Version, error) { return &m.version, nil } |
| 127 | func (m *mockOpenVPNClient) LoadStats() (*client.LoadStats, error) { return &m.loadStats, nil } |
| 128 | func (m *mockOpenVPNClient) Users() (client.Users, error) { return m.users, nil } |
| 129 | func (m *mockOpenVPNClient) Command(_ string, _ socket.Processor) error { |
| 130 | // mocks are done on the individual commands. e.g. in Version() below |
| 131 | panic("should be called in the mock") |
| 132 | } |
| 133 | |
| 134 | var ( |
| 135 | testVersion = client.Version{Major: 1, Minor: 1, Patch: 1, Management: 1} |
| 136 | testLoadStats = client.LoadStats{NumOfClients: 1, BytesIn: 1, BytesOut: 1} |
| 137 | testUsers = client.Users{{ |
| 138 | CommonName: "common_name", |
| 139 | RealAddress: "1.2.3.4:4321", |
| 140 | VirtualAddress: "1.2.3.4", |
| 141 | BytesReceived: 1, |
| 142 | BytesSent: 2, |
| 143 | ConnectedSince: 3, |
| 144 | Username: "name", |
| 145 | }} |
| 146 | testUsersUNDEF = client.Users{{ |
| 147 | CommonName: "common_name", |
| 148 | RealAddress: "1.2.3.4:4321", |
| 149 | VirtualAddress: "1.2.3.4", |
| 150 | BytesReceived: 1, |
| 151 | BytesSent: 2, |
| 152 | ConnectedSince: 3, |
| 153 | Username: "UNDEF", |
| 154 | }} |
| 155 | ) |