github.com/netdata/go.d.plugin@v0.58.1/modules/tengine/tengine_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package tengine 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 testStatusData, _ = os.ReadFile("testdata/status.txt") 18 ) 19 20 func TestTengine_Cleanup(t *testing.T) { New().Cleanup() } 21 22 func TestNew(t *testing.T) { 23 job := New() 24 25 assert.Implements(t, (*module.Module)(nil), job) 26 assert.Equal(t, defaultURL, job.URL) 27 assert.Equal(t, defaultHTTPTimeout, job.Timeout.Duration) 28 } 29 30 func TestTengine_Init(t *testing.T) { 31 job := New() 32 33 require.True(t, job.Init()) 34 assert.NotNil(t, job.apiClient) 35 } 36 37 func TestTengine_Check(t *testing.T) { 38 ts := httptest.NewServer( 39 http.HandlerFunc( 40 func(w http.ResponseWriter, r *http.Request) { 41 _, _ = w.Write(testStatusData) 42 })) 43 defer ts.Close() 44 45 job := New() 46 job.URL = ts.URL 47 require.True(t, job.Init()) 48 assert.True(t, job.Check()) 49 } 50 51 func TestTengine_CheckNG(t *testing.T) { 52 job := New() 53 54 job.URL = "http://127.0.0.1:38001/us" 55 require.True(t, job.Init()) 56 assert.False(t, job.Check()) 57 } 58 59 func TestTengine_Charts(t *testing.T) { assert.NotNil(t, New().Charts()) } 60 61 func TestTengine_Collect(t *testing.T) { 62 ts := httptest.NewServer( 63 http.HandlerFunc( 64 func(w http.ResponseWriter, r *http.Request) { 65 _, _ = w.Write(testStatusData) 66 })) 67 defer ts.Close() 68 69 job := New() 70 job.URL = ts.URL 71 require.True(t, job.Init()) 72 require.True(t, job.Check()) 73 74 expected := map[string]int64{ 75 "bytes_in": 5944, 76 "bytes_out": 20483, 77 "conn_total": 354, 78 "http_200": 1536, 79 "http_206": 0, 80 "http_2xx": 1536, 81 "http_302": 43, 82 "http_304": 0, 83 "http_3xx": 50, 84 "http_403": 1, 85 "http_404": 75, 86 "http_416": 0, 87 "http_499": 0, 88 "http_4xx": 80, 89 "http_500": 0, 90 "http_502": 1, 91 "http_503": 0, 92 "http_504": 0, 93 "http_508": 0, 94 "http_5xx": 1, 95 "http_other_detail_status": 11, 96 "http_other_status": 0, 97 "http_ups_4xx": 26, 98 "http_ups_5xx": 1, 99 "req_total": 1672, 100 "rt": 1339, 101 "ups_req": 268, 102 "ups_rt": 644, 103 "ups_tries": 268, 104 } 105 106 assert.Equal(t, expected, job.Collect()) 107 } 108 109 func TestTengine_InvalidData(t *testing.T) { 110 ts := httptest.NewServer( 111 http.HandlerFunc( 112 func(w http.ResponseWriter, r *http.Request) { 113 _, _ = w.Write([]byte("hello and goodbye")) 114 })) 115 defer ts.Close() 116 117 job := New() 118 job.URL = ts.URL 119 require.True(t, job.Init()) 120 assert.False(t, job.Check()) 121 } 122 123 func TestTengine_404(t *testing.T) { 124 ts := httptest.NewServer( 125 http.HandlerFunc( 126 func(w http.ResponseWriter, r *http.Request) { 127 w.WriteHeader(http.StatusNotFound) 128 })) 129 defer ts.Close() 130 131 job := New() 132 job.URL = ts.URL 133 require.True(t, job.Init()) 134 assert.False(t, job.Check()) 135 }