Jump into Microservices Testing with Docker Compose and Skyramp
Tags: microservices, docker, github, testing
Published: 2023-11-30 17:21:29


Distributed applications leveraging microservices have become a predominant approach for building scalable and maintainable software systems. As developers embrace the microservices paradigm, the need for effective testing solutions becomes increasingly vital. [Skyramp](https://skyramp.dev), a powerful testing tool designed specifically for distributed systems, offers a robust solution to automate the testing of applications built on microservices. 

My [previous blog posts](https://dev.to/search?q=skyramp%20kubernetes%20dan%20gross) have focused on the array of options Skyramp provides for testing distributed applications deployed to Kubernetes clusters. However, Kubernetes is not required to reap the benefits of using Skyramp for test automation. You can also setup and deploy your system-under-test using [Docker Compose](https://docs.docker.com/compose/). In this article, we'll explore how you can leverage Skyramp in conjunction with Docker Compose to streamline your microservices testing process.

For a quick overview on all the points this article will cover, you can check out this short video, "Skyramp & GitHub Codespaces with Docker," as a reference:

{% embed https://www.youtube.com/watch?v=9z0Kaqt_BTI %}

## A Fly-By of Skyramp and Docker Compose

Before diving into the details, let's briefly introduce Skyramp and Docker Compose.

### Skyramp: A Testing Powerhouse for Microservices
Skyramp is a testing tool specifically tailored for distributed systems built on microservices architecture. It provides a comprehensive set of features for automating the testing of various components within a microservices ecosystem. This includes: test generation, functional integration and performance testing, mocking services, hot code reload, and an integrated dashboard. While previous posts have centered around Kubernetes integration, it's important to note that Skyramp is versatile and can be employed with Docker Compose in place of Kubernetes.

Below is an [architecture diagram](https://skyramp.dev/docs/docker/reference/architecture/) showing where the Skyramp components fit in a Docker Compose setup for conducting automated testing:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g16ypcopvp39b6fhw69u.png)

### Docker Compose: Simplifying Orchestration
Docker Compose is a tool for defining and running multi-container Docker applications. It allows developers to define a multi-container environment in a single file, making it easy to set up and manage complex microservices architectures. Docker Compose is an excellent choice for local development and testing, providing a lightweight alternative to full-scale orchestration solutions.

## Setting the Stage

To set some context before jumping into the walkthrough, let's first look at a few key concepts:

### Example Repo from Skyramp
Skyramp provides a sample project, [sample-microservices](https://github.com/letsramp/sample-microservices/tree/docker-compose-demo), which serves as an excellent starting point for demonstrating testing and mocking with a full-featured distributed application. The application is based on Google's [Online Boutique](https://github.com/GoogleCloudPlatform/microservices-demo) repo, which is an e-commerce store consisting of 11 different microservices.  The `docker-compose-demo` branch referenced above showcases how Skyramp can be seamlessly integrated with Docker Compose for testing microservices with no local setup required. You can also clone the repository and explore the structure of the microservices setup for your own purposes. 

### Mocks for Simulating Dependencies
One powerful aspect of Skyramp is its ability to handle dependencies and external services through [mocks](https://skyramp.dev/docs/docker/mocker/how-to-mock-services/). Define mocked services with a [mock description](https://skyramp.dev/docs/docker/mocker/mock-description/) for Skyramp's Mocker to simulate the behavior of external dependencies. This allows you to isolate and test individual microservices in a controlled environment. 

### Dashboard for Test Results 
Skyramp includes a [dashboard](https://skyramp.dev/docs/docker/reference/dashboard/) for organization and analysis of your test results. Integrate the Skyramp dashboard into your testing setup to gain insights into tests, performance metrics, and potential issues. The dashboard enhances visibility, making it easier to identify and address issues in your microservices deployment. To spin up the dashboard for Docker locally with the [CLI](https://skyramp.dev/docs/docker/reference/cli-commands/dashboard/up/), simply run `skyramp dashboard up --docker` from your terminal.

### Docker-Specific Docs
Because Docker Compose is markedly different than Kubernetes in terms of setup and certain behaviors, the Skyramp docs have been tailored to provide [Docker-specific](https://skyramp.dev/docs/docker/) instructions. There you can discover the many ways Skyramp can help you with your testing strategy for Docker Compose deployments.

## A Simple Walkthrough

As mentioned above, we will use the Online Boutique distributed application for our example system-under-test. We can easily create a running test environment using [GitHub Codespaces](https://docs.github.com/en/codespaces). To launch your own environment so that you may follow along, simply click the link below and then click "Create codespace."

[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/letsramp/sample-microservices/tree/docker-compose-demo?machine=standardLinux32gb)

### Bringing Up Services

Once the Codespace launches, you will see a VSCode window in your browser. The repo source files are pre-loaded and a terminal pane is available. In the root folder of the repo, you will see a shell script called `workflow-1.sh`. Executing this script will bring up the system-under-test using a pre-configured `docker-compose.yml` file under the `skyramp/docker-compose` folder. Go ahead and give it a spin from the command line:

```bash
sh workflow-1.sh &
```
At this point you should see a screen similar to this one, which shows the output from the running docker containers:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0rsidopfhblv99xn7fg.png)

The system-under-test deployment is comprised of the services we need to conduct a test of the checkout process, namely the product catalog, cart, payment, and checkout services.

Our pre-defined test scenario in this case will aim to test the checkout flow. First, change directories in the terminal:

```bash
cd skyramp/docker-compose
```
Next, let's go ahead and run our test. The test itself is defined under `tests` as `checkout-test.yml`, which you can review in the VSCode window from the codespace or by navigating [here](https://github.com/letsramp/sample-microservices/blob/docker-compose-demo/skyramp/docker-compose/tests/checkout-test.yaml). To run, execute:

```bash
skyramp tester start checkout-test --address localhost:35142
```
Uh-oh, it looks like the test failed because the checkout process could not connect to the payment service.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92g32tnscsjjdcxu9u14.png)

Truth be told, we did that on purpose. This allows us to demonstrate how we can bring up a mock of the payment service using [Skyramp Mocker](https://www.skyramp.dev/docs/docker/mocker/). Simply run:

```bash
skyramp mocker apply --address localhost:35142
```
The mock is defined under `mocks` as `payment-service-k8s.yaml`, which you can review in the VSCode window from the codespace or by navigating [here](https://github.com/letsramp/sample-microservices/blob/docker-compose-demo/skyramp/docker-compose/mocks/payment-service-k8s.yaml).

With the payment service mock in place, let's re-run our checkout test:

```bash
skyramp tester start checkout-test --address localhost:35142
```

Here we see the test scenario passes, so we have successfully tested our checkout components in concert to validate that the microservices are behaving as expected in the flow. Great work, give yourself a pat on the back.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42u6cexjrv1chjbqu9sq.png)

## Wrapping Up
Incorporating Skyramp with Docker Compose offers a seamless and efficient approach to testing microservices locally. By leveraging Docker Compose's simplicity for setting up multi-container environments and Skyramp's comprehensive testing capabilities, developers can ensure the reliability and performance of their microservices architectures. Whether you're new to microservices testing or looking to enhance your existing workflows, the combination of Skyramp and Docker Compose provides a powerful solution for robust and automated testing. As always, happy testing. 

Finally, please [join our Discord](https://discord.gg/qBnEZM6JTb) for any questions, comments, or feedback.