github.com/netdata/go.d.plugin@v0.58.1/modules/phpdaemon/phpdaemon_test.go (about) 1 // SPDX-License-Identifier: GPL-3.0-or-later 2 3 package phpdaemon 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 const ( 17 testURL = "http://127.0.0.1:38001" 18 ) 19 20 var testFullStatusData, _ = os.ReadFile("testdata/fullstatus.json") 21 22 func Test_testData(t *testing.T) { 23 assert.NotEmpty(t, testFullStatusData) 24 } 25 26 func TestNew(t *testing.T) { 27 job := New() 28 29 assert.Implements(t, (*module.Module)(nil), job) 30 assert.Equal(t, defaultURL, job.URL) 31 assert.Equal(t, defaultHTTPTimeout, job.Timeout.Duration) 32 } 33 34 func TestPHPDaemon_Init(t *testing.T) { 35 job := New() 36 37 require.True(t, job.Init()) 38 assert.NotNil(t, job.client) 39 } 40 41 func TestPHPDaemon_Check(t *testing.T) { 42 ts := httptest.NewServer( 43 http.HandlerFunc( 44 func(w http.ResponseWriter, r *http.Request) { 45 _, _ = w.Write(testFullStatusData) 46 })) 47 defer ts.Close() 48 49 job := New() 50 job.URL = ts.URL 51 require.True(t, job.Init()) 52 assert.True(t, job.Check()) 53 } 54 55 func TestPHPDaemon_CheckNG(t *testing.T) { 56 job := New() 57 job.URL = testURL 58 require.True(t, job.Init()) 59 assert.False(t, job.Check()) 60 } 61 62 func TestPHPDaemon_Charts(t *testing.T) { 63 job := New() 64 65 assert.NotNil(t, job.Charts()) 66 assert.False(t, job.charts.Has(uptimeChart.ID)) 67 68 ts := httptest.NewServer( 69 http.HandlerFunc( 70 func(w http.ResponseWriter, r *http.Request) { 71 _, _ = w.Write(testFullStatusData) 72 })) 73 defer ts.Close() 74 75 job.URL = ts.URL 76 require.True(t, job.Init()) 77 assert.True(t, job.Check()) 78 assert.True(t, job.charts.Has(uptimeChart.ID)) 79 } 80 81 func TestPHPDaemon_Cleanup(t *testing.T) { 82 assert.NotPanics(t, New().Cleanup) 83 } 84 85 func TestPHPDaemon_Collect(t *testing.T) { 86 ts := httptest.NewServer( 87 http.HandlerFunc( 88 func(w http.ResponseWriter, r *http.Request) { 89 _, _ = w.Write(testFullStatusData) 90 })) 91 defer ts.Close() 92 93 job := New() 94 job.URL = ts.URL 95 require.True(t, job.Init()) 96 assert.True(t, job.Check()) 97 98 expected := map[string]int64{ 99 "alive": 350, 100 "busy": 200, 101 "idle": 50, 102 "init": 20, 103 "initialized": 10, 104 "preinit": 20, 105 "reloading": 100, 106 "shutdown": 500, 107 "uptime": 15765, 108 } 109 110 assert.Equal(t, expected, job.Collect()) 111 112 } 113 114 func TestPHPDaemon_InvalidData(t *testing.T) { 115 ts := httptest.NewServer( 116 http.HandlerFunc( 117 func(w http.ResponseWriter, r *http.Request) { 118 _, _ = w.Write([]byte("hello and goodbye")) 119 })) 120 defer ts.Close() 121 122 job := New() 123 job.URL = ts.URL 124 require.True(t, job.Init()) 125 assert.False(t, job.Check()) 126 } 127 128 func TestPHPDaemon_404(t *testing.T) { 129 ts := httptest.NewServer( 130 http.HandlerFunc( 131 func(w http.ResponseWriter, r *http.Request) { 132 w.WriteHeader(http.StatusNotFound) 133 })) 134 defer ts.Close() 135 136 job := New() 137 job.URL = ts.URL 138 require.True(t, job.Init()) 139 assert.False(t, job.Check()) 140 }