| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package couchbase |
| 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 | |
| 23 | dataVer660BucketsBasicStats, _ = os.ReadFile("testdata/6.6.0/buckets_basic_stats.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 | "dataVer660BucketsBasicStats": dataVer660BucketsBasicStats, |
| 31 | } { |
| 32 | require.NotNil(t, data, name) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestCollector_ConfigurationSerialize(t *testing.T) { |
| 37 | collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) |
| 38 | } |
| 39 | |
| 40 | func TestCollector_Init(t *testing.T) { |
| 41 | tests := map[string]struct { |
| 42 | config Config |
| 43 | wantFail bool |
| 44 | }{ |
| 45 | "success on default config": { |
| 46 | config: New().Config, |
| 47 | }, |
| 48 | "fails on unset 'URL'": { |
| 49 | wantFail: true, |
| 50 | config: Config{ |
| 51 | HTTPConfig: web.HTTPConfig{ |
| 52 | RequestConfig: web.RequestConfig{ |
| 53 | URL: "", |
| 54 | }, |
| 55 | }, |
| 56 | }, |
| 57 | }, |
| 58 | "fails on invalid URL": { |
| 59 | wantFail: true, |
| 60 | config: Config{ |
| 61 | HTTPConfig: web.HTTPConfig{ |
| 62 | RequestConfig: web.RequestConfig{ |
| 63 | URL: "127.0.0.1:9090", |
| 64 | }, |
| 65 | }, |
| 66 | }, |
| 67 | }, |
| 68 | } |
| 69 | |
| 70 | for name, test := range tests { |
| 71 | t.Run(name, func(t *testing.T) { |
| 72 | collr := New() |
| 73 | collr.Config = test.config |
| 74 | |
| 75 | if test.wantFail { |
| 76 | assert.Error(t, collr.Init(context.Background())) |
| 77 | } else { |
| 78 | assert.NoError(t, collr.Init(context.Background())) |
| 79 | } |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestCollector_Check(t *testing.T) { |
| 85 | tests := map[string]struct { |
| 86 | prepare func(*testing.T) (collr *Collector, cleanup func()) |
| 87 | wantFail bool |
| 88 | }{ |
| 89 | "success on valid response v6.6.0": { |
| 90 | prepare: prepareCouchbaseV660, |
| 91 | }, |
| 92 | "fails on response with invalid data": { |
| 93 | wantFail: true, |
| 94 | prepare: prepareCouchbaseInvalidData, |
| 95 | }, |
| 96 | "fails on 404 response": { |
| 97 | wantFail: true, |
| 98 | prepare: prepareCouchbase404, |
| 99 | }, |
| 100 | "fails on connection refused": { |
| 101 | wantFail: true, |
| 102 | prepare: prepareCouchbaseConnectionRefused, |
| 103 | }, |
| 104 | } |
| 105 | |
| 106 | for name, test := range tests { |
| 107 | t.Run(name, func(t *testing.T) { |
| 108 | collr, cleanup := test.prepare(t) |
| 109 | defer cleanup() |
| 110 | |
| 111 | if test.wantFail { |
| 112 | assert.Error(t, collr.Check(context.Background())) |
| 113 | } else { |
| 114 | assert.NoError(t, collr.Check(context.Background())) |
| 115 | } |
| 116 | }) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestCollector_Collect(t *testing.T) { |
| 121 | tests := map[string]struct { |
| 122 | prepare func(t *testing.T) (collr *Collector, cleanup func()) |
| 123 | wantCollected map[string]int64 |
| 124 | }{ |
| 125 | "success on valid response v6.6.0": { |
| 126 | prepare: prepareCouchbaseV660, |
| 127 | wantCollected: map[string]int64{ |
| 128 | "bucket_beer-sample_data_used": 13990431, |
| 129 | "bucket_beer-sample_disk_fetches": 1, |
| 130 | "bucket_beer-sample_disk_used": 27690472, |
| 131 | "bucket_beer-sample_item_count": 7303, |
| 132 | "bucket_beer-sample_mem_used": 34294872, |
| 133 | "bucket_beer-sample_ops_per_sec": 1100, |
| 134 | "bucket_beer-sample_quota_percent_used": 32706, |
| 135 | "bucket_beer-sample_vb_active_num_non_resident": 1, |
| 136 | "bucket_gamesim-sample_data_used": 5371804, |
| 137 | "bucket_gamesim-sample_disk_fetches": 1, |
| 138 | "bucket_gamesim-sample_disk_used": 13821793, |
| 139 | "bucket_gamesim-sample_item_count": 586, |
| 140 | "bucket_gamesim-sample_mem_used": 29586696, |
| 141 | "bucket_gamesim-sample_ops_per_sec": 1100, |
| 142 | "bucket_gamesim-sample_quota_percent_used": 28216, |
| 143 | "bucket_gamesim-sample_vb_active_num_non_resident": 1, |
| 144 | "bucket_travel-sample_data_used": 53865472, |
| 145 | "bucket_travel-sample_disk_fetches": 1, |
| 146 | "bucket_travel-sample_disk_used": 62244260, |
| 147 | "bucket_travel-sample_item_count": 31591, |
| 148 | "bucket_travel-sample_mem_used": 54318184, |
| 149 | "bucket_travel-sample_ops_per_sec": 1100, |
| 150 | "bucket_travel-sample_quota_percent_used": 51801, |
| 151 | "bucket_travel-sample_vb_active_num_non_resident": 1, |
| 152 | }, |
| 153 | }, |
| 154 | "fails on response with invalid data": { |
| 155 | prepare: prepareCouchbaseInvalidData, |
| 156 | }, |
| 157 | "fails on 404 response": { |
| 158 | prepare: prepareCouchbase404, |
| 159 | }, |
| 160 | "fails on connection refused": { |
| 161 | prepare: prepareCouchbaseConnectionRefused, |
| 162 | }, |
| 163 | } |
| 164 | |
| 165 | for name, test := range tests { |
| 166 | t.Run(name, func(t *testing.T) { |
| 167 | collr, cleanup := test.prepare(t) |
| 168 | defer cleanup() |
| 169 | |
| 170 | mx := collr.Collect(context.Background()) |
| 171 | |
| 172 | assert.Equal(t, test.wantCollected, mx) |
| 173 | collecttest.TestMetricsHasAllChartsDims(t, collr.Charts(), mx) |
| 174 | }) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | func prepareCouchbaseV660(t *testing.T) (collr *Collector, cleanup func()) { |
| 179 | t.Helper() |
| 180 | srv := httptest.NewServer(http.HandlerFunc( |
| 181 | func(w http.ResponseWriter, r *http.Request) { |
| 182 | _, _ = w.Write(dataVer660BucketsBasicStats) |
| 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 prepareCouchbaseInvalidData(t *testing.T) (*Collector, func()) { |
| 193 | t.Helper() |
| 194 | srv := httptest.NewServer(http.HandlerFunc( |
| 195 | func(w http.ResponseWriter, r *http.Request) { |
| 196 | _, _ = w.Write([]byte("hello and\n goodbye")) |
| 197 | })) |
| 198 | collr := New() |
| 199 | collr.URL = srv.URL |
| 200 | require.NoError(t, collr.Init(context.Background())) |
| 201 | |
| 202 | return collr, srv.Close |
| 203 | } |
| 204 | |
| 205 | func prepareCouchbase404(t *testing.T) (*Collector, func()) { |
| 206 | t.Helper() |
| 207 | srv := httptest.NewServer(http.HandlerFunc( |
| 208 | func(w http.ResponseWriter, r *http.Request) { |
| 209 | w.WriteHeader(http.StatusNotFound) |
| 210 | })) |
| 211 | collr := New() |
| 212 | collr.URL = srv.URL |
| 213 | require.NoError(t, collr.Init(context.Background())) |
| 214 | |
| 215 | return collr, srv.Close |
| 216 | } |
| 217 | |
| 218 | func prepareCouchbaseConnectionRefused(t *testing.T) (*Collector, func()) { |
| 219 | t.Helper() |
| 220 | collr := New() |
| 221 | collr.URL = "http://127.0.0.1:38001" |
| 222 | require.NoError(t, collr.Init(context.Background())) |
| 223 | |
| 224 | return collr, func() {} |
| 225 | } |