github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-php-fpm/lib/php-fpm_test.go (about) 1 //go:build linux 2 3 package mpphpfpm 4 5 import ( 6 "testing" 7 8 "github.com/jarcoal/httpmock" 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestGetStatus(t *testing.T) { 14 httpmock.Activate() 15 defer httpmock.DeactivateAndReset() 16 17 jsonStr := `{ 18 "pool":"www", 19 "process manager":"dynamic", 20 "start time":1461398921, 21 "start since":1624, 22 "accepted conn":664, 23 "listen queue":1, 24 "max listen queue":3, 25 "listen queue len":2, 26 "idle processes":40, 27 "active processes":10, 28 "total processes":50, 29 "max active processes":100, 30 "max children reached":200, 31 "slow requests":1000 32 }` 33 34 httpmock.RegisterResponder("GET", "http://httpmock/status", 35 httpmock.NewStringResponder(200, jsonStr)) 36 37 p := PhpFpmPlugin{ 38 URL: "http://httpmock/status", 39 Prefix: "php-fpm", 40 Timeout: 5, 41 } 42 status, err := getStatus(p) 43 44 require.NoError(t, err) 45 assert.EqualValues(t, 50, status.TotalProcesses) 46 assert.EqualValues(t, 10, status.ActiveProcesses) 47 assert.EqualValues(t, 40, status.IdleProcesses) 48 assert.EqualValues(t, 100, status.MaxActiveProcesses) 49 assert.EqualValues(t, 200, status.MaxChildrenReached) 50 assert.EqualValues(t, 1, status.ListenQueue) 51 assert.EqualValues(t, 2, status.ListenQueueLen) 52 assert.EqualValues(t, 3, status.MaxListenQueue) 53 assert.EqualValues(t, 1000, status.SlowRequests) 54 assert.EqualValues(t, 0, status.MemoryPeak) 55 } 56 57 func TestGetStatus_MemoryPeak(t *testing.T) { 58 httpmock.Activate() 59 defer httpmock.DeactivateAndReset() 60 61 jsonStr := `{ 62 "pool":"www", 63 "process manager":"dynamic", 64 "start time":1461398921, 65 "start since":1624, 66 "accepted conn":664, 67 "listen queue":1, 68 "max listen queue":3, 69 "listen queue len":2, 70 "idle processes":40, 71 "active processes":10, 72 "total processes":50, 73 "max active processes":100, 74 "max children reached":200, 75 "slow requests":1000, 76 "memory peak":1280000 77 }` 78 79 httpmock.RegisterResponder("GET", "http://httpmock/status", 80 httpmock.NewStringResponder(200, jsonStr)) 81 82 p := PhpFpmPlugin{ 83 URL: "http://httpmock/status", 84 Prefix: "php-fpm", 85 Timeout: 5, 86 } 87 status, err := getStatus(p) 88 89 require.NoError(t, err) 90 assert.EqualValues(t, 1280000, status.MemoryPeak) 91 } 92 93 func TestSocketFlag_Set(t *testing.T) { 94 tests := []struct { 95 Name string 96 URL string 97 98 Network string 99 Address string 100 Err string 101 }{ 102 { 103 Name: "parse absolute filepath", 104 URL: "/dev/null", 105 Network: "unix", 106 Address: "/dev/null", 107 }, 108 { 109 Name: "parse relative filepath", 110 URL: "a/b/c", 111 Network: "unix", 112 Address: "a/b/c", 113 }, 114 { 115 Name: "parse filename", 116 URL: "file", 117 Network: "unix", 118 Address: "file", 119 }, 120 { 121 Name: "parse Go's address style", 122 URL: "localhost:9000", 123 Network: "tcp", 124 Address: "localhost:9000", 125 }, 126 { 127 Name: "parse ipv6 address", 128 URL: "[::1]:1000", 129 Network: "tcp", 130 Address: "[::1]:1000", 131 }, 132 { 133 Name: "parse unix:// scheme", 134 URL: "unix:///path/to", 135 Network: "unix", 136 Address: "/path/to", 137 }, 138 { 139 Name: "parse tcp:// scheme", 140 URL: "tcp://localhost", 141 Network: "tcp", 142 Address: "localhost:9000", 143 }, 144 { 145 Name: "parse tcp:// scheme hostport", 146 URL: "tcp://localhost:9000/", 147 Network: "tcp", 148 Address: "localhost:9000", 149 }, 150 { 151 Name: "parse error: empty scheme", 152 URL: "://test/", 153 Err: "parse", 154 }, 155 { 156 Name: "parse error: unknown scheme", 157 URL: "aaa://test/", 158 Err: "parse", 159 }, 160 { 161 Name: "parse error: syntax", 162 URL: ":@:", 163 Err: "parse", 164 }, 165 { 166 Name: "parse error: no host or port", 167 URL: "?aaa", 168 Err: "parse", 169 }, 170 } 171 for _, tt := range tests { 172 t.Run(tt.Name, func(t *testing.T) { 173 var p SocketFlag 174 err := p.Set(tt.URL) 175 assert.Equal(t, tt.Network, p.Network) 176 assert.Equal(t, tt.Address, p.Address) 177 if tt.Err == "" { 178 assert.NoError(t, err) 179 } else { 180 assert.Error(t, err, tt.Err) 181 } 182 }) 183 } 184 }