From 267ff1e073bbdb2bef6cfbc7523467c99baeb231 Mon Sep 17 00:00:00 2001 From: Laurent Broudoux Date: Wed, 25 Sep 2024 22:13:09 +0200 Subject: [PATCH] Enabling CI and starting refactoring tests Signed-off-by: Laurent Broudoux --- .../{worflows => workflows}/build-test.yml | 10 +- .../service/order_event_publisher_test.go | 41 ++++++ internal/test/helper.go | 126 ++++++++++++++++++ 3 files changed, 171 insertions(+), 6 deletions(-) rename .github/{worflows => workflows}/build-test.yml (87%) create mode 100644 internal/service/order_event_publisher_test.go create mode 100644 internal/test/helper.go diff --git a/.github/worflows/build-test.yml b/.github/workflows/build-test.yml similarity index 87% rename from .github/worflows/build-test.yml rename to .github/workflows/build-test.yml index 997877e..922adbc 100644 --- a/.github/worflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -1,9 +1,7 @@ -name: build-test - +name: build-verify on: push: - branches: - - '**' + branches: [ "main" ] paths-ignore: - '.github/**' - '.gitignore' @@ -16,7 +14,7 @@ on: - '.gitignore' - 'LICENSE' - '*.md' - +permissions: read-all jobs: build: runs-on: ubuntu-latest @@ -35,4 +33,4 @@ jobs: run: go build -v ./... - name: Test - run: go test -v ./... + run: go test -v ./... \ No newline at end of file diff --git a/internal/service/order_event_publisher_test.go b/internal/service/order_event_publisher_test.go new file mode 100644 index 0000000..8c0351e --- /dev/null +++ b/internal/service/order_event_publisher_test.go @@ -0,0 +1,41 @@ +/* + * Copyright The Microcks Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package service_test + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/require" + + helper "github.com/microcks/microcks-testcontainers-go-demo/internal/test" +) + +func TestEventIsConsumedAndProcessedByService(t *testing.T) { + t.Run("Test", testEventIsConsumedAndProcessedByService(func(t *testing.T, ts *helper.TestContext) { + // Test code goes here which can leverage the context + fmt.Println("My test code got executed") + })) +} + +func testEventIsConsumedAndProcessedByService(test func(t *testing.T, ts *helper.TestContext)) func(*testing.T) { + return func(t *testing.T) { + tc, err := helper.SetupTestContext(t) + require.NoError(t, err) + defer helper.TeardownTestContext(tc) + test(t, tc) + } +} diff --git a/internal/test/helper.go b/internal/test/helper.go new file mode 100644 index 0000000..58087eb --- /dev/null +++ b/internal/test/helper.go @@ -0,0 +1,126 @@ +/* + * Copyright The Microcks Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package test + +import ( + "context" + "fmt" + "testing" + + "github.com/confluentinc/confluent-kafka-go/v2/kafka" + server "github.com/microcks/microcks-testcontainers-go-demo/cmd/run" + app "github.com/microcks/microcks-testcontainers-go-demo/internal" + "github.com/stretchr/testify/require" + "github.com/testcontainers/testcontainers-go" + "github.com/testcontainers/testcontainers-go/network" + "microcks.io/testcontainers-go/ensemble" + + kafkaTC "microcks.io/testcontainers-go/kafka" + //kafkaTC "github.com/testcontainers/testcontainers-go/modules/kafka" + kafkaCon "microcks.io/testcontainers-go/ensemble/async/connection/kafka" +) + +type TestContext struct { + microcksEnsemble *ensemble.MicrocksContainersEnsemble + kafkaContainer *kafkaTC.KafkaContainer + appServices *app.ApplicationServices +} + +func SetupTestContext(t *testing.T) (*TestContext, error) { + fmt.Println("SetupTestContext called") + ctx := context.Background() + + // Common network. + net, err := network.New(ctx, network.WithCheckDuplicate()) + if err != nil { + require.NoError(t, err) + return nil, err + } + + // Configure and staratup a new KafkaContainer. + kafkaContainer, err := kafkaTC.Run(ctx, + "confluentinc/confluent-local:7.5.0", + network.WithNetwork([]string{"kafka"}, net), + + testcontainers.WithEnv(map[string]string{ + "KAFKA_LISTENERS": "PLAINTEXT://0.0.0.0:9093,BROKER://0.0.0.0:9092,CONTROLLER://0.0.0.0:9094,TC://0.0.0.0:19092", + "KAFKA_LISTENER_SECURITY_PROTOCOL_MAP": "PLAINTEXT:PLAINTEXT,BROKER:PLAINTEXT,CONTROLLER:PLAINTEXT,TC:PLAINTEXT", + "KAFKA_ADVERTISED_LISTENERS": "PLAINTEXT://%s:%d,BROKER://%s:9092,TC://kafka:19092", + }), + ) + t.Cleanup(func() { + if err := kafkaContainer.Terminate(ctx); err != nil { + t.Fatalf("failed to terminate Kafka container: %s", err) + } + }) + + // Configure and staratup a new MicrocksContainersEnsemble. + microcksEnsemble, err := ensemble.RunContainers(ctx, + ensemble.WithMainArtifact("../../testdata/order-service-openapi.yaml"), + ensemble.WithMainArtifact("../../testdata/order-events-asyncapi.yaml"), + ensemble.WithMainArtifact("../../testdata/apipastries-openapi.yaml"), + ensemble.WithSecondaryArtifact("../../testdata/apipastries-postman-collection.json"), + ensemble.WithPostman(), + ensemble.WithAsyncFeature(), + ensemble.WithNetwork(net), + ensemble.WithHostAccessPorts([]int{server.DefaultApplicationPort}), + ensemble.WithKafkaConnection(kafkaCon.Connection{ + BootstrapServers: "kafka:19092", + }), + ) + t.Cleanup(func() { + if err := microcksEnsemble.Terminate(ctx); err != nil { + t.Fatalf("failed to terminate container: %s", err) + } + }) + + brokerURL, err := kafkaContainer.Brokers(ctx) + if err != nil || len(brokerURL) == 0 { + return nil, err + } + fmt.Println("BrokerURL is " + brokerURL[0]) + + // Configure and start the application. + baseApiUrl, err := microcksEnsemble.GetMicrocksContainer().RestMockEndpoint(ctx, "API Pastries", "0.0.1") + require.NoError(t, err) + + applicationProperties := &app.ApplicationProperties{ + PastriesBaseUrl: baseApiUrl, + OrderEventsCreatedTopic: "orders-created", + OrderEventsReviewedTopic: "orders-reviewed", + KafkaConfigMap: &kafka.ConfigMap{ + "bootstrap.servers": brokerURL[0], + "group.id": "order-service", + "auto.offset.reset": "latest", + }, + } + + appServicesChan := make(chan app.ApplicationServices) + go server.Run(*applicationProperties, appServicesChan) + defer server.Close() + + appServices := <-appServicesChan + + return &TestContext{ + microcksEnsemble: microcksEnsemble, + kafkaContainer: kafkaContainer, + appServices: &appServices, + }, nil +} + +func TeardownTestContext(tc *TestContext) { + fmt.Println("TeardownTestContext called") +}