| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package squid |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/netdata/netdata/go/plugins/pkg/web" |
| 13 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/collecttest" |
| 14 | |
| 15 | "github.com/stretchr/testify/assert" |
| 16 | "github.com/stretchr/testify/require" |
| 17 | ) |
| 18 | |
| 19 | var ( |
| 20 | dataConfigJSON, _ = os.ReadFile("testdata/config.json") |
| 21 | dataConfigYAML, _ = os.ReadFile("testdata/config.yaml") |
| 22 | dataCounters, _ = os.ReadFile("testdata/counters.txt") |
| 23 | ) |
| 24 | |
| 25 | func Test_testDataIsValid(t *testing.T) { |
| 26 | for name, data := range map[string][]byte{ |
| 27 | "dataConfigJSON": dataConfigJSON, |
| 28 | "dataConfigYAML": dataConfigYAML, |
| 29 | "dataCounters": dataCounters, |
| 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_Init(t *testing.T) { |
| 40 | tests := map[string]struct { |
| 41 | wantFail bool |
| 42 | config Config |
| 43 | }{ |
| 44 | "success with default": { |
| 45 | wantFail: false, |
| 46 | config: New().Config, |
| 47 | }, |
| 48 | "fail when URL not set": { |
| 49 | wantFail: true, |
| 50 | config: Config{ |
| 51 | HTTPConfig: web.HTTPConfig{ |
| 52 | RequestConfig: web.RequestConfig{URL: ""}, |
| 53 | }, |
| 54 | }, |
| 55 | }, |
| 56 | } |
| 57 | |
| 58 | for name, test := range tests { |
| 59 | t.Run(name, func(t *testing.T) { |
| 60 | collr := New() |
| 61 | collr.Config = test.config |
| 62 | |
| 63 | if test.wantFail { |
| 64 | assert.Error(t, collr.Init(context.Background())) |
| 65 | } else { |
| 66 | assert.NoError(t, collr.Init(context.Background())) |
| 67 | } |
| 68 | }) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestCollector_Charts(t *testing.T) { |
| 73 | assert.NotNil(t, New().Charts()) |
| 74 | } |
| 75 | |
| 76 | func TestCollector_Check(t *testing.T) { |
| 77 | tests := map[string]struct { |
| 78 | wantFail bool |
| 79 | prepare func(t *testing.T) (*Collector, func()) |
| 80 | }{ |
| 81 | "success case": { |
| 82 | wantFail: false, |
| 83 | prepare: prepareCaseSuccess, |
| 84 | }, |
| 85 | "fails on unexpected response": { |
| 86 | wantFail: true, |
| 87 | prepare: prepareCaseUnexpectedResponse, |
| 88 | }, |
| 89 | "fails on empty response": { |
| 90 | wantFail: true, |
| 91 | prepare: prepareCaseEmptyResponse, |
| 92 | }, |
| 93 | "fails on connection refused": { |
| 94 | wantFail: true, |
| 95 | prepare: prepareCaseConnectionRefused, |
| 96 | }, |
| 97 | } |
| 98 | |
| 99 | for name, test := range tests { |
| 100 | t.Run(name, func(t *testing.T) { |
| 101 | collr, cleanup := test.prepare(t) |
| 102 | defer cleanup() |
| 103 | |
| 104 | if test.wantFail { |
| 105 | assert.Error(t, collr.Check(context.Background())) |
| 106 | } else { |
| 107 | assert.NoError(t, collr.Check(context.Background())) |
| 108 | } |
| 109 | }) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func TestCollector_Collect(t *testing.T) { |
| 114 | tests := map[string]struct { |
| 115 | prepare func(t *testing.T) (*Collector, func()) |
| 116 | wantMetrics map[string]int64 |
| 117 | wantCharts int |
| 118 | }{ |
| 119 | "success case": { |
| 120 | prepare: prepareCaseSuccess, |
| 121 | wantCharts: len(charts), |
| 122 | wantMetrics: map[string]int64{ |
| 123 | "client_http.errors": 5, |
| 124 | "client_http.hit_kbytes_out": 11, |
| 125 | "client_http.hits": 1, |
| 126 | "client_http.kbytes_in": 566, |
| 127 | "client_http.kbytes_out": 16081, |
| 128 | "client_http.requests": 9019, |
| 129 | "server.all.errors": 0, |
| 130 | "server.all.kbytes_in": 0, |
| 131 | "server.all.kbytes_out": 0, |
| 132 | "server.all.requests": 0, |
| 133 | }, |
| 134 | }, |
| 135 | "fails on unexpected response": { |
| 136 | prepare: prepareCaseUnexpectedResponse, |
| 137 | }, |
| 138 | "fails on empty response": { |
| 139 | prepare: prepareCaseEmptyResponse, |
| 140 | }, |
| 141 | "fails on connection refused": { |
| 142 | prepare: prepareCaseConnectionRefused, |
| 143 | }, |
| 144 | } |
| 145 | |
| 146 | for name, test := range tests { |
| 147 | t.Run(name, func(t *testing.T) { |
| 148 | collr, cleanup := test.prepare(t) |
| 149 | defer cleanup() |
| 150 | |
| 151 | mx := collr.Collect(context.Background()) |
| 152 | |
| 153 | require.Equal(t, test.wantMetrics, mx) |
| 154 | |
| 155 | if len(test.wantMetrics) > 0 { |
| 156 | assert.Equal(t, test.wantCharts, len(*collr.Charts())) |
| 157 | collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) |
| 158 | } |
| 159 | }) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | func prepareCaseSuccess(t *testing.T) (*Collector, func()) { |
| 164 | t.Helper() |
| 165 | srv := httptest.NewServer(http.HandlerFunc( |
| 166 | func(w http.ResponseWriter, r *http.Request) { |
| 167 | switch r.URL.Path { |
| 168 | case urlPathServerStats: |
| 169 | _, _ = w.Write(dataCounters) |
| 170 | default: |
| 171 | w.WriteHeader(http.StatusNotFound) |
| 172 | } |
| 173 | })) |
| 174 | |
| 175 | collr := New() |
| 176 | collr.URL = srv.URL |
| 177 | require.NoError(t, collr.Init(context.Background())) |
| 178 | |
| 179 | return collr, srv.Close |
| 180 | } |
| 181 | |
| 182 | func prepareCaseUnexpectedResponse(t *testing.T) (*Collector, func()) { |
| 183 | t.Helper() |
| 184 | resp := []byte(` |
| 185 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
| 186 | Nulla malesuada erat id magna mattis, eu viverra tellus rhoncus. |
| 187 | Fusce et felis pulvinar, posuere sem non, porttitor eros.`) |
| 188 | |
| 189 | srv := httptest.NewServer(http.HandlerFunc( |
| 190 | func(w http.ResponseWriter, r *http.Request) { |
| 191 | _, _ = w.Write(resp) |
| 192 | })) |
| 193 | |
| 194 | collr := New() |
| 195 | collr.URL = srv.URL |
| 196 | require.NoError(t, collr.Init(context.Background())) |
| 197 | |
| 198 | return collr, srv.Close |
| 199 | } |
| 200 | |
| 201 | func prepareCaseEmptyResponse(t *testing.T) (*Collector, func()) { |
| 202 | t.Helper() |
| 203 | resp := []byte(``) |
| 204 | |
| 205 | srv := httptest.NewServer(http.HandlerFunc( |
| 206 | func(w http.ResponseWriter, r *http.Request) { |
| 207 | _, _ = w.Write(resp) |
| 208 | })) |
| 209 | |
| 210 | collr := New() |
| 211 | collr.URL = srv.URL |
| 212 | require.NoError(t, collr.Init(context.Background())) |
| 213 | |
| 214 | return collr, srv.Close |
| 215 | } |
| 216 | |
| 217 | func prepareCaseConnectionRefused(t *testing.T) (*Collector, func()) { |
| 218 | t.Helper() |
| 219 | collr := New() |
| 220 | collr.URL = "http://127.0.0.1:65001" |
| 221 | require.NoError(t, collr.Init(context.Background())) |
| 222 | |
| 223 | return collr, func() {} |
| 224 | } |