| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nginxunit |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/stretchr/testify/assert" |
| 13 | "github.com/stretchr/testify/require" |
| 14 | |
| 15 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 16 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest" |
| 17 | ) |
| 18 | |
| 19 | var ( |
| 20 | dataConfigJSON, _ = os.ReadFile("testdata/config.json") |
| 21 | dataConfigYAML, _ = os.ReadFile("testdata/config.yaml") |
| 22 | |
| 23 | dataVer1291Status, _ = os.ReadFile("testdata/v1.29.1/status.json") |
| 24 | ) |
| 25 | |
| 26 | func Test_testDataIsValid(t *testing.T) { |
| 27 | for name, data := range map[string][]byte{ |
| 28 | "dataConfigJSON": dataConfigJSON, |
| 29 | "dataConfigYAML": dataConfigYAML, |
| 30 | "dataVer1291Status": dataVer1291Status, |
| 31 | } { |
| 32 | require.NotNil(t, data, name) |
| 33 | |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestCollector_ConfigurationSerialize(t *testing.T) { |
| 38 | collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) |
| 39 | } |
| 40 | |
| 41 | func TestCollector_Init(t *testing.T) { |
| 42 | tests := map[string]struct { |
| 43 | wantFail bool |
| 44 | config Config |
| 45 | }{ |
| 46 | "success with default": { |
| 47 | wantFail: false, |
| 48 | config: New().Config, |
| 49 | }, |
| 50 | "fail when URL not set": { |
| 51 | wantFail: true, |
| 52 | config: Config{ |
| 53 | HTTPConfig: web.HTTPConfig{ |
| 54 | RequestConfig: web.RequestConfig{URL: ""}, |
| 55 | }, |
| 56 | }, |
| 57 | }, |
| 58 | } |
| 59 | |
| 60 | for name, test := range tests { |
| 61 | t.Run(name, func(t *testing.T) { |
| 62 | collr := New() |
| 63 | collr.Config = test.config |
| 64 | |
| 65 | if test.wantFail { |
| 66 | assert.Error(t, collr.Init(context.Background())) |
| 67 | } else { |
| 68 | assert.NoError(t, collr.Init(context.Background())) |
| 69 | } |
| 70 | }) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestCollector_Check(t *testing.T) { |
| 75 | tests := map[string]struct { |
| 76 | wantFail bool |
| 77 | prepare func(t *testing.T) (collr *Collector, cleanup func()) |
| 78 | }{ |
| 79 | "success on valid response": { |
| 80 | wantFail: false, |
| 81 | prepare: caseOk, |
| 82 | }, |
| 83 | "fail on unexpected JSON response": { |
| 84 | wantFail: true, |
| 85 | prepare: caseUnexpectedJsonResponse, |
| 86 | }, |
| 87 | "fail on invalid data response": { |
| 88 | wantFail: true, |
| 89 | prepare: caseInvalidDataResponse, |
| 90 | }, |
| 91 | "fail on connection refused": { |
| 92 | wantFail: true, |
| 93 | prepare: caseConnectionRefused, |
| 94 | }, |
| 95 | "fail on 404 response": { |
| 96 | wantFail: true, |
| 97 | prepare: case404, |
| 98 | }, |
| 99 | } |
| 100 | |
| 101 | for name, test := range tests { |
| 102 | t.Run(name, func(t *testing.T) { |
| 103 | collr, cleanup := test.prepare(t) |
| 104 | defer cleanup() |
| 105 | |
| 106 | if test.wantFail { |
| 107 | assert.Error(t, collr.Check(context.Background())) |
| 108 | } else { |
| 109 | assert.NoError(t, collr.Check(context.Background())) |
| 110 | } |
| 111 | }) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestCollector_Charts(t *testing.T) { |
| 116 | assert.NotNil(t, New().Charts()) |
| 117 | } |
| 118 | |
| 119 | func TestCollector_Collect(t *testing.T) { |
| 120 | tests := map[string]struct { |
| 121 | prepare func(t *testing.T) (collr *Collector, cleanup func()) |
| 122 | wantNumOfCharts int |
| 123 | wantMetrics map[string]int64 |
| 124 | }{ |
| 125 | "success on valid response": { |
| 126 | prepare: caseOk, |
| 127 | wantNumOfCharts: len(charts), |
| 128 | wantMetrics: map[string]int64{ |
| 129 | "connections_accepted": 1, |
| 130 | "connections_active": 1, |
| 131 | "connections_closed": 1, |
| 132 | "connections_idle": 1, |
| 133 | "requests_total": 1, |
| 134 | }, |
| 135 | }, |
| 136 | "fail on unexpected JSON response": { |
| 137 | prepare: caseUnexpectedJsonResponse, |
| 138 | wantMetrics: nil, |
| 139 | }, |
| 140 | "fail on invalid data response": { |
| 141 | prepare: caseInvalidDataResponse, |
| 142 | wantMetrics: nil, |
| 143 | }, |
| 144 | "fail on connection refused": { |
| 145 | prepare: caseConnectionRefused, |
| 146 | wantMetrics: nil, |
| 147 | }, |
| 148 | "fail on 404 response": { |
| 149 | prepare: case404, |
| 150 | wantMetrics: nil, |
| 151 | }, |
| 152 | } |
| 153 | |
| 154 | for name, test := range tests { |
| 155 | t.Run(name, func(t *testing.T) { |
| 156 | collr, cleanup := test.prepare(t) |
| 157 | defer cleanup() |
| 158 | |
| 159 | _ = collr.Check(context.Background()) |
| 160 | |
| 161 | mx := collr.Collect(context.Background()) |
| 162 | |
| 163 | require.Equal(t, test.wantMetrics, mx) |
| 164 | |
| 165 | if len(test.wantMetrics) > 0 { |
| 166 | assert.Equal(t, test.wantNumOfCharts, len(*collr.Charts()), "want charts") |
| 167 | |
| 168 | collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) |
| 169 | } |
| 170 | }) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func caseOk(t *testing.T) (*Collector, func()) { |
| 175 | t.Helper() |
| 176 | srv := httptest.NewServer(http.HandlerFunc( |
| 177 | func(w http.ResponseWriter, r *http.Request) { |
| 178 | switch r.URL.Path { |
| 179 | case urlPathStatus: |
| 180 | _, _ = w.Write(dataVer1291Status) |
| 181 | default: |
| 182 | w.WriteHeader(http.StatusNotFound) |
| 183 | } |
| 184 | })) |
| 185 | collr := New() |
| 186 | collr.URL = srv.URL |
| 187 | require.NoError(t, collr.Init(context.Background())) |
| 188 | |
| 189 | return collr, srv.Close |
| 190 | } |
| 191 | |
| 192 | func caseUnexpectedJsonResponse(t *testing.T) (*Collector, func()) { |
| 193 | t.Helper() |
| 194 | resp := ` |
| 195 | { |
| 196 | "elephant": { |
| 197 | "burn": false, |
| 198 | "mountain": true, |
| 199 | "fog": false, |
| 200 | "skin": -1561907625, |
| 201 | "burst": "anyway", |
| 202 | "shadow": 1558616893 |
| 203 | }, |
| 204 | "start": "ever", |
| 205 | "base": 2093056027, |
| 206 | "mission": -2007590351, |
| 207 | "victory": 999053756, |
| 208 | "die": false |
| 209 | } |
| 210 | ` |
| 211 | srv := httptest.NewServer(http.HandlerFunc( |
| 212 | func(w http.ResponseWriter, r *http.Request) { |
| 213 | _, _ = w.Write([]byte(resp)) |
| 214 | })) |
| 215 | collr := New() |
| 216 | collr.URL = srv.URL |
| 217 | require.NoError(t, collr.Init(context.Background())) |
| 218 | |
| 219 | return collr, srv.Close |
| 220 | } |
| 221 | |
| 222 | func caseInvalidDataResponse(t *testing.T) (*Collector, func()) { |
| 223 | t.Helper() |
| 224 | srv := httptest.NewServer(http.HandlerFunc( |
| 225 | func(w http.ResponseWriter, r *http.Request) { |
| 226 | _, _ = w.Write([]byte("hello and\n goodbye")) |
| 227 | })) |
| 228 | collr := New() |
| 229 | collr.URL = srv.URL |
| 230 | require.NoError(t, collr.Init(context.Background())) |
| 231 | |
| 232 | return collr, srv.Close |
| 233 | } |
| 234 | |
| 235 | func caseConnectionRefused(t *testing.T) (*Collector, func()) { |
| 236 | t.Helper() |
| 237 | collr := New() |
| 238 | collr.URL = "http://127.0.0.1:65001" |
| 239 | require.NoError(t, collr.Init(context.Background())) |
| 240 | |
| 241 | return collr, func() {} |
| 242 | } |
| 243 | |
| 244 | func case404(t *testing.T) (*Collector, func()) { |
| 245 | t.Helper() |
| 246 | srv := httptest.NewServer(http.HandlerFunc( |
| 247 | func(w http.ResponseWriter, r *http.Request) { |
| 248 | w.WriteHeader(http.StatusNotFound) |
| 249 | })) |
| 250 | collr := New() |
| 251 | collr.URL = srv.URL |
| 252 | require.NoError(t, collr.Init(context.Background())) |
| 253 | |
| 254 | return collr, srv.Close |
| 255 | } |