github.com/netdata/go.d.plugin@v0.58.1/modules/openvpn/openvpn_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package openvpn 4 5 import ( 6 "testing" 7 8 "github.com/netdata/go.d.plugin/agent/module" 9 "github.com/netdata/go.d.plugin/modules/openvpn/client" 10 "github.com/netdata/go.d.plugin/pkg/matcher" 11 "github.com/netdata/go.d.plugin/pkg/socket" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 var ( 17 testVersion = client.Version{Major: 1, Minor: 1, Patch: 1, Management: 1} 18 testLoadStats = client.LoadStats{NumOfClients: 1, BytesIn: 1, BytesOut: 1} 19 testUsers = client.Users{{ 20 CommonName: "common_name", 21 RealAddress: "1.2.3.4:4321", 22 VirtualAddress: "1.2.3.4", 23 BytesReceived: 1, 24 BytesSent: 2, 25 ConnectedSince: 3, 26 Username: "name", 27 }} 28 testUsersUNDEF = client.Users{{ 29 CommonName: "common_name", 30 RealAddress: "1.2.3.4:4321", 31 VirtualAddress: "1.2.3.4", 32 BytesReceived: 1, 33 BytesSent: 2, 34 ConnectedSince: 3, 35 Username: "UNDEF", 36 }} 37 ) 38 39 func TestNew(t *testing.T) { 40 job := New() 41 42 assert.Implements(t, (*module.Module)(nil), job) 43 assert.Equal(t, defaultAddress, job.Address) 44 assert.Equal(t, defaultConnectTimeout, job.ConnectTimeout.Duration) 45 assert.Equal(t, defaultReadTimeout, job.ReadTimeout.Duration) 46 assert.Equal(t, defaultWriteTimeout, job.WriteTimeout.Duration) 47 assert.NotNil(t, job.charts) 48 assert.NotNil(t, job.collectedUsers) 49 } 50 51 func TestOpenVPN_Init(t *testing.T) { 52 assert.True(t, New().Init()) 53 } 54 55 func TestOpenVPN_Check(t *testing.T) { 56 job := New() 57 58 require.True(t, job.Init()) 59 job.client = prepareMockOpenVPNClient() 60 require.True(t, job.Check()) 61 } 62 63 func TestOpenVPN_Charts(t *testing.T) { 64 assert.NotNil(t, New().Charts()) 65 } 66 67 func TestOpenVPN_Cleanup(t *testing.T) { 68 job := New() 69 70 assert.NotPanics(t, job.Cleanup) 71 require.True(t, job.Init()) 72 job.client = prepareMockOpenVPNClient() 73 require.True(t, job.Check()) 74 job.Cleanup() 75 } 76 77 func TestOpenVPN_Collect(t *testing.T) { 78 job := New() 79 80 require.True(t, job.Init()) 81 job.perUserMatcher = matcher.TRUE() 82 job.client = prepareMockOpenVPNClient() 83 require.True(t, job.Check()) 84 85 expected := map[string]int64{ 86 "bytes_in": 1, 87 "bytes_out": 1, 88 "clients": 1, 89 "name_bytes_received": 1, 90 "name_bytes_sent": 2, 91 } 92 93 mx := job.Collect() 94 require.NotNil(t, mx) 95 delete(mx, "name_connection_time") 96 assert.Equal(t, expected, mx) 97 } 98 99 func TestOpenVPN_Collect_UNDEFUsername(t *testing.T) { 100 job := New() 101 102 require.True(t, job.Init()) 103 job.perUserMatcher = matcher.TRUE() 104 cl := prepareMockOpenVPNClient() 105 cl.users = testUsersUNDEF 106 job.client = cl 107 require.True(t, job.Check()) 108 109 expected := map[string]int64{ 110 "bytes_in": 1, 111 "bytes_out": 1, 112 "clients": 1, 113 "common_name_bytes_received": 1, 114 "common_name_bytes_sent": 2, 115 } 116 117 mx := job.Collect() 118 require.NotNil(t, mx) 119 delete(mx, "common_name_connection_time") 120 assert.Equal(t, expected, mx) 121 } 122 123 func prepareMockOpenVPNClient() *mockOpenVPNClient { 124 return &mockOpenVPNClient{ 125 version: testVersion, 126 loadStats: testLoadStats, 127 users: testUsers, 128 } 129 } 130 131 type mockOpenVPNClient struct { 132 version client.Version 133 loadStats client.LoadStats 134 users client.Users 135 } 136 137 func (m *mockOpenVPNClient) Connect() error { return nil } 138 func (m *mockOpenVPNClient) Disconnect() error { return nil } 139 func (m mockOpenVPNClient) Version() (*client.Version, error) { return &m.version, nil } 140 func (m mockOpenVPNClient) LoadStats() (*client.LoadStats, error) { return &m.loadStats, nil } 141 func (m mockOpenVPNClient) Users() (client.Users, error) { return m.users, nil } 142 func (m *mockOpenVPNClient) Command(_ string, _ socket.Processor) error { 143 // mocks are done on the individual commands. e.g. in Version() below 144 panic("should be called in the mock") 145 }