| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package phpdaemon |
| 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 | dataFullStatusMetrics, _ = os.ReadFile("testdata/fullstatus.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 | "dataFullStatusMetrics": dataFullStatusMetrics, |
| 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 | collr := New() |
| 41 | |
| 42 | require.NoError(t, collr.Init(context.Background())) |
| 43 | } |
| 44 | |
| 45 | func TestCollector_Check(t *testing.T) { |
| 46 | ts := httptest.NewServer( |
| 47 | http.HandlerFunc( |
| 48 | func(w http.ResponseWriter, r *http.Request) { |
| 49 | _, _ = w.Write(dataFullStatusMetrics) |
| 50 | })) |
| 51 | defer ts.Close() |
| 52 | |
| 53 | collr := New() |
| 54 | collr.URL = ts.URL |
| 55 | require.NoError(t, collr.Init(context.Background())) |
| 56 | assert.NoError(t, collr.Check(context.Background())) |
| 57 | } |
| 58 | |
| 59 | func TestCollector_CheckNG(t *testing.T) { |
| 60 | collr := New() |
| 61 | collr.URL = "http://127.0.0.1:38001" |
| 62 | require.NoError(t, collr.Init(context.Background())) |
| 63 | assert.Error(t, collr.Check(context.Background())) |
| 64 | } |
| 65 | |
| 66 | func TestCollector_Charts(t *testing.T) { |
| 67 | collr := New() |
| 68 | |
| 69 | assert.NotNil(t, collr.Charts()) |
| 70 | assert.False(t, collr.charts.Has(uptimeChart.ID)) |
| 71 | |
| 72 | ts := httptest.NewServer( |
| 73 | http.HandlerFunc( |
| 74 | func(w http.ResponseWriter, r *http.Request) { |
| 75 | _, _ = w.Write(dataFullStatusMetrics) |
| 76 | })) |
| 77 | defer ts.Close() |
| 78 | |
| 79 | collr.URL = ts.URL |
| 80 | require.NoError(t, collr.Init(context.Background())) |
| 81 | assert.NoError(t, collr.Check(context.Background())) |
| 82 | assert.True(t, collr.charts.Has(uptimeChart.ID)) |
| 83 | } |
| 84 | |
| 85 | func TestCollector_Cleanup(t *testing.T) { |
| 86 | assert.NotPanics(t, func() { New().Cleanup(context.Background()) }) |
| 87 | } |
| 88 | |
| 89 | func TestCollector_Collect(t *testing.T) { |
| 90 | ts := httptest.NewServer( |
| 91 | http.HandlerFunc( |
| 92 | func(w http.ResponseWriter, r *http.Request) { |
| 93 | _, _ = w.Write(dataFullStatusMetrics) |
| 94 | })) |
| 95 | defer ts.Close() |
| 96 | |
| 97 | collr := New() |
| 98 | collr.URL = ts.URL |
| 99 | require.NoError(t, collr.Init(context.Background())) |
| 100 | assert.NoError(t, collr.Check(context.Background())) |
| 101 | |
| 102 | expected := map[string]int64{ |
| 103 | "alive": 350, |
| 104 | "busy": 200, |
| 105 | "idle": 50, |
| 106 | "init": 20, |
| 107 | "initialized": 10, |
| 108 | "preinit": 20, |
| 109 | "reloading": 100, |
| 110 | "shutdown": 500, |
| 111 | "uptime": 15765, |
| 112 | } |
| 113 | |
| 114 | assert.Equal(t, expected, collr.Collect(context.Background())) |
| 115 | |
| 116 | } |
| 117 | |
| 118 | func TestCollector_InvalidData(t *testing.T) { |
| 119 | ts := httptest.NewServer( |
| 120 | http.HandlerFunc( |
| 121 | func(w http.ResponseWriter, r *http.Request) { |
| 122 | _, _ = w.Write([]byte("hello and goodbye")) |
| 123 | })) |
| 124 | defer ts.Close() |
| 125 | |
| 126 | collr := New() |
| 127 | collr.URL = ts.URL |
| 128 | require.NoError(t, collr.Init(context.Background())) |
| 129 | assert.Error(t, collr.Check(context.Background())) |
| 130 | } |
| 131 | |
| 132 | func TestCollector_404(t *testing.T) { |
| 133 | ts := httptest.NewServer( |
| 134 | http.HandlerFunc( |
| 135 | func(w http.ResponseWriter, r *http.Request) { |
| 136 | w.WriteHeader(http.StatusNotFound) |
| 137 | })) |
| 138 | defer ts.Close() |
| 139 | |
| 140 | collr := New() |
| 141 | collr.URL = ts.URL |
| 142 | require.NoError(t, collr.Init(context.Background())) |
| 143 | assert.Error(t, collr.Check(context.Background())) |
| 144 | } |