// Test response to empty string as response objectgT func TestHttpJsonEmptyResponse(t *testing.T) { httpjson := genMockHttpJson(empty, 200) var acc testutil.Accumulator err := httpjson[0].Gather(&acc) assert.NotNil(t, err) assert.Equal(t, 0, acc.NFields()) }
// Test response to malformed JSON func TestHttpJsonBadJson(t *testing.T) { httpjson := genMockHttpJson(invalidJSON, 200) var acc testutil.Accumulator err := httpjson[0].Gather(&acc) assert.NotNil(t, err) assert.Equal(t, 0, acc.NFields()) }
// Test response to HTTP 405 func TestHttpJsonBadMethod(t *testing.T) { httpjson := genMockHttpJson(validJSON, 200) httpjson[0].Method = "NOT_A_REAL_METHOD" var acc testutil.Accumulator err := httpjson[0].Gather(&acc) assert.NotNil(t, err) assert.Equal(t, 0, acc.NFields()) }
func TestCommandError(t *testing.T) { e := &Exec{ runner: newRunnerMock(nil, fmt.Errorf("exit status code 1")), Command: "badcommand", } var acc testutil.Accumulator err := e.Gather(&acc) require.Error(t, err) assert.Equal(t, acc.NFields(), 0, "No new points should have been added") }
func TestExecMalformed(t *testing.T) { e := &Exec{ runner: newRunnerMock([]byte(malformedJson), nil), Command: "badcommand arg1", } var acc testutil.Accumulator err := e.Gather(&acc) require.Error(t, err) assert.Equal(t, acc.NFields(), 0, "No new points should have been added") }
// Test that the proper values are ignored or collected func TestHttpJson200(t *testing.T) { httpjson := genMockHttpJson(validJSON, 200) for _, service := range httpjson { var acc testutil.Accumulator err := service.Gather(&acc) require.NoError(t, err) assert.Equal(t, 4, acc.NFields()) for _, srv := range service.Servers { tags := map[string]string{"server": srv} mname := "httpjson_" + service.Name acc.AssertContainsTaggedFields(t, mname, expectedFields, tags) } } }
// Test that the proper values are ignored or collected func TestHttpJson200Tags(t *testing.T) { httpjson := genMockHttpJson(validJSONTags, 200) for _, service := range httpjson { if service.Name == "other_webapp" { var acc testutil.Accumulator err := service.Gather(&acc) require.NoError(t, err) assert.Equal(t, 2, acc.NFields()) for _, srv := range service.Servers { tags := map[string]string{"server": srv, "role": "master", "build": "123"} fields := map[string]interface{}{"value": float64(15)} mname := "httpjson_" + service.Name acc.AssertContainsTaggedFields(t, mname, fields, tags) } } } }
func TestExec(t *testing.T) { e := &Exec{ runner: newRunnerMock([]byte(validJson), nil), Command: "testcommand arg1", } var acc testutil.Accumulator err := e.Gather(&acc) require.NoError(t, err) assert.Equal(t, acc.NFields(), 4, "non-numeric measurements should be ignored") fields := map[string]interface{}{ "num_processes": float64(82), "cpu_used": float64(8234), "cpu_free": float64(32), "percent": float64(0.81), } acc.AssertContainsFields(t, "exec", fields) }
func TestDiskStats(t *testing.T) { var mps MockPS defer mps.AssertExpectations(t) var acc testutil.Accumulator var err error du := []*disk.DiskUsageStat{ { Path: "/", Fstype: "ext4", Total: 128, Free: 23, InodesTotal: 1234, InodesFree: 234, }, { Path: "/home", Fstype: "ext4", Total: 256, Free: 46, InodesTotal: 2468, InodesFree: 468, }, } mps.On("DiskUsage").Return(du, nil) err = (&DiskStats{ps: &mps}).Gather(&acc) require.NoError(t, err) numDiskPoints := acc.NFields() expectedAllDiskPoints := 12 assert.Equal(t, expectedAllDiskPoints, numDiskPoints) tags1 := map[string]string{ "path": "/", "fstype": "ext4", } tags2 := map[string]string{ "path": "/home", "fstype": "ext4", } fields1 := map[string]interface{}{ "total": uint64(128), //tags1) "used": uint64(105), //tags1) "free": uint64(23), //tags1) "inodes_total": uint64(1234), //tags1) "inodes_free": uint64(234), //tags1) "inodes_used": uint64(1000), //tags1) } fields2 := map[string]interface{}{ "total": uint64(256), //tags2) "used": uint64(210), //tags2) "free": uint64(46), //tags2) "inodes_total": uint64(2468), //tags2) "inodes_free": uint64(468), //tags2) "inodes_used": uint64(2000), //tags2) } acc.AssertContainsTaggedFields(t, "disk", fields1, tags1) acc.AssertContainsTaggedFields(t, "disk", fields2, tags2) // We expect 6 more DiskPoints to show up with an explicit match on "/" // and /home not matching the /dev in Mountpoints err = (&DiskStats{ps: &mps, Mountpoints: []string{"/", "/dev"}}).Gather(&acc) assert.Equal(t, expectedAllDiskPoints+6, acc.NFields()) // We should see all the diskpoints as Mountpoints includes both // / and /home err = (&DiskStats{ps: &mps, Mountpoints: []string{"/", "/home"}}).Gather(&acc) assert.Equal(t, 2*expectedAllDiskPoints+6, acc.NFields()) }