github.com/netdata/go.d.plugin@v0.58.1/modules/phpfpm/phpfpm_test.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package phpfpm
     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  var (
    17  	testStatusJSON, _           = os.ReadFile("testdata/status.json")
    18  	testStatusFullJSON, _       = os.ReadFile("testdata/status-full.json")
    19  	testStatusFullNoIdleJSON, _ = os.ReadFile("testdata/status-full-no-idle.json")
    20  	testStatusText, _           = os.ReadFile("testdata/status.txt")
    21  	testStatusFullText, _       = os.ReadFile("testdata/status-full.txt")
    22  )
    23  
    24  func Test_readTestData(t *testing.T) {
    25  	assert.NotNil(t, testStatusJSON)
    26  	assert.NotNil(t, testStatusFullJSON)
    27  	assert.NotNil(t, testStatusFullNoIdleJSON)
    28  	assert.NotNil(t, testStatusText)
    29  	assert.NotNil(t, testStatusFullText)
    30  }
    31  
    32  func TestNew(t *testing.T) {
    33  	job := New()
    34  
    35  	assert.Implements(t, (*module.Module)(nil), job)
    36  }
    37  
    38  func TestPhpfpm_Init(t *testing.T) {
    39  	job := New()
    40  
    41  	got := job.Init()
    42  
    43  	require.True(t, got)
    44  	assert.NotNil(t, job.client)
    45  }
    46  
    47  func TestPhpfpm_Check(t *testing.T) {
    48  	ts := httptest.NewServer(
    49  		http.HandlerFunc(
    50  			func(w http.ResponseWriter, r *http.Request) {
    51  				_, _ = w.Write(testStatusText)
    52  			}))
    53  	defer ts.Close()
    54  
    55  	job := New()
    56  	job.URL = ts.URL
    57  	job.Init()
    58  	require.True(t, job.Init())
    59  
    60  	got := job.Check()
    61  
    62  	assert.True(t, got)
    63  }
    64  
    65  func TestPhpfpm_CheckReturnsFalseOnFailure(t *testing.T) {
    66  	job := New()
    67  	job.URL = "http://127.0.0.1:38001/us"
    68  	require.True(t, job.Init())
    69  
    70  	got := job.Check()
    71  
    72  	assert.False(t, got)
    73  }
    74  
    75  func TestPhpfpm_Charts(t *testing.T) {
    76  	job := New()
    77  
    78  	got := job.Charts()
    79  
    80  	assert.NotNil(t, got)
    81  }
    82  
    83  func TestPhpfpm_CollectJSON(t *testing.T) {
    84  	ts := httptest.NewServer(
    85  		http.HandlerFunc(
    86  			func(w http.ResponseWriter, r *http.Request) {
    87  				_, _ = w.Write(testStatusJSON)
    88  			}))
    89  	defer ts.Close()
    90  
    91  	job := New()
    92  	job.URL = ts.URL + "/?json"
    93  	require.True(t, job.Init())
    94  
    95  	got := job.Collect()
    96  
    97  	want := map[string]int64{
    98  		"active":    1,
    99  		"idle":      1,
   100  		"maxActive": 1,
   101  		"reached":   0,
   102  		"requests":  21,
   103  		"slow":      0,
   104  	}
   105  	assert.Equal(t, want, got)
   106  }
   107  
   108  func TestPhpfpm_CollectJSONFull(t *testing.T) {
   109  	ts := httptest.NewServer(
   110  		http.HandlerFunc(
   111  			func(w http.ResponseWriter, r *http.Request) {
   112  				_, _ = w.Write(testStatusFullJSON)
   113  			}))
   114  	defer ts.Close()
   115  
   116  	job := New()
   117  	job.URL = ts.URL + "/?json"
   118  	require.True(t, job.Init())
   119  
   120  	got := job.Collect()
   121  
   122  	want := map[string]int64{
   123  		"active":    1,
   124  		"idle":      1,
   125  		"maxActive": 1,
   126  		"reached":   0,
   127  		"requests":  22,
   128  		"slow":      0,
   129  		"minReqCpu": 0,
   130  		"maxReqCpu": 10,
   131  		"avgReqCpu": 5,
   132  		"minReqDur": 0,
   133  		"maxReqDur": 919,
   134  		"avgReqDur": 459,
   135  		"minReqMem": 2093045,
   136  		"maxReqMem": 2097152,
   137  		"avgReqMem": 2095098,
   138  	}
   139  	assert.Equal(t, want, got)
   140  }
   141  
   142  func TestPhpfpm_CollectNoIdleProcessesJSONFull(t *testing.T) {
   143  	ts := httptest.NewServer(
   144  		http.HandlerFunc(
   145  			func(w http.ResponseWriter, r *http.Request) {
   146  				_, _ = w.Write(testStatusFullNoIdleJSON)
   147  			}))
   148  	defer ts.Close()
   149  
   150  	job := New()
   151  	job.URL = ts.URL + "/?json"
   152  	require.True(t, job.Init())
   153  
   154  	got := job.Collect()
   155  
   156  	want := map[string]int64{
   157  		"active":    1,
   158  		"idle":      1,
   159  		"maxActive": 1,
   160  		"reached":   0,
   161  		"requests":  22,
   162  		"slow":      0,
   163  	}
   164  	assert.Equal(t, want, got)
   165  }
   166  
   167  func TestPhpfpm_CollectText(t *testing.T) {
   168  	ts := httptest.NewServer(
   169  		http.HandlerFunc(
   170  			func(w http.ResponseWriter, r *http.Request) {
   171  				_, _ = w.Write(testStatusText)
   172  			}))
   173  	defer ts.Close()
   174  
   175  	job := New()
   176  	job.URL = ts.URL
   177  	require.True(t, job.Init())
   178  
   179  	got := job.Collect()
   180  
   181  	want := map[string]int64{
   182  		"active":    1,
   183  		"idle":      1,
   184  		"maxActive": 1,
   185  		"reached":   0,
   186  		"requests":  19,
   187  		"slow":      0,
   188  	}
   189  	assert.Equal(t, want, got)
   190  }
   191  
   192  func TestPhpfpm_CollectTextFull(t *testing.T) {
   193  	ts := httptest.NewServer(
   194  		http.HandlerFunc(
   195  			func(w http.ResponseWriter, r *http.Request) {
   196  				_, _ = w.Write(testStatusFullText)
   197  			}))
   198  	defer ts.Close()
   199  
   200  	job := New()
   201  	job.URL = ts.URL
   202  	require.True(t, job.Init())
   203  
   204  	got := job.Collect()
   205  
   206  	want := map[string]int64{
   207  		"active":    1,
   208  		"idle":      1,
   209  		"maxActive": 1,
   210  		"reached":   0,
   211  		"requests":  20,
   212  		"slow":      0,
   213  		"minReqCpu": 0,
   214  		"maxReqCpu": 10,
   215  		"avgReqCpu": 5,
   216  		"minReqDur": 0,
   217  		"maxReqDur": 536,
   218  		"avgReqDur": 268,
   219  		"minReqMem": 2093045,
   220  		"maxReqMem": 2097152,
   221  		"avgReqMem": 2095098,
   222  	}
   223  	assert.Equal(t, want, got)
   224  }
   225  
   226  func TestPhpfpm_CollectReturnsNothingWhenInvalidData(t *testing.T) {
   227  	ts := httptest.NewServer(
   228  		http.HandlerFunc(
   229  			func(w http.ResponseWriter, r *http.Request) {
   230  				_, _ = w.Write([]byte("hello and goodbye\nfrom someone\nfoobar"))
   231  			}))
   232  	defer ts.Close()
   233  
   234  	job := New()
   235  	job.URL = ts.URL
   236  	require.True(t, job.Init())
   237  
   238  	got := job.Collect()
   239  
   240  	assert.Len(t, got, 0)
   241  }
   242  
   243  func TestPhpfpm_CollectReturnsNothingWhenEmptyData(t *testing.T) {
   244  	ts := httptest.NewServer(
   245  		http.HandlerFunc(
   246  			func(w http.ResponseWriter, r *http.Request) {
   247  				_, _ = w.Write([]byte{})
   248  			}))
   249  	defer ts.Close()
   250  
   251  	job := New()
   252  	job.URL = ts.URL
   253  	require.True(t, job.Init())
   254  
   255  	got := job.Collect()
   256  
   257  	assert.Len(t, got, 0)
   258  }
   259  
   260  func TestPhpfpm_CollectReturnsNothingWhenBadStatusCode(t *testing.T) {
   261  	ts := httptest.NewServer(
   262  		http.HandlerFunc(
   263  			func(w http.ResponseWriter, r *http.Request) {
   264  				w.WriteHeader(http.StatusNotFound)
   265  			}))
   266  	defer ts.Close()
   267  
   268  	job := New()
   269  	job.URL = ts.URL
   270  	require.True(t, job.Init())
   271  
   272  	got := job.Collect()
   273  
   274  	assert.Len(t, got, 0)
   275  }
   276  
   277  func TestPhpfpm_Cleanup(t *testing.T) {
   278  	New().Cleanup()
   279  }