Skip to content

Commit

Permalink
all: use testify's require for checking errors in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Hahling <[email protected]>
  • Loading branch information
rolinh committed Nov 17, 2023
1 parent 438766a commit 17685c5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 53 deletions.
15 changes: 7 additions & 8 deletions cmd/observe/agent_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ func Test_getAgentEventsRequest(t *testing.T) {
selectorOpts.since = ""
selectorOpts.until = ""
req, err := getAgentEventsRequest()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetAgentEventsRequest{Number: defaults.EventsPrintCount}, req)
selectorOpts.since = "2021-04-26T00:00:00Z"
selectorOpts.until = "2021-04-26T00:01:00Z"
req, err = getAgentEventsRequest()
assert.NoError(t, err)
require.NoError(t, err)
since, err := time.Parse(time.RFC3339, selectorOpts.since)
assert.NoError(t, err)
require.NoError(t, err)
until, err := time.Parse(time.RFC3339, selectorOpts.until)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetAgentEventsRequest{
Number: defaults.EventsPrintCount,
Since: timestamppb.New(since),
Expand All @@ -61,14 +61,13 @@ func Test_getAgentEventsRequestWithoutSince(t *testing.T) {
selectorOpts.since = ""
selectorOpts.until = ""
req, err := getAgentEventsRequest()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetAgentEventsRequest{Number: defaults.EventsPrintCount}, req)
selectorOpts.until = "2021-04-26T00:01:00Z"
req, err = getAgentEventsRequest()
assert.NoError(t, err)
assert.NoError(t, err)
require.NoError(t, err)
until, err := time.Parse(time.RFC3339, selectorOpts.until)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetAgentEventsRequest{
Number: defaults.EventsPrintCount,
Until: timestamppb.New(until),
Expand Down
15 changes: 8 additions & 7 deletions cmd/observe/debug_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@ import (
observerpb "github.com/cilium/cilium/api/v1/observer"
"github.com/cilium/hubble/pkg/defaults"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
)

func Test_getDebugEventsRequest(t *testing.T) {
selectorOpts.since = ""
selectorOpts.until = ""
req, err := getDebugEventsRequest()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetDebugEventsRequest{Number: defaults.EventsPrintCount}, req)
selectorOpts.since = "2021-04-26T01:00:00Z"
selectorOpts.until = "2021-04-26T01:01:00Z"
req, err = getDebugEventsRequest()
assert.NoError(t, err)
require.NoError(t, err)
since, err := time.Parse(time.RFC3339, selectorOpts.since)
assert.NoError(t, err)
require.NoError(t, err)
until, err := time.Parse(time.RFC3339, selectorOpts.until)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetDebugEventsRequest{
Number: defaults.EventsPrintCount,
Since: timestamppb.New(since),
Expand All @@ -38,13 +39,13 @@ func Test_getDebugEventsRequestWithoutSince(t *testing.T) {
selectorOpts.since = ""
selectorOpts.until = ""
req, err := getDebugEventsRequest()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetDebugEventsRequest{Number: defaults.EventsPrintCount}, req)
selectorOpts.until = "2021-04-26T01:01:00Z"
req, err = getDebugEventsRequest()
assert.NoError(t, err)
require.NoError(t, err)
until, err := time.Parse(time.RFC3339, selectorOpts.until)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetDebugEventsRequest{
Number: defaults.EventsPrintCount,
Until: timestamppb.New(until),
Expand Down
4 changes: 2 additions & 2 deletions cmd/observe/flows_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,9 @@ func TestTrafficDirection(t *testing.T) {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
if tc.err != "" {
assert.Errorf(t, err, tc.err)
require.Errorf(t, err, tc.err)
} else {
assert.NoError(t, err)
require.NoError(t, err)
}
assert.Nil(t, f.blacklist)
})
Expand Down
34 changes: 17 additions & 17 deletions cmd/observe/flows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ func Test_getFlowsRequest(t *testing.T) {
selectorOpts.until = ""
filter := newFlowFilter()
req, err := getFlowsRequest(filter, nil, nil)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetFlowsRequest{Number: defaults.FlowPrintCount}, req)
selectorOpts.since = "2021-03-23T00:00:00Z"
selectorOpts.until = "2021-03-24T00:00:00Z"
req, err = getFlowsRequest(filter, nil, nil)
assert.NoError(t, err)
require.NoError(t, err)
since, err := time.Parse(time.RFC3339, selectorOpts.since)
assert.NoError(t, err)
require.NoError(t, err)
until, err := time.Parse(time.RFC3339, selectorOpts.until)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetFlowsRequest{
Number: defaults.FlowPrintCount,
Since: timestamppb.New(since),
Expand All @@ -67,13 +67,13 @@ func Test_getFlowsRequestWithoutSince(t *testing.T) {
selectorOpts.until = ""
filter := newFlowFilter()
req, err := getFlowsRequest(filter, nil, nil)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetFlowsRequest{Number: defaults.FlowPrintCount}, req)
selectorOpts.until = "2021-03-24T00:00:00Z"
req, err = getFlowsRequest(filter, nil, nil)
assert.NoError(t, err)
require.NoError(t, err)
until, err := time.Parse(time.RFC3339, selectorOpts.until)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetFlowsRequest{
Number: defaults.FlowPrintCount,
Until: timestamppb.New(until),
Expand All @@ -90,16 +90,16 @@ func Test_getFlowsRequestWithRawFilters(t *testing.T) {
`{"destination_label":["k8s:k8s-app=kube-dns"]}`,
}
req, err := getFlowsRequest(newFlowFilter(), allowlist, denylist)
assert.NoError(t, err)
require.NoError(t, err)

// convert filters in the request back to JSON and check if it matches the original allowlist/denylist.
var b strings.Builder
err = json.NewEncoder(&b).Encode(req.GetWhitelist())
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("[%s]\n", strings.Join(allowlist, ",")), b.String())
b.Reset()
err = json.NewEncoder(&b).Encode(req.GetBlacklist())
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("[%s]\n", strings.Join(denylist, ",")), b.String())
}

Expand All @@ -124,7 +124,7 @@ func Test_getFlowFiltersYAML(t *testing.T) {
denylist:
- '{"source_port":["80"]}'
`
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, expected, out)
}

Expand All @@ -133,7 +133,7 @@ func Test_getFlowsRequest_ExperimentalFieldMask_valid(t *testing.T) {
experimentalOpts.fieldMask = []string{"time", "verdict"}
filter := newFlowFilter()
req, err := getFlowsRequest(filter, nil, nil)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetFlowsRequest{
Number: 20,
Experimental: &observerpb.GetFlowsRequest_Experimental{
Expand All @@ -146,7 +146,7 @@ func Test_getFlowsRequest_ExperimentalFieldMask_invalid(t *testing.T) {
experimentalOpts.fieldMask = []string{"time", "verdict", "invalid-field"}
filter := newFlowFilter()
_, err := getFlowsRequest(filter, nil, nil)
assert.ErrorContains(t, err, "invalid-field")
require.ErrorContains(t, err, "invalid-field")
}

func Test_getFlowsRequest_ExperimentalUseDefaultFieldMask(t *testing.T) {
Expand All @@ -157,7 +157,7 @@ func Test_getFlowsRequest_ExperimentalUseDefaultFieldMask(t *testing.T) {
filter := newFlowFilter()
require.NoError(t, handleFlowArgs(os.Stdout, filter, false))
req, err := getFlowsRequest(filter, nil, nil)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, &observerpb.GetFlowsRequest{
Number: 20,
Experimental: &observerpb.GetFlowsRequest_Experimental{
Expand All @@ -172,20 +172,20 @@ func Test_getFlowsRequest_ExperimentalFieldMask_non_json_output(t *testing.T) {
experimentalOpts.fieldMask = []string{"time", "verdict"}
filter := newFlowFilter()
err := handleFlowArgs(os.Stdout, filter, false)
assert.ErrorContains(t, err, "not compatible")
require.ErrorContains(t, err, "not compatible")
}

func Test_getFlowsRequestWithInputFile(t *testing.T) {
// Don't set the number flag in GetFlowsRequest by default if --input-file is specified
selectorOpts.last = 0
otherOpts.inputFile = "myfile"
req, err := getFlowsRequest(newFlowFilter(), nil, nil)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, uint64(0), req.GetNumber())

// .. but you can explicitly specify --last flag
selectorOpts.last = 42
req, err = getFlowsRequest(newFlowFilter(), nil, nil)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, uint64(42), req.GetNumber())
}
36 changes: 18 additions & 18 deletions cmd/observe/io_reader_observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ func Test_getFlowsBasic(t *testing.T) {
var flowStrings []string
for _, f := range flows {
b, err := f.MarshalJSON()
assert.NoError(t, err)
require.NoError(t, err)
flowStrings = append(flowStrings, string(b))
}
server := NewIOReaderObserver(strings.NewReader(strings.Join(flowStrings, "\n") + "\n"))
req := observerpb.GetFlowsRequest{}
client, err := server.GetFlows(context.Background(), &req)
assert.NoError(t, err)
require.NoError(t, err)
for i := 0; i < len(flows); i++ {
_, err = client.Recv()
assert.NoError(t, err)
require.NoError(t, err)
}
_, err = client.Recv()
assert.Equal(t, io.EOF, err)
Expand All @@ -56,7 +56,7 @@ func Test_getFlowsTimeRange(t *testing.T) {
var flowStrings []string
for _, f := range flows {
b, err := f.MarshalJSON()
assert.NoError(t, err)
require.NoError(t, err)
flowStrings = append(flowStrings, string(b))
}
server := NewIOReaderObserver(strings.NewReader(strings.Join(flowStrings, "\n") + "\n"))
Expand All @@ -65,9 +65,9 @@ func Test_getFlowsTimeRange(t *testing.T) {
Until: &timestamppb.Timestamp{Seconds: 150},
}
client, err := server.GetFlows(context.Background(), &req)
assert.NoError(t, err)
require.NoError(t, err)
res, err := client.Recv()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, flows[1], res)
_, err = client.Recv()
assert.Equal(t, io.EOF, err)
Expand All @@ -91,7 +91,7 @@ func Test_getFlowsLast(t *testing.T) {
var flowStrings []string
for _, f := range flows {
b, err := f.MarshalJSON()
assert.NoError(t, err)
require.NoError(t, err)
flowStrings = append(flowStrings, string(b))
}
server := NewIOReaderObserver(strings.NewReader(strings.Join(flowStrings, "\n") + "\n"))
Expand All @@ -100,12 +100,12 @@ func Test_getFlowsLast(t *testing.T) {
First: false,
}
client, err := server.GetFlows(context.Background(), &req)
assert.NoError(t, err)
require.NoError(t, err)
res, err := client.Recv()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, flows[1], res)
res, err = client.Recv()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, flows[2], res)
_, err = client.Recv()
assert.Equal(t, io.EOF, err)
Expand All @@ -129,7 +129,7 @@ func Test_getFlowsFirst(t *testing.T) {
var flowStrings []string
for _, f := range flows {
b, err := f.MarshalJSON()
assert.NoError(t, err)
require.NoError(t, err)
flowStrings = append(flowStrings, string(b))
}
server := NewIOReaderObserver(strings.NewReader(strings.Join(flowStrings, "\n") + "\n"))
Expand All @@ -138,12 +138,12 @@ func Test_getFlowsFirst(t *testing.T) {
First: true,
}
client, err := server.GetFlows(context.Background(), &req)
assert.NoError(t, err)
require.NoError(t, err)
res, err := client.Recv()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, flows[0], res)
res, err = client.Recv()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, flows[1], res)
_, err = client.Recv()
assert.Equal(t, io.EOF, err)
Expand All @@ -167,7 +167,7 @@ func Test_getFlowsFilter(t *testing.T) {
var flowStrings []string
for _, f := range flows {
b, err := f.MarshalJSON()
assert.NoError(t, err)
require.NoError(t, err)
flowStrings = append(flowStrings, string(b))
}
server := NewIOReaderObserver(strings.NewReader(strings.Join(flowStrings, "\n") + "\n"))
Expand All @@ -179,12 +179,12 @@ func Test_getFlowsFilter(t *testing.T) {
},
}
client, err := server.GetFlows(context.Background(), &req)
assert.NoError(t, err)
require.NoError(t, err)
res, err := client.Recv()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, flows[0], res)
res, err = client.Recv()
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, flows[2], res)
_, err = client.Recv()
assert.Equal(t, io.EOF, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ func TestHostname(t *testing.T) {

func TestPrinter_AgentEventDetails(t *testing.T) {
startTS := timestamppb.New(time.Now())
assert.NoError(t, startTS.CheckValid())
require.NoError(t, startTS.CheckValid())

tests := []struct {
name string
Expand Down

0 comments on commit 17685c5

Please sign in to comment.