| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nginxvts |
| 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/tlscfg" |
| 16 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 17 | "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi" |
| 18 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest" |
| 19 | ) |
| 20 | |
| 21 | var ( |
| 22 | dataConfigJSON, _ = os.ReadFile("testdata/config.json") |
| 23 | dataConfigYAML, _ = os.ReadFile("testdata/config.yaml") |
| 24 | |
| 25 | dataVer0118Response, _ = os.ReadFile("testdata/vts-v0.1.18.json") |
| 26 | ) |
| 27 | |
| 28 | func Test_testDataIsValid(t *testing.T) { |
| 29 | for name, data := range map[string][]byte{ |
| 30 | "dataConfigJSON": dataConfigJSON, |
| 31 | "dataConfigYAML": dataConfigYAML, |
| 32 | "dataVer0118Response": dataVer0118Response, |
| 33 | } { |
| 34 | require.NotNil(t, data, name) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestCollector_ConfigurationSerialize(t *testing.T) { |
| 39 | collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) |
| 40 | } |
| 41 | |
| 42 | func TestCollector_Init(t *testing.T) { |
| 43 | tests := map[string]struct { |
| 44 | config Config |
| 45 | wantNumOfCharts int |
| 46 | wantFail bool |
| 47 | }{ |
| 48 | "default": { |
| 49 | wantNumOfCharts: numOfCharts( |
| 50 | mainCharts, |
| 51 | sharedZonesCharts, |
| 52 | serverZonesCharts, |
| 53 | ), |
| 54 | config: New().Config, |
| 55 | }, |
| 56 | "URL not set": { |
| 57 | wantFail: true, |
| 58 | config: Config{ |
| 59 | HTTPConfig: web.HTTPConfig{ |
| 60 | RequestConfig: web.RequestConfig{URL: ""}, |
| 61 | }}, |
| 62 | }, |
| 63 | "invalid TLSCA": { |
| 64 | wantFail: true, |
| 65 | config: Config{ |
| 66 | HTTPConfig: web.HTTPConfig{ |
| 67 | ClientConfig: web.ClientConfig{ |
| 68 | TLSConfig: tlscfg.TLSConfig{TLSCA: "testdata/tls"}, |
| 69 | }, |
| 70 | }}, |
| 71 | }, |
| 72 | } |
| 73 | |
| 74 | for name, test := range tests { |
| 75 | t.Run(name, func(t *testing.T) { |
| 76 | collr := New() |
| 77 | collr.Config = test.config |
| 78 | |
| 79 | if test.wantFail { |
| 80 | assert.Error(t, collr.Init(context.Background())) |
| 81 | } else { |
| 82 | assert.NoError(t, collr.Init(context.Background())) |
| 83 | assert.Equal(t, test.wantNumOfCharts, len(*collr.Charts())) |
| 84 | } |
| 85 | }) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestCollector_Check(t *testing.T) { |
| 90 | tests := map[string]struct { |
| 91 | prepare func(*testing.T) (collr *Collector, cleanup func()) |
| 92 | wantFail bool |
| 93 | }{ |
| 94 | "valid data": {prepare: prepareNginxVTSValidData}, |
| 95 | "invalid data": {prepare: prepareNginxVTSInvalidData, wantFail: true}, |
| 96 | "404": {prepare: prepareNginxVTS404, wantFail: true}, |
| 97 | "connection refused": {prepare: prepareNginxVTSConnectionRefused, wantFail: true}, |
| 98 | } |
| 99 | |
| 100 | for name, test := range tests { |
| 101 | t.Run(name, func(t *testing.T) { |
| 102 | collr, cleanup := test.prepare(t) |
| 103 | defer cleanup() |
| 104 | |
| 105 | if test.wantFail { |
| 106 | assert.Error(t, collr.Check(context.Background())) |
| 107 | } else { |
| 108 | assert.NoError(t, collr.Check(context.Background())) |
| 109 | } |
| 110 | }) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func TestCollector_Charts(t *testing.T) { |
| 115 | assert.Nil(t, New().Charts()) |
| 116 | } |
| 117 | |
| 118 | func TestCollector_Cleanup(t *testing.T) { |
| 119 | assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) |
| 120 | } |
| 121 | |
| 122 | func TestCollector_Collect(t *testing.T) { |
| 123 | tests := map[string]struct { |
| 124 | prepare func(t *testing.T) (collr *Collector, cleanup func()) |
| 125 | wantCollected map[string]int64 |
| 126 | checkCharts bool |
| 127 | }{ |
| 128 | "right metrics": { |
| 129 | prepare: prepareNginxVTSValidData, |
| 130 | wantCollected: map[string]int64{ |
| 131 | // Nginx running time |
| 132 | "uptime": 319, |
| 133 | // Nginx connections |
| 134 | "connections_active": 2, |
| 135 | "connections_reading": 0, |
| 136 | "connections_writing": 1, |
| 137 | "connections_waiting": 1, |
| 138 | "connections_accepted": 12, |
| 139 | "connections_handled": 12, |
| 140 | "connections_requests": 17, |
| 141 | // Nginx shared memory |
| 142 | "sharedzones_maxsize": 1048575, |
| 143 | "sharedzones_usedsize": 45799, |
| 144 | "sharedzones_usednode": 13, |
| 145 | // Nginx traffic |
| 146 | "total_requestcounter": 2, |
| 147 | "total_inbytes": 156, |
| 148 | "total_outbytes": 692, |
| 149 | // Nginx response code |
| 150 | "total_responses_1xx": 1, |
| 151 | "total_responses_2xx": 2, |
| 152 | "total_responses_3xx": 3, |
| 153 | "total_responses_4xx": 4, |
| 154 | "total_responses_5xx": 5, |
| 155 | // Nginx cache |
| 156 | "total_cache_miss": 2, |
| 157 | "total_cache_bypass": 4, |
| 158 | "total_cache_expired": 6, |
| 159 | "total_cache_stale": 8, |
| 160 | "total_cache_updating": 10, |
| 161 | "total_cache_revalidated": 12, |
| 162 | "total_cache_hit": 14, |
| 163 | "total_cache_scarce": 16, |
| 164 | }, |
| 165 | checkCharts: true, |
| 166 | }, |
| 167 | } |
| 168 | |
| 169 | for name, test := range tests { |
| 170 | t.Run(name, func(t *testing.T) { |
| 171 | collr, cleanup := test.prepare(t) |
| 172 | defer cleanup() |
| 173 | |
| 174 | mx := collr.Collect(context.Background()) |
| 175 | |
| 176 | assert.Equal(t, test.wantCollected, mx) |
| 177 | if test.checkCharts { |
| 178 | collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) |
| 179 | } |
| 180 | }) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | func prepareNginxVTS(t *testing.T, createNginxVTS func() *Collector) (collr *Collector, cleanup func()) { |
| 185 | t.Helper() |
| 186 | collr = createNginxVTS() |
| 187 | srv := prepareNginxVTSEndpoint() |
| 188 | collr.URL = srv.URL |
| 189 | |
| 190 | require.NoError(t, collr.Init(context.Background())) |
| 191 | |
| 192 | return collr, srv.Close |
| 193 | } |
| 194 | |
| 195 | func prepareNginxVTSValidData(t *testing.T) (collr *Collector, cleanup func()) { |
| 196 | return prepareNginxVTS(t, New) |
| 197 | } |
| 198 | |
| 199 | func prepareNginxVTSInvalidData(t *testing.T) (*Collector, func()) { |
| 200 | t.Helper() |
| 201 | srv := httptest.NewServer(http.HandlerFunc( |
| 202 | func(w http.ResponseWriter, r *http.Request) { |
| 203 | _, _ = w.Write([]byte("hello and\n goodbye")) |
| 204 | })) |
| 205 | collr := New() |
| 206 | collr.URL = srv.URL |
| 207 | require.NoError(t, collr.Init(context.Background())) |
| 208 | |
| 209 | return collr, srv.Close |
| 210 | } |
| 211 | |
| 212 | func prepareNginxVTS404(t *testing.T) (*Collector, func()) { |
| 213 | t.Helper() |
| 214 | srv := httptest.NewServer(http.HandlerFunc( |
| 215 | func(w http.ResponseWriter, r *http.Request) { |
| 216 | w.WriteHeader(http.StatusNotFound) |
| 217 | })) |
| 218 | collr := New() |
| 219 | collr.URL = srv.URL |
| 220 | require.NoError(t, collr.Init(context.Background())) |
| 221 | |
| 222 | return collr, srv.Close |
| 223 | } |
| 224 | |
| 225 | func prepareNginxVTSConnectionRefused(t *testing.T) (*Collector, func()) { |
| 226 | t.Helper() |
| 227 | collr := New() |
| 228 | collr.URL = "http://127.0.0.1:18080" |
| 229 | require.NoError(t, collr.Init(context.Background())) |
| 230 | |
| 231 | return collr, func() {} |
| 232 | } |
| 233 | |
| 234 | func prepareNginxVTSEndpoint() *httptest.Server { |
| 235 | return httptest.NewServer(http.HandlerFunc( |
| 236 | func(w http.ResponseWriter, r *http.Request) { |
| 237 | switch r.URL.Path { |
| 238 | case "/": |
| 239 | _, _ = w.Write(dataVer0118Response) |
| 240 | default: |
| 241 | w.WriteHeader(http.StatusNotFound) |
| 242 | } |
| 243 | })) |
| 244 | } |
| 245 | |
| 246 | func numOfCharts(charts ...collectorapi.Charts) (num int) { |
| 247 | for _, v := range charts { |
| 248 | num += len(v) |
| 249 | } |
| 250 | return num |
| 251 | } |