diff --git a/pkg/test/integration_test.go b/pkg/test/integration_test.go index 9660886..6246875 100644 --- a/pkg/test/integration_test.go +++ b/pkg/test/integration_test.go @@ -27,6 +27,7 @@ import ( "github.com/jhillyerd/goldiff" "github.com/rs/zerolog" "github.com/rs/zerolog/log" + "github.com/stretchr/testify/suite" ) const ( @@ -35,172 +36,128 @@ const ( ) // TODO: Add suites for domain and full addressing modes. - -func TestSuite(t *testing.T) { - stopServer, err := startServer() - if err != nil { - t.Fatal(err) - } - defer stopServer() - - testCases := []struct { - name string - test func(*testing.T) - }{ - {"basic", testBasic}, - {"fullname", testFullname}, - {"encodedHeader", testEncodedHeader}, - {"ipv4Recipient", testIPv4Recipient}, - {"ipv6Recipient", testIPv6Recipient}, - } - for _, tc := range testCases { - t.Run(tc.name, tc.test) - } +type IntegrationSuite struct { + suite.Suite + stopServer func() } -func testBasic(t *testing.T) { +func (s *IntegrationSuite) SetupSuite() { + stopServer, err := startServer() + s.NoError(err) + s.stopServer = stopServer +} + +func (s *IntegrationSuite) TearDownSuite() { + s.stopServer() +} + +func TestIntegrationSuite(t *testing.T) { + suite.Run(t, new(IntegrationSuite)) +} + +func (s *IntegrationSuite) TestBasic() { client, err := client.New(restBaseURL) - if err != nil { - t.Fatal(err) - } + s.Require().NoError(err) from := "fromuser@inbucket.org" to := []string{"recipient@inbucket.org"} input := readTestData("basic.txt") // Send mail. err = smtpclient.SendMail(smtpHost, nil, from, to, input) - if err != nil { - t.Fatal(err) - } + s.Require().NoError(err) // Confirm receipt. msg, err := client.GetMessage("recipient", "latest") - if err != nil { - t.Fatal(err) - } - if msg == nil { - t.Errorf("Got nil message, wanted non-nil message.") - } + s.Require().NoError(err) + s.NotNil(msg) // Compare to golden. got := formatMessage(msg) - goldiff.File(t, got, "testdata", "basic.golden") + goldiff.File(s.T(), got, "testdata", "basic.golden") } -func testFullname(t *testing.T) { +func (s *IntegrationSuite) TestFullname() { client, err := client.New(restBaseURL) - if err != nil { - t.Fatal(err) - } + s.Require().NoError(err) from := "fromuser@inbucket.org" to := []string{"recipient@inbucket.org"} input := readTestData("fullname.txt") // Send mail. err = smtpclient.SendMail(smtpHost, nil, from, to, input) - if err != nil { - t.Fatal(err) - } + s.Require().NoError(err) // Confirm receipt. msg, err := client.GetMessage("recipient", "latest") - if err != nil { - t.Fatal(err) - } - if msg == nil { - t.Errorf("Got nil message, wanted non-nil message.") - } + s.Require().NoError(err) + s.NotNil(msg) // Compare to golden. got := formatMessage(msg) - goldiff.File(t, got, "testdata", "fullname.golden") + goldiff.File(s.T(), got, "testdata", "fullname.golden") } -func testEncodedHeader(t *testing.T) { +func (s *IntegrationSuite) TestEncodedHeader() { client, err := client.New(restBaseURL) - if err != nil { - t.Fatal(err) - } + s.Require().NoError(err) from := "fromuser@inbucket.org" to := []string{"recipient@inbucket.org"} input := readTestData("encodedheader.txt") // Send mail. err = smtpclient.SendMail(smtpHost, nil, from, to, input) - if err != nil { - t.Fatal(err) - } + s.Require().NoError(err) // Confirm receipt. msg, err := client.GetMessage("recipient", "latest") - if err != nil { - t.Fatal(err) - } - if msg == nil { - t.Errorf("Got nil message, wanted non-nil message.") - } + s.Require().NoError(err) + s.NotNil(msg) // Compare to golden. got := formatMessage(msg) - goldiff.File(t, got, "testdata", "encodedheader.golden") + goldiff.File(s.T(), got, "testdata", "encodedheader.golden") } -func testIPv4Recipient(t *testing.T) { +func (s *IntegrationSuite) TestIPv4Recipient() { client, err := client.New(restBaseURL) - if err != nil { - t.Fatal(err) - } + s.Require().NoError(err) from := "fromuser@inbucket.org" to := []string{"ip4recipient@[192.168.123.123]"} input := readTestData("no-to.txt") // Send mail. err = smtpclient.SendMail(smtpHost, nil, from, to, input) - if err != nil { - t.Fatal(err) - } + s.Require().NoError(err) // Confirm receipt. msg, err := client.GetMessage("ip4recipient", "latest") - if err != nil { - t.Fatal(err) - } - if msg == nil { - t.Errorf("Got nil message, wanted non-nil message.") - } + s.Require().NoError(err) + s.NotNil(msg) // Compare to golden. got := formatMessage(msg) - goldiff.File(t, got, "testdata", "no-to-ipv4.golden") + goldiff.File(s.T(), got, "testdata", "no-to-ipv4.golden") } -func testIPv6Recipient(t *testing.T) { +func (s *IntegrationSuite) TestIPv6Recipient() { client, err := client.New(restBaseURL) - if err != nil { - t.Fatal(err) - } + s.Require().NoError(err) from := "fromuser@inbucket.org" to := []string{"ip6recipient@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]"} input := readTestData("no-to.txt") // Send mail. err = smtpclient.SendMail(smtpHost, nil, from, to, input) - if err != nil { - t.Fatal(err) - } + s.Require().NoError(err) // Confirm receipt. msg, err := client.GetMessage("ip6recipient", "latest") - if err != nil { - t.Fatal(err) - } - if msg == nil { - t.Errorf("Got nil message, wanted non-nil message.") - } + s.Require().NoError(err) + s.NotNil(msg) // Compare to golden. got := formatMessage(msg) - goldiff.File(t, got, "testdata", "no-to-ipv6.golden") + goldiff.File(s.T(), got, "testdata", "no-to-ipv6.golden") } func formatMessage(m *client.Message) []byte { diff --git a/pkg/test/storage_suite.go b/pkg/test/storage_suite.go index a57b01f..31231d8 100644 --- a/pkg/test/storage_suite.go +++ b/pkg/test/storage_suite.go @@ -22,8 +22,8 @@ import ( type StoreFactory func( config.Storage, *extension.Host) (store storage.Store, destroy func(), err error) -// suite is passed to each test function; embeds `testing.T` to provide testing primitives. -type suite struct { +// storeSuite is passed to each test function; embeds `testing.T` to provide testing primitives. +type storeSuite struct { *testing.T store storage.Store extHost *extension.Host @@ -34,7 +34,7 @@ func StoreSuite(t *testing.T, factory StoreFactory) { t.Helper() testCases := []struct { name string - test func(suite) + test func(storeSuite) conf config.Storage }{ {"metadata", testMetadata, config.Storage{}}, @@ -59,7 +59,7 @@ func StoreSuite(t *testing.T, factory StoreFactory) { } defer destroy() - s := suite{ + s := storeSuite{ T: t, store: store, extHost: extHost, @@ -70,7 +70,7 @@ func StoreSuite(t *testing.T, factory StoreFactory) { } // testMetadata verifies message metadata is stored and retrieved correctly. -func testMetadata(s suite) { +func testMetadata(s storeSuite) { mailbox := "testmailbox" from := &mail.Address{Name: "From Person", Address: "from@person.com"} to := []*mail.Address{ @@ -137,7 +137,7 @@ func testMetadata(s suite) { } // testContent generates some binary content and makes sure it is correctly retrieved. -func testContent(s suite) { +func testContent(s storeSuite) { content := make([]byte, 5000) for i := 0; i < len(content); i++ { content[i] = byte(i % 256) @@ -191,7 +191,7 @@ func testContent(s suite) { // testDeliveryOrder delivers several messages to the same mailbox, meanwhile querying its contents // with a new GetMessages call each cycle. -func testDeliveryOrder(s suite) { +func testDeliveryOrder(s storeSuite) { mailbox := "fred" subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"} for i, subj := range subjects { @@ -211,7 +211,7 @@ func testDeliveryOrder(s suite) { // testLatest delivers several messages to the same mailbox, and confirms the id `latest` returns // the last message sent. -func testLatest(s suite) { +func testLatest(s storeSuite) { mailbox := "fred" subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"} for _, subj := range subjects { @@ -233,14 +233,14 @@ func testLatest(s suite) { } // testNaming ensures the store does not enforce local part mailbox naming. -func testNaming(s suite) { +func testNaming(s storeSuite) { DeliverToStore(s.T, s.store, "fred@fish.net", "disk #27", time.Now()) GetAndCountMessages(s.T, s.store, "fred", 0) GetAndCountMessages(s.T, s.store, "fred@fish.net", 1) } // testSize verifies message content size metadata values. -func testSize(s suite) { +func testSize(s storeSuite) { mailbox := "fred" subjects := []string{"a", "br", "much longer than the others"} sentIds := make([]string, len(subjects)) @@ -264,7 +264,7 @@ func testSize(s suite) { } // testSeen verifies a message can be marked as seen. -func testSeen(s suite) { +func testSeen(s storeSuite) { mailbox := "lisa" id1, _ := DeliverToStore(s.T, s.store, mailbox, "whatever", time.Now()) id2, _ := DeliverToStore(s.T, s.store, mailbox, "hello?", time.Now()) @@ -300,7 +300,7 @@ func testSeen(s suite) { } // testDelete creates and deletes some messages. -func testDelete(s suite) { +func testDelete(s storeSuite) { mailbox := "fred" subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"} for _, subj := range subjects { @@ -351,7 +351,7 @@ func testDelete(s suite) { } // testPurge makes sure mailboxes can be purged. -func testPurge(s suite) { +func testPurge(s storeSuite) { mailbox := "fred" subjects := []string{"alpha", "bravo", "charlie", "delta", "echo"} @@ -384,7 +384,7 @@ func testPurge(s suite) { } // testMsgCap verifies the message cap is enforced. -func testMsgCap(s suite) { +func testMsgCap(s storeSuite) { mbCap := 10 mailbox := "captain" @@ -413,7 +413,7 @@ func testMsgCap(s suite) { } // testNoMsgCap verfies a cap of 0 is not enforced. -func testNoMsgCap(s suite) { +func testNoMsgCap(s storeSuite) { mailbox := "captain" for i := 0; i < 20; i++ { subj := fmt.Sprintf("subject %v", i) @@ -424,7 +424,7 @@ func testNoMsgCap(s suite) { // testVisitMailboxes creates some mailboxes and confirms the VisitMailboxes method visits all of // them. -func testVisitMailboxes(s suite) { +func testVisitMailboxes(s storeSuite) { // Deliver 2 test messages to each of 5 mailboxes. boxes := []string{"abby", "bill", "christa", "donald", "evelyn"} for _, name := range boxes {