Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Replacing custom testing helpers with testify, where possible #955

Merged
merged 4 commits into from
Jun 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ install:
- pip install --user --upgrade gcloud gsutil
- bin/setup-ci-secrets $encrypted_5ba036e89377_key $encrypted_5ba036e89377_iv
- make travis
- go get ./...

script:
- bin/lint .
Expand Down
8 changes: 4 additions & 4 deletions common/sched_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/benbjohnson/clock"
wt "github.com/weaveworks/weave/testing"
"github.com/stretchr/testify/require"
)

// Ensure we can add new calls while forwarding the clock
Expand All @@ -31,7 +31,7 @@ func TestSchedCallsBasic(t *testing.T) {
}

t.Logf("Now: %s - calls: %d", clk.Now(), schedQueue.Count())
wt.AssertEqualInt(t, (int)(schedQueue.Count()), testSecs, "Number of calls")
require.Equal(t, testSecs, (int)(schedQueue.Count()), "Number of calls")
}

// Ensure we can create a 100 seconds gap in the middle of the time travel
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestSchedCallsGap(t *testing.T) {
}

t.Logf("Now: %s - calls: %d", clk.Now(), schedQueue.Count())
wt.AssertEqualInt(t, (int)(schedQueue.Count()), testSecs-100+1, "Number of calls")
require.Equal(t, testSecs-100+1, (int)(schedQueue.Count()), "Number of calls")
}

func TestSchedCallsStop(t *testing.T) {
Expand Down Expand Up @@ -88,5 +88,5 @@ func TestSchedCallsStop(t *testing.T) {
}

t.Logf("Now: %s - calls: %d", clk.Now(), schedQueue.Count())
wt.AssertEqualInt(t, (int)(schedQueue.Count()), testSecs/2, "Number of calls")
require.Equal(t, testSecs/2, (int)(schedQueue.Count()), "Number of calls")
}
1 change: 1 addition & 0 deletions ipam/allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ipam

import (
"fmt"

"github.com/weaveworks/weave/ipam/address"
)

Expand Down
46 changes: 23 additions & 23 deletions ipam/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/weaveworks/weave/common"
"github.com/weaveworks/weave/ipam/address"
"github.com/weaveworks/weave/router"
wt "github.com/weaveworks/weave/testing"
)

