3 Tools to Improve Your Code Quality: GitHub Codespaces, Kubernetes, and Skyramp
Tags: github, kubernetes, testing, vscode
Published: 2023-11-02 13:46:01

In the world of developing distributed applications with microservices, code quality is paramount. It can make the difference between a well-functioning application, and one that's prone to bugs and vulnerabilities. To help you maintain and improve your code quality, we'll explore three powerful tools in this blog: [GitHub Codespaces](https://docs.github.com/codespaces), [Kubernetes](https://kubernetes.io/), and [Skyramp](https://www.skyramp.dev). You’ll be able to try this combo for yourself with an instant dev environment.

## Putting the Pieces Together

In previous blog posts, we covered using [GitHub Codespaces for debugging](https://dev.to/dangross/turbocharge-your-debugging-with-skyramps-hot-code-reload-2ckm), [generating tests in a Kubernetes cluster](https://dev.to/dangross/test-generation-for-distributed-apps-made-easy-with-skyramp-1fei), and running [Skyramp's built-in dashboard](https://dev.to/dangross/all-your-tests-and-mocks-in-one-place-with-skyramp-dashboard-pjf) to manage test results. In this blog, we’ll bring the pieces together to show how you can use all these tools to construct and run test scenarios for your system-under-test.

## A Word about Component Testing

Within the space beyond unit testing and prior to end-to-end testing, there is a critical stage known as component testing. [This infodeck](https://martinfowler.com/articles/microservice-testing/#testing-component-introduction) on Martin Fowler's [website](http://martinfowler.com) covering testing strategies for microservices describes component testing this way: 

_"In a microservice architecture, the components are the services themselves. By writing tests at this granularity, the contract of the API is driven through tests from the perspective of a consumer."_

When you're developing a distributed application with microservices, it's essential to ensure that each component functions correctly, both in isolation as well as when integrated with other components. Component testing is the key to ensuring this, and Skyramp is here to assist you in this endeavor.

## Instant Dev Environment

GitHub Codespaces can be used to provide a cloud-based, integrated development environment that makes it easier to set up and run your tests with Skyramp. You can clone your repository, create a dedicated Codespace for your microservices, and then execute tests. A straightforward testing workflow is crucial to identify and fix issues early in the development process, saving you time and reducing headaches in the long run.

Here are some benefits of using GitHub Codespaces for component testing:

* **On-Demand Environments**: Easily spin up a development environment for your microservices project, reducing setup time and eliminating conflicts between dependencies.

* **Consistency**: Ensure that all developers work in identical environments, reducing the "it works on my machine" problem.

* **Collaboration**: Multiple developers can collaborate on the same project without worrying about where to setup and run environments.

* **Easy Test and Debug**: Run tests and debug seamlessly within the Codespace environment using a familiar VSCode interface.

We've prepared a GitHub Codespace from a [branch](https://github.com/letsramp/sample-microservices/tree/devcontainer-dashboard) of the Skyramp `sample-microservices` repo on GitHub. This branch is based on the [Online Boutique](https://github.com/GoogleCloudPlatform/microservices-demo) demo project from Google Cloud. The project is a web-based e-commerce store that consists of an 11-tier microservices application. Skyramp added support for REST, which we will use for the example in this blog.

The GitHub Codespace can be launched from the link below and then clicking "Create Codespace".

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

That will setup a VSCode environment within the browser that looks like this:

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

## Kubernetes Clusters in GitHub Codespaces

For microservices to function as a cohesive system, you need a way to test their interaction in a realistic environment. This is where Kubernetes comes into play. Kubernetes allows you to manage containerized applications in clusters, making it a perfect fit for deploying and testing microservices.

We will be using the Online Boutique application mentioned earlier for our example, which is built around Kubernetes. Skyramp can then be utilized to easily deploy and test the distributed app consisting of various microservices. We have created tests in the repo for testing 4 primary components of the application - Product Catalog, Cart, Payment, and Checkout.

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

Let's get started by changing the directory in the terminal of the Codespace to `skyramp/rest-demo`:

```bash
cd skyramp/rest-demo
```
Then, we'll create a new Kubernetes cluster with the Skyramp CLI:

```bash
skyramp cluster create -l
```
Expected output:
```bash
Creating local cluster     [##############################################################] 100 %
Successfully created local cluster.
Starting/restarting Resolver     [##############################################################] 100 %
Resolver manages DNS changes needed to access the cluster and needs sudo access.
[sudo] updating file /etc/resolv.conf
```
Next, let's bring up the system using Skyramp Deployer: 
```bash
skyramp deployer up rest-app
```
Expected output:
```bash
email-service-745ddcd9c4-6zf8s                 ready
frontend-6f8fc574f7-w6hgb                      ready
product-catalog-service-5467566c8f-5n86m       ready
skyramp-worker-6c8bfdf587-bpf9m                ready
cart-service-56cd75c447-kshpg                  ready
currency-service-5b966cbfb8-tffff              ready
ad-service-7cb787976d-ldjm7                    ready
payment-service-5f9b47cfd4-26r9d               ready
redis-6fc54f68c4-fwg75                         ready
recommendation-service-6f8cd558b8-6cppj        ready
checkout-service-c5546db56-vdxld               ready
shipping-service-599947db74-flqhq              ready
All pods are ready.
```
The system-under-test is now up and running. We have prepared several test scenarios which will be easy to run and then review the results with Skyramp.

## A Complement of Tests

Quality assurance is not limited to early unit tests and final end-to-end tests. You need a comprehensive suite of tests, which include functional integration and performance tests, as well as the component tests highlighted earlier. This is where Skyramp completes the picture. Skyramp is an automated testing platform that can help you create and manage a wide range of tests for this stage of development. 

Skyramp takes a modular approach to testing, as illustrated below with examples taken from the testing scenario we used for this blog. 

* **Endpoints**: Skyramp will use endpoint definitions for networking details of services used in tests as shown in this endpoint YAML file:

```yaml
version: v1
services:
    - name: product-catalog-service
      port: 60000
      alias: product-catalog-service
      protocol: rest
endpoints:
  - name: product-catalog-service-get-products
    path: "/get-products"
    serviceName: product-catalog-service
    methods:
      - type: 'GET'
        name: get-products
```

* **Scenarios**: Skyramp uses scenario definitions for request behavior of service methods or chains of requests, and specific asserts within test scenarios:

```yaml
version: v1
scenarios:
  - name: scenario_123
    steps:
      - requestName: listProductsRequest
      - asserts: requests.listProductsRequest.res[0].id == "OLJCESPC7Z"
      - requestName: addCartRequest
      - asserts: requests.addCartRequest.res.success == "200 OK"
      - requestName: getCartRequest
      - asserts: requests.getCartRequest.res.user_id == "abcde"
      - requestName: chargeRequest
      - asserts: requests.chargeRequest.res.transaction_id != null
      - requestName: checkoutRequest
      - asserts: requests.checkoutRequest.res.items[0].item.product_id == "OLJCESPC7Z"
```

* **Tests**: Skyramp uses test definitions as a starting point to execute the proper behavior of tests:

```yaml
version: v1
test:
  name: "[REST] Checkout system load test testcase"
  target: rest-app
  testPattern:
    - startAt: 1s
      atOnce: 2
      scenarioName: scenario1
      duration: 10s
```

## Running the Test, Checking the Results

We’ve found it incredibly valuable to have a central dashboard that aggregates and visualizes the results of your tests so you can get the most out of these tools, and ensure that your code quality is continually improving. Skyramp provides just that, and it can be run in GitHub Codespaces! Let's fire up the Skyramp Dashboard and run our test scenario against the services in the cluster.

Back to the Codespaces terminal, run this command:

```bash
skyramp dashboard up &
```

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

Click "Open in Browser" from the dialog box popup regarding port forwarding and you will see the Skyramp Dashboard interface in a new browser tab. Click on "Tests". There will be no tests yet to display, but we can change that. Back to the Codespace tab, run Skyramp Tester in the terminal to execute our `full-system` test:

```bash
skyramp tester start full-system -n test-rest-demo
```

Now back to the Skyramp Dashboard tab in the browser and you will see the results of the test run there:

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

Looks like a passing grade! Notice the cells in the table can be expanded to view details of the test results.

You can explore further and even tweak the tests by checking out the YAML files in the `endpoints`, `scenarios`, and `tests` folders under `skyramp/rest-demo`. Also, visit the [Skyramp Docs](https://www.skyramp.dev/docs/) to learn more about all the functionality available for testing with Skyramp.

## The Power of 3

By combining GitHub Codespaces, Kubernetes, and Skyramp, especially with a robust testing dashboard, you can ensure that your code quality remains high throughout the development lifecycle. As a result, your applications will be more reliable, ultimately leading to greater user satisfaction and a stronger competitive edge in the landscape of microservices-based distributed apps. As always, happy testing! 

For more info, visit: [Skyramp.dev](https://www.skyramp.dev) & Join the [Skyramp Community Discord](https://discord.com/invite/qBnEZM6JTb)