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

Remove more Convey #3158

Merged
merged 1 commit into from
Sep 19, 2019
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
27 changes: 13 additions & 14 deletions go/beacon_srv/internal/beacon/beacon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import (
"testing"

"github.com/golang/mock/gomock"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"

"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/xtest/graph"
)

// TestBeaconDiversity tests that diversity is calculated correctly.
func TestBeaconDiversity(t *testing.T) {
var tests = []struct {
name string
Expand All @@ -49,17 +50,15 @@ func TestBeaconDiversity(t *testing.T) {
diversity: 2,
},
}
Convey("Diversity is calculated correctly", t, func() {
mctrl := gomock.NewController(t)
defer mctrl.Finish()
g := graph.NewDefaultGraph(mctrl)
bseg := testBeaconOrErr(g, tests[0].beacon...)
for _, test := range tests {
Convey(test.name, func() {
other := testBeaconOrErr(g, test.beacon...)
diversity := bseg.Beacon.Diversity(other.Beacon)
SoMsg("Diversity", diversity, ShouldEqual, test.diversity)
})
}
})
mctrl := gomock.NewController(t)
defer mctrl.Finish()
g := graph.NewDefaultGraph(mctrl)
bseg := testBeaconOrErr(g, tests[0].beacon...)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
other := testBeaconOrErr(g, test.beacon...)
diversity := bseg.Beacon.Diversity(other.Beacon)
assert.Equal(t, test.diversity, diversity)
})
}
}
12 changes: 7 additions & 5 deletions go/beacon_srv/internal/beacon/beacondbtest/beacondbtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,18 @@ var (
timeout = 3 * time.Second
)

// Testable extends the beacon db interface with methods that are needed for testing.
// Testable extends the beacon db interface with methods that are needed for
// testing.
type Testable interface {
beacon.DB
// Prepare should reset the internal state so that the DB is empty and is ready to be tested.
// Prepare should reset the internal state so that the DB is empty and is
// ready to be tested.
Prepare(t *testing.T, ctx context.Context)
}