const (
Expand Down Expand Up @@ -39,12 +39,12 @@ func TestAllocFree(t *testing.T) {

alloc.claimRingForTesting()
addr1, err := alloc.Allocate(container1, cidr1.HostRange(), nil)
wt.AssertNoErr(t, err)
wt.AssertEqualString(t, addr1.String(), testAddr1, "address")
require.NoError(t, err)
require.Equal(t, testAddr1, addr1.String(), "address")

addr2, err := alloc.Allocate(container1, cidr2.HostRange(), nil)
wt.AssertNoErr(t, err)
wt.AssertEqualString(t, addr2.String(), testAddr2, "address")
require.NoError(t, err)
require.Equal(t, testAddr2, addr2.String(), "address")

// Ask for another address for a different container and check it's different
addr1b, _ := alloc.Allocate(container2, cidr1.HostRange(), nil)
Expand All @@ -54,20 +54,20 @@ func TestAllocFree(t *testing.T) {

// Ask for the first container again and we should get the same addresses again
addr1a, _ := alloc.Allocate(container1, cidr1.HostRange(), nil)
wt.AssertEqualString(t, addr1a.String(), testAddr1, "address")
require.Equal(t, testAddr1, addr1a.String(), "address")
addr2a, _ := alloc.Allocate(container1, cidr2.HostRange(), nil)
wt.AssertEqualString(t, addr2a.String(), testAddr2, "address")
require.Equal(t, testAddr2, addr2a.String(), "address")

// Now delete the first container, and we should get its addresses back
wt.AssertSuccess(t, alloc.Delete(container1))
require.NoError(t, alloc.Delete(container1))
addr3, _ := alloc.Allocate(container3, cidr1.HostRange(), nil)
wt.AssertEqualString(t, addr3.String(), testAddr1, "address")
require.Equal(t, testAddr1, addr3.String(), "address")
addr4, _ := alloc.Allocate(container3, cidr2.HostRange(), nil)
wt.AssertEqualString(t, addr4.String(), testAddr2, "address")
require.Equal(t, testAddr2, addr4.String(), "address")

alloc.ContainerDied(container2)
alloc.ContainerDied(container3)
wt.AssertEquals(t, alloc.NumFreeAddresses(subnet), address.Offset(spaceSize))
require.Equal(t, address.Offset(spaceSize), alloc.NumFreeAddresses(subnet))
}

func TestBootstrap(t *testing.T) {
Expand Down Expand Up @@ -144,10 +144,10 @@ func TestAllocatorClaim(t *testing.T) {
addr1, _ := address.ParseIP(testAddr1)

err := alloc.Claim(container3, addr1, nil)
wt.AssertNoErr(t, err)
require.NoError(t, err)
// Check we get this address back if we try an allocate
addr3, _ := alloc.Allocate(container3, subnet, nil)
wt.AssertEqualString(t, addr3.String(), testAddr1, "address")
require.Equal(t, testAddr1, addr3.String(), "address")
}

func (alloc *Allocator) pause() func() {
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestCancel(t *testing.T) {
res2, _ := alloc2.Allocate("bar", subnet, nil)
common.Debug.Printf("res2 = %s", res2.String())
if res1 == res2 {
wt.Fatalf(t, "Error: got same ips!")
require.FailNow(t, "Error: got same ips!")
}

// Now we're going to pause alloc2 and ask alloc1
Expand All @@ -215,7 +215,7 @@ func TestCancel(t *testing.T) {
cancelChan <- true
unpause()
if <-doneChan {
wt.Fatalf(t, "Error: got result from Allocate")
require.FailNow(t, "Error: got result from Allocate")
}
}

Expand All @@ -235,7 +235,7 @@ func TestGossipShutdown(t *testing.T) {
alloc.Shutdown()

_, err := alloc.Allocate(container2, subnet, nil) // trying to allocate after shutdown should fail
wt.AssertFalse(t, err == nil, "no address")
require.False(t, err == nil, "no address")

CheckAllExpectedMessagesSent(alloc)
}
Expand All @@ -250,24 +250,24 @@ func TestTransfer(t *testing.T) {
alloc3 := allocs[2] // This will be 'master' and get the first range

_, err := alloc2.Allocate("foo", subnet, nil)
wt.AssertTrue(t, err == nil, "Failed to get address")
require.True(t, err == nil, "Failed to get address")

_, err = alloc3.Allocate("bar", subnet, nil)
wt.AssertTrue(t, err == nil, "Failed to get address")
require.True(t, err == nil, "Failed to get address")

router.GossipBroadcast(alloc2.Gossip())
router.GossipBroadcast(alloc3.Gossip())
router.removePeer(alloc2.ourName)
router.removePeer(alloc3.ourName)
alloc2.Stop()
alloc3.Stop()
wt.AssertSuccess(t, alloc1.AdminTakeoverRanges(alloc2.ourName.String()))
wt.AssertSuccess(t, alloc1.AdminTakeoverRanges(alloc3.ourName.String()))
require.NoError(t, alloc1.AdminTakeoverRanges(alloc2.ourName.String()))
require.NoError(t, alloc1.AdminTakeoverRanges(alloc3.ourName.String()))

wt.AssertEquals(t, alloc1.NumFreeAddresses(subnet), address.Offset(1022))
require.Equal(t, address.Offset(1022), alloc1.NumFreeAddresses(subnet))

_, err = alloc1.Allocate("foo", subnet, nil)
wt.AssertTrue(t, err == nil, "Failed to get address")
require.True(t, err == nil, "Failed to get address")
alloc1.Stop()
}

Expand Down Expand Up @@ -388,7 +388,7 @@ func TestAllocatorFuzz(t *testing.T) {
if err != nil {
panic(err)
}
wt.AssertSuccess(t, alloc.Free(res.name, oldAddr))
require.NoError(t, alloc.Free(res.name, oldAddr))
}

// Do a Allocate on an existing container & allocator
Expand Down
38 changes: 19 additions & 19 deletions ipam/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ import (
"time"

"github.com/gorilla/mux"

"github.com/stretchr/testify/require"
"github.com/weaveworks/weave/common"
"github.com/weaveworks/weave/ipam/address"
wt "github.com/weaveworks/weave/testing"
)

func HTTPPost(t *testing.T, url string) string {
resp, err := http.Post(url, "", nil)
wt.AssertNoErr(t, err)
wt.AssertStatus(t, resp.StatusCode, http.StatusOK, "http response")
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode, "http response")
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return string(body)
}

func HTTPGet(t *testing.T, url string) string {
resp, err := http.Get(url)
wt.AssertNoErr(t, err)
require.NoError(t, err)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return string(body)
Expand Down Expand Up @@ -87,28 +87,28 @@ func TestHttp(t *testing.T) {

// Allocate an address in each subnet, and check we got what we expected
cidr1a := HTTPPost(t, allocURL(port, testCIDR1, containerID))
wt.AssertEqualString(t, cidr1a, testAddr1, "address")
require.Equal(t, testAddr1, cidr1a, "address")
cidr2a := HTTPPost(t, allocURL(port, testCIDR2, containerID))
wt.AssertEqualString(t, cidr2a, testAddr2, "address")
require.Equal(t, testAddr2, cidr2a, "address")
// Now, make the same requests again to check the operation is idempotent
check := HTTPGet(t, allocURL(port, testCIDR1, containerID))
wt.AssertEqualString(t, check, cidr1a, "address")
require.Equal(t, cidr1a, check, "address")
check = HTTPGet(t, allocURL(port, testCIDR2, containerID))
wt.AssertEqualString(t, check, cidr2a, "address")
require.Equal(t, cidr2a, check, "address")

// Ask the http server for a pair of addresses for another container and check they're different
cidr1b := HTTPPost(t, allocURL(port, testCIDR1, container2))
wt.AssertFalse(t, cidr1b == testAddr1, "address")
require.False(t, cidr1b == testAddr1, "address")
cidr2b := HTTPPost(t, allocURL(port, testCIDR2, container2))
wt.AssertFalse(t, cidr2b == testAddr2, "address")
require.False(t, cidr2b == testAddr2, "address")

// Now free the first container, and we should get its addresses back when we ask
doHTTP("DELETE", identURL(port, containerID))

cidr1c := HTTPPost(t, allocURL(port, testCIDR1, container3))
wt.AssertEqualString(t, cidr1c, testAddr1, "address")
require.Equal(t, testAddr1, cidr1c, "address")
cidr2c := HTTPPost(t, allocURL(port, testCIDR2, container3))
wt.AssertEqualString(t, cidr2c, testAddr2, "address")
require.Equal(t, testAddr2, cidr2c, "address")

// Would like to shut down the http server at the end of this test
// but it's complicated.
Expand All @@ -132,16 +132,16 @@ func TestBadHttp(t *testing.T) {
testAddr1 := parts[0]
// Verb that's not handled
resp, err := doHTTP("HEAD", fmt.Sprintf("http://localhost:%d/ip/%s/%s", port, containerID, testAddr1))
wt.AssertNoErr(t, err)
wt.AssertStatus(t, resp.StatusCode, http.StatusNotFound, "http response")
require.NoError(t, err)
require.Equal(t, http.StatusNotFound, resp.StatusCode, "http response")
// Mis-spelled URL
resp, err = doHTTP("POST", fmt.Sprintf("http://localhost:%d/xip/%s/", port, containerID))
wt.AssertNoErr(t, err)
wt.AssertStatus(t, resp.StatusCode, http.StatusNotFound, "http response")
require.NoError(t, err)
require.Equal(t, http.StatusNotFound, resp.StatusCode, "http response")
// Malformed URL
resp, err = doHTTP("POST", fmt.Sprintf("http://localhost:%d/ip/%s/foo/bar/baz", port, containerID))
wt.AssertNoErr(t, err)
wt.AssertStatus(t, resp.StatusCode, http.StatusNotFound, "http response")
require.NoError(t, err)
require.Equal(t, http.StatusNotFound, resp.StatusCode, "http response")
}

func TestHTTPCancel(t *testing.T) {
Expand Down Expand Up @@ -180,6 +180,6 @@ func impTestHTTPCancel(t *testing.T) {
unpause()
res := <-done
if res != nil {
wt.Fatalf(t, "Error: Allocate returned non-nil")
require.FailNow(t, "Error: Allocate returned non-nil")
}
}
1 change: 1 addition & 0 deletions ipam/paxos/paxos.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package paxos

import (
"fmt"

"github.com/weaveworks/weave/router"
)

Expand Down
3 changes: 2 additions & 1 deletion ipam/paxos/paxos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package paxos

import (
"fmt"
"github.com/weaveworks/weave/router"
"math/rand"
"testing"
"time"

"github.com/weaveworks/weave/router"
)

type TestNode struct {
Expand Down
Loading