master
go 155 lines 4.04 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package lighttpd
4
5 import (
6 "context"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "testing"
11
12 "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest"
13
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
16 )
17
18 var (
19 dataConfigJSON, _ = os.ReadFile("testdata/config.json")
20 dataConfigYAML, _ = os.ReadFile("testdata/config.yaml")
21
22 dataStatusMetrics, _ = os.ReadFile("testdata/status.txt")
23 dataApacheStatusMetrics, _ = os.ReadFile("testdata/apache-status.txt")
24 )
25
26 func Test_testDataIsValid(t *testing.T) {
27 for name, data := range map[string][]byte{
28 "dataConfigJSON": dataConfigJSON,
29 "dataConfigYAML": dataConfigYAML,
30 } {
31 require.NotNil(t, data, name)
32 }
33 }
34
35 func TestCollector_ConfigurationSerialize(t *testing.T) {
36 collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML)
37 }
38
39 func TestCollector_Cleanup(t *testing.T) { New().Cleanup(context.Background()) }
40
41 func TestCollector_Init(t *testing.T) {
42 collr := New()
43
44 require.NoError(t, collr.Init(context.Background()))
45 }
46
47 func TestCollector_InitNG(t *testing.T) {
48 collr := New()
49
50 collr.URL = ""
51 assert.Error(t, collr.Init(context.Background()))
52 }
53
54 func TestCollector_Check(t *testing.T) {
55 ts := httptest.NewServer(
56 http.HandlerFunc(
57 func(w http.ResponseWriter, r *http.Request) {
58 _, _ = w.Write(dataStatusMetrics)
59 }))
60 defer ts.Close()
61
62 collr := New()
63 collr.URL = ts.URL + "/server-status?auto"
64 require.NoError(t, collr.Init(context.Background()))
65 assert.NoError(t, collr.Check(context.Background()))
66 }
67
68 func TestCollector_CheckNG(t *testing.T) {
69 collr := New()
70
71 collr.URL = "http://127.0.0.1:38001/server-status?auto"
72 require.NoError(t, collr.Init(context.Background()))
73 assert.Error(t, collr.Check(context.Background()))
74 }
75
76 func TestCollector_Charts(t *testing.T) { assert.NotNil(t, New().Charts()) }
77
78 func TestCollector_Collect(t *testing.T) {
79 ts := httptest.NewServer(
80 http.HandlerFunc(
81 func(w http.ResponseWriter, r *http.Request) {
82 _, _ = w.Write(dataStatusMetrics)
83 }))
84 defer ts.Close()
85
86 collr := New()
87 collr.URL = ts.URL + "/server-status?auto"
88 require.NoError(t, collr.Init(context.Background()))
89 require.NoError(t, collr.Check(context.Background()))
90
91 expected := map[string]int64{
92 "scoreboard_waiting": 125,
93 "scoreboard_request_end": 0,
94 "busy_servers": 3,
95 "scoreboard_keepalive": 1,
96 "scoreboard_read": 1,
97 "scoreboard_request_start": 0,
98 "scoreboard_response_start": 0,
99 "scoreboard_close": 0,
100 "scoreboard_open": 0,
101 "scoreboard_hard_error": 0,
102 "scoreboard_handle_request": 1,
103 "idle_servers": 125,
104 "total_kBytes": 4,
105 "uptime": 11,
106 "scoreboard_read_post": 0,
107 "scoreboard_write": 0,
108 "scoreboard_response_end": 0,
109 "total_accesses": 12,
110 }
111
112 assert.Equal(t, expected, collr.Collect(context.Background()))
113 }
114
115 func TestCollector_InvalidData(t *testing.T) {
116 ts := httptest.NewServer(
117 http.HandlerFunc(
118 func(w http.ResponseWriter, r *http.Request) {
119 _, _ = w.Write([]byte("hello and goodbye"))
120 }))
121 defer ts.Close()
122
123 collr := New()
124 collr.URL = ts.URL + "/server-status?auto"
125 require.NoError(t, collr.Init(context.Background()))
126 assert.Error(t, collr.Check(context.Background()))
127 }
128
129 func TestCollector_ApacheData(t *testing.T) {
130 ts := httptest.NewServer(
131 http.HandlerFunc(
132 func(w http.ResponseWriter, r *http.Request) {
133 _, _ = w.Write(dataApacheStatusMetrics)
134 }))
135 defer ts.Close()
136
137 collr := New()
138 collr.URL = ts.URL + "/server-status?auto"
139 require.NoError(t, collr.Init(context.Background()))
140 require.Error(t, collr.Check(context.Background()))
141 }
142
143 func TestCollector_404(t *testing.T) {
144 ts := httptest.NewServer(
145 http.HandlerFunc(
146 func(w http.ResponseWriter, r *http.Request) {
147 w.WriteHeader(http.StatusNotFound)
148 }))
149 defer ts.Close()
150
151 collr := New()
152 collr.URL = ts.URL + "/server-status?auto"
153 require.NoError(t, collr.Init(context.Background()))
154 assert.Error(t, collr.Check(context.Background()))
155 }