| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package hdfs |
| 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 | dataUnknownNodeMetrics, _ = os.ReadFile("testdata/unknownnode.json") |
| 23 | dataDataNodeMetrics, _ = os.ReadFile("testdata/datanode.json") |
| 24 | dataNameNodeMetrics, _ = os.ReadFile("testdata/namenode.json") |
| 25 | ) |
| 26 | |
| 27 | func Test_testDataIsValid(t *testing.T) { |
| 28 | for name, data := range map[string][]byte{ |
| 29 | "dataConfigJSON": dataConfigJSON, |
| 30 | "dataConfigYAML": dataConfigYAML, |
| 31 | "dataUnknownNodeMetrics": dataUnknownNodeMetrics, |
| 32 | "dataDataNodeMetrics": dataDataNodeMetrics, |
| 33 | "dataNameNodeMetrics": dataNameNodeMetrics, |
| 34 | } { |
| 35 | require.NotNil(t, data, name) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestCollector_ConfigurationSerialize(t *testing.T) { |
| 40 | collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) |
| 41 | } |
| 42 | |
| 43 | func TestCollector_Init(t *testing.T) { |
| 44 | collr := New() |
| 45 | |
| 46 | assert.NoError(t, collr.Init(context.Background())) |
| 47 | } |
| 48 | |
| 49 | func TestCollector_InitErrorOnCreatingClientWrongTLSCA(t *testing.T) { |
| 50 | collr := New() |
| 51 | collr.ClientConfig.TLSConfig.TLSCA = "testdata/tls" |
| 52 | |
| 53 | assert.Error(t, collr.Init(context.Background())) |
| 54 | } |
| 55 | |
| 56 | func TestCollector_Check(t *testing.T) { |
| 57 | ts := httptest.NewServer( |
| 58 | http.HandlerFunc( |
| 59 | func(w http.ResponseWriter, r *http.Request) { |
| 60 | _, _ = w.Write(dataNameNodeMetrics) |
| 61 | })) |
| 62 | defer ts.Close() |
| 63 | |
| 64 | collr := New() |
| 65 | collr.URL = ts.URL |
| 66 | require.NoError(t, collr.Init(context.Background())) |
| 67 | |
| 68 | assert.NoError(t, collr.Check(context.Background())) |
| 69 | assert.NotZero(t, collr.nodeType) |
| 70 | } |
| 71 | |
| 72 | func TestCollector_CheckDataNode(t *testing.T) { |
| 73 | ts := httptest.NewServer( |
| 74 | http.HandlerFunc( |
| 75 | func(w http.ResponseWriter, r *http.Request) { |
| 76 | _, _ = w.Write(dataDataNodeMetrics) |
| 77 | })) |
| 78 | defer ts.Close() |
| 79 | |
| 80 | collr := New() |
| 81 | collr.URL = ts.URL |
| 82 | require.NoError(t, collr.Init(context.Background())) |
| 83 | |
| 84 | assert.NoError(t, collr.Check(context.Background())) |
| 85 | assert.Equal(t, dataNodeType, collr.nodeType) |
| 86 | } |
| 87 | |
| 88 | func TestCollector_CheckNameNode(t *testing.T) { |
| 89 | ts := httptest.NewServer( |
| 90 | http.HandlerFunc( |
| 91 | func(w http.ResponseWriter, r *http.Request) { |
| 92 | _, _ = w.Write(dataNameNodeMetrics) |
| 93 | })) |
| 94 | defer ts.Close() |
| 95 | |
| 96 | collr := New() |
| 97 | collr.URL = ts.URL |
| 98 | require.NoError(t, collr.Init(context.Background())) |
| 99 | |
| 100 | assert.NoError(t, collr.Check(context.Background())) |
| 101 | assert.Equal(t, nameNodeType, collr.nodeType) |
| 102 | } |
| 103 | |
| 104 | func TestCollector_CheckErrorOnNodeTypeDetermination(t *testing.T) { |
| 105 | ts := httptest.NewServer( |
| 106 | http.HandlerFunc( |
| 107 | func(w http.ResponseWriter, r *http.Request) { |
| 108 | _, _ = w.Write(dataUnknownNodeMetrics) |
| 109 | })) |
| 110 | defer ts.Close() |
| 111 | |
| 112 | collr := New() |
| 113 | collr.URL = ts.URL |
| 114 | require.NoError(t, collr.Init(context.Background())) |
| 115 | |
| 116 | assert.Error(t, collr.Check(context.Background())) |
| 117 | } |
| 118 | |
| 119 | func TestCollector_CheckNoResponse(t *testing.T) { |
| 120 | collr := New() |
| 121 | collr.URL = "http://127.0.0.1:38001/jmx" |
| 122 | require.NoError(t, collr.Init(context.Background())) |
| 123 | |
| 124 | assert.Error(t, collr.Check(context.Background())) |
| 125 | } |
| 126 | |
| 127 | func TestCollector_Charts(t *testing.T) { |
| 128 | assert.Nil(t, New().Charts()) |
| 129 | } |
| 130 | |
| 131 | func TestCollector_ChartsUnknownNode(t *testing.T) { |
| 132 | collr := New() |
| 133 | |
| 134 | assert.Nil(t, collr.Charts()) |
| 135 | } |
| 136 | |
| 137 | func TestCollector_ChartsDataNode(t *testing.T) { |
| 138 | collr := New() |
| 139 | collr.nodeType = dataNodeType |
| 140 | |
| 141 | assert.Equal(t, dataNodeCharts(), collr.Charts()) |
| 142 | } |
| 143 | |
| 144 | func TestCollector_ChartsNameNode(t *testing.T) { |
| 145 | collr := New() |
| 146 | collr.nodeType = nameNodeType |
| 147 | |
| 148 | assert.Equal(t, nameNodeCharts(), collr.Charts()) |
| 149 | } |
| 150 | |
| 151 | func TestCollector_Cleanup(t *testing.T) { |
| 152 | New().Cleanup(context.Background()) |
| 153 | } |
| 154 | |
| 155 | func TestCollector_CollectDataNode(t *testing.T) { |
| 156 | ts := httptest.NewServer( |
| 157 | http.HandlerFunc( |
| 158 | func(w http.ResponseWriter, r *http.Request) { |
| 159 | _, _ = w.Write(dataDataNodeMetrics) |
| 160 | })) |
| 161 | defer ts.Close() |
| 162 | |
| 163 | collr := New() |
| 164 | collr.URL = ts.URL |
| 165 | require.NoError(t, collr.Init(context.Background())) |
| 166 | require.NoError(t, collr.Check(context.Background())) |
| 167 | |
| 168 | expected := map[string]int64{ |
| 169 | "dna_bytes_read": 80689178, |
| 170 | "dna_bytes_written": 500960407, |
| 171 | "fsds_capacity_remaining": 32920760320, |
| 172 | "fsds_capacity_total": 53675536384, |
| 173 | "fsds_capacity_used": 20754776064, |
| 174 | "fsds_capacity_used_dfs": 1186058240, |
| 175 | "fsds_capacity_used_non_dfs": 19568717824, |
| 176 | "fsds_num_failed_volumes": 0, |
| 177 | "jvm_gc_count": 155, |
| 178 | "jvm_gc_num_info_threshold_exceeded": 0, |
| 179 | "jvm_gc_num_warn_threshold_exceeded": 0, |
| 180 | "jvm_gc_time_millis": 672, |
| 181 | "jvm_gc_total_extra_sleep_time": 8783, |
| 182 | "jvm_log_error": 1, |
| 183 | "jvm_log_fatal": 0, |
| 184 | "jvm_log_info": 257, |
| 185 | "jvm_log_warn": 2, |
| 186 | "jvm_mem_heap_committed": 60500, |
| 187 | "jvm_mem_heap_max": 843, |
| 188 | "jvm_mem_heap_used": 18885, |
| 189 | "jvm_threads_blocked": 0, |
| 190 | "jvm_threads_new": 0, |
| 191 | "jvm_threads_runnable": 11, |
| 192 | "jvm_threads_terminated": 0, |
| 193 | "jvm_threads_timed_waiting": 25, |
| 194 | "jvm_threads_waiting": 11, |
| 195 | "rpc_call_queue_length": 0, |
| 196 | "rpc_num_open_connections": 0, |
| 197 | "rpc_processing_time_avg_time": 0, |
| 198 | "rpc_queue_time_avg_time": 0, |
| 199 | "rpc_queue_time_num_ops": 0, |
| 200 | "rpc_received_bytes": 7, |
| 201 | "rpc_sent_bytes": 187, |
| 202 | } |
| 203 | |
| 204 | assert.Equal(t, expected, collr.Collect(context.Background())) |
| 205 | } |
| 206 | |
| 207 | func TestCollector_CollectNameNode(t *testing.T) { |
| 208 | ts := httptest.NewServer( |
| 209 | http.HandlerFunc( |
| 210 | func(w http.ResponseWriter, r *http.Request) { |
| 211 | _, _ = w.Write(dataNameNodeMetrics) |
| 212 | })) |
| 213 | defer ts.Close() |
| 214 | |
| 215 | collr := New() |
| 216 | collr.URL = ts.URL |
| 217 | require.NoError(t, collr.Init(context.Background())) |
| 218 | require.NoError(t, collr.Check(context.Background())) |
| 219 | |
| 220 | expected := map[string]int64{ |
| 221 | "fsns_blocks_total": 15, |
| 222 | "fsns_capacity_remaining": 65861697536, |
| 223 | "fsns_capacity_total": 107351072768, |
| 224 | "fsns_capacity_used": 41489375232, |
| 225 | "fsns_capacity_used_dfs": 2372116480, |
| 226 | "fsns_capacity_used_non_dfs": 39117258752, |
| 227 | "fsns_corrupt_blocks": 0, |
| 228 | "fsns_files_total": 12, |
| 229 | "fsns_missing_blocks": 0, |
| 230 | "fsns_num_dead_data_nodes": 0, |
| 231 | "fsns_num_live_data_nodes": 2, |
| 232 | "fsns_stale_data_nodes": 0, |
| 233 | "fsns_total_load": 2, |
| 234 | "fsns_under_replicated_blocks": 0, |
| 235 | "fsns_volume_failures_total": 0, |
| 236 | "jvm_gc_count": 1699, |
| 237 | "jvm_gc_num_info_threshold_exceeded": 0, |
| 238 | "jvm_gc_num_warn_threshold_exceeded": 0, |
| 239 | "jvm_gc_time_millis": 3483, |
| 240 | "jvm_gc_total_extra_sleep_time": 1944, |
| 241 | "jvm_log_error": 0, |
| 242 | "jvm_log_fatal": 0, |
| 243 | "jvm_log_info": 3382077, |
| 244 | "jvm_log_warn": 3378983, |
| 245 | "jvm_mem_heap_committed": 67000, |
| 246 | "jvm_mem_heap_max": 843, |
| 247 | "jvm_mem_heap_used": 26603, |
| 248 | "jvm_threads_blocked": 0, |
| 249 | "jvm_threads_new": 0, |
| 250 | "jvm_threads_runnable": 7, |
| 251 | "jvm_threads_terminated": 0, |
| 252 | "jvm_threads_timed_waiting": 34, |
| 253 | "jvm_threads_waiting": 6, |
| 254 | "rpc_call_queue_length": 0, |
| 255 | "rpc_num_open_connections": 2, |
| 256 | "rpc_processing_time_avg_time": 0, |
| 257 | "rpc_queue_time_avg_time": 58, |
| 258 | "rpc_queue_time_num_ops": 585402, |
| 259 | "rpc_received_bytes": 240431351, |
| 260 | "rpc_sent_bytes": 25067414, |
| 261 | } |
| 262 | |
| 263 | assert.Equal(t, expected, collr.Collect(context.Background())) |
| 264 | } |
| 265 | |
| 266 | func TestCollector_CollectUnknownNode(t *testing.T) { |
| 267 | ts := httptest.NewServer( |
| 268 | http.HandlerFunc( |
| 269 | func(w http.ResponseWriter, r *http.Request) { |
| 270 | _, _ = w.Write(dataUnknownNodeMetrics) |
| 271 | })) |
| 272 | defer ts.Close() |
| 273 | |
| 274 | collr := New() |
| 275 | collr.URL = ts.URL |
| 276 | require.NoError(t, collr.Init(context.Background())) |
| 277 | |
| 278 | assert.Panics(t, func() { _ = collr.Collect(context.Background()) }) |
| 279 | } |
| 280 | |
| 281 | func TestCollector_CollectNoResponse(t *testing.T) { |
| 282 | collr := New() |
| 283 | collr.URL = "http://127.0.0.1:38001/jmx" |
| 284 | require.NoError(t, collr.Init(context.Background())) |
| 285 | |
| 286 | assert.Nil(t, collr.Collect(context.Background())) |
| 287 | } |
| 288 | |
| 289 | func TestCollector_CollectReceiveInvalidResponse(t *testing.T) { |
| 290 | ts := httptest.NewServer( |
| 291 | http.HandlerFunc( |
| 292 | func(w http.ResponseWriter, r *http.Request) { |
| 293 | _, _ = w.Write([]byte("hello and\ngoodbye!\n")) |
| 294 | })) |
| 295 | defer ts.Close() |
| 296 | |
| 297 | collr := New() |
| 298 | collr.URL = ts.URL |
| 299 | require.NoError(t, collr.Init(context.Background())) |
| 300 | |
| 301 | assert.Nil(t, collr.Collect(context.Background())) |
| 302 | } |
| 303 | |
| 304 | func TestCollector_CollectReceive404(t *testing.T) { |
| 305 | ts := httptest.NewServer( |
| 306 | http.HandlerFunc( |
| 307 | func(w http.ResponseWriter, r *http.Request) { |
| 308 | w.WriteHeader(http.StatusNotFound) |
| 309 | })) |
| 310 | defer ts.Close() |
| 311 | |
| 312 | collr := New() |
| 313 | collr.URL = ts.URL |
| 314 | require.NoError(t, collr.Init(context.Background())) |
| 315 | |
| 316 | assert.Nil(t, collr.Collect(context.Background())) |
| 317 | } |