Skip to content

Commit

Permalink
Enabling CI and starting refactoring tests
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent Broudoux <[email protected]>
  • Loading branch information
lbroudoux committed Sep 25, 2024
1 parent 0b6c9e9 commit 267ff1e
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: build-test

name: build-verify
on:
push:
branches:
- '**'
branches: [ "main" ]
paths-ignore:
- '.github/**'
- '.gitignore'
Expand All @@ -16,7 +14,7 @@ on:
- '.gitignore'
- 'LICENSE'
- '*.md'

permissions: read-all
jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -35,4 +33,4 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -v ./...
run: go test -v ./...
41 changes: 41 additions & 0 deletions internal/service/order_event_publisher_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
126 changes: 126 additions & 0 deletions internal/test/helper.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 267ff1e

Please sign in to comment.