From f85e701efdfef4f2009d0c667bad19deb67bcd25 Mon Sep 17 00:00:00 2001 From: Francesco Guardiani Date: Thu, 5 Mar 2020 19:33:36 +0100 Subject: [PATCH] Moved integration tests and reconfigured CI (#347) Signed-off-by: Francesco Guardiani --- .circleci/config.yml | 6 +- hack/integration-test.sh | 11 +++ hack/presubmit-test.sh | 19 ----- hack/unit-test.sh | 11 +++ .../kafka_sarama/message_benchmark_test.go | 80 ++++++++++++++++--- ...producer_message_encoder_benchmark_test.go | 10 +-- .../producer_message_encoder_test.go | 2 - .../integration/amqp_binding}/amqp_test.go | 9 +-- test/{ => integration}/http/conversion.go | 3 +- .../http/conversion_v1_test.go | 0 test/{ => integration}/http/direct.go | 0 test/{ => integration}/http/direct_v1_test.go | 0 test/{ => integration}/http/loopback.go | 0 .../http/loopback_setters_test.go | 0 .../http/loopback_v03_test.go | 0 .../http/loopback_v1_test.go | 0 test/{ => integration}/http/middleware.go | 3 +- .../http/middleware_v1_test.go | 0 test/{ => integration}/http/tap_handler.go | 0 test/{ => integration}/http/validation.go | 3 +- .../integration/http_binding}/http_test.go | 2 +- .../kafka_sarama_binding}/kafka_test.go | 70 ++-------------- 22 files changed, 117 insertions(+), 112 deletions(-) create mode 100755 hack/integration-test.sh delete mode 100755 hack/presubmit-test.sh create mode 100755 hack/unit-test.sh rename {pkg/bindings/amqp => test/integration/amqp_binding}/amqp_test.go (93%) rename test/{ => integration}/http/conversion.go (99%) rename test/{ => integration}/http/conversion_v1_test.go (100%) rename test/{ => integration}/http/direct.go (100%) rename test/{ => integration}/http/direct_v1_test.go (100%) rename test/{ => integration}/http/loopback.go (100%) rename test/{ => integration}/http/loopback_setters_test.go (100%) rename test/{ => integration}/http/loopback_v03_test.go (100%) rename test/{ => integration}/http/loopback_v1_test.go (100%) rename test/{ => integration}/http/middleware.go (99%) rename test/{ => integration}/http/middleware_v1_test.go (100%) rename test/{ => integration}/http/tap_handler.go (100%) rename test/{ => integration}/http/validation.go (99%) rename {pkg/bindings/http => test/integration/http_binding}/http_test.go (99%) rename {pkg/bindings/kafka_sarama => test/integration/kafka_sarama_binding}/kafka_test.go (65%) diff --git a/.circleci/config.yml b/.circleci/config.yml index 5f4ab171..87f0eee9 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -48,7 +48,11 @@ jobs: - run: name: Run unit tests command: | - TEST_AMQP_URL=amqp://localhost/test ./hack/presubmit-test.sh | tee ${TEST_RESULTS}/go-test.out + ./hack/unit-test.sh | tee ${TEST_RESULTS}/go-unit-test.out + - run: + name: Run integrations tests + command: | + TEST_AMQP_URL=amqp://localhost/test ./hack/integration-test.sh | tee ${TEST_RESULTS}/go-integration-test.out - save_cache: key: gomod-cache-{{ checksum "go.sum" }} diff --git a/hack/integration-test.sh b/hack/integration-test.sh new file mode 100755 index 00000000..eb993478 --- /dev/null +++ b/hack/integration-test.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +# Run integration tests not in parallel +go test -v -parallel 1 ./test/... -coverprofile ${TEST_RESULTS:-.}/integration_test_cover.out -timeout 60s + +# Remove test only deps. +go mod tidy diff --git a/hack/presubmit-test.sh b/hack/presubmit-test.sh deleted file mode 100755 index 351e7a28..00000000 --- a/hack/presubmit-test.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit -set -o nounset -set -o pipefail - -# Test everything in pkg and cmd, except amqp -go test -v ./pkg/... ./cmd/... -coverprofile cover.out -timeout 15s - -# AMQP & Kafka cannot run tests in parallel -go test -v -parallel 1 -tags amqp ./pkg/bindings/amqp -coverprofile amqp_bindings_cover.out -timeout 15s -go test -v -parallel 1 -tags amqp ./pkg/cloudevents/transport/amqp -coverprofile amqp_transport_cover.out -timeout 15s -go test -v -parallel 1 -tags kafka ./pkg/bindings/kafka_sarama -coverprofile kafka_bindings_cover.out -timeout 15s - -# Test everything in test with a slightly longer timeout -go test ./test/... -timeout 60s - -# Remove test only deps. -go mod tidy diff --git a/hack/unit-test.sh b/hack/unit-test.sh new file mode 100755 index 00000000..be0d97d2 --- /dev/null +++ b/hack/unit-test.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +# Test everything in pkg and cmd, except amqp +go test -v ./pkg/... ./cmd/... -coverprofile ${TEST_RESULTS:-.}/unit_test_cover.out -timeout 15s + +# Remove test only deps. +go mod tidy diff --git a/pkg/bindings/kafka_sarama/message_benchmark_test.go b/pkg/bindings/kafka_sarama/message_benchmark_test.go index 7fa8509b..3d5d88e5 100644 --- a/pkg/bindings/kafka_sarama/message_benchmark_test.go +++ b/pkg/bindings/kafka_sarama/message_benchmark_test.go @@ -1,12 +1,14 @@ -// +build kafka - package kafka_sarama_test import ( "context" "testing" + "github.com/Shopify/sarama" + + cloudevents "github.com/cloudevents/sdk-go" "github.com/cloudevents/sdk-go/pkg/binding" + "github.com/cloudevents/sdk-go/pkg/binding/test" "github.com/cloudevents/sdk-go/pkg/bindings/kafka_sarama" "github.com/cloudevents/sdk-go/pkg/event" ) @@ -16,54 +18,112 @@ var M binding.Message var Event event.Event var Err error +var ( + e = test.FullEvent() + structuredConsumerMessageWithoutKey = &sarama.ConsumerMessage{ + Value: test.MustJSON(e), + Headers: []*sarama.RecordHeader{{ + Key: []byte("content-type"), + Value: []byte(cloudevents.ApplicationCloudEventsJSON), + }}, + } + structuredConsumerMessageWithKey = &sarama.ConsumerMessage{ + Key: []byte("aaa"), + Value: test.MustJSON(e), + Headers: []*sarama.RecordHeader{{ + Key: []byte("content-type"), + Value: []byte(cloudevents.ApplicationCloudEventsJSON), + }}, + } + binaryConsumerMessageWithoutKey = &sarama.ConsumerMessage{ + Value: []byte("hello world!"), + Headers: mustToSaramaConsumerHeaders(map[string]string{ + "ce_type": e.Type(), + "ce_source": e.Source(), + "ce_id": e.ID(), + "ce_time": test.Timestamp.String(), + "ce_specversion": "1.0", + "ce_dataschema": test.Schema.String(), + "ce_datacontenttype": "text/json", + "ce_subject": "topic", + "ce_exta": "someext", + }), + } + binaryConsumerMessageWithKey = &sarama.ConsumerMessage{ + Key: []byte("akey"), + Value: []byte("hello world!"), + Headers: mustToSaramaConsumerHeaders(map[string]string{ + "ce_type": e.Type(), + "ce_source": e.Source(), + "ce_id": e.ID(), + "ce_time": test.Timestamp.String(), + "ce_specversion": "1.0", + "ce_dataschema": test.Schema.String(), + "ce_datacontenttype": "text/json", + "ce_subject": "topic", + "ce_exta": "someext", + }), + } +) + func BenchmarkNewStructuredMessageWithoutKey(b *testing.B) { for i := 0; i < b.N; i++ { - M, Err = kafka_sarama.NewMessageFromConsumerMessage(kafka_sarama.StructuredConsumerMessageWithoutKey) + M, Err = kafka_sarama.NewMessageFromConsumerMessage(structuredConsumerMessageWithoutKey) } } func BenchmarkNewStructuredMessageWithKey(b *testing.B) { for i := 0; i < b.N; i++ { - M, Err = kafka_sarama.NewMessageFromConsumerMessage(kafka_sarama.StructuredConsumerMessageWithKey) + M, Err = kafka_sarama.NewMessageFromConsumerMessage(structuredConsumerMessageWithKey) } } func BenchmarkNewBinaryMessageWithoutKey(b *testing.B) { for i := 0; i < b.N; i++ { - M, Err = kafka_sarama.NewMessageFromConsumerMessage(kafka_sarama.BinaryConsumerMessageWithoutKey) + M, Err = kafka_sarama.NewMessageFromConsumerMessage(binaryConsumerMessageWithoutKey) } } func BenchmarkNewBinaryMessageWithKey(b *testing.B) { for i := 0; i < b.N; i++ { - M, Err = kafka_sarama.NewMessageFromConsumerMessage(kafka_sarama.BinaryConsumerMessageWithKey) + M, Err = kafka_sarama.NewMessageFromConsumerMessage(binaryConsumerMessageWithKey) } } func BenchmarkNewStructuredMessageWithoutKeyToEvent(b *testing.B) { for i := 0; i < b.N; i++ { - M, Err = kafka_sarama.NewMessageFromConsumerMessage(kafka_sarama.StructuredConsumerMessageWithoutKey) + M, Err = kafka_sarama.NewMessageFromConsumerMessage(structuredConsumerMessageWithoutKey) Event, _, Err = binding.ToEvent(context.TODO(), M, nil) } } func BenchmarkNewStructuredMessageWithKeyToEvent(b *testing.B) { for i := 0; i < b.N; i++ { - M, Err = kafka_sarama.NewMessageFromConsumerMessage(kafka_sarama.StructuredConsumerMessageWithKey) + M, Err = kafka_sarama.NewMessageFromConsumerMessage(structuredConsumerMessageWithKey) Event, _, Err = binding.ToEvent(context.TODO(), M, nil) } } func BenchmarkNewBinaryMessageWithoutKeyToEvent(b *testing.B) { for i := 0; i < b.N; i++ { - M, Err = kafka_sarama.NewMessageFromConsumerMessage(kafka_sarama.BinaryConsumerMessageWithoutKey) + M, Err = kafka_sarama.NewMessageFromConsumerMessage(binaryConsumerMessageWithoutKey) Event, _, Err = binding.ToEvent(context.TODO(), M, nil) } } func BenchmarkNewBinaryMessageWithKeyToEvent(b *testing.B) { for i := 0; i < b.N; i++ { - M, Err = kafka_sarama.NewMessageFromConsumerMessage(kafka_sarama.BinaryConsumerMessageWithKey) + M, Err = kafka_sarama.NewMessageFromConsumerMessage(binaryConsumerMessageWithKey) Event, _, Err = binding.ToEvent(context.TODO(), M, nil) } } + +func mustToSaramaConsumerHeaders(m map[string]string) []*sarama.RecordHeader { + res := make([]*sarama.RecordHeader, len(m)) + i := 0 + for k, v := range m { + res[i] = &sarama.RecordHeader{Key: []byte(k), Value: []byte(v)} + i++ + } + return res +} diff --git a/pkg/bindings/kafka_sarama/producer_message_encoder_benchmark_test.go b/pkg/bindings/kafka_sarama/producer_message_encoder_benchmark_test.go index 7bd4d549..77652d0e 100644 --- a/pkg/bindings/kafka_sarama/producer_message_encoder_benchmark_test.go +++ b/pkg/bindings/kafka_sarama/producer_message_encoder_benchmark_test.go @@ -1,5 +1,3 @@ -// +build kafka - package kafka_sarama_test import ( @@ -45,27 +43,27 @@ func init() { func BenchmarkEncodeStructuredMessageSkipKey(b *testing.B) { for i := 0; i < b.N; i++ { ProducerMessage = &sarama.ProducerMessage{} - Err = kafka_sarama.EncodeKafkaProducerMessage(ctxSkipKey, structuredMessageWithoutKey, ProducerMessage, binding.TransformerFactories{}) + Err = kafka_sarama.EncodeKafkaProducerMessage(ctxSkipKey, structuredMessageWithoutKey, ProducerMessage, nil) } } func BenchmarkEncodeStructuredMessage(b *testing.B) { for i := 0; i < b.N; i++ { ProducerMessage = &sarama.ProducerMessage{} - Err = kafka_sarama.EncodeKafkaProducerMessage(ctx, structuredMessageWithKey, ProducerMessage, binding.TransformerFactories{}) + Err = kafka_sarama.EncodeKafkaProducerMessage(ctx, structuredMessageWithKey, ProducerMessage, nil) } } func BenchmarkEncodeBinaryMessageSkipKey(b *testing.B) { for i := 0; i < b.N; i++ { ProducerMessage = &sarama.ProducerMessage{} - Err = kafka_sarama.EncodeKafkaProducerMessage(ctxSkipKey, binaryMessageWithoutKey, ProducerMessage, binding.TransformerFactories{}) + Err = kafka_sarama.EncodeKafkaProducerMessage(ctxSkipKey, binaryMessageWithoutKey, ProducerMessage, nil) } } func BenchmarkEncodeBinaryMessage(b *testing.B) { for i := 0; i < b.N; i++ { ProducerMessage = &sarama.ProducerMessage{} - Err = kafka_sarama.EncodeKafkaProducerMessage(ctx, binaryMessageWithKey, ProducerMessage, binding.TransformerFactories{}) + Err = kafka_sarama.EncodeKafkaProducerMessage(ctx, binaryMessageWithKey, ProducerMessage, nil) } } diff --git a/pkg/bindings/kafka_sarama/producer_message_encoder_test.go b/pkg/bindings/kafka_sarama/producer_message_encoder_test.go index f7425042..7ceb6041 100644 --- a/pkg/bindings/kafka_sarama/producer_message_encoder_test.go +++ b/pkg/bindings/kafka_sarama/producer_message_encoder_test.go @@ -1,5 +1,3 @@ -// +build kafka - package kafka_sarama import ( diff --git a/pkg/bindings/amqp/amqp_test.go b/test/integration/amqp_binding/amqp_test.go similarity index 93% rename from pkg/bindings/amqp/amqp_test.go rename to test/integration/amqp_binding/amqp_test.go index 7d81f42f..21104d11 100644 --- a/pkg/bindings/amqp/amqp_test.go +++ b/test/integration/amqp_binding/amqp_test.go @@ -1,6 +1,4 @@ -// +build amqp - -package amqp +package amqp_binding import ( "context" @@ -15,6 +13,7 @@ import ( "github.com/cloudevents/sdk-go/pkg/binding" "github.com/cloudevents/sdk-go/pkg/binding/test" + amqp2 "github.com/cloudevents/sdk-go/pkg/bindings/amqp" "github.com/cloudevents/sdk-go/pkg/event" ) @@ -99,13 +98,13 @@ func testClient(t testing.TB) (client *amqp.Client, session *amqp.Session, addr return client, session, addr } -func testSenderReceiver(t testing.TB, senderOptions ...SenderOptionFunc) (io.Closer, binding.Sender, binding.Receiver) { +func testSenderReceiver(t testing.TB, senderOptions ...amqp2.SenderOptionFunc) (io.Closer, binding.Sender, binding.Receiver) { c, ss, a := testClient(t) r, err := ss.NewReceiver(amqp.LinkSourceAddress(a)) require.NoError(t, err) s, err := ss.NewSender(amqp.LinkTargetAddress(a)) require.NoError(t, err) - return c, NewSender(s, senderOptions...), &receiver{r} + return c, amqp2.NewSender(s, senderOptions...), amqp2.NewReceiver(r) } func BenchmarkSendReceive(b *testing.B) { diff --git a/test/http/conversion.go b/test/integration/http/conversion.go similarity index 99% rename from test/http/conversion.go rename to test/integration/http/conversion.go index d267afff..07b65148 100644 --- a/test/http/conversion.go +++ b/test/integration/http/conversion.go @@ -11,10 +11,11 @@ import ( "testing" "time" + "github.com/google/uuid" + "github.com/cloudevents/sdk-go" "github.com/cloudevents/sdk-go/pkg/cloudevents/transport" cehttp "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/http" - "github.com/google/uuid" ) // Conversion Test: diff --git a/test/http/conversion_v1_test.go b/test/integration/http/conversion_v1_test.go similarity index 100% rename from test/http/conversion_v1_test.go rename to test/integration/http/conversion_v1_test.go diff --git a/test/http/direct.go b/test/integration/http/direct.go similarity index 100% rename from test/http/direct.go rename to test/integration/http/direct.go diff --git a/test/http/direct_v1_test.go b/test/integration/http/direct_v1_test.go similarity index 100% rename from test/http/direct_v1_test.go rename to test/integration/http/direct_v1_test.go diff --git a/test/http/loopback.go b/test/integration/http/loopback.go similarity index 100% rename from test/http/loopback.go rename to test/integration/http/loopback.go diff --git a/test/http/loopback_setters_test.go b/test/integration/http/loopback_setters_test.go similarity index 100% rename from test/http/loopback_setters_test.go rename to test/integration/http/loopback_setters_test.go diff --git a/test/http/loopback_v03_test.go b/test/integration/http/loopback_v03_test.go similarity index 100% rename from test/http/loopback_v03_test.go rename to test/integration/http/loopback_v03_test.go diff --git a/test/http/loopback_v1_test.go b/test/integration/http/loopback_v1_test.go similarity index 100% rename from test/http/loopback_v1_test.go rename to test/integration/http/loopback_v1_test.go diff --git a/test/http/middleware.go b/test/integration/http/middleware.go similarity index 99% rename from test/http/middleware.go rename to test/integration/http/middleware.go index 54830a66..3dc3bc92 100644 --- a/test/http/middleware.go +++ b/test/integration/http/middleware.go @@ -2,10 +2,11 @@ package http import ( "context" - "github.com/google/uuid" "net/http/httptest" "testing" + "github.com/google/uuid" + cloudevents "github.com/cloudevents/sdk-go" cehttp "github.com/cloudevents/sdk-go/pkg/cloudevents/transport/http" ) diff --git a/test/http/middleware_v1_test.go b/test/integration/http/middleware_v1_test.go similarity index 100% rename from test/http/middleware_v1_test.go rename to test/integration/http/middleware_v1_test.go diff --git a/test/http/tap_handler.go b/test/integration/http/tap_handler.go similarity index 100% rename from test/http/tap_handler.go rename to test/integration/http/tap_handler.go diff --git a/test/http/validation.go b/test/integration/http/validation.go similarity index 99% rename from test/http/validation.go rename to test/integration/http/validation.go index e11cea95..6c6f0575 100644 --- a/test/http/validation.go +++ b/test/integration/http/validation.go @@ -6,9 +6,10 @@ import ( "strings" "testing" - cloudevents "github.com/cloudevents/sdk-go" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + + cloudevents "github.com/cloudevents/sdk-go" ) var ( diff --git a/pkg/bindings/http/http_test.go b/test/integration/http_binding/http_test.go similarity index 99% rename from pkg/bindings/http/http_test.go rename to test/integration/http_binding/http_test.go index 094728b4..9c2fc7da 100644 --- a/pkg/bindings/http/http_test.go +++ b/test/integration/http_binding/http_test.go @@ -1,4 +1,4 @@ -package http_test +package http_binding_test import ( "context" diff --git a/pkg/bindings/kafka_sarama/kafka_test.go b/test/integration/kafka_sarama_binding/kafka_test.go similarity index 65% rename from pkg/bindings/kafka_sarama/kafka_test.go rename to test/integration/kafka_sarama_binding/kafka_test.go index b5053a80..2f21f32c 100644 --- a/pkg/bindings/kafka_sarama/kafka_test.go +++ b/test/integration/kafka_sarama_binding/kafka_test.go @@ -1,6 +1,4 @@ -// +build kafka - -package kafka_sarama +package kafka_sarama_binding import ( "context" @@ -13,9 +11,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - cloudevents "github.com/cloudevents/sdk-go" "github.com/cloudevents/sdk-go/pkg/binding" "github.com/cloudevents/sdk-go/pkg/binding/test" + "github.com/cloudevents/sdk-go/pkg/bindings/kafka_sarama" "github.com/cloudevents/sdk-go/pkg/event" ) @@ -23,54 +21,6 @@ const ( TEST_GROUP_ID = "test_group_id" ) -var ( - e = test.FullEvent() - StructuredConsumerMessageWithoutKey = &sarama.ConsumerMessage{ - Value: test.MustJSON(e), - Headers: []*sarama.RecordHeader{{ - Key: []byte(contentTypeHeader), - Value: []byte(cloudevents.ApplicationCloudEventsJSON), - }}, - } - StructuredConsumerMessageWithKey = &sarama.ConsumerMessage{ - Key: []byte("aaa"), - Value: test.MustJSON(e), - Headers: []*sarama.RecordHeader{{ - Key: []byte(contentTypeHeader), - Value: []byte(cloudevents.ApplicationCloudEventsJSON), - }}, - } - BinaryConsumerMessageWithoutKey = &sarama.ConsumerMessage{ - Value: []byte("hello world!"), - Headers: mustToSaramaConsumerHeaders(map[string]string{ - "ce_type": e.Type(), - "ce_source": e.Source(), - "ce_id": e.ID(), - "ce_time": test.Timestamp.String(), - "ce_specversion": "1.0", - "ce_dataschema": test.Schema.String(), - "ce_datacontenttype": "text/json", - "ce_subject": "topic", - "ce_exta": "someext", - }), - } - BinaryConsumerMessageWithKey = &sarama.ConsumerMessage{ - Key: []byte("akey"), - Value: []byte("hello world!"), - Headers: mustToSaramaConsumerHeaders(map[string]string{ - "ce_type": e.Type(), - "ce_source": e.Source(), - "ce_id": e.ID(), - "ce_time": test.Timestamp.String(), - "ce_specversion": "1.0", - "ce_dataschema": test.Schema.String(), - "ce_datacontenttype": "text/json", - "ce_subject": "topic", - "ce_exta": "someext", - }), - } -) - func TestSendStructuredMessageToStructuredWithKey(t *testing.T) { close, s, r := testSenderReceiver(t) defer close() @@ -153,12 +103,12 @@ func testClient(t testing.TB) sarama.Client { return client } -func testSenderReceiver(t testing.TB, options ...SenderOptionFunc) (func(), binding.Sender, binding.Receiver) { +func testSenderReceiver(t testing.TB, options ...kafka_sarama.SenderOptionFunc) (func(), binding.Sender, binding.Receiver) { client := testClient(t) topicName := "test-ce-client-" + uuid.New().String() - r := NewReceiver(client, TEST_GROUP_ID, topicName) - s, err := NewSender(client, topicName, options...) + r := kafka_sarama.NewReceiver(client, TEST_GROUP_ID, topicName) + s, err := kafka_sarama.NewSender(client, topicName, options...) require.NoError(t, err) return func() { @@ -176,13 +126,3 @@ func BenchmarkSendReceive(b *testing.B) { defer c() // Cleanup test.BenchmarkSendReceive(b, s, r) } - -func mustToSaramaConsumerHeaders(m map[string]string) []*sarama.RecordHeader { - res := make([]*sarama.RecordHeader, len(m)) - i := 0 - for k, v := range m { - res[i] = &sarama.RecordHeader{Key: []byte(k), Value: []byte(v)} - i++ - } - return res -}