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

agent: Replace gocheck with built-in go test #32214

Merged
merged 21 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
34babab
pkg/lock: Replace gocheck with built-in go test
sayboras Apr 24, 2024
690578e
pkg/backoff: Replace gocheck with built-in go test
sayboras Apr 24, 2024
6480c0e
pkg/cleanup: Replace gocheck with built-in go test
sayboras Apr 24, 2024
447fa3d
pkg/common: Replace gocheck with built-in go test
sayboras Apr 24, 2024
4e6eeb0
pkg/controller: Replace gocheck with built-in go test
sayboras Apr 24, 2024
80c1741
pkg/debug: Replace gocheck with built-in go test
sayboras Apr 24, 2024
76dd388
pkg/eventqueue: Replace gocheck with built-in go test
sayboras Apr 28, 2024
da4787c
pkg/health: Replace gocheck with built-in go test
sayboras Apr 28, 2024
ab4cc9d
pkg/iana: Replace gocheck with built-in go test
sayboras Apr 28, 2024
be84749
pkg/ipmasq: Replace gocheck with built-in go test
sayboras Apr 28, 2024
002668f
pkg/math: Replace gocheck with built-in go test
sayboras Apr 28, 2024
ff17251
pkg/node: Replace gocheck with built-in go test
sayboras Apr 28, 2024
6333525
pkg/option: Replace gocheck with built-in go test
sayboras Apr 28, 2024
7a5f6d9
pkg/pidfile: Replace gocheck with built-in go test
sayboras Apr 28, 2024
d68a327
pkg/revert: Replace gocheck with built-in go test
sayboras Apr 28, 2024
5980aa2
pkg/safetime: Replace gocheck with built-in go test
sayboras Apr 28, 2024
5694d38
pkg/spanstat: Replace gocheck with built-in go test
sayboras Apr 28, 2024
b1806fb
pkg/status: Replace gocheck with built-in go test
sayboras Apr 28, 2024
82ae380
pkg/trigger: Replace gocheck with built-in go test
sayboras Apr 28, 2024
8edb990
pkg/version: Replace gocheck with built-in go test
sayboras Apr 28, 2024
0841c73
pkg/versioncheck: Replace gocheck with built-in go test
sayboras Apr 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 8 additions & 16 deletions pkg/backoff/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@ import (
"testing"
"time"

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

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

type BackoffSuite struct{}

var _ = check.Suite(&BackoffSuite{})

func (b *BackoffSuite) TestJitter(c *check.C) {
func TestJitter(t *testing.T) {
var prev time.Duration
for i := 0; i < 100; i++ {
current := CalculateDuration(time.Second, time.Minute, 2.0, true, 1)
c.Assert(current, check.Not(check.Equals), prev)
require.NotEqual(t, prev, current)
prev = current
}
}
Expand All @@ -44,15 +36,15 @@ func (f *fakeNodeManager) ClusterSizeDependantInterval(baseInterval time.Duratio
return time.Duration(int64(waitNanoseconds))
}

func (b *BackoffSuite) TestNewNodeManager(c *check.C) {
func TestNewNodeManager(t *testing.T) {
mgr := NewNodeManager(func(baseInterval time.Duration) time.Duration { return 2 * baseInterval })
c.Assert(mgr.ClusterSizeDependantInterval(1*time.Second), check.Equals, 2*time.Second)
require.Equal(t, 2*time.Second, mgr.ClusterSizeDependantInterval(1*time.Second))

mgr = NewNodeManager(nil)
c.Assert(mgr.ClusterSizeDependantInterval(1*time.Second), check.Equals, 1*time.Second)
require.Equal(t, 1*time.Second, mgr.ClusterSizeDependantInterval(1*time.Second))
}

func (b *BackoffSuite) TestClusterSizeDependantInterval(c *check.C) {
func TestClusterSizeDependantInterval(t *testing.T) {
var (
nnodes = 0
nodeManager = fakeNodeManager{
Expand All @@ -79,7 +71,7 @@ func (b *BackoffSuite) TestClusterSizeDependantInterval(c *check.C) {
}
}

func (b *BackoffSuite) TestJitterDistribution(c *check.C) {
func TestJitterDistribution(t *testing.T) {
nodeBackoff := &Exponential{
Min: time.Second,
Factor: 2.0,
Expand Down
15 changes: 3 additions & 12 deletions pkg/cleanup/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@ import (
"sync"
"testing"

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

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) {
TestingT(t)
}

type CleanupTestSuite struct{}

var _ = Suite(&CleanupTestSuite{})

func (cts *CleanupTestSuite) TestHandleCleanup(c *C) {
func TestHandleCleanup(t *testing.T) {
wg := &sync.WaitGroup{}
ch := make(chan struct{})
i := 0
Expand All @@ -28,5 +19,5 @@ func (cts *CleanupTestSuite) TestHandleCleanup(c *C) {
})
close(ch)
wg.Wait()
c.Assert(i, Equals, 1)
require.Equal(t, 1, i)
}
36 changes: 13 additions & 23 deletions pkg/common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,21 @@ import (
"strings"
"testing"

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

"github.com/cilium/cilium/pkg/checker"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) {
check.TestingT(t)
func TestC2GoArray(t *testing.T) {
require.Equal(t, []byte{0, 0x01, 0x02, 0x03}, C2GoArray("0x0, 0x1, 0x2, 0x3"))
require.Equal(t, []byte{0, 0xFF, 0xFF, 0xFF}, C2GoArray("0x0, 0xff, 0xff, 0xff"))
require.Equal(t, []byte{0xa, 0xbc, 0xde, 0xf1}, C2GoArray("0xa, 0xbc, 0xde, 0xf1"))
require.Equal(t, []byte{0}, C2GoArray("0x0"))
require.Equal(t, []byte{}, C2GoArray(""))
}

type CommonSuite struct{}

var _ = check.Suite(&CommonSuite{})

func (s *CommonSuite) TestC2GoArray(c *check.C) {
c.Assert(C2GoArray("0x0, 0x1, 0x2, 0x3"), checker.DeepEquals, []byte{0, 0x01, 0x02, 0x03})
c.Assert(C2GoArray("0x0, 0xff, 0xff, 0xff"), checker.DeepEquals, []byte{0, 0xFF, 0xFF, 0xFF})
c.Assert(C2GoArray("0xa, 0xbc, 0xde, 0xf1"), checker.DeepEquals, []byte{0xa, 0xbc, 0xde, 0xf1})
c.Assert(C2GoArray("0x0"), checker.DeepEquals, []byte{0})
c.Assert(C2GoArray(""), checker.DeepEquals, []byte{})
}

func (s *CommonSuite) TestGoArray2C(c *check.C) {
func TestGoArray2C(t *testing.T) {
tests := []struct {
input []byte
output string
Expand Down Expand Up @@ -59,11 +49,11 @@ func (s *CommonSuite) TestGoArray2C(c *check.C) {
}

for _, test := range tests {
c.Assert(GoArray2C(test.input), check.Equals, test.output)
require.Equal(t, test.output, GoArray2C(test.input))
}
}

func (s *CommonSuite) TestGetNumPossibleCPUsFromReader(c *check.C) {
func TestGetNumPossibleCPUsFromReader(t *testing.T) {
log := logging.DefaultLogger.WithField(logfields.LogSubsys, "utils-test")
tests := []struct {
in string
Expand All @@ -76,9 +66,9 @@ func (s *CommonSuite) TestGetNumPossibleCPUsFromReader(c *check.C) {
{"foobar", 0},
}

for _, t := range tests {
possibleCpus := getNumPossibleCPUsFromReader(log, strings.NewReader(t.in))
c.Assert(possibleCpus, check.Equals, t.expected)
for _, tt := range tests {
possibleCpus := getNumPossibleCPUsFromReader(log, strings.NewReader(tt.in))
require.Equal(t, tt.expected, possibleCpus)
}

}
77 changes: 33 additions & 44 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,19 @@ import (
"testing"
"time"

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

"github.com/cilium/cilium/pkg/testutils"
)

// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) {
TestingT(t)
}

type ControllerSuite struct{}

var _ = Suite(&ControllerSuite{})

func (b *ControllerSuite) TestUpdateRemoveController(c *C) {
func TestUpdateRemoveController(t *testing.T) {
mngr := NewManager()
mngr.UpdateController("test", ControllerParams{})
c.Assert(mngr.RemoveController("test"), IsNil)
c.Assert(mngr.RemoveController("not-exist"), Not(IsNil))
require.NoError(t, mngr.RemoveController("test"))
require.Error(t, mngr.RemoveController("not-exits"))
}

func (b *ControllerSuite) TestCreateController(c *C) {
func TestCreateController(t *testing.T) {
var iterations atomic.Uint32
mngr := NewManager()
created := mngr.CreateController("test", ControllerParams{
Expand All @@ -40,7 +31,7 @@ func (b *ControllerSuite) TestCreateController(c *C) {
return nil
},
})
c.Assert(created, Equals, true)
require.True(t, created)

// Second creation is a no-op.
created = mngr.CreateController("test", ControllerParams{
Expand All @@ -49,14 +40,13 @@ func (b *ControllerSuite) TestCreateController(c *C) {
return nil
},
})
c.Assert(created, Equals, false)

c.Assert(mngr.RemoveControllerAndWait("test"), IsNil)
require.False(t, created)

c.Assert(iterations.Load(), Equals, uint32(1))
require.NoError(t, mngr.RemoveControllerAndWait("test"))
require.Equal(t, uint32(1), iterations.Load())
}

func (b *ControllerSuite) TestStopFunc(c *C) {
func TestStopFunc(t *testing.T) {
stopFuncRan := false
waitChan := make(chan struct{})

Expand All @@ -70,16 +60,16 @@ func (b *ControllerSuite) TestStopFunc(c *C) {
return nil
},
})
c.Assert(mngr.RemoveController("test"), IsNil)
require.NoError(t, mngr.RemoveController("test"))
select {
case <-waitChan:
case <-time.After(2 * time.Second):
c.Error("StopFunc did not run")
t.Error("StopFunc did not run")
}
c.Assert(stopFuncRan, Equals, true)
require.True(t, stopFuncRan)
}

func (b *ControllerSuite) TestSelfExit(c *C) {
func TestSelfExit(t *testing.T) {
var iterations atomic.Uint32
waitChan := make(chan bool)

Expand All @@ -97,22 +87,22 @@ func (b *ControllerSuite) TestSelfExit(c *C) {
})
select {
case <-waitChan:
c.Error("Controller exited")
t.Error("Controller exited")
case <-time.After(time.Second):
}
c.Assert(iterations.Load(), Equals, uint32(1))
require.Equal(t, uint32(1), iterations.Load())

// The controller is inactive, and waiting for the next update or stop.
// A controller will only stop when explicitly removed and stopped.
mngr.RemoveController("test")
select {
case <-waitChan:
case <-time.After(time.Second):
c.Error("Controller did not exit")
t.Error("Controller did not exit")
}
}

func (b *ControllerSuite) TestRemoveAll(c *C) {
func TestRemoveAll(t *testing.T) {
mngr := NewManager()
// create
mngr.UpdateController("test1", ControllerParams{})
Expand All @@ -129,7 +119,7 @@ type testObj struct {
cnt int
}

func (b *ControllerSuite) TestRunController(c *C) {
func TestRunController(t *testing.T) {
mngr := NewManager()
o := &testObj{}

Expand All @@ -149,21 +139,20 @@ func (b *ControllerSuite) TestRunController(c *C) {

for n := 0; ctrl.GetSuccessCount() < 2; n++ {
if n > 100 {
c.Fatalf("time out while waiting for controller to succeed, last error: %s", ctrl.GetLastError())
t.Fatalf("time out while waiting for controller to succeed, last error: %s", ctrl.GetLastError())
}

time.Sleep(time.Duration(100) * time.Millisecond)
}

c.Assert(GetGlobalStatus(), Not(IsNil))

c.Assert(ctrl.GetSuccessCount(), Not(Equals), 0)
c.Assert(ctrl.GetFailureCount(), Equals, 2)
c.Assert(ctrl.GetLastError(), IsNil)
c.Assert(mngr.RemoveController("test"), IsNil)
require.NotNil(t, GetGlobalStatus())
require.NotEqual(t, 0, ctrl.GetSuccessCount())
require.Equal(t, 2, ctrl.GetFailureCount())
require.NoError(t, ctrl.GetLastError())
require.NoError(t, mngr.RemoveController("test"))
}

func (b *ControllerSuite) TestCancellation(c *C) {
func TestCancellation(t *testing.T) {
mngr := NewManager()

started := make(chan struct{})
Expand All @@ -182,7 +171,7 @@ func (b *ControllerSuite) TestCancellation(c *C) {
select {
case <-started:
case <-time.After(time.Minute):
c.Fatalf("timeout while waiting for controller to start")
t.Fatalf("timeout while waiting for controller to start")
}

mngr.RemoveAll()
Expand All @@ -191,7 +180,7 @@ func (b *ControllerSuite) TestCancellation(c *C) {
select {
case <-cancelled:
case <-time.After(time.Minute):
c.Fatalf("timeout while waiting for controller to be cancelled")
t.Fatalf("timeout while waiting for controller to be cancelled")
}
}

Expand All @@ -207,28 +196,28 @@ func (m *Manager) terminationChannel(name string) chan struct{} {
return c
}

func (b *ControllerSuite) TestWaitForTermination(c *C) {
func TestWaitForTermination(t *testing.T) {
mngr := NewManager()
mngr.UpdateController("test1", ControllerParams{})
mngr.UpdateController("test1", ControllerParams{})

// Ensure that the channel does not get closed while the controller is
// still running
c.Assert(testutils.WaitUntil(func() bool {
require.Nil(t, testutils.WaitUntil(func() bool {
select {
case <-mngr.terminationChannel("test1"):
return false
default:
return true
}
}, 20*time.Millisecond), IsNil)
}, 20*time.Millisecond))

c.Assert(mngr.RemoveControllerAndWait("test1"), IsNil)
require.Nil(t, mngr.RemoveControllerAndWait("test1"))

// The controller must have been terminated already due to AndWait above
select {
case <-mngr.terminationChannel("test1"):
default:
c.Fail()
t.Fail()
}
}