Nov 6, 2023
Test Generation for Distributed Apps Made Easy with Skyramp
#kubernetes #testing #openapi #microservices
In the ever-evolving world of software development, the importance of testing to ensure quality cannot be overstated. This rings especially true when it comes to distributed applications with loosely coupled microservices. These kinds of distributed applications bring a host of complexities that require a thoughtful approach to testing. With the advent of testing tools provided by Skyramp, combined with technology building blocks like Kubernetes and OpenAPI, test generation for distributed apps is easier and more efficient than ever before. This blog will dive into the process of generating tests for your distributed applications by showing a step-by-step example using Skyramp and the aforementioned standards.
Talking About Test Generation
Before we delve into the step-by-step walkthrough of generating tests, let's first understand why test generation is beneficial for distributed applications. Distributed applications, most often composed of various microservices, can be particularly intricate in terms of communication, dependencies, and data flow. Manually writing tests for each component can be time-consuming and prone to human error. Automated test generation can streamline this process, ensuring comprehensive coverage and robust testing.
OpenAPI, formerly known as Swagger, is a set of specifications that defines a standard, language-agnostic interface to RESTful APIs. It's often used to document and describe the capabilities of a microservice, including the available endpoints, input/output parameters, and response formats. This can act as the foundation for automated test generation by providing a clear and structured definition of a service.
Leveraging OpenAPI, Skyramp can analyze your microservices and automatically create test cases based on your API specifications and service dependencies. This allows you to verify that the various components of your distributed application work seamlessly together, saving you time. It should be noted that Skyramp supports other protocols for test generation like gRPC and Protocol Buffers, but we'll save that for another day.
Generating Tests: A Step-By-Step Walkthrough
Now, let's get into the nitty-gritty of how to generate tests for your distributed applications using Skyramp. We will use our tried and true sample-microservices repo on GitHub, which is an 11-tier distributed app with REST support already configured for deployment to a Kubernetes cluster. The app itself is a complete web-based e-commerce store created by Google, known as Online Boutique.
Below is the step-by-step walkthrough to guide you along the journey. These steps were tested on Mac, but should work on Linux or Windows with a bash shell.
Step 1: Install the Skyramp CLI
The first step is to install the Skyramp Command Line Interface (CLI). See the Skyramp Docs for specifics, or you can run the following command in your terminal:
NOTE: You will also need Docker installed to continue.
Step 2: Clone the Repository
Next, clone the sample repository discussed above. This contains the code and configuration files necessary for your distributed application.
Step 3: Create a Cluster
Now, create a cluster using the Skyramp CLI. This cluster will serve as the environment in which your microservices will be deployed and tested.
Step 4: Change to the Demo Directory
Navigate to the directory containing the prepared REST demo project under "rest-demo".
Step 5: Deploy the Cluster with Skyramp Deployer
Use the Skyramp Deployer to bring up the microservices within the cluster.
The output will look like this:
Step 6: OpenAPI Configuration
In this project, there is a pre-defined OpenAPI specification file (demo.yaml). This file describes the API endpoints and their expected behavior. Take a peek at the contents of this file.
Notice that the definition centers around charging a credit card. We can use this definition to test the payment service running in our cluster.
Step 7: Generate a Test
Now comes the fun part - generating a test using the Skyramp CLI. You can specify the protocol, API schema, alias, and port for your test. Run the command below to see how this works.
This command will automatically generate test cases for your distributed application based on the provided OpenAPI schema.
Step 8: Tune Your Test
Upon running the test generation command from the step above, Skyramp will create three files that contain your test cases and configuration. You can review and customize these files to fit your specific testing needs. The output will look something like this:
Let's make a few edits to the files that were generated.
1) Change the serviceName
value in the generated "endpoint" file to use the payment service running in the cluster:
2) Change the requestName
parameter in the generated "test" file to scenarioName
and assign the value "scenario_123".
3) Modify the generated "scenario" file to include a scenarios
section at the end of the file that looks like this:
4) Also modify generated parameters and values in the "scenario" file under the requests
section as shown below. Many of the parameters will be auto-generated based on the OpenAPI definition, but we can improve the values.
Step 9: Run and Refine
After completing the edits from above, we are ready to run our test against the payment service. Simply run Skyramp Tester to execute the test scenario we composed.
The output should indicate a passed test such as this:
You can continue to fine-tune your tests by modifying the generated files. Working with these files, you can see the modular approach Skyramp takes to testing. Adjust the test cases, add custom scenarios, and configure test environments as needed to ensure comprehensive testing for your distributed applications.
This Was Only a Test
In conclusion, automated test generation with Skyramp simplifies the testing process for distributed applications. It enhances the efficiency and reliability of testing scenarios, ensuring that your microservices work harmoniously to deliver a robust and error-free application.
By following the step-by-step walkthrough outlined in this blog, you can see how to effectively generate and customize tests for your distributed apps, saving time and ensuring the quality of your software. Happy testing with Skyramp!