github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-plack/lib/plack_test.go (about)

     1  package mpplack
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestGraphDefinition(t *testing.T) {
    13  	var plack PlackPlugin
    14  
    15  	graphdef := plack.GraphDefinition()
    16  	if len(graphdef) != 3 {
    17  		t.Errorf("GetTempfilename: %d should be 3", len(graphdef))
    18  	}
    19  }
    20  
    21  func TestParse(t *testing.T) {
    22  	var plack PlackPlugin
    23  	stub := `{
    24    "Uptime": "1410520211",
    25    "TotalAccesses": "2",
    26    "IdleWorkers": "2",
    27    "TotalKbytes": "5",
    28    "BusyWorkers": "1",
    29    "stats": [
    30      {
    31        "pid": 11062,
    32        "method": "GET",
    33        "ss": 51,
    34        "remote_addr": "127.0.0.1",
    35        "host": "localhost:8000",
    36        "protocol": "HTTP/1.1",
    37        "status": "_",
    38        "uri": "/server-status?json"
    39      },
    40      {
    41        "ss": 41,
    42        "remote_addr": "127.0.0.1",
    43        "host": "localhost:8000",
    44        "protocol": "HTTP/1.1",
    45        "pid": 11063,
    46        "method": "GET",
    47        "status": "_",
    48        "uri": "/server-status?json"
    49      },
    50      {
    51        "ss": 0,
    52        "remote_addr": "127.0.0.1",
    53        "host": "localhost:8000",
    54        "protocol": "HTTP/1.1",
    55        "pid": 11064,
    56        "method": "GET",
    57        "status": "A",
    58        "uri": "/server-status?json"
    59      }
    60    ]
    61  }
    62  `
    63  
    64  	plackStats := bytes.NewBufferString(stub)
    65  
    66  	stat, err := plack.parseStats(plackStats)
    67  	fmt.Println(stat)
    68  	assert.Nil(t, err)
    69  	assert.EqualValues(t, reflect.TypeOf(stat["requests"]).String(), "uint64")
    70  	assert.EqualValues(t, stat["requests"], 2)
    71  	assert.EqualValues(t, reflect.TypeOf(stat["bytes_sent"]).String(), "uint64")
    72  	assert.EqualValues(t, stat["bytes_sent"], 5)
    73  
    74  	stubWithIntUptime := `
    75  {"TotalKbytes":"36","IdleWorkers":"0","BusyWorkers":"0","TotalAccesses":"670","stats":[],"Uptime":1474047568}
    76  `
    77  
    78  	plackStatsWithIntUptime := bytes.NewBufferString(stubWithIntUptime)
    79  
    80  	statWithIntUptime, err := plack.parseStats(plackStatsWithIntUptime)
    81  	fmt.Println(statWithIntUptime)
    82  	assert.Nil(t, err)
    83  }
    84  
    85  func TestParseWithInsufficientResponse(t *testing.T) {
    86  	var plack PlackPlugin
    87  	stub := `
    88  {"TotalKbytes":"36","IdleWorkers":"","BusyWorkers":"3","TotalAccesses":"670","stats":[],"Uptime":1474047568}
    89  `
    90  	plackStats := bytes.NewBufferString(stub)
    91  
    92  	stat, err := plack.parseStats(plackStats)
    93  	fmt.Println(stat)
    94  	assert.Nil(t, err)
    95  	assert.EqualValues(t, stat["bytes_sent"], 36)
    96  	assert.EqualValues(t, stat["busy_workers"], 3)
    97  	assert.EqualValues(t, stat["requests"], uint(670))
    98  	assert.Nil(t, stat["idle_workers"])
    99  }
   100  
   101  func TestParseWithNumberResponse(t *testing.T) {
   102  	var plack PlackPlugin
   103  	stub := `
   104  {"TotalKbytes":36,"IdleWorkers":5,"BusyWorkers":3,"TotalAccesses":670,"stats":[],"Uptime":"1474047568"}
   105  `
   106  	plackStats := bytes.NewBufferString(stub)
   107  
   108  	stat, err := plack.parseStats(plackStats)
   109  	fmt.Println(stat)
   110  	assert.Nil(t, err)
   111  	assert.EqualValues(t, stat["bytes_sent"], 36)
   112  	assert.EqualValues(t, stat["busy_workers"], 3)
   113  	assert.EqualValues(t, stat["idle_workers"], 5)
   114  	assert.EqualValues(t, stat["requests"], uint(670))
   115  }