github.com/netdata/go.d.plugin@v0.58.1/modules/dnsmasq_dhcp/dhcp_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package dnsmasq_dhcp 4 5 import ( 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 const ( 13 testLeasesPath = "testdata/dnsmasq.leases" 14 testConfPath = "testdata/dnsmasq.conf" 15 testConfDir = "testdata/dnsmasq.d" 16 ) 17 18 func TestNew(t *testing.T) { 19 job := New() 20 21 assert.IsType(t, (*DnsmasqDHCP)(nil), job) 22 } 23 24 func TestDnsmasqDHCP_Init(t *testing.T) { 25 job := New() 26 job.LeasesPath = testLeasesPath 27 job.ConfPath = testConfPath 28 job.ConfDir = testConfDir 29 30 assert.True(t, job.Init()) 31 } 32 33 func TestDnsmasqDHCP_InitEmptyLeasesPath(t *testing.T) { 34 job := New() 35 job.LeasesPath = "" 36 37 assert.False(t, job.Init()) 38 } 39 40 func TestDnsmasqDHCP_InitInvalidLeasesPath(t *testing.T) { 41 job := New() 42 job.LeasesPath = testLeasesPath 43 job.LeasesPath += "!" 44 45 assert.False(t, job.Init()) 46 } 47 48 func TestDnsmasqDHCP_InitZeroDHCPRanges(t *testing.T) { 49 job := New() 50 job.LeasesPath = testLeasesPath 51 job.ConfPath = "testdata/dnsmasq3.conf" 52 job.ConfDir = "" 53 54 assert.True(t, job.Init()) 55 } 56 57 func TestDnsmasqDHCP_Check(t *testing.T) { 58 job := New() 59 job.LeasesPath = testLeasesPath 60 job.ConfPath = testConfPath 61 job.ConfDir = testConfDir 62 63 require.True(t, job.Init()) 64 assert.True(t, job.Check()) 65 } 66 67 func TestDnsmasqDHCP_Charts(t *testing.T) { 68 job := New() 69 job.LeasesPath = testLeasesPath 70 job.ConfPath = testConfPath 71 job.ConfDir = testConfDir 72 73 require.True(t, job.Init()) 74 75 assert.NotNil(t, job.Charts()) 76 } 77 78 func TestDnsmasqDHCP_Cleanup(t *testing.T) { 79 assert.NotPanics(t, New().Cleanup) 80 } 81 82 func TestDnsmasqDHCP_Collect(t *testing.T) { 83 job := New() 84 job.LeasesPath = testLeasesPath 85 job.ConfPath = testConfPath 86 job.ConfDir = testConfDir 87 88 require.True(t, job.Init()) 89 require.True(t, job.Check()) 90 91 expected := map[string]int64{ 92 "dhcp_range_1230::1-1230::64_allocated_leases": 7, 93 "dhcp_range_1230::1-1230::64_utilization": 7, 94 "dhcp_range_1231::1-1231::64_allocated_leases": 1, 95 "dhcp_range_1231::1-1231::64_utilization": 1, 96 "dhcp_range_1232::1-1232::64_allocated_leases": 1, 97 "dhcp_range_1232::1-1232::64_utilization": 1, 98 "dhcp_range_1233::1-1233::64_allocated_leases": 1, 99 "dhcp_range_1233::1-1233::64_utilization": 1, 100 "dhcp_range_1234::1-1234::64_allocated_leases": 1, 101 "dhcp_range_1234::1-1234::64_utilization": 1, 102 "dhcp_range_192.168.0.1-192.168.0.100_allocated_leases": 6, 103 "dhcp_range_192.168.0.1-192.168.0.100_utilization": 6, 104 "dhcp_range_192.168.1.1-192.168.1.100_allocated_leases": 5, 105 "dhcp_range_192.168.1.1-192.168.1.100_utilization": 5, 106 "dhcp_range_192.168.2.1-192.168.2.100_allocated_leases": 4, 107 "dhcp_range_192.168.2.1-192.168.2.100_utilization": 4, 108 "dhcp_range_192.168.200.1-192.168.200.100_allocated_leases": 1, 109 "dhcp_range_192.168.200.1-192.168.200.100_utilization": 1, 110 "dhcp_range_192.168.3.1-192.168.3.100_allocated_leases": 1, 111 "dhcp_range_192.168.3.1-192.168.3.100_utilization": 1, 112 "dhcp_range_192.168.4.1-192.168.4.100_allocated_leases": 1, 113 "dhcp_range_192.168.4.1-192.168.4.100_utilization": 1, 114 "ipv4_dhcp_hosts": 6, 115 "ipv4_dhcp_ranges": 6, 116 "ipv6_dhcp_hosts": 5, 117 "ipv6_dhcp_ranges": 5, 118 } 119 120 assert.Equal(t, expected, job.Collect()) 121 } 122 123 func TestDnsmasqDHCP_CollectFailedToOpenLeasesPath(t *testing.T) { 124 job := New() 125 job.LeasesPath = testLeasesPath 126 job.ConfPath = testConfPath 127 job.ConfDir = testConfDir 128 129 require.True(t, job.Init()) 130 require.True(t, job.Check()) 131 132 job.LeasesPath = "" 133 assert.Nil(t, job.Collect()) 134 }