| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package phpfpm |
| 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 | dataStatusJSON, _ = os.ReadFile("testdata/status.json") |
| 23 | dataStatusFullJSON, _ = os.ReadFile("testdata/status-full.json") |
| 24 | dataStatusFullNoIdleJSON, _ = os.ReadFile("testdata/status-full-no-idle.json") |
| 25 | dataStatusText, _ = os.ReadFile("testdata/status.txt") |
| 26 | dataStatusFullText, _ = os.ReadFile("testdata/status-full.txt") |
| 27 | ) |
| 28 | |
| 29 | func Test_testDataIsValid(t *testing.T) { |
| 30 | for name, data := range map[string][]byte{ |
| 31 | "dataConfigJSON": dataConfigJSON, |
| 32 | "dataConfigYAML": dataConfigYAML, |
| 33 | "dataStatusJSON": dataStatusJSON, |
| 34 | "dataStatusFullJSON": dataStatusFullJSON, |
| 35 | "dataStatusFullNoIdleJSON": dataStatusFullNoIdleJSON, |
| 36 | "dataStatusText": dataStatusText, |
| 37 | "dataStatusFullText": dataStatusFullText, |
| 38 | } { |
| 39 | require.NotNil(t, data, name) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestCollector_ConfigurationSerialize(t *testing.T) { |
| 44 | collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) |
| 45 | } |
| 46 | |
| 47 | func TestCollector_Init(t *testing.T) { |
| 48 | collr := New() |
| 49 | |
| 50 | require.NoError(t, collr.Init(context.Background())) |
| 51 | assert.NotNil(t, collr.client) |
| 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(dataStatusText) |
| 59 | })) |
| 60 | defer ts.Close() |
| 61 | |
| 62 | collr := New() |
| 63 | collr.URL = ts.URL |
| 64 | require.NoError(t, collr.Init(context.Background())) |
| 65 | |
| 66 | assert.NoError(t, collr.Check(context.Background())) |
| 67 | } |
| 68 | |
| 69 | func TestCollector_CheckReturnsFalseOnFailure(t *testing.T) { |
| 70 | collr := New() |
| 71 | collr.URL = "http://127.0.0.1:38001/us" |
| 72 | require.NoError(t, collr.Init(context.Background())) |
| 73 | |
| 74 | assert.Error(t, collr.Check(context.Background())) |
| 75 | } |
| 76 | |
| 77 | func TestCollector_Charts(t *testing.T) { |
| 78 | collr := New() |
| 79 | |
| 80 | assert.NotNil(t, collr.Charts()) |
| 81 | } |
| 82 | |
| 83 | func TestCollector_CollectJSON(t *testing.T) { |
| 84 | ts := httptest.NewServer( |
| 85 | http.HandlerFunc( |
| 86 | func(w http.ResponseWriter, r *http.Request) { |
| 87 | _, _ = w.Write(dataStatusJSON) |
| 88 | })) |
| 89 | defer ts.Close() |
| 90 | |
| 91 | collr := New() |
| 92 | collr.URL = ts.URL + "/?json" |
| 93 | require.NoError(t, collr.Init(context.Background())) |
| 94 | |
| 95 | got := collr.Collect(context.Background()) |
| 96 | |
| 97 | want := map[string]int64{ |
| 98 | "active": 1, |
| 99 | "idle": 1, |
| 100 | "maxActive": 1, |
| 101 | "reached": 0, |
| 102 | "requests": 21, |
| 103 | "slow": 0, |
| 104 | } |
| 105 | assert.Equal(t, want, got) |
| 106 | } |
| 107 | |
| 108 | func TestCollector_CollectJSONFull(t *testing.T) { |
| 109 | ts := httptest.NewServer( |
| 110 | http.HandlerFunc( |
| 111 | func(w http.ResponseWriter, r *http.Request) { |
| 112 | _, _ = w.Write(dataStatusFullJSON) |
| 113 | })) |
| 114 | defer ts.Close() |
| 115 | |
| 116 | collr := New() |
| 117 | collr.URL = ts.URL + "/?json" |
| 118 | require.NoError(t, collr.Init(context.Background())) |
| 119 | |
| 120 | got := collr.Collect(context.Background()) |
| 121 | |
| 122 | want := map[string]int64{ |
| 123 | "active": 1, |
| 124 | "idle": 1, |
| 125 | "maxActive": 1, |
| 126 | "reached": 0, |
| 127 | "requests": 22, |
| 128 | "slow": 0, |
| 129 | "minReqCpu": 0, |
| 130 | "maxReqCpu": 10, |
| 131 | "avgReqCpu": 5, |
| 132 | "minReqDur": 0, |
| 133 | "maxReqDur": 919, |
| 134 | "avgReqDur": 459, |
| 135 | "minReqMem": 2093045, |
| 136 | "maxReqMem": 2097152, |
| 137 | "avgReqMem": 2095098, |
| 138 | } |
| 139 | assert.Equal(t, want, got) |
| 140 | } |
| 141 | |
| 142 | func TestCollector_CollectNoIdleProcessesJSONFull(t *testing.T) { |
| 143 | ts := httptest.NewServer( |
| 144 | http.HandlerFunc( |
| 145 | func(w http.ResponseWriter, r *http.Request) { |
| 146 | _, _ = w.Write(dataStatusFullNoIdleJSON) |
| 147 | })) |
| 148 | defer ts.Close() |
| 149 | |
| 150 | collr := New() |
| 151 | collr.URL = ts.URL + "/?json" |
| 152 | require.NoError(t, collr.Init(context.Background())) |
| 153 | |
| 154 | got := collr.Collect(context.Background()) |
| 155 | |
| 156 | want := map[string]int64{ |
| 157 | "active": 1, |
| 158 | "idle": 1, |
| 159 | "maxActive": 1, |
| 160 | "reached": 0, |
| 161 | "requests": 22, |
| 162 | "slow": 0, |
| 163 | } |
| 164 | assert.Equal(t, want, got) |
| 165 | } |
| 166 | |
| 167 | func TestCollector_CollectText(t *testing.T) { |
| 168 | ts := httptest.NewServer( |
| 169 | http.HandlerFunc( |
| 170 | func(w http.ResponseWriter, r *http.Request) { |
| 171 | _, _ = w.Write(dataStatusText) |
| 172 | })) |
| 173 | defer ts.Close() |
| 174 | |
| 175 | collr := New() |
| 176 | collr.URL = ts.URL |
| 177 | require.NoError(t, collr.Init(context.Background())) |
| 178 | |
| 179 | got := collr.Collect(context.Background()) |
| 180 | |
| 181 | want := map[string]int64{ |
| 182 | "active": 1, |
| 183 | "idle": 1, |
| 184 | "maxActive": 1, |
| 185 | "reached": 0, |
| 186 | "requests": 19, |
| 187 | "slow": 0, |
| 188 | } |
| 189 | assert.Equal(t, want, got) |
| 190 | } |
| 191 | |
| 192 | func TestCollector_CollectTextFull(t *testing.T) { |
| 193 | ts := httptest.NewServer( |
| 194 | http.HandlerFunc( |
| 195 | func(w http.ResponseWriter, r *http.Request) { |
| 196 | _, _ = w.Write(dataStatusFullText) |
| 197 | })) |
| 198 | defer ts.Close() |
| 199 | |
| 200 | collr := New() |
| 201 | collr.URL = ts.URL |
| 202 | require.NoError(t, collr.Init(context.Background())) |
| 203 | |
| 204 | got := collr.Collect(context.Background()) |
| 205 | |
| 206 | want := map[string]int64{ |
| 207 | "active": 1, |
| 208 | "idle": 1, |
| 209 | "maxActive": 1, |
| 210 | "reached": 0, |
| 211 | "requests": 20, |
| 212 | "slow": 0, |
| 213 | "minReqCpu": 0, |
| 214 | "maxReqCpu": 10, |
| 215 | "avgReqCpu": 5, |
| 216 | "minReqDur": 0, |
| 217 | "maxReqDur": 536, |
| 218 | "avgReqDur": 268, |
| 219 | "minReqMem": 2093045, |
| 220 | "maxReqMem": 2097152, |
| 221 | "avgReqMem": 2095098, |
| 222 | } |
| 223 | assert.Equal(t, want, got) |
| 224 | } |
| 225 | |
| 226 | func TestCollector_CollectReturnsNothingWhenInvalidData(t *testing.T) { |
| 227 | ts := httptest.NewServer( |
| 228 | http.HandlerFunc( |
| 229 | func(w http.ResponseWriter, r *http.Request) { |
| 230 | _, _ = w.Write([]byte("hello and goodbye\nfrom someone\nfoobar")) |
| 231 | })) |
| 232 | defer ts.Close() |
| 233 | |
| 234 | collr := New() |
| 235 | collr.URL = ts.URL |
| 236 | require.NoError(t, collr.Init(context.Background())) |
| 237 | |
| 238 | assert.Len(t, collr.Collect(context.Background()), 0) |
| 239 | } |
| 240 | |
| 241 | func TestCollector_CollectReturnsNothingWhenEmptyData(t *testing.T) { |
| 242 | ts := httptest.NewServer( |
| 243 | http.HandlerFunc( |
| 244 | func(w http.ResponseWriter, r *http.Request) { |
| 245 | _, _ = w.Write([]byte{}) |
| 246 | })) |
| 247 | defer ts.Close() |
| 248 | |
| 249 | collr := New() |
| 250 | collr.URL = ts.URL |
| 251 | require.NoError(t, collr.Init(context.Background())) |
| 252 | |
| 253 | assert.Len(t, collr.Collect(context.Background()), 0) |
| 254 | } |
| 255 | |
| 256 | func TestCollector_CollectReturnsNothingWhenBadStatusCode(t *testing.T) { |
| 257 | ts := httptest.NewServer( |
| 258 | http.HandlerFunc( |
| 259 | func(w http.ResponseWriter, r *http.Request) { |
| 260 | w.WriteHeader(http.StatusNotFound) |
| 261 | })) |
| 262 | defer ts.Close() |
| 263 | |
| 264 | collr := New() |
| 265 | collr.URL = ts.URL |
| 266 | require.NoError(t, collr.Init(context.Background())) |
| 267 | |
| 268 | assert.Len(t, collr.Collect(context.Background()), 0) |
| 269 | } |
| 270 | |
| 271 | func TestCollector_Cleanup(t *testing.T) { |
| 272 | New().Cleanup(context.Background()) |
| 273 | } |