diff --git a/go/beacon_srv/internal/beacon/beacon_test.go b/go/beacon_srv/internal/beacon/beacon_test.go index b5e50a4835..f7f8b1989d 100644 --- a/go/beacon_srv/internal/beacon/beacon_test.go +++ b/go/beacon_srv/internal/beacon/beacon_test.go @@ -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 @@ -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) + }) + } } diff --git a/go/beacon_srv/internal/beacon/beacondbtest/beacondbtest.go b/go/beacon_srv/internal/beacon/beacondbtest/beacondbtest.go index 77ab4add4c..9e17060e40 100644 --- a/go/beacon_srv/internal/beacon/beacondbtest/beacondbtest.go +++ b/go/beacon_srv/internal/beacon/beacondbtest/beacondbtest.go @@ -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) { diff --git a/go/beacon_srv/internal/beacon/metrics_test.go b/go/beacon_srv/internal/beacon/metrics_test.go index 01d7db0308..13fe568e02 100644 --- a/go/beacon_srv/internal/beacon/metrics_test.go +++ b/go/beacon_srv/internal/beacon/metrics_test.go @@ -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) @@ -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) } diff --git a/go/border/rpkt/BUILD.bazel b/go/border/rpkt/BUILD.bazel index 7d7af5a757..ecc5d9bdac 100644 --- a/go/border/rpkt/BUILD.bazel +++ b/go/border/rpkt/BUILD.bazel @@ -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", ], ) diff --git a/go/border/rpkt/rpkt_test.go b/go/border/rpkt/rpkt_test.go index dbca573199..4aa8965ce0 100644 --- a/go/border/rpkt/rpkt_test.go +++ b/go/border/rpkt/rpkt_test.go @@ -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" @@ -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}, @@ -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") } diff --git a/go/lib/as_conf/BUILD.bazel b/go/lib/as_conf/BUILD.bazel index 6c5c42c869..089e73cc3e 100644 --- a/go/lib/as_conf/BUILD.bazel +++ b/go/lib/as_conf/BUILD.bazel @@ -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", + ], ) diff --git a/go/lib/as_conf/conf_test.go b/go/lib/as_conf/conf_test.go index e8b5e746a6..3a095a534e 100644 --- a/go/lib/as_conf/conf_test.go +++ b/go/lib/as_conf/conf_test.go @@ -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) } diff --git a/go/lib/ctrl/seg/BUILD.bazel b/go/lib/ctrl/seg/BUILD.bazel index 42260bfd2f..675bb12b5e 100644 --- a/go/lib/ctrl/seg/BUILD.bazel +++ b/go/lib/ctrl/seg/BUILD.bazel @@ -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", ], ) diff --git a/go/lib/ctrl/seg/segs_test.go b/go/lib/ctrl/seg/segs_test.go index 4ab12865bc..f5a77b92cb 100644 --- a/go/lib/ctrl/seg/segs_test.go +++ b/go/lib/ctrl/seg/segs_test.go @@ -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" @@ -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) + }) + } } diff --git a/go/lib/log/logparse/BUILD.bazel b/go/lib/log/logparse/BUILD.bazel index 8e5c45e063..919cea28c3 100644 --- a/go/lib/log/logparse/BUILD.bazel +++ b/go/lib/log/logparse/BUILD.bazel @@ -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", ], ) diff --git a/go/lib/log/logparse/logentry_test.go b/go/lib/log/logparse/logentry_test.go index 34b3cb3745..2e197b035b 100644 --- a/go/lib/log/logparse/logentry_test.go +++ b/go/lib/log/logparse/logentry_test.go @@ -20,22 +20,20 @@ import ( "testing" "time" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/scionproto/scion/go/lib/common" "github.com/scionproto/scion/go/lib/log" - "github.com/scionproto/scion/go/lib/xtest" ) func TestParseFrom(t *testing.T) { defaultTs := mustParse("2018-07-19 14:39:29.489625+0000", t) - tests := []struct { - Name string + tests := map[string]struct { Input string Entries []LogEntry }{ - { - Name: "SingleLineTest", + "SingleLineTest": { Input: "2018-07-19 14:39:29.489625+0000 [ERROR] Txt", Entries: []LogEntry{ { @@ -45,8 +43,7 @@ func TestParseFrom(t *testing.T) { }, }, }, - { - Name: "MultilineTest>", + "MultilineTest": { Input: "2018-07-19 14:39:29.489625+0000 [CRIT] (CliSrvExt 2-ff00:0: > ...\n" + "> SCIONDPathReplyEntry:", Entries: []LogEntry{ @@ -57,8 +54,7 @@ func TestParseFrom(t *testing.T) { }, }, }, - { - Name: "MultilineTestSpace", + "MultilineTestSpace": { Input: "2018-07-19 14:39:29.489625+0000 [CRIT] (CliSrvExt 2-ff00:0: > ...\n" + " SCIONDPathReplyEntry:", Entries: []LogEntry{ @@ -69,12 +65,10 @@ func TestParseFrom(t *testing.T) { }, }, }, - { - Name: "MissingLevel", + "MissingLevel": { Input: "2018-07-19 14:39:29.489625+0000 Txt", }, - { - Name: "MultiEntry", + "MultiEntry": { Input: "2018-07-19 14:39:29.489625+0000 [ERROR] Txt\n" + "2018-07-19 14:39:30.489625+0000 [INFO] Txt2", Entries: []LogEntry{ @@ -91,28 +85,24 @@ func TestParseFrom(t *testing.T) { }, }, } - Convey("ParseFrom", t, func() { - for _, tc := range tests { - Convey(tc.Name, func() { - r := strings.NewReader(tc.Input) - var entries []LogEntry - ParseFrom(r, tc.Name, tc.Name, - func(e LogEntry) { entries = append(entries, e) }) - SoMsg("entries len", len(entries), ShouldEqual, len(tc.Entries)) - for i, e := range entries { - SoMsg("entry ts", e.Timestamp, ShouldResemble, tc.Entries[i].Timestamp) - SoMsg("entry element", e.Element, ShouldEqual, tc.Name) - SoMsg("entry level", e.Level, ShouldEqual, tc.Entries[i].Level) - SoMsg("entry entry", e.Lines, ShouldResemble, tc.Entries[i].Lines) - } - }) - } - }) + for name, test := range tests { + t.Run(name, func(t *testing.T) { + r := strings.NewReader(test.Input) + var entries []LogEntry + ParseFrom(r, name, name, + func(e LogEntry) { + assert.Equal(t, e.Element, name) + e.Element = "" + entries = append(entries, e) + }) + assert.ElementsMatch(t, test.Entries, entries) + }) + } } func mustParse(ts string, t *testing.T) time.Time { tts, err := time.Parse(common.TimeFmt, ts) - xtest.FailOnErr(t, err) + require.NoError(t, err) return tts } diff --git a/go/lib/pathdb/pathdbtest/pathdbtest.go b/go/lib/pathdb/pathdbtest/pathdbtest.go index ad16c438db..394cc8e43a 100644 --- a/go/lib/pathdb/pathdbtest/pathdbtest.go +++ b/go/lib/pathdb/pathdbtest/pathdbtest.go @@ -63,19 +63,18 @@ var ( timeout = time.Second ) -// TestablePathDB extends the path db interface with methods that are needed for testing. +// TestablePathDB extends the path db interface with methods that are needed +// for testing. type TestablePathDB interface { pathdb.PathDB - // 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) } -// TestPathDB should be used to test any implementation of the PathDB interface. -// An implementation of the PathDB interface should at least have one test method that calls -// this test-suite. The calling test code should have a top level Convey block. -// -// setup should return a PathDB in a clean state, i.e. no entries in the DB. -// cleanup can be used to release any resources that have been allocated during setup. +// TestPathDB should be used to test any implementation of the PathDB +// interface. An implementation of the PathDB interface should at least have +// one test method that calls this test-suite. func TestPathDB(t *testing.T, db TestablePathDB) { testWrapper := func(test func(*testing.T, *gomock.Controller, pathdb.ReadWrite)) func(t *testing.T) { diff --git a/go/lib/sciond/BUILD.bazel b/go/lib/sciond/BUILD.bazel index b77a622b7d..5a2123b44f 100644 --- a/go/lib/sciond/BUILD.bazel +++ b/go/lib/sciond/BUILD.bazel @@ -29,7 +29,7 @@ go_test( srcs = ["types_test.go"], embed = [":go_default_library"], deps = [ - "//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", ], ) diff --git a/go/lib/sciond/types_test.go b/go/lib/sciond/types_test.go index 5c959ae75c..1e71281d3e 100644 --- a/go/lib/sciond/types_test.go +++ b/go/lib/sciond/types_test.go @@ -1,4 +1,5 @@ // Copyright 2018 ETH Zurich +// Copyright 2019 ETH Zurich, Anapaya Systems // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -16,105 +17,84 @@ package sciond import ( "testing" - . "github.com/smartystreets/goconvey/convey" - - "github.com/scionproto/scion/go/lib/xtest" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestNewPathInterface(t *testing.T) { - testCases := []struct { - Name string - In string - PI PathInterface - Valid bool + tests := map[string]struct { + In string + PI PathInterface + Err assert.ErrorAssertionFunc }{ - { - Name: "AS, IF wildcard omitted", - In: "1", - Valid: false, + "AS, IF wildcard omitted": { + In: "1", + Err: assert.Error, }, - { - Name: "IF wildcard omitted", - In: "1-0", - Valid: false, + "IF wildcard omitted": { + In: "1-0", + Err: assert.Error, }, - { - Name: "basic wildcard", - In: "1-0#0", - PI: mustPathInterface(t, "1-0#0"), - Valid: true, + "basic wildcard": { + In: "1-0#0", + PI: mustPathInterface(t, "1-0#0"), + Err: assert.NoError, }, - { - Name: "AS wildcard, interface set", - In: "1-0#1", - PI: mustPathInterface(t, "1-0#1"), - Valid: true, + "AS wildcard, interface set": { + In: "1-0#1", + PI: mustPathInterface(t, "1-0#1"), + Err: assert.NoError, }, - { - Name: "ISD wildcard, AS set", - In: "0-1#0", - PI: mustPathInterface(t, "0-1#0"), - Valid: true, + "ISD wildcard, AS set": { + In: "0-1#0", + PI: mustPathInterface(t, "0-1#0"), + Err: assert.NoError, }, - { - Name: "ISD wildcard, AS set, interface set", - In: "0-1#1", - PI: mustPathInterface(t, "0-1#1"), - Valid: true, + "ISD wildcard, AS set, interface set": { + In: "0-1#1", + PI: mustPathInterface(t, "0-1#1"), + Err: assert.NoError, }, - { - Name: "ISD wildcard, AS set and interface omitted", - In: "0-1", - Valid: false, + "ISD wildcard, AS set and interface omitted": { + In: "0-1", + Err: assert.Error, }, - { - Name: "IF wildcard omitted, AS set", - In: "1-1", - Valid: false, + "IF wildcard omitted, AS set": { + In: "1-1", + Err: assert.Error, }, - { - Name: "bad -", - In: "1-1-0", - Valid: false, + "bad -": { + In: "1-1-0", + Err: assert.Error, }, - { - Name: "bad #", - In: "1-1#0#", - Valid: false, + "bad #": { + In: "1-1#0#", + Err: assert.Error, }, - { - Name: "bad IF", - In: "1-1#e", - Valid: false, + "bad IF": { + In: "1-1#e", + Err: assert.Error, }, - { - Name: "bad AS", - In: "1-12323433243534#0", - Valid: false, + "bad AS": { + In: "1-12323433243534#0", + Err: assert.Error, }, - { - Name: "bad ISD", - In: "1123212-23#0", - Valid: false, + "bad ISD": { + In: "1123212-23#0", + Err: assert.Error, }, } - Convey("TestNewPathInterface", t, func() { - for _, tc := range testCases { - Convey(tc.Name, func() { - pi, err := NewPathInterface(tc.In) - if tc.Valid { - SoMsg("err", err, ShouldBeNil) - SoMsg("pi", pi, ShouldResemble, tc.PI) - } else { - SoMsg("err", err, ShouldNotBeNil) - } - }) - } - }) + for name, test := range tests { + t.Run(name, func(t *testing.T) { + pi, err := NewPathInterface(test.In) + test.Err(t, err) + assert.Equal(t, test.PI, pi) + }) + } } func mustPathInterface(t *testing.T, str string) PathInterface { t.Helper() pi, err := NewPathInterface(str) - xtest.FailOnErr(t, err) + require.NoError(t, err) return pi } diff --git a/go/lib/snet/BUILD.bazel b/go/lib/snet/BUILD.bazel index fd4bcfb52f..1ed30edcb4 100644 --- a/go/lib/snet/BUILD.bazel +++ b/go/lib/snet/BUILD.bazel @@ -58,5 +58,6 @@ go_test( "//go/lib/xtest: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", ], ) diff --git a/go/lib/snet/raw_test.go b/go/lib/snet/raw_test.go index b6c554d1e5..0042c6ec89 100644 --- a/go/lib/snet/raw_test.go +++ b/go/lib/snet/raw_test.go @@ -17,29 +17,23 @@ package snet import ( "testing" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" "github.com/scionproto/scion/go/lib/common" "github.com/scionproto/scion/go/lib/layers" ) func TestExtensionSort(t *testing.T) { - type TestCase struct { - Description string + tests := map[string]struct { InputSlice []common.Extension ExpectedSlice []common.Extension - } - testCases := []*TestCase{ - { - Description: "nil list", - }, - { - Description: "empty list", + }{ + "nil list": {}, + "empty list": { InputSlice: []common.Extension{}, ExpectedSlice: []common.Extension{}, }, - { - Description: "one item", + "one item": { InputSlice: []common.Extension{ &layers.ExtnUnknown{ClassField: common.HopByHopClass, TypeField: 42}, }, @@ -47,8 +41,7 @@ func TestExtensionSort(t *testing.T) { &layers.ExtnUnknown{ClassField: common.HopByHopClass, TypeField: 42}, }, }, - { - Description: "scmp should go first", + "scmp should go first": { InputSlice: []common.Extension{ &layers.ExtnUnknown{ClassField: common.HopByHopClass, TypeField: 42}, &layers.ExtnSCMP{}, @@ -58,8 +51,7 @@ func TestExtensionSort(t *testing.T) { &layers.ExtnUnknown{ClassField: common.HopByHopClass, TypeField: 42}, }, }, - { - Description: "HBH extensions go before e2e, in stable fashion", + "HBH extensions go before e2e, in stable fashion": { InputSlice: []common.Extension{ &layers.ExtnUnknown{ClassField: common.End2EndClass, TypeField: 42}, &layers.ExtnUnknown{ClassField: common.End2EndClass, TypeField: 43}, @@ -74,12 +66,10 @@ func TestExtensionSort(t *testing.T) { }, }, } - Convey("", t, func() { - for _, tc := range testCases { - Convey(tc.Description, func() { - StableSortExtensions(tc.InputSlice) - So(tc.InputSlice, ShouldResemble, tc.ExpectedSlice) - }) - } - }) + for name, test := range tests { + t.Run(name, func(t *testing.T) { + StableSortExtensions(test.InputSlice) + assert.Equal(t, test.ExpectedSlice, test.InputSlice) + }) + } } diff --git a/go/lib/snet/router_test.go b/go/lib/snet/router_test.go index 87a66cf052..81fdf9ab50 100644 --- a/go/lib/snet/router_test.go +++ b/go/lib/snet/router_test.go @@ -18,83 +18,73 @@ import ( "net" "testing" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" "github.com/scionproto/scion/go/lib/addr" "github.com/scionproto/scion/go/lib/overlay" ) func TestLocalMachineBuildAppAddress(t *testing.T) { - Convey("", t, func() { - testCases := []struct { - Description string - Machine *LocalMachine - ExpectedAppAddr *addr.AppAddr - }{ - { - Description: "nil IP", - Machine: &LocalMachine{}, - ExpectedAppAddr: &addr.AppAddr{ - L3: addr.HostFromIP(nil), - L4: addr.NewL4UDPInfo(0), - }, + tests := map[string]struct { + Machine *LocalMachine + ExpectedAppAddr *addr.AppAddr + }{ + "nil IP": { + Machine: &LocalMachine{}, + ExpectedAppAddr: &addr.AppAddr{ + L3: addr.HostFromIP(nil), + L4: addr.NewL4UDPInfo(0), }, - { - Description: "only default IP", - Machine: &LocalMachine{ - InterfaceIP: net.IP{192, 0, 2, 1}, - }, - ExpectedAppAddr: &addr.AppAddr{ - L3: addr.HostFromIP(net.IP{192, 0, 2, 1}), - L4: addr.NewL4UDPInfo(0), - }, + }, + "only default IP": { + Machine: &LocalMachine{ + InterfaceIP: net.IP{192, 0, 2, 1}, }, - { - Description: "if public IP is set, it is used to construct app address", - Machine: &LocalMachine{ - InterfaceIP: net.IP{192, 168, 0, 1}, - PublicIP: net.IP{192, 0, 2, 1}, - }, - ExpectedAppAddr: &addr.AppAddr{ - L3: addr.HostFromIP(net.IP{192, 0, 2, 1}), - L4: addr.NewL4UDPInfo(0), - }, + ExpectedAppAddr: &addr.AppAddr{ + L3: addr.HostFromIP(net.IP{192, 0, 2, 1}), + L4: addr.NewL4UDPInfo(0), }, - } + }, + "if public IP is set, it is used to construct app address": { + Machine: &LocalMachine{ + InterfaceIP: net.IP{192, 168, 0, 1}, + PublicIP: net.IP{192, 0, 2, 1}, + }, + ExpectedAppAddr: &addr.AppAddr{ + L3: addr.HostFromIP(net.IP{192, 0, 2, 1}), + L4: addr.NewL4UDPInfo(0), + }, + }, + } - for _, tc := range testCases { - Convey(tc.Description, func() { - So(tc.Machine.AppAddress(), ShouldResemble, tc.ExpectedAppAddr) - }) - } - }) + for name, test := range tests { + t.Run(name, func(t *testing.T) { + assert.Equal(t, test.ExpectedAppAddr, test.Machine.AppAddress()) + }) + } } func TestLocalMachineBuildBindAddress(t *testing.T) { - Convey("", t, func() { - testCases := []struct { - Description string - Machine *LocalMachine - ExpectedBindAddr *overlay.OverlayAddr - }{ - { - Description: "bind IP is computed based on default IP", - Machine: &LocalMachine{ - InterfaceIP: net.IP{192, 0, 2, 1}, - }, - ExpectedBindAddr: mustNewOverlayAddr( - addr.HostFromIP(net.IP{192, 0, 2, 1}), - addr.NewL4UDPInfo(0), - ), + tests := map[string]struct { + Machine *LocalMachine + ExpectedBindAddr *overlay.OverlayAddr + }{ + "bind IP is computed based on default IP": { + Machine: &LocalMachine{ + InterfaceIP: net.IP{192, 0, 2, 1}, }, - } + ExpectedBindAddr: mustNewOverlayAddr( + addr.HostFromIP(net.IP{192, 0, 2, 1}), + addr.NewL4UDPInfo(0), + ), + }, + } - for _, tc := range testCases { - Convey(tc.Description, func() { - So(tc.Machine.BindAddress(), ShouldResemble, tc.ExpectedBindAddr) - }) - } - }) + for name, test := range tests { + t.Run(name, func(t *testing.T) { + assert.Equal(t, test.ExpectedBindAddr, test.Machine.BindAddress()) + }) + } } func mustNewOverlayAddr(l3 addr.HostAddr, l4 addr.L4Info) *overlay.OverlayAddr { diff --git a/go/lib/sock/reliable/reconnect/util_test.go b/go/lib/sock/reliable/reconnect/util_test.go index 102352587a..22dad4f94a 100644 --- a/go/lib/sock/reliable/reconnect/util_test.go +++ b/go/lib/sock/reliable/reconnect/util_test.go @@ -17,20 +17,15 @@ package reconnect_test import ( "testing" - . "github.com/smartystreets/goconvey/convey" - "github.com/scionproto/scion/go/lib/sock/reliable/reconnect" ) +// TestState tests that State check returns immediately after creating a new object. func TestState(t *testing.T) { - Convey("", t, func() { - Convey("State check returns immediately after creating a new object", func() { - s := reconnect.NewState() - select { - case <-s.Up(): - default: - t.Fatalf("Expected method to return immediately, but it didn't") - } - }) - }) + s := reconnect.NewState() + select { + case <-s.Up(): + default: + t.Fatalf("Expected method to return immediately, but it didn't") + } } diff --git a/go/lib/svc/internal/proto/BUILD.bazel b/go/lib/svc/internal/proto/BUILD.bazel index 6cce155f97..0bff8f7751 100644 --- a/go/lib/svc/internal/proto/BUILD.bazel +++ b/go/lib/svc/internal/proto/BUILD.bazel @@ -12,5 +12,8 @@ go_test( name = "go_default_test", srcs = ["proto_test.go"], 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", + ], ) diff --git a/go/lib/svc/internal/proto/proto_test.go b/go/lib/svc/internal/proto/proto_test.go index 854e7dbfd5..9d92501910 100644 --- a/go/lib/svc/internal/proto/proto_test.go +++ b/go/lib/svc/internal/proto/proto_test.go @@ -18,39 +18,39 @@ import ( "bytes" "testing" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) +// TestSVCResolutionPogsSerialization tests that serializing and deserializing +// via pogs returns the initial object. func TestSVCResolutionPogsSerialization(t *testing.T) { // Sanity check to test that the Go reflection helper type is in sync with // the capnp data definition. - Convey("Serializing and deserializing via pogs should return the initial object", t, func() { - message := SVCResolutionReply{ - Transports: []Transport{ - {Key: "foo", Value: "bar"}, - {Key: "bar", Value: "baz"}, - }, - } - buffer := &bytes.Buffer{} + message := SVCResolutionReply{ + Transports: []Transport{ + {Key: "foo", Value: "bar"}, + {Key: "bar", Value: "baz"}, + }, + } + buffer := &bytes.Buffer{} - err := message.SerializeTo(buffer) - SoMsg("serialize error", err, ShouldBeNil) + err := message.SerializeTo(buffer) + require.NoError(t, err) - var newMessage SVCResolutionReply - err = newMessage.DecodeFrom(buffer) - SoMsg("decode error", err, ShouldBeNil) - SoMsg("data", newMessage, ShouldResemble, message) - }) + var newMessage SVCResolutionReply + err = newMessage.DecodeFrom(buffer) + require.NoError(t, err) + assert.Equal(t, message, newMessage) } +// TestSVCResolutionReplyString tests that the String function writes the correct data. func TestSVCResolutionReplyString(t *testing.T) { - Convey("String function should write correct data", t, func() { - message := SVCResolutionReply{ - Transports: []Transport{ - {Key: "foo", Value: "bar"}, - {Key: "bar", Value: "baz"}, - }, - } - So(message.String(), ShouldEqual, "SVCResolutionReply([foo:bar bar:baz])") - }) + message := SVCResolutionReply{ + Transports: []Transport{ + {Key: "foo", Value: "bar"}, + {Key: "bar", Value: "baz"}, + }, + } + assert.Equal(t, "SVCResolutionReply([foo:bar bar:baz])", message.String()) } diff --git a/go/tools/scion-pki/internal/pkicmn/BUILD.bazel b/go/tools/scion-pki/internal/pkicmn/BUILD.bazel index 71e4238138..b78f2aee95 100644 --- a/go/tools/scion-pki/internal/pkicmn/BUILD.bazel +++ b/go/tools/scion-pki/internal/pkicmn/BUILD.bazel @@ -19,6 +19,7 @@ go_test( "//go/lib/addr:go_default_library", "//go/lib/common: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", ], ) diff --git a/go/tools/scion-pki/internal/pkicmn/pkicmn_test.go b/go/tools/scion-pki/internal/pkicmn/pkicmn_test.go index 97217483d9..f828785220 100644 --- a/go/tools/scion-pki/internal/pkicmn/pkicmn_test.go +++ b/go/tools/scion-pki/internal/pkicmn/pkicmn_test.go @@ -21,7 +21,8 @@ import ( "path/filepath" "testing" - . "github.com/smartystreets/goconvey/convey" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/scionproto/scion/go/lib/addr" "github.com/scionproto/scion/go/lib/common" @@ -41,28 +42,19 @@ var ases = []addr.AS{ xtest.MustParseAS("ff00:0:31"), } -type testStructure struct { - scenario string - selector string - isdAsMap map[addr.ISD][]addr.IA - err string -} - func setupTest(t *testing.T) func() { // 1. Create a tmp dir which would be the RootDir dir, err := ioutil.TempDir("", "pkicmn") - xtest.FailOnErr(t, err) + require.NoError(t, err) RootDir = dir // 2. Create a folder for ISD named ISD1 isdPath := filepath.Join(dir, fmt.Sprintf("ISD%d", ISD)) err = os.Mkdir(isdPath, 0755) - xtest.FailOnErr(t, err) + require.NoError(t, err) // 3. Create folders for ASes inside ISD1 for _, as := range ases { err = os.Mkdir(filepath.Join(isdPath, fmt.Sprintf("AS%s", as.FileFmt())), 0755) - if err != nil { - So(err, ShouldBeNil) - } + require.NoError(t, err) } return func() { os.RemoveAll(dir) @@ -70,94 +62,72 @@ func setupTest(t *testing.T) func() { } func TestProcessSelector(t *testing.T) { - Convey("Given root directory structure", t, func() { - teardown := setupTest(t) - tests := []testStructure{ - { - scenario: "Empty selector string", - selector: "", - isdAsMap: nil, - err: ErrInvalidSelector, - }, - { - scenario: "ISD only selector with empty AS selector", - selector: "1", - isdAsMap: map[addr.ISD][]addr.IA{ - addr.ISD(1): getIAFromASes(addr.ISD(1), ases), - }, - err: "", - }, - { - scenario: "ISD only selector with empty AS selector with wrong ISD", - selector: "2", - isdAsMap: nil, - err: ErrNoISDDirFound, - }, - { - scenario: "Wildcard ISD selector with empty AS selector", - selector: "*", - isdAsMap: map[addr.ISD][]addr.IA{ - addr.ISD(1): getIAFromASes(addr.ISD(1), ases), - }, - err: "", - }, - { - scenario: "Wildcard ISD selector with non empty AS selector", - selector: "*-ff00:0:10", - isdAsMap: nil, - err: ErrInvalidSelector, + setupTest(t) + tests := map[string]struct { + selector string + isdAsMap map[addr.ISD][]addr.IA + err string + }{ + "Empty selector string": { + err: ErrInvalidSelector, + }, + "ISD only selector with empty AS selector": { + selector: "1", + isdAsMap: map[addr.ISD][]addr.IA{ + addr.ISD(1): getIAFromASes(addr.ISD(1), ases), }, - { - scenario: "Wildcard AS selector with fixed ISD selector", - selector: "1-*", - isdAsMap: map[addr.ISD][]addr.IA{ - addr.ISD(1): getIAFromASes(addr.ISD(1), ases), - }, - err: "", + }, + "ISD only selector with empty AS selector with wrong ISD": { + selector: "2", + err: ErrNoISDDirFound, + }, + "Wildcard ISD selector with empty AS selector": { + selector: "*", + isdAsMap: map[addr.ISD][]addr.IA{ + addr.ISD(1): getIAFromASes(addr.ISD(1), ases), }, - { - scenario: "Fixed ISD-AS selector", - selector: "1-ff00:0:10", - isdAsMap: map[addr.ISD][]addr.IA{ - addr.ISD(1): getIAFromASes(addr.ISD(1), ases[:1]), - }, - err: "", + }, + "Wildcard ISD selector with non empty AS selector": { + selector: "*-ff00:0:10", + err: ErrInvalidSelector, + }, + "Wildcard AS selector with fixed ISD selector": { + selector: "1-*", + isdAsMap: map[addr.ISD][]addr.IA{ + addr.ISD(1): getIAFromASes(addr.ISD(1), ases), }, - { - scenario: "Fixed ISD-AS selector with wrong AS format", - selector: "1-ff00_0_10", - isdAsMap: nil, - err: ErrInvalidSelector, + }, + "Fixed ISD-AS selector": { + selector: "1-ff00:0:10", + isdAsMap: map[addr.ISD][]addr.IA{ + addr.ISD(1): getIAFromASes(addr.ISD(1), ases[:1]), }, - { - scenario: "Fixed ISD-AS selector with non-existent AS number", - selector: "1-ff00:0:12", - isdAsMap: nil, - err: ErrNoASDirFound, - }, - { - scenario: "Selector with more than one token", - selector: "1-ff00:0:10-*", - isdAsMap: nil, - err: ErrInvalidSelector, - }, - } - for _, test := range tests { - Convey(test.scenario, func() { - isdAsMap, err := ProcessSelector(test.selector) - So(isdAsMap, ShouldResemble, test.isdAsMap) - if test.err != "" { - be := err.(common.BasicError) - So(be.Msg, ShouldEqual, test.err) - } else { - So(err, ShouldBeNil) - } - }) - } - Reset(func() { - teardown() + }, + "Fixed ISD-AS selector with wrong AS format": { + selector: "1-ff00_0_10", + err: ErrInvalidSelector, + }, + "Fixed ISD-AS selector with non-existent AS number": { + selector: "1-ff00:0:12", + err: ErrNoASDirFound, + }, + "Selector with more than one token": { + selector: "1-ff00:0:10-*", + err: ErrInvalidSelector, + }, + } + for name, test := range tests { + t.Run(name, func(t *testing.T) { + isdAsMap, err := ProcessSelector(test.selector) + assert.Equal(t, test.isdAsMap, isdAsMap) + if test.err != "" { + be := err.(common.BasicError) + assert.Equal(t, common.ErrMsg(test.err), be.Msg) + } else { + assert.NoError(t, err) + } }) - }) + } } func getIAFromASes(isd addr.ISD, asList []addr.AS) []addr.IA {