master
go 128 lines 3.28 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package fluentd
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 dataPluginsMetrics, _ = os.ReadFile("testdata/plugins.json")
23 )
24
25 func Test_testDataIsValid(t *testing.T) {
26 for name, data := range map[string][]byte{
27 "dataConfigJSON": dataConfigJSON,
28 "dataConfigYAML": dataConfigYAML,
29 "dataPluginsMetrics": dataPluginsMetrics,
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_Init(t *testing.T) {
40 // OK
41 collr := New()
42 assert.NoError(t, collr.Init(context.Background()))
43 assert.NotNil(t, collr.apiClient)
44
45 //NG
46 collr = New()
47 collr.URL = ""
48 assert.Error(t, collr.Init(context.Background()))
49 }
50
51 func TestCollector_Check(t *testing.T) {
52 ts := httptest.NewServer(http.HandlerFunc(
53 func(w http.ResponseWriter, r *http.Request) {
54 _, _ = w.Write(dataPluginsMetrics)
55 }))
56 defer ts.Close()
57
58 // OK
59 collr := New()
60 collr.URL = ts.URL
61 require.NoError(t, collr.Init(context.Background()))
62 require.NoError(t, collr.Check(context.Background()))
63
64 // NG
65 collr = New()
66 collr.URL = "http://127.0.0.1:38001/api/plugins.json"
67 require.NoError(t, collr.Init(context.Background()))
68 require.Error(t, collr.Check(context.Background()))
69 }
70
71 func TestCollector_Charts(t *testing.T) {
72 assert.NotNil(t, New().Charts())
73 }
74
75 func TestCollector_Cleanup(t *testing.T) {
76 New().Cleanup(context.Background())
77 }
78
79 func TestCollector_Collect(t *testing.T) {
80 ts := httptest.NewServer(http.HandlerFunc(
81 func(w http.ResponseWriter, r *http.Request) {
82 _, _ = w.Write(dataPluginsMetrics)
83 }))
84 defer ts.Close()
85
86 collr := New()
87 collr.URL = ts.URL
88
89 require.NoError(t, collr.Init(context.Background()))
90 require.NoError(t, collr.Check(context.Background()))
91
92 expected := map[string]int64{
93 "output_stdout_stdout_output_retry_count": 0,
94 "output_td_tdlog_output_retry_count": 0,
95 "output_td_tdlog_output_buffer_queue_length": 0,
96 "output_td_tdlog_output_buffer_total_queued_size": 0,
97 }
98 assert.Equal(t, expected, collr.Collect(context.Background()))
99 assert.Len(t, collr.charts.Get("retry_count").Dims, 2)
100 assert.Len(t, collr.charts.Get("buffer_queue_length").Dims, 1)
101 assert.Len(t, collr.charts.Get("buffer_total_queued_size").Dims, 1)
102 }
103
104 func TestCollector_InvalidData(t *testing.T) {
105 ts := httptest.NewServer(http.HandlerFunc(
106 func(w http.ResponseWriter, r *http.Request) {
107 _, _ = w.Write([]byte("hello and goodbye"))
108 }))
109 defer ts.Close()
110
111 collr := New()
112 collr.URL = ts.URL
113 require.NoError(t, collr.Init(context.Background()))
114 assert.Error(t, collr.Check(context.Background()))
115 }
116
117 func TestCollector_404(t *testing.T) {
118 ts := httptest.NewServer(http.HandlerFunc(
119 func(w http.ResponseWriter, r *http.Request) {
120 w.WriteHeader(404)
121 }))
122 defer ts.Close()
123
124 collr := New()
125 collr.URL = ts.URL
126 require.NoError(t, collr.Init(context.Background()))
127 assert.Error(t, collr.Check(context.Background()))
128 }