2015-11-02 11:09:53 +00:00
|
|
|
package jolokia
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-11-02 11:09:53 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
_ "github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const validMultiValueJSON = `
|
|
|
|
{
|
|
|
|
"request":{
|
|
|
|
"mbean":"java.lang:type=Memory",
|
|
|
|
"attribute":"HeapMemoryUsage",
|
|
|
|
"type":"read"
|
|
|
|
},
|
|
|
|
"value":{
|
|
|
|
"init":67108864,
|
|
|
|
"committed":456130560,
|
|
|
|
"max":477626368,
|
|
|
|
"used":203288528
|
|
|
|
},
|
|
|
|
"timestamp":1446129191,
|
|
|
|
"status":200
|
|
|
|
}`
|
|
|
|
|
|
|
|
const validSingleValueJSON = `
|
|
|
|
{
|
|
|
|
"request":{
|
|
|
|
"path":"used",
|
|
|
|
"mbean":"java.lang:type=Memory",
|
|
|
|
"attribute":"HeapMemoryUsage",
|
|
|
|
"type":"read"
|
|
|
|
},
|
|
|
|
"value":209274376,
|
|
|
|
"timestamp":1446129256,
|
|
|
|
"status":200
|
|
|
|
}`
|
|
|
|
|
|
|
|
const invalidJSON = "I don't think this is JSON"
|
|
|
|
|
|
|
|
const empty = ""
|
|
|
|
|
|
|
|
var Servers = []Server{Server{Name: "as1", Host: "127.0.0.1", Port: "8080"}}
|
2016-04-14 21:00:41 +00:00
|
|
|
var HeapMetric = Metric{Name: "heap_memory_usage",
|
2016-04-14 22:49:06 +00:00
|
|
|
Mbean: "java.lang:type=Memory", Attribute: "HeapMemoryUsage"}
|
2016-04-14 21:00:41 +00:00
|
|
|
var UsedHeapMetric = Metric{Name: "heap_memory_usage",
|
|
|
|
Mbean: "java.lang:type=Memory", Attribute: "HeapMemoryUsage"}
|
2015-11-02 11:09:53 +00:00
|
|
|
|
|
|
|
type jolokiaClientStub struct {
|
|
|
|
responseBody string
|
|
|
|
statusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c jolokiaClientStub) MakeRequest(req *http.Request) (*http.Response, error) {
|
|
|
|
resp := http.Response{}
|
|
|
|
resp.StatusCode = c.statusCode
|
|
|
|
resp.Body = ioutil.NopCloser(strings.NewReader(c.responseBody))
|
|
|
|
return &resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generates a pointer to an HttpJson object that uses a mock HTTP client.
|
|
|
|
// Parameters:
|
|
|
|
// response : Body of the response that the mock HTTP client should return
|
|
|
|
// statusCode: HTTP status code the mock HTTP client should return
|
|
|
|
//
|
|
|
|
// Returns:
|
|
|
|
// *HttpJson: Pointer to an HttpJson object that uses the generated mock HTTP client
|
|
|
|
func genJolokiaClientStub(response string, statusCode int, servers []Server, metrics []Metric) *Jolokia {
|
|
|
|
return &Jolokia{
|
|
|
|
jClient: jolokiaClientStub{responseBody: response, statusCode: statusCode},
|
|
|
|
Servers: servers,
|
|
|
|
Metrics: metrics,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the proper values are ignored or collected
|
|
|
|
func TestHttpJsonMultiValue(t *testing.T) {
|
|
|
|
jolokia := genJolokiaClientStub(validMultiValueJSON, 200, Servers, []Metric{HeapMetric})
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := jolokia.Gather(&acc)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
2016-01-27 23:15:14 +00:00
|
|
|
assert.Equal(t, 1, len(acc.Metrics))
|
2015-11-02 11:09:53 +00:00
|
|
|
|
2016-01-06 23:55:28 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"heap_memory_usage_init": 67108864.0,
|
|
|
|
"heap_memory_usage_committed": 456130560.0,
|
|
|
|
"heap_memory_usage_max": 477626368.0,
|
|
|
|
"heap_memory_usage_used": 203288528.0,
|
|
|
|
}
|
|
|
|
tags := map[string]string{
|
2016-04-18 20:50:58 +00:00
|
|
|
"server_host": "127.0.0.1",
|
|
|
|
"server_port": "8080",
|
|
|
|
"server_name": "as1",
|
2016-01-06 23:55:28 +00:00
|
|
|
}
|
|
|
|
acc.AssertContainsTaggedFields(t, "jolokia", fields, tags)
|
2015-11-02 11:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the proper values are ignored or collected
|
|
|
|
func TestHttpJsonOn404(t *testing.T) {
|
|
|
|
|
2016-01-06 23:55:28 +00:00
|
|
|
jolokia := genJolokiaClientStub(validMultiValueJSON, 404, Servers,
|
|
|
|
[]Metric{UsedHeapMetric})
|
2015-11-02 11:09:53 +00:00
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2016-01-06 23:55:28 +00:00
|
|
|
acc.SetDebug(true)
|
2015-11-02 11:09:53 +00:00
|
|
|
err := jolokia.Gather(&acc)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
2016-01-27 23:15:14 +00:00
|
|
|
assert.Equal(t, 0, len(acc.Metrics))
|
2015-11-02 11:09:53 +00:00
|
|
|
}
|
2016-04-14 21:00:41 +00:00
|
|
|
|
|
|
|
// Test that the proper values are ignored or collected
|
|
|
|
func TestHttpInvalidJson(t *testing.T) {
|
|
|
|
|
|
|
|
jolokia := genJolokiaClientStub(invalidJSON, 200, Servers,
|
|
|
|
[]Metric{UsedHeapMetric})
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
acc.SetDebug(true)
|
|
|
|
err := jolokia.Gather(&acc)
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, 0, len(acc.Metrics))
|
|
|
|
}
|