master
go 147 lines 3.76 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package tengine
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 dataStatusMetrics, _ = os.ReadFile("testdata/status.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 "dataStatusMetrics": dataStatusMetrics,
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_Cleanup(t *testing.T) {
40 New().Cleanup(context.Background())
41 }
42
43 func TestCollector_Init(t *testing.T) {
44 collr := New()
45
46 require.NoError(t, collr.Init(context.Background()))
47 }
48
49 func TestCollector_Check(t *testing.T) {
50 ts := httptest.NewServer(
51 http.HandlerFunc(
52 func(w http.ResponseWriter, r *http.Request) {
53 _, _ = w.Write(dataStatusMetrics)
54 }))
55 defer ts.Close()
56
57 collr := New()
58 collr.URL = ts.URL
59 require.NoError(t, collr.Init(context.Background()))
60 assert.NoError(t, collr.Check(context.Background()))
61 }
62
63 func TestCollector_CheckNG(t *testing.T) {
64 collr := New()
65
66 collr.URL = "http://127.0.0.1:38001/us"
67 require.NoError(t, collr.Init(context.Background()))
68 assert.Error(t, collr.Check(context.Background()))
69 }
70
71 func TestCollector_Charts(t *testing.T) { assert.NotNil(t, New().Charts()) }
72
73 func TestCollector_Collect(t *testing.T) {
74 ts := httptest.NewServer(
75 http.HandlerFunc(
76 func(w http.ResponseWriter, r *http.Request) {
77 _, _ = w.Write(dataStatusMetrics)
78 }))
79 defer ts.Close()
80
81 collr := New()
82 collr.URL = ts.URL
83 require.NoError(t, collr.Init(context.Background()))
84 require.NoError(t, collr.Check(context.Background()))
85
86 expected := map[string]int64{
87 "bytes_in": 5944,
88 "bytes_out": 20483,
89 "conn_total": 354,
90 "http_200": 1536,
91 "http_206": 0,
92 "http_2xx": 1536,
93 "http_302": 43,
94 "http_304": 0,
95 "http_3xx": 50,
96 "http_403": 1,
97 "http_404": 75,
98 "http_416": 0,
99 "http_499": 0,
100 "http_4xx": 80,
101 "http_500": 0,
102 "http_502": 1,
103 "http_503": 0,
104 "http_504": 0,
105 "http_508": 0,
106 "http_5xx": 1,
107 "http_other_detail_status": 11,
108 "http_other_status": 0,
109 "http_ups_4xx": 26,
110 "http_ups_5xx": 1,
111 "req_total": 1672,
112 "rt": 1339,
113 "ups_req": 268,
114 "ups_rt": 644,
115 "ups_tries": 268,
116 }
117
118 assert.Equal(t, expected, collr.Collect(context.Background()))
119 }
120
121 func TestCollector_InvalidData(t *testing.T) {
122 ts := httptest.NewServer(
123 http.HandlerFunc(
124 func(w http.ResponseWriter, r *http.Request) {
125 _, _ = w.Write([]byte("hello and goodbye"))
126 }))
127 defer ts.Close()
128
129 collr := New()
130 collr.URL = ts.URL
131 require.NoError(t, collr.Init(context.Background()))
132 assert.Error(t, collr.Check(context.Background()))
133 }
134
135 func TestCollector_404(t *testing.T) {
136 ts := httptest.NewServer(
137 http.HandlerFunc(
138 func(w http.ResponseWriter, r *http.Request) {
139 w.WriteHeader(http.StatusNotFound)
140 }))
141 defer ts.Close()
142
143 collr := New()
144 collr.URL = ts.URL
145 require.NoError(t, collr.Init(context.Background()))
146 assert.Error(t, collr.Check(context.Background()))
147 }