github.com/netdata/go.d.plugin@v0.58.1/modules/portcheck/portcheck_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package portcheck 4 5 import ( 6 "errors" 7 "net" 8 "strings" 9 "testing" 10 "time" 11 12 "github.com/netdata/go.d.plugin/agent/module" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestNew(t *testing.T) { 18 job := New() 19 20 assert.Implements(t, (*module.Module)(nil), job) 21 } 22 23 func TestPortCheck_Init(t *testing.T) { 24 job := New() 25 26 job.Host = "127.0.0.1" 27 job.Ports = []int{39001, 39002} 28 assert.True(t, job.Init()) 29 assert.Len(t, job.ports, 2) 30 } 31 func TestPortCheck_InitNG(t *testing.T) { 32 job := New() 33 34 assert.False(t, job.Init()) 35 job.Host = "127.0.0.1" 36 assert.False(t, job.Init()) 37 job.Ports = []int{39001, 39002} 38 assert.True(t, job.Init()) 39 } 40 41 func TestPortCheck_Check(t *testing.T) { 42 assert.True(t, New().Check()) 43 } 44 45 func TestPortCheck_Cleanup(t *testing.T) { 46 New().Cleanup() 47 } 48 49 func TestPortCheck_Charts(t *testing.T) { 50 job := New() 51 job.Ports = []int{1, 2} 52 job.Host = "localhost" 53 require.True(t, job.Init()) 54 assert.Len(t, *job.Charts(), len(chartsTmpl)*len(job.Ports)) 55 } 56 57 func TestPortCheck_Collect(t *testing.T) { 58 job := New() 59 60 job.Host = "127.0.0.1" 61 job.Ports = []int{39001, 39002} 62 job.UpdateEvery = 5 63 job.dial = testDial(nil) 64 require.True(t, job.Init()) 65 require.True(t, job.Check()) 66 67 copyLatency := func(dst, src map[string]int64) { 68 for k := range dst { 69 if strings.HasSuffix(k, "latency") { 70 dst[k] = src[k] 71 } 72 } 73 } 74 75 expected := map[string]int64{ 76 "port_39001_current_state_duration": int64(job.UpdateEvery), 77 "port_39001_failed": 0, 78 "port_39001_latency": 0, 79 "port_39001_success": 1, 80 "port_39001_timeout": 0, 81 "port_39002_current_state_duration": int64(job.UpdateEvery), 82 "port_39002_failed": 0, 83 "port_39002_latency": 0, 84 "port_39002_success": 1, 85 "port_39002_timeout": 0, 86 } 87 collected := job.Collect() 88 copyLatency(expected, collected) 89 90 assert.Equal(t, expected, collected) 91 92 expected = map[string]int64{ 93 "port_39001_current_state_duration": int64(job.UpdateEvery) * 2, 94 "port_39001_failed": 0, 95 "port_39001_latency": 0, 96 "port_39001_success": 1, 97 "port_39001_timeout": 0, 98 "port_39002_current_state_duration": int64(job.UpdateEvery) * 2, 99 "port_39002_failed": 0, 100 "port_39002_latency": 0, 101 "port_39002_success": 1, 102 "port_39002_timeout": 0, 103 } 104 collected = job.Collect() 105 copyLatency(expected, collected) 106 107 assert.Equal(t, expected, collected) 108 109 job.dial = testDial(errors.New("checkStateFailed")) 110 111 expected = map[string]int64{ 112 "port_39001_current_state_duration": int64(job.UpdateEvery), 113 "port_39001_failed": 1, 114 "port_39001_latency": 0, 115 "port_39001_success": 0, 116 "port_39001_timeout": 0, 117 "port_39002_current_state_duration": int64(job.UpdateEvery), 118 "port_39002_failed": 1, 119 "port_39002_latency": 0, 120 "port_39002_success": 0, 121 "port_39002_timeout": 0, 122 } 123 collected = job.Collect() 124 copyLatency(expected, collected) 125 126 assert.Equal(t, expected, collected) 127 128 job.dial = testDial(timeoutError{}) 129 130 expected = map[string]int64{ 131 "port_39001_current_state_duration": int64(job.UpdateEvery), 132 "port_39001_failed": 0, 133 "port_39001_latency": 0, 134 "port_39001_success": 0, 135 "port_39001_timeout": 1, 136 "port_39002_current_state_duration": int64(job.UpdateEvery), 137 "port_39002_failed": 0, 138 "port_39002_latency": 0, 139 "port_39002_success": 0, 140 "port_39002_timeout": 1, 141 } 142 collected = job.Collect() 143 copyLatency(expected, collected) 144 145 assert.Equal(t, expected, collected) 146 } 147 148 func testDial(err error) dialFunc { 149 return func(_, _ string, _ time.Duration) (net.Conn, error) { return &net.TCPConn{}, err } 150 } 151 152 type timeoutError struct{} 153 154 func (timeoutError) Error() string { return "timeout" } 155 func (timeoutError) Timeout() bool { return true }