// Test should be used to test any implementation of the BeaconDB interface.
// An implementation of the BeaconDB interface should at least have one test method that calls
// this test-suite. The calling test code should have a top level Convey block.
// Test should be used to test any implementation of the BeaconDB interface. An
// implementation of the BeaconDB interface should at least have one test
// method that calls this test-suite.
func Test(t *testing.T, db Testable) {
testWrapper := func(test func(*testing.T, *gomock.Controller,
beacon.DBReadWrite)) func(t *testing.T) {
Expand Down
9 changes: 2 additions & 7 deletions go/beacon_srv/internal/beacon/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ import (
"context"
"testing"

. "github.com/smartystreets/goconvey/convey"

"github.com/scionproto/scion/go/beacon_srv/internal/beacon"
"github.com/scionproto/scion/go/beacon_srv/internal/beacon/beacondbsqlite"
"github.com/scionproto/scion/go/beacon_srv/internal/beacon/beacondbtest"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/xtest"
)

var testIA = addr.IA{I: 1, A: 0xff0000000333}
var testIA = xtest.MustParseIA("1-ff00:0:333")

var _ beacondbtest.Testable = (*TestBackend)(nil)

Expand All @@ -43,7 +40,5 @@ func (b *TestBackend) Prepare(t *testing.T, _ context.Context) {

func TestBeaconDBSuite(t *testing.T) {
tdb := &TestBackend{}
Convey("BeaconDBSuite", t, func() {
beacondbtest.Test(t, tdb)
})
beacondbtest.Test(t, tdb)
}
2 changes: 2 additions & 0 deletions go/border/rpkt/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ go_test(
"//go/lib/spath:go_default_library",
"//go/lib/spkt:go_default_library",
"//go/lib/topology:go_default_library",
"//go/lib/xtest:go_default_library",
"@com_github_smartystreets_goconvey//convey:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
],
)
83 changes: 36 additions & 47 deletions go/border/rpkt/rpkt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@
package rpkt

import (
"fmt"
"io/ioutil"
"net"
"testing"

. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"

"github.com/scionproto/scion/go/border/brconf"
"github.com/scionproto/scion/go/border/rctx"
Expand All @@ -30,22 +28,15 @@ import (
"github.com/scionproto/scion/go/lib/l4"
"github.com/scionproto/scion/go/lib/spkt"
"github.com/scionproto/scion/go/lib/topology"
"github.com/scionproto/scion/go/lib/xtest"
)

var rawUdpPkt = "testdata/udp-scion.bin"

func MustLoad(path string) common.RawBytes {
data, err := ioutil.ReadFile(path)
if err != nil {
panic(fmt.Sprintf("Unable to load file: %v", err))
}
return common.RawBytes(data)
}
var rawUdpPkt = "udp-scion.bin"

// Prepare the packet from raw
func prepareRtrPacketSample() *RtrPkt {
func prepareRtrPacketSample(t *testing.T) *RtrPkt {
r := NewRtrPkt()
r.Raw = MustLoad(rawUdpPkt)
r.Raw = xtest.MustReadFromFile(t, rawUdpPkt)
// Set some other data that are required for the parsing to succeed:
var config = &brconf.BRConf{
IA: addr.IA{I: 1, A: 2},
Expand All @@ -59,44 +50,42 @@ func prepareRtrPacketSample() *RtrPkt {
}

func TestParseBasic(t *testing.T) {
Convey("Parse packet, basic", t, func() {
r := prepareRtrPacketSample()
r := prepareRtrPacketSample(t)

r.parseBasic()
srcIA, _ := r.SrcIA()
dstIA, _ := r.DstIA()
srcHost, _ := r.SrcHost()
dstHost, _ := r.DstHost()
r.parseBasic()
srcIA, _ := r.SrcIA()
dstIA, _ := r.DstIA()
srcHost, _ := r.SrcHost()
dstHost, _ := r.DstHost()

SoMsg("Source IA", srcIA, ShouldResemble, addr.IA{I: 1, A: 10})
SoMsg("Destination IA", dstIA, ShouldResemble, addr.IA{I: 2, A: 25})
SoMsg("Source host IP", srcHost.IP().Equal(net.IPv4(127, 1, 1, 111)), ShouldBeTrue)
SoMsg("Destination host IP", dstHost.IP().Equal(net.IPv4(127, 2, 2, 222)), ShouldBeTrue)
SoMsg("CmnHdr", r.CmnHdr, ShouldResemble, spkt.CmnHdr{
Ver: 0,
DstType: addr.HostTypeIPv4,
SrcType: addr.HostTypeIPv4,
TotalLen: 1280,
HdrLen: 17,
CurrInfoF: 4 + 9, // 4 accounts for common header + address headers.
CurrHopF: 4 + 12,
NextHdr: 17,
})
})
assert.Equal(t, addr.IA{I: 1, A: 10}, srcIA, "Source IA")
assert.Equal(t, addr.IA{I: 2, A: 25}, dstIA, "Destination IA")
assert.True(t, srcHost.IP().Equal(net.IPv4(127, 1, 1, 111)), "Source host IP")
assert.True(t, dstHost.IP().Equal(net.IPv4(127, 2, 2, 222)), "Destination host IP")
expectedHdr := spkt.CmnHdr{
Ver: 0,
DstType: addr.HostTypeIPv4,
SrcType: addr.HostTypeIPv4,
TotalLen: 1280,
HdrLen: 17,
CurrInfoF: 4 + 9, // 4 accounts for common header + address headers.
CurrHopF: 4 + 12,
NextHdr: 17,
}
assert.Equal(t, expectedHdr, r.CmnHdr)
}

func TestParse(t *testing.T) {
r := prepareRtrPacketSample()
r := prepareRtrPacketSample(t)

// Verify additional fields that appear after complete parse only
Convey("Parse packet, complete", t, func() {
r.Parse()
l4hdr, _ := r.L4Hdr(false)
SoMsg("L4Hdr must be expected UDP", l4hdr, ShouldResemble, &l4.UDP{
SrcPort: 44887,
DstPort: 3000,
TotalLen: 1144,
Checksum: common.RawBytes{0x7c, 0x46},
})
})
r.Parse()
l4hdr, _ := r.L4Hdr(false)
expected := &l4.UDP{
SrcPort: 44887,
DstPort: 3000,
TotalLen: 1144,
Checksum: common.RawBytes{0x7c, 0x46},
}
assert.Equal(t, expected, l4hdr, "L4Hdr must be expected UDP")
}
5 changes: 4 additions & 1 deletion go/lib/as_conf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ go_test(
srcs = ["conf_test.go"],
data = glob(["testdata/**"]),
embed = [":go_default_library"],
deps = ["@com_github_smartystreets_goconvey//convey:go_default_library"],
deps = [
"@com_github_stretchr_testify//assert:go_default_library",
"@com_github_stretchr_testify//require:go_default_library",
],
)
22 changes: 11 additions & 11 deletions go/lib/as_conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ package as_conf
import (
"testing"

. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_ASConf(t *testing.T) {
Convey("Loading test config `testdata/basic.yml`", t, func() {
if err := Load("testdata/basic.yml"); err != nil {
t.Fatalf("Error loading config: %v", err)
}
c := CurrConf
So(c, ShouldResemble, &ASConf{
1, 21600, 5, true, 60,
})
})
// TestASConf tests that loading test config `testdata/basic.yml` works.
func TestASConf(t *testing.T) {
err := Load("testdata/basic.yml")
require.NoError(t, err)
c := CurrConf
expectedConf := &ASConf{
1, 21600, 5, true, 60,
}
assert.Equal(t, expectedConf, c)
}
1 change: 1 addition & 0 deletions go/lib/ctrl/seg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ go_test(
"//go/proto:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_smartystreets_goconvey//convey:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
],
)
74 changes: 34 additions & 40 deletions go/lib/ctrl/seg/segs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"time"

"github.com/golang/mock/gomock"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"

"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
Expand Down Expand Up @@ -86,46 +86,40 @@ func allocHopEntry(inIA, outIA addr.IA, hopF common.RawBytes) *HopEntry {
}
}

func Test_FilterSegments(t *testing.T) {
Convey("Test filtering segments", t, func() {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
func TestFilterSegments(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

seg110_120 := allocPathSegment(ctrl, []addr.IA{core1_110, core1_120})
seg120_110 := allocPathSegment(ctrl, []addr.IA{core1_120, core1_110})
seg110_120 := allocPathSegment(ctrl, []addr.IA{core1_110, core1_120})
seg120_110 := allocPathSegment(ctrl, []addr.IA{core1_120, core1_110})

testCases := []struct {
Name string
Segs []*PathSegment
Filtered []*PathSegment
KeepF func(*PathSegment) bool
}{
{
Name: "Keep all",
Segs: []*PathSegment{seg110_120},
Filtered: []*PathSegment{seg110_120},
KeepF: func(s *PathSegment) bool { return true },
},
{
Name: "Drop all",
Segs: []*PathSegment{seg110_120},
Filtered: []*PathSegment{},
KeepF: func(s *PathSegment) bool { return false },
},
{
Name: "First IA core 1_110",
Segs: []*PathSegment{seg110_120, seg120_110},
Filtered: []*PathSegment{seg120_110},
KeepF: func(s *PathSegment) bool { return core1_120.Equal(s.FirstIA()) },
},
}
tests := map[string]struct {
Segs []*PathSegment
Filtered []*PathSegment
KeepF func(*PathSegment) bool
}{
"Keep all": {
Segs: []*PathSegment{seg110_120},
Filtered: []*PathSegment{seg110_120},
KeepF: func(s *PathSegment) bool { return true },
},
"Drop all": {
Segs: []*PathSegment{seg110_120},
Filtered: []*PathSegment{},
KeepF: func(s *PathSegment) bool { return false },
},
"First IA core 1_110": {
Segs: []*PathSegment{seg110_120, seg120_110},
Filtered: []*PathSegment{seg120_110},
KeepF: func(s *PathSegment) bool { return core1_120.Equal(s.FirstIA()) },
},
}

for _, tc := range testCases {
Convey(tc.Name, func() {
segs := Segments(tc.Segs)
segs.FilterSegs(tc.KeepF)
SoMsg("Filtering not exact", segs, ShouldResemble, Segments(tc.Filtered))
})
}
})
for name, test := range tests {
t.Run(name, func(t *testing.T) {
segs := Segments(test.Segs)
segs.FilterSegs(test.KeepF)
assert.Equal(t, Segments(test.Filtered), segs)
})
}
}
4 changes: 2 additions & 2 deletions go/lib/log/logparse/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ go_test(
deps = [
"//go/lib/common:go_default_library",
"//go/lib/log:go_default_library",
"//go/lib/xtest:go_default_library",
"@com_github_smartystreets_goconvey//convey:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
"@com_github_stretchr_testify//require:go_default_library",
],
)
Loading