github.com/netdata/go.d.plugin@v0.58.1/modules/lighttpd/lighttpd_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package lighttpd 4 5 import ( 6 "net/http" 7 "net/http/httptest" 8 "os" 9 "testing" 10 11 "github.com/netdata/go.d.plugin/agent/module" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 var ( 17 testStatusData, _ = os.ReadFile("testdata/status.txt") 18 testApacheStatusData, _ = os.ReadFile("testdata/apache-status.txt") 19 ) 20 21 func TestLighttpd_Cleanup(t *testing.T) { New().Cleanup() } 22 23 func TestNew(t *testing.T) { 24 job := New() 25 26 assert.Implements(t, (*module.Module)(nil), job) 27 assert.Equal(t, defaultURL, job.URL) 28 assert.Equal(t, defaultHTTPTimeout, job.Timeout.Duration) 29 } 30 31 func TestLighttpd_Init(t *testing.T) { 32 job := New() 33 34 require.True(t, job.Init()) 35 assert.NotNil(t, job.apiClient) 36 } 37 38 func TestLighttpd_InitNG(t *testing.T) { 39 job := New() 40 41 job.URL = "" 42 assert.False(t, job.Init()) 43 } 44 45 func TestLighttpd_Check(t *testing.T) { 46 ts := httptest.NewServer( 47 http.HandlerFunc( 48 func(w http.ResponseWriter, r *http.Request) { 49 _, _ = w.Write(testStatusData) 50 })) 51 defer ts.Close() 52 53 job := New() 54 job.URL = ts.URL + "/server-status?auto" 55 require.True(t, job.Init()) 56 assert.True(t, job.Check()) 57 } 58 59 func TestLighttpd_CheckNG(t *testing.T) { 60 job := New() 61 62 job.URL = "http://127.0.0.1:38001/server-status?auto" 63 require.True(t, job.Init()) 64 assert.False(t, job.Check()) 65 } 66 67 func TestLighttpd_Charts(t *testing.T) { assert.NotNil(t, New().Charts()) } 68 69 func TestLighttpd_Collect(t *testing.T) { 70 ts := httptest.NewServer( 71 http.HandlerFunc( 72 func(w http.ResponseWriter, r *http.Request) { 73 _, _ = w.Write(testStatusData) 74 })) 75 defer ts.Close() 76 77 job := New() 78 job.URL = ts.URL + "/server-status?auto" 79 require.True(t, job.Init()) 80 require.True(t, job.Check()) 81 82 expected := map[string]int64{ 83 "scoreboard_waiting": 125, 84 "scoreboard_request_end": 0, 85 "busy_servers": 3, 86 "scoreboard_keepalive": 1, 87 "scoreboard_read": 1, 88 "scoreboard_request_start": 0, 89 "scoreboard_response_start": 0, 90 "scoreboard_close": 0, 91 "scoreboard_open": 0, 92 "scoreboard_hard_error": 0, 93 "scoreboard_handle_request": 1, 94 "idle_servers": 125, 95 "total_kBytes": 4, 96 "uptime": 11, 97 "scoreboard_read_post": 0, 98 "scoreboard_write": 0, 99 "scoreboard_response_end": 0, 100 "total_accesses": 12, 101 } 102 103 assert.Equal(t, expected, job.Collect()) 104 } 105 106 func TestLighttpd_InvalidData(t *testing.T) { 107 ts := httptest.NewServer( 108 http.HandlerFunc( 109 func(w http.ResponseWriter, r *http.Request) { 110 _, _ = w.Write([]byte("hello and goodbye")) 111 })) 112 defer ts.Close() 113 114 job := New() 115 job.URL = ts.URL + "/server-status?auto" 116 require.True(t, job.Init()) 117 assert.False(t, job.Check()) 118 } 119 120 func TestLighttpd_ApacheData(t *testing.T) { 121 ts := httptest.NewServer( 122 http.HandlerFunc( 123 func(w http.ResponseWriter, r *http.Request) { 124 _, _ = w.Write(testApacheStatusData) 125 })) 126 defer ts.Close() 127 128 job := New() 129 job.URL = ts.URL + "/server-status?auto" 130 require.True(t, job.Init()) 131 require.False(t, job.Check()) 132 } 133 134 func TestLighttpd_404(t *testing.T) { 135 ts := httptest.NewServer( 136 http.HandlerFunc( 137 func(w http.ResponseWriter, r *http.Request) { 138 w.WriteHeader(http.StatusNotFound) 139 })) 140 defer ts.Close() 141 142 job := New() 143 job.URL = ts.URL + "/server-status?auto" 144 require.True(t, job.Init()) 145 assert.False(t, job.Check()) 146 }