Giter Site home page Giter Site logo

microcks / microcks-testcontainers-go Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 3.0 113 KB

Go lib for Testcontainers that enables embedding Microcks into your unit tests with lightweight, throwaway instance thanks to containers.

Home Page: https://microcks.io

License: Apache License 2.0

Makefile 1.18% Go 98.82%
api contract-testing go microcks mocking testcontainers

microcks-testcontainers-go's Introduction

Microcks Testcontainers Go

Go library for Testcontainers that enables embedding Microcks into your Go unit tests with lightweight, throwaway instance thanks to containers

License Project Chat Go version GitHub release

Build Status

Latest released version is v0.1.0.

Current development version is v0.2.0.

How to use it?

Include it into your project dependencies

To get the latest version, use go1.21+ and fetch using the go get command. For example:

go get microcks.io/testcontainers-go@latest

To get a specific version, use go1.21+ and fetch the desired version using the go get command. For example:

go get microcks.io/[email protected]

Startup the container

You just have to specify the container image you'd like to use. This library requires a Microcks uber distribution (with no MongoDB dependency).

import (
    microcks "microcks.io/testcontainers-go"
)

microcksContainer, err := microcks.RunContainer(ctx, testcontainers.WithImage("quay.io/microcks/microcks-uber:nightly"))

Import content in Microcks

To use Microcks mocks or contract-testing features, you first need to import OpenAPI, Postman Collection, GraphQL or gRPC artifacts. Artifacts can be imported as main/Primary ones or as secondary ones. See Multi-artifacts support for details.

You can do it before starting the container using simple paths:

import (
    microcks "microcks.io/testcontainers-go"
)

microcksContainer, err := microcks.RunContainer(ctx, 
    testcontainers.WithImage("quay.io/microcks/microcks-uber:nightly"),
    microcks.WithMainArtifact("testdata/apipastries-openapi.yaml"),
    microcks.WithSecondaryArtifact("testdata/apipastries-postman-collection.json"),
)

or once the container started using ImportAsMainArtifact and ImportAsSecondaryArtifact functions:

status, err := microcksContainer.ImportAsMainArtifact(context.Background(), "testdata/apipastries-openapi.yaml")
if err != nil {
    log.Fatal(err)
}

status, err = microcksContainer.ImportAsSecondaryArtifact(context.Background(), "testdata/apipastries-postman-collection.json")
if err != nil {
    log.Fatal(err)
}

status if the status of the Http response from the microcks container and should be equal to 201 in case of success.

Please refer to our microcks_test for comprehensive example on how to use it.

Using mock endpoints for your dependencies

During your test setup, you'd probably need to retrieve mock endpoints provided by Microcks containers to setup your base API url calls. You can do it like this:

baseApiUrl := microcksContainer.RestMockEndpoint(ctx, "API Pastries", "0.0.1")

The container provides methods for different supported API styles/protocols (Soap, GraphQL, gRPC,...).

The container also provides HttpEndpoint() for raw access to those API endpoints.

Launching new contract-tests

If you want to ensure that your application under test is conformant to an OpenAPI contract (or many contracts), you can launch a Microcks contract/conformance test using the local server port you're actually running:

import (
    client "microcks.io/go-client"
    microcks "microcks.io/testcontainers-go"
)

// Build a new TestRequest.
testRequest := client.TestRequest{
    ServiceId:    "API Pastries:0.0.1",
    RunnerType:   client.TestRunnerTypeOPENAPISCHEMA,
    TestEndpoint: "http://bad-impl:3001",
    Timeout:      2000,
}

testResult, err := microcksContainer.TestEndpoint(context.Background(), &testRequest)
require.NoError(t, err)

require.False(t, testResult.Success)
require.Equal(t, "http://bad-impl:3001", testResult.TestedEndpoint)

The testResult gives you access to all details regarding success of failure on different test cases.

Advanced features with MicrocksContainersEnsemble

The MicrocksContainer referenced above supports essential features of Microcks provided by the main Microcks container. The list of supported features is the following:

  • Mocking of REST APIs using different kinds of artifacts,
  • Contract-testing of REST APIs using OPEN_API_SCHEMA runner/strategy,
  • Mocking and contract-testing of SOAP WebServices,
  • Mocking and contract-testing of GraphQL APIs,
  • Mocking and contract-testing of gRPC APIs.

To support features like Asynchronous API and POSTMAN contract-testing, we introduced MicrocksContainersEnsemble that allows managing additional Microcks services. MicrocksContainersEnsemble allow you to implement Different levels of API contract testing in the Inner Loop with Testcontainers!

A MicrocksContainersEnsemble conforms to Testcontainers lifecycle methods and presents roughly the same interface as a MicrocksContainer. You can create and build an ensemble that way:

import (
    ensemble "microcks.io/testcontainers-go/ensemble"
)

ensembleContainers, err := ensemble.RunContainers(ctx, 
    ensemble.WithMainArtifact("testdata/apipastries-openapi.yaml"),
    ensemble.WithSecondaryArtifact("testdata/apipastries-postman-collection.json"),
)

A MicrocksContainer is wrapped by an ensemble and is still available to import artifacts and execute test methods. You have to access it using:

microcks := ensemble.GetMicrocksContainer();
microcks.ImportAsMainArtifact(...);
microcks.Logs(...);

Please refer to our ensemble tests for comprehensive example on how to use it.

Postman contract-testing

On this ensemble you may want to enable additional features such as Postman contract-testing:

import (
    ensemble "microcks.io/testcontainers-go/ensemble"
)

ensembleContainers, err := ensemble.RunContainers(ctx,
    // Microcks container in ensemble
    ensemble.WithMainArtifact("testdata/apipastries-openapi.yaml"),
    ensemble.WithSecondaryArtifact("testdata/apipastries-postman-collection.json"),

    // Postman container in ensemble
    ensemble.WithPostman(true),
)

You can execute a POSTMAN test using an ensemble that way:

// Build a new TestRequest.
testRequest := client.TestRequest{
    ServiceId:    "API Pastries:0.0.1",
    RunnerType:   client.TestRunnerTypePOSTMAN,
    TestEndpoint: "http://good-impl:3003",
    Timeout:      2000,
}

testResult := ensemble.
    GetMicrocksContainer().
    TestEndpoint(context.Background(), testRequest);

microcks-testcontainers-go's People

Contributors

dependabot[bot] avatar julienbreux avatar lbroudoux avatar mdelapenya avatar oliviernguyenquoc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

microcks-testcontainers-go's Issues

Allow specification of artifacts to load on MicrocksContainer creation

Reason/Context

As of today, loading artifacts into the Microcks container should be done after the container is started-up using 2 methods:

  • importAsMainArtifact(ctx, artifact) or,
  • importAsSecondaryArtifact(ctx, artifact)

However, this way of doing things doesn't really fit with existing modules' logic - all of them have WithSomething() fluent API style methods that allow config definition at creation. Moreover, our approach can sometimes be harder to achieve cause it means you have to retrieve the container once initialized (depending on the test framework, this can be tricky) to import artifacts.

Description

Ideally, we'd like to be able to have something like:

microcksContainer, err := microcks.RunContainer(ctx, 
   testcontainers.WithImage("quay.io/microcks/microcks-uber:nightly"),
   testcontainers.WithPrimaryArtifact("testdata/apipastries-openapi.yaml"))

Implementation ideas

See if implementation ideas from microcks/microcks-testcontainers-java#26 can be reused/adapted here to Go

Add contract-testing support

Reason/Context

We must have feature parity with microcks-testcontainers.java and microcks-testcontainers-go ๐Ÿ˜‰

Description

Adding a TestEndpoint() functino

Implementation ideas

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.