| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nginx |
| 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 | dataTengineStatusMetrics, _ = os.ReadFile("testdata/tengine-status.txt") |
| 24 | ) |
| 25 | |
| 26 | func Test_testDataIsValid(t *testing.T) { |
| 27 | for name, data := range map[string][]byte{ |
| 28 | "dataConfigJSON": dataConfigJSON, |
| 29 | "dataConfigYAML": dataConfigYAML, |
| 30 | "dataStatusMetrics": dataStatusMetrics, |
| 31 | "dataTengineStatusMetrics": dataTengineStatusMetrics, |
| 32 | } { |
| 33 | require.NotNil(t, data, name) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestCollector_ConfigurationSerialize(t *testing.T) { |
| 38 | collecttest.TestConfigurationSerialize(t, &Collector{}, dataConfigJSON, dataConfigYAML) |
| 39 | } |
| 40 | |
| 41 | func TestCollector_Cleanup(t *testing.T) { |
| 42 | New().Cleanup(context.Background()) |
| 43 | } |
| 44 | |
| 45 | func TestCollector_Init(t *testing.T) { |
| 46 | collr := New() |
| 47 | |
| 48 | require.NoError(t, collr.Init(context.Background())) |
| 49 | } |
| 50 | |
| 51 | func TestCollector_Check(t *testing.T) { |
| 52 | ts := httptest.NewServer( |
| 53 | http.HandlerFunc( |
| 54 | func(w http.ResponseWriter, r *http.Request) { |
| 55 | _, _ = w.Write(dataStatusMetrics) |
| 56 | })) |
| 57 | defer ts.Close() |
| 58 | |
| 59 | collr := New() |
| 60 | collr.URL = ts.URL |
| 61 | require.NoError(t, collr.Init(context.Background())) |
| 62 | assert.NoError(t, collr.Check(context.Background())) |
| 63 | } |
| 64 | |
| 65 | func TestCollector_CheckNG(t *testing.T) { |
| 66 | collr := New() |
| 67 | |
| 68 | collr.URL = "http://127.0.0.1:38001/us" |
| 69 | require.NoError(t, collr.Init(context.Background())) |
| 70 | assert.Error(t, collr.Check(context.Background())) |
| 71 | } |
| 72 | |
| 73 | func TestCollector_Charts(t *testing.T) { |
| 74 | assert.NotNil(t, New().Charts()) |
| 75 | } |
| 76 | |
| 77 | func TestCollector_Collect(t *testing.T) { |
| 78 | ts := httptest.NewServer( |
| 79 | http.HandlerFunc( |
| 80 | func(w http.ResponseWriter, r *http.Request) { |
| 81 | _, _ = w.Write(dataStatusMetrics) |
| 82 | })) |
| 83 | defer ts.Close() |
| 84 | |
| 85 | collr := New() |
| 86 | collr.URL = ts.URL |
| 87 | require.NoError(t, collr.Init(context.Background())) |
| 88 | require.NoError(t, collr.Check(context.Background())) |
| 89 | |
| 90 | expected := map[string]int64{ |
| 91 | "accepts": 36, |
| 92 | "active": 1, |
| 93 | "handled": 36, |
| 94 | "reading": 0, |
| 95 | "requests": 126, |
| 96 | "waiting": 0, |
| 97 | "writing": 1, |
| 98 | } |
| 99 | |
| 100 | assert.Equal(t, expected, collr.Collect(context.Background())) |
| 101 | } |
| 102 | |
| 103 | func TestCollector_CollectTengine(t *testing.T) { |
| 104 | ts := httptest.NewServer( |
| 105 | http.HandlerFunc( |
| 106 | func(w http.ResponseWriter, r *http.Request) { |
| 107 | _, _ = w.Write(dataTengineStatusMetrics) |
| 108 | })) |
| 109 | defer ts.Close() |
| 110 | |
| 111 | collr := New() |
| 112 | collr.URL = ts.URL |
| 113 | require.NoError(t, collr.Init(context.Background())) |
| 114 | require.NoError(t, collr.Check(context.Background())) |
| 115 | |
| 116 | expected := map[string]int64{ |
| 117 | "accepts": 1140, |
| 118 | "active": 1, |
| 119 | "handled": 1140, |
| 120 | "reading": 0, |
| 121 | "request_time": 75806, |
| 122 | "requests": 1140, |
| 123 | "waiting": 0, |
| 124 | "writing": 1, |
| 125 | } |
| 126 | |
| 127 | assert.Equal(t, expected, collr.Collect(context.Background())) |
| 128 | } |
| 129 | |
| 130 | func TestCollector_InvalidData(t *testing.T) { |
| 131 | ts := httptest.NewServer( |
| 132 | http.HandlerFunc( |
| 133 | func(w http.ResponseWriter, r *http.Request) { |
| 134 | _, _ = w.Write([]byte("hello and goodbye")) |
| 135 | })) |
| 136 | defer ts.Close() |
| 137 | |
| 138 | collr := New() |
| 139 | collr.URL = ts.URL |
| 140 | require.NoError(t, collr.Init(context.Background())) |
| 141 | assert.Error(t, collr.Check(context.Background())) |
| 142 | } |
| 143 | |
| 144 | func TestCollector_404(t *testing.T) { |
| 145 | ts := httptest.NewServer( |
| 146 | http.HandlerFunc( |
| 147 | func(w http.ResponseWriter, r *http.Request) { |
| 148 | w.WriteHeader(http.StatusNotFound) |
| 149 | })) |
| 150 | defer ts.Close() |
| 151 | |
| 152 | collr := New() |
| 153 | collr.URL = ts.URL |
| 154 | require.NoError(t, collr.Init(context.Background())) |
| 155 | assert.Error(t, collr.Check(context.Background())) |
| 156 | } |