Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add nomad input plugin #10106

Merged
merged 14 commits into from
Dec 8, 2021
Prev Previous commit
Next Next commit
rewrite test completely
  • Loading branch information
efbar committed Nov 17, 2021
commit 2b67013fe2edd897f73880661f5dbac3111bfb22
194 changes: 88 additions & 106 deletions plugins/inputs/nomad/nomad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,126 +2,108 @@ package nomad

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

func TestNomadStats(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/v1/metrics" {
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintln(w, responseKeyMetrics)
require.NoError(t, err)
}
}))
defer ts.Close()

labelFilter, _ := filter.NewIncludeExcludeFilter([]string{"host"}, nil)

n := &Nomad{
URL: ts.URL,
labelFilter: labelFilter,
var applyTests = []struct {
name string
expected []telegraf.Metric
}{
{
name: "Metrics",
expected: []telegraf.Metric{
testutil.MustMetric(
"nomad.nomad.rpc.query",
map[string]string{
"host": "node1",
},
map[string]interface{}{
"count": int(7),
"max": float64(1),
"min": float64(1),
"mean": float64(1),
"rate": float64(0.7),
"sum": float64(7),
"sumsq": float64(0),
},
time.Unix(1636843140, 0),
1,
),
testutil.MustMetric(
"nomad.client.allocated.cpu",
map[string]string{
"node_scheduling_eligibility": "eligible",
"host": "node1",
"node_id": "2bbff078-8473-a9de-6c5e-42b4e053e12f",
"datacenter": "dc1",
"node_class": "none",
"node_status": "ready",
},
map[string]interface{}{
"value": float32(500),
},
time.Unix(1636843140, 0),
2,
),
testutil.MustMetric(
"nomad.memberlist.gossip",
map[string]string{
"host": "node1",
},
map[string]interface{}{
"count": int(20),
"max": float64(0.03747599944472313),
"mean": float64(0.013159099989570678),
"min": float64(0.003459000028669834),
"rate": float64(0.026318199979141355),
"stddev": float64(0.009523742715522742),
"sum": float64(0.26318199979141355),
"sumsq": float64(0),
},
time.Unix(1636843140, 0),
1,
),
},
},
}

var acc testutil.Accumulator
err := acc.GatherError(n.Gather)
require.NoError(t, err)
for _, tt := range applyTests {

srebhan marked this conversation as resolved.
Show resolved Hide resolved
fields := map[string]interface{}{
"value": float32(500),
}
tags := map[string]string{
"node_scheduling_eligibility": "eligible",
"host": "node1",
"node_id": "2bbff078-8473-a9de-6c5e-42b4e053e12f",
"datacenter": "dc1",
"node_class": "none",
"node_status": "ready",
}
acc.AssertContainsTaggedFields(t, "nomad.client.allocated.cpu", fields, tags)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/v1/metrics" {
w.WriteHeader(http.StatusOK)
responseKeyMetrics, _ := ioutil.ReadFile("testdata/response_key_metrics.json")
_, err := fmt.Fprintln(w, string(responseKeyMetrics))
require.NoError(t, err)
}
}))
defer ts.Close()
srebhan marked this conversation as resolved.
Show resolved Hide resolved

fields = map[string]interface{}{
"count": int(7),
"max": float64(1),
"min": float64(1),
"mean": float64(1),
"rate": float64(0.7),
"sum": float64(7),
"sumsq": float64(0),
}
tags = map[string]string{
"host": "node1",
}
acc.AssertContainsTaggedFields(t, "nomad.nomad.rpc.query", fields, tags)
plugin := &Nomad{
URL: ts.URL,
}

fields = map[string]interface{}{
"count": int(20),
"max": float64(0.03747599944472313),
"min": float64(0.003459000028669834),
"rate": float64(0.026318199979141355),
"sum": float64(0.26318199979141355),
"sumsq": float64(0),
"mean": float64(0.013159099989570678),
}
tags = map[string]string{
"host": "node1",
}
acc.AssertContainsTaggedFields(t, "nomad.memberlist.gossip", fields, tags)
t.Run(tt.name, func(t *testing.T) {

srebhan marked this conversation as resolved.
Show resolved Hide resolved
srebhan marked this conversation as resolved.
Show resolved Hide resolved
}
err := plugin.Init()
require.NoError(t, err)

var responseKeyMetrics = `
{
"Counters": [
{
"Count": 7,
"Labels": {
"host": "node1"
},
"Max": 1,
"Mean": 1,
"Min": 1,
"Name": "nomad.nomad.rpc.query",
"Rate": 0.7,
"Stddev": 0,
"Sum": 7
}
],
"Gauges": [
{
"Labels": {
"node_scheduling_eligibility": "eligible",
"host": "node1",
"node_id": "2bbff078-8473-a9de-6c5e-42b4e053e12f",
"datacenter": "dc1",
"node_class": "none",
"node_status": "ready"
},
"Name": "nomad.client.allocated.cpu",
"Value": 500
}
],
"Points" : [],
"Samples" : [
{
"Count": 20,
"Labels": {
"host": "node1"
},
"Max": 0.03747599944472313,
"Mean": 0.013159099989570678,
"Min": 0.003459000028669834,
"Name": "nomad.memberlist.gossip",
"Rate": 0.026318199979141355,
"Stddev": 0.009523742715522742,
"Sum": 0.26318199979141355
}
],
"Timestamp": "2021-11-13 22:39:00 +0000 UTC"
}
`
acc := testutil.Accumulator{}
err = plugin.Gather(&acc)
require.NoError(t, err)

testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics())

srebhan marked this conversation as resolved.
Show resolved Hide resolved
})
}
}