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

     1  package mph2o
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestGraphDefinition(t *testing.T) {
    12  	var h2o H2OPlugin
    13  
    14  	graphdef := h2o.GraphDefinition()
    15  	if len(graphdef) != 17 {
    16  		t.Errorf("GetTempfilename: %d should be 17", len(graphdef))
    17  	}
    18  }
    19  
    20  func TestParse(t *testing.T) {
    21  	var h2o H2OPlugin
    22  	stub := `{
    23   "server-version": "2.3.0-DEV",
    24   "openssl-version": "LibreSSL 2.4.5",
    25   "current-time": "01/Dec/2017:08:18:16 +0000",
    26   "restart-time": "01/Dec/2017:08:13:28 +0000",
    27   "uptime": 288,
    28   "generation": null,
    29   "connections": 1,
    30   "max-connections": 1024,
    31   "listeners": 4,
    32   "worker-threads": 2,
    33   "num-sessions": 14,
    34   "requests": [
    35    {"host": "10.0.2.2", "user": null, "at": "20171201T081816.181999+0000", "method": "GET", "path": "/server-status/json", "query": "", "protocol": "HTTP/2", "referer": null, "user-agent": "curl/7.54.0", "connect-time": "0.065440", "request-header-time": "0", "request-body-time": "0", "request-total-time": "0", "process-time": null, "response-time": null, "connection-id": "13", "ssl.protocol-version": "TLSv1.2", "ssl.session-reused": "0", "ssl.cipher": "ECDHE-RSA-CHACHA20-POLY1305-OLD", "ssl.cipher-bits": "256", "ssl.session-ticket": null, "http1.request-index": null, "http2.stream-id": "1", "http2.priority.received.exclusive": "0", "http2.priority.received.parent": "0", "http2.priority.received.weight": "16", "http2.priority.actual.parent": "0", "http2.priority.actual.weight": "16", "authority": "localhost:8443"}
    36   ],
    37   "status-errors.400": 0,
    38   "status-errors.403": 0,
    39   "status-errors.404": 2,
    40   "status-errors.405": 0,
    41   "status-errors.416": 0,
    42   "status-errors.417": 0,
    43   "status-errors.500": 0,
    44   "status-errors.502": 0,
    45   "status-errors.503": 0,
    46   "http2-errors.protocol": 0, 
    47   "http2-errors.internal": 0, 
    48   "http2-errors.flow-control": 0, 
    49   "http2-errors.settings-timeout": 0, 
    50   "http2-errors.stream-closed": 0, 
    51   "http2-errors.frame-size": 0, 
    52   "http2-errors.refused-stream": 0, 
    53   "http2-errors.cancel": 0, 
    54   "http2-errors.compression": 0, 
    55   "http2-errors.connect": 0, 
    56   "http2-errors.enhance-your-calm": 0, 
    57   "http2-errors.inadequate-security": 0, 
    58   "http2.read-closed": 3, 
    59   "http2.write-closed": 0
    60  ,
    61   "connect-time-0": 0,
    62   "connect-time-25": 0,
    63   "connect-time-50": 0,
    64   "connect-time-75": 0,
    65   "connect-time-99": 0
    66  , "header-time-0": 0,
    67   "header-time-25": 0,
    68   "header-time-50": 0,
    69   "header-time-75": 0,
    70   "header-time-99": 0
    71  , "body-time-0": 0,
    72   "body-time-25": 0,
    73   "body-time-50": 0,
    74   "body-time-75": 0,
    75   "body-time-99": 0
    76  , "request-total-time-0": 0,
    77   "request-total-time-25": 0,
    78   "request-total-time-50": 0,
    79   "request-total-time-75": 0,
    80   "request-total-time-99": 0
    81  , "process-time-0": 0,
    82   "process-time-25": 0,
    83   "process-time-50": 0,
    84   "process-time-75": 0,
    85   "process-time-99": 0
    86  , "response-time-0": 0,
    87   "response-time-25": 0,
    88   "response-time-50": 0,
    89   "response-time-75": 0,
    90   "response-time-99": 0
    91  , "duration-0": 0,
    92   "duration-25": 0,
    93   "duration-50": 0,
    94   "duration-75": 0,
    95   "duration-99": 0
    96  ,
    97   "requests": [
    98    {"host": "10.0.2.2", "user": null, "at": "20171201T081816.181999+0000", "method": "GET", "path": "/server-status/json", "query": "", "protocol": "HTTP/2", "referer": null, "user-agent": "curl/7.54.0", "connect-time": "0.065440", "request-header-time": "0", "request-body-time": "0", "request-total-time": "0", "process-time": null, "response-time": null, "connection-id": "13", "ssl.protocol-version": "TLSv1.2", "ssl.session-reused": "0", "ssl.cipher": "ECDHE-RSA-CHACHA20-POLY1305-OLD", "ssl.cipher-bits": "256", "ssl.session-ticket": null, "http1.request-index": null, "http2.stream-id": "1", "http2.priority.received.exclusive": "0", "http2.priority.received.parent": "0", "http2.priority.received.weight": "16", "http2.priority.actual.parent": "0", "http2.priority.actual.weight": "16", "authority": "localhost:8443"}
    99   ],
   100   "status-errors.400": 0,
   101   "status-errors.403": 0,
   102   "status-errors.404": 2,
   103   "status-errors.405": 0,
   104   "status-errors.416": 0,
   105   "status-errors.417": 0,
   106   "status-errors.500": 0,
   107   "status-errors.502": 0,
   108   "status-errors.503": 0,
   109   "http2-errors.protocol": 0, 
   110   "http2-errors.internal": 0, 
   111   "http2-errors.flow-control": 0, 
   112   "http2-errors.settings-timeout": 0, 
   113   "http2-errors.stream-closed": 0, 
   114   "http2-errors.frame-size": 0, 
   115   "http2-errors.refused-stream": 0, 
   116   "http2-errors.cancel": 0, 
   117   "http2-errors.compression": 0, 
   118   "http2-errors.connect": 0, 
   119   "http2-errors.enhance-your-calm": 0, 
   120   "http2-errors.inadequate-security": 0, 
   121   "http2.read-closed": 3, 
   122   "http2.write-closed": 0
   123  ,
   124   "connect-time-0": 0,
   125   "connect-time-25": 0,
   126   "connect-time-50": 0,
   127   "connect-time-75": 0,
   128   "connect-time-99": 0
   129  , "header-time-0": 0,
   130   "header-time-25": 0,
   131   "header-time-50": 0,
   132   "header-time-75": 0,
   133   "header-time-99": 0
   134  , "body-time-0": 0,
   135   "body-time-25": 0,
   136   "body-time-50": 0,
   137   "body-time-75": 0,
   138   "body-time-99": 0
   139  , "request-total-time-0": 0,
   140   "request-total-time-25": 0,
   141   "request-total-time-50": 0,
   142   "request-total-time-75": 0,
   143   "request-total-time-99": 0
   144  , "process-time-0": 0,
   145   "process-time-25": 0,
   146   "process-time-50": 0,
   147   "process-time-75": 0,
   148   "process-time-99": 0
   149  , "response-time-0": 0,
   150   "response-time-25": 0,
   151   "response-time-50": 0,
   152   "response-time-75": 0,
   153   "response-time-99": 0
   154  , "duration-0": 0,
   155   "duration-25": 0,
   156   "duration-50": 0,
   157   "duration-75": 0,
   158   "duration-99": 0
   159  }`
   160  
   161  	h2oStats := strings.NewReader(stub)
   162  
   163  	stat, err := h2o.parseStats(h2oStats)
   164  	fmt.Println(stat)
   165  	assert.Nil(t, err)
   166  	assert.EqualValues(t, 288, stat["uptime"])
   167  	assert.EqualValues(t, 1, stat["requests"])
   168  	assert.EqualValues(t, 1, stat["connections"])
   169  	assert.EqualValues(t, 2, stat["status_errors_404"])
   170  	assert.EqualValues(t, 3, stat["http2_read_closed"])
   171  	assert.EqualValues(t, 0, stat["connect_time_25"])
   172  }