Skip to content

Commit

Permalink
Fix http and hddtemp.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Rebhan committed Apr 7, 2021
1 parent 455f252 commit fd5cc43
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 38 deletions.
47 changes: 11 additions & 36 deletions plugins/inputs/hddtemp/go-hddtemp/hddtemp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package hddtemp

import (
"net"
"reflect"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,10 +12,7 @@ func TestFetch(t *testing.T) {
defer l.Close()

disks, err := New().Fetch(l.Addr().String())

if err != nil {
t.Error("expecting err to be nil")
}
require.NoError(t, err)

expected := []Disk{
{
Expand All @@ -26,29 +22,20 @@ func TestFetch(t *testing.T) {
Unit: "C",
},
}

if !reflect.DeepEqual(expected, disks) {
t.Error("disks' slice is different from expected")
}
require.Equal(t, expected, disks, "disks' slice is different from expected")
}

func TestFetchWrongAddress(t *testing.T) {
_, err := New().Fetch("127.0.0.1:1")

if err == nil {
t.Error("expecting err to be non-nil")
}
require.Error(t, err)
}

func TestFetchStatus(t *testing.T) {
l := serve(t, []byte("|/dev/sda|foobar|SLP|C|"))
defer l.Close()

disks, err := New().Fetch(l.Addr().String())

if err != nil {
t.Error("expecting err to be nil")
}
require.NoError(t, err)

expected := []Disk{
{
Expand All @@ -59,21 +46,15 @@ func TestFetchStatus(t *testing.T) {
Status: "SLP",
},
}

if !reflect.DeepEqual(expected, disks) {
t.Error("disks' slice is different from expected")
}
require.Equal(t, expected, disks, "disks' slice is different from expected")
}

func TestFetchTwoDisks(t *testing.T) {
l := serve(t, []byte("|/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|"))
defer l.Close()

disks, err := New().Fetch(l.Addr().String())

if err != nil {
t.Error("expecting err to be nil")
}
require.NoError(t, err)

expected := []Disk{
{
Expand All @@ -90,26 +71,20 @@ func TestFetchTwoDisks(t *testing.T) {
Status: "SLP",
},
}

if !reflect.DeepEqual(expected, disks) {
t.Error("disks' slice is different from expected")
}
require.Equal(t, expected, disks, "disks' slice is different from expected")
}

func serve(t *testing.T, data []byte) net.Listener {
l, err := net.Listen("tcp", "127.0.0.1:0")

if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

go func(t *testing.T) {
conn, err := l.Accept()

require.NoError(t, err)

conn.Write(data)
conn.Close()
_, err = conn.Write(data)
require.NoError(t, err)
require.NoError(t, conn.Close())
}(t)

return l
Expand Down
4 changes: 2 additions & 2 deletions plugins/inputs/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestHTTPwithJSONFormat(t *testing.T) {
plugin.SetParser(p)

var acc testutil.Accumulator
plugin.Init()
require.NoError(t, plugin.Init())
require.NoError(t, acc.GatherError(plugin.Gather))

require.Len(t, acc.Metrics, 1)
Expand Down Expand Up @@ -246,7 +246,7 @@ func TestBodyAndContentEncoding(t *testing.T) {
tt.plugin.SetParser(parser)

var acc testutil.Accumulator
tt.plugin.Init()
require.NoError(t, tt.plugin.Init())
err = tt.plugin.Gather(&acc)
require.NoError(t, err)
})
Expand Down

0 comments on commit fd5cc43

Please sign in to comment.