telegraf/plugins/outputs/appdynamics/appdynamics_test.go

104 lines
3.2 KiB
Go
Raw Normal View History

2016-05-13 05:29:27 +00:00
package appdynamics
2016-05-12 23:13:59 +00:00
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/stretchr/testify/assert"
)
2016-05-13 05:29:27 +00:00
// TestAppdynamicsError - attempt to initialize Appdynamics with invalid controller user name value
2016-05-12 23:13:59 +00:00
func TestAppdynamicsError(t *testing.T) {
a := Appdynamics{
ControllerTierURL: "https://foo.saas.appdynamics.com/controller/rest/applications/bar/tiers/baz?output=JSON",
ControllerUserName: "apiuser@foo.bar.com",
ControllerPassword: "pass123",
AgentURL: "http://localhost:8293/machineagent/metrics?name=Server|Component:%d|Custom+Metrics|",
}
2016-05-13 05:29:27 +00:00
2016-05-12 23:13:59 +00:00
assert.Error(t, a.Connect())
}
// TestAppdynamicsOK - successfully initialize Appdynamics and process metrics calls
func TestAppdynamicsOK(t *testing.T) {
// channel to collect received calls
ch := make(chan string, 1)
h := func(w http.ResponseWriter, r *http.Request) {
s := r.URL.String()
fmt.Fprintf(w, "Hi there, I love %s!", s)
ch <- r.URL.RawQuery
}
http.HandleFunc("/", h)
go http.ListenAndServe(":8293", nil)
time.Sleep(time.Millisecond * 100)
a := Appdynamics{
ControllerTierURL: "https://foo.saas.appdynamics.com/controller/rest/applications/bar/tiers/baz?output=JSON",
ControllerUserName: "apiuser@foo.bar",
ControllerPassword: "pass123",
AgentURL: "http://localhost:8293/machineagent/metrics?name=Server|Component:%d|Custom+Metrics|",
}
2016-05-13 05:29:27 +00:00
2016-05-12 23:13:59 +00:00
// this error is expected since we are not connecting to actual controller
assert.Error(t, a.Connect())
// reset agent url value with '123' tier id
a.AgentURL = fmt.Sprintf(a.AgentURL, 123)
assert.Equal(t, a.AgentURL, "http://localhost:8293/machineagent/metrics?name=Server|Component:123|Custom+Metrics|")
2016-05-13 05:29:27 +00:00
tm := time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)
2016-05-12 23:13:59 +00:00
// counter type - appd-type: sum
m, _ := telegraf.NewMetric(
"foo",
2016-05-13 05:29:27 +00:00
map[string]string{"metric_type": "counter"},
2016-05-12 23:13:59 +00:00
map[string]interface{}{"value": float64(1.23)},
2016-05-13 05:29:27 +00:00
tm,
2016-05-12 23:13:59 +00:00
)
metrics := []telegraf.Metric{m}
assert.NoError(t, a.Write(metrics))
call := <-ch
assert.Equal(t, "name=Server|Component:123|Custom+Metrics|foo&value=1.23&type=sum", call)
// gauge type - appd-type: average
m, _ = telegraf.NewMetric(
"foo",
map[string]string{"metric_type": "gauge"},
map[string]interface{}{"value": float64(4.56)},
2016-05-13 05:29:27 +00:00
tm,
2016-05-12 23:13:59 +00:00
)
metrics = []telegraf.Metric{m}
assert.NoError(t, a.Write(metrics))
call = <-ch
assert.Equal(t, "name=Server|Component:123|Custom+Metrics|foo&value=4.56&type=average", call)
// other type - defaults to appd-type: sum
m, _ = telegraf.NewMetric(
"foo",
2016-05-13 05:29:27 +00:00
map[string]string{"metric_type": "other"},
2016-05-12 23:13:59 +00:00
map[string]interface{}{"value": float64(7.89)},
2016-05-13 05:29:27 +00:00
tm,
2016-05-12 23:13:59 +00:00
)
metrics = []telegraf.Metric{m}
assert.NoError(t, a.Write(metrics))
call = <-ch
assert.Equal(t, "name=Server|Component:123|Custom+Metrics|foo&value=7.89&type=sum", call)
// invalid: missing value
m, _ = telegraf.NewMetric(
"foo",
map[string]string{"metric_type": "bar"},
map[string]interface{}{"values": float64(7.89)},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
metrics = []telegraf.Metric{m}
assert.NoError(t, a.Write(metrics))
select {
case call = <-ch:
t.Error("No messages expected, but got: ", call)
default:
}
}