github.com/netdata/go.d.plugin@v0.58.1/modules/hdfs/hdfs_test.go (about)

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