Skip to content

Commit

Permalink
pkg/command: Replace gocheck with built-in go test
Browse files Browse the repository at this point in the history
Signed-off-by: Tam Mach <[email protected]>
  • Loading branch information
sayboras committed Apr 30, 2024
1 parent 46c3de3 commit 48fe4bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
42 changes: 19 additions & 23 deletions pkg/command/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ package exec

import (
"context"
"strings"
"testing"
"time"

. "github.com/cilium/checkmate"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"

"github.com/cilium/cilium/pkg/logging"
)
Expand All @@ -18,54 +19,49 @@ const (
timeout = 250 * time.Millisecond
)

// Hook up gocheck into the "go test" runner.
type ExecTestSuite struct{}

var (
_ = Suite(&ExecTestSuite{})
fooLog = logging.DefaultLogger.WithField("foo", "bar")
)

func Test(t *testing.T) {
TestingT(t)
}

func (s *ExecTestSuite) TestWithTimeout(c *C) {
func TestWithTimeout(t *testing.T) {
cmd := WithTimeout(timeout, "sleep", "inf")
err := cmd.Start()
c.Assert(err, IsNil)
require.NoError(t, err)
err = cmd.Wait()
c.Assert(err, ErrorMatches, "signal: killed")
require.Error(t, err)
require.Contains(t, err.Error(), "signal: killed")
}

func (s *ExecTestSuite) TestWithCancel(c *C) {
func TestWithCancel(t *testing.T) {
cmd, cancel := WithCancel(context.Background(), "sleep", "inf")
c.Assert(cancel, NotNil)
require.NotNil(t, cancel)
err := cmd.Start()
c.Assert(err, IsNil)
require.NoError(t, err)
cancel()
}

func (s *ExecTestSuite) TestCanceled(c *C) {
func TestCanceled(t *testing.T) {
cmd, cancel := WithCancel(context.Background(), "sleep", "inf")
c.Assert(cancel, NotNil)
require.NotNil(t, cancel)
cancel()
_, err := cmd.CombinedOutput(fooLog, true)
c.Assert(err, ErrorMatches, ".*: context canceled")
require.Error(t, err)
require.Contains(t, err.Error(), "context canceled")
}

func (s *ExecTestSuite) TestCombinedOutput(c *C) {
func TestCombinedOutput(t *testing.T) {
cmd := CommandContext(context.Background(), "echo", "foo")
out, err := cmd.CombinedOutput(fooLog, true)
c.Assert(err, IsNil)
c.Assert(string(out), Equals, "foo\n")
require.NoError(t, err)
require.Equal(t, "foo\n", string(out))
}

func (s *ExecTestSuite) TestCombinedOutputFailedTimeout(c *C) {
func TestCombinedOutputFailedTimeout(t *testing.T) {
cmd := WithTimeout(timeout, "sleep", "inf")
time.Sleep(timeout)
_, err := cmd.CombinedOutput(fooLog, true)
c.Assert(err, ErrorMatches, "Command execution failed for .*: context deadline exceeded")
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), "context deadline exceeded"))
}

// LoggingHook is a simple hook which saves Warn messages to a slice of strings.
Expand Down
20 changes: 7 additions & 13 deletions pkg/command/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ package command
import (
"testing"

. "github.com/cilium/checkmate"
"github.com/stretchr/testify/require"
)

func Test(t *testing.T) { TestingT(t) }

type CMDHelpersSuite struct{}

var _ = Suite(&CMDHelpersSuite{})

func (s *CMDHelpersSuite) TestDumpJSON(c *C) {
func TestDumpJSON(t *testing.T) {
type sampleData struct {
ID int
Name string
Expand All @@ -27,18 +21,18 @@ func (s *CMDHelpersSuite) TestDumpJSON(c *C) {
}

err := dumpJSON(tt, "")
c.Assert(err, IsNil)
require.NoError(t, err)

err = dumpJSON(tt, "{.Id}")
c.Assert(err, IsNil)
require.NoError(t, err)

err = dumpJSON(tt, "{{.Id}}")
if err == nil {
c.Fatalf("Dumpjson jsonpath no error with invalid path '%s'", err)
t.Fatalf("Dumpjson jsonpath no error with invalid path '%s'", err)
}
}

func (s *CMDHelpersSuite) TestDumpYAML(c *C) {
func TestDumpYAML(t *testing.T) {
type sampleData struct {
ID int
Name string
Expand All @@ -50,5 +44,5 @@ func (s *CMDHelpersSuite) TestDumpYAML(c *C) {
}

err := dumpYAML(tt)
c.Assert(err, IsNil)
require.NoError(t, err)
}

0 comments on commit 48fe4bf

Please sign in to comment.