Integrating Cloud Spanner Emulator
Search engines Cloud’s The company database features globally distributed, dependable, very highly scalable, therefore being an ideal choice for applications with critical functions that need consistent and high performance.
To guarantee that Spanner integrates seamlessly into your apps, as a developer, you must do extensive testing. The goal of integration testing is to confirm that, after modifications to individual components, the various parts of a system function as a whole. For Spanner, integration testing guarantees that data activities in your application, such error handling and transactions, are properly communicated with the database.
This article explains how to use GitHub Actions and the Spanner emulator to build up integration testing for Spanner. For the purpose of quickly developing applications supported by a Spanner database, the emulator imitates the behavior of Spanner outside of Google Cloud.
They will test an example Golang backend service that maintains player profiles for a hypothetical game. These ideas may be used to different services and apps in different languages and sectors of the economy.
Here, you testing the “integration” between Spanner and the profile service to make sure code updates to the service will function as intended in a quick feedback loop. Not every service in our stack has undergone complete end-to-end testing. Before going live in production, testing at that level should be conducted using a real staging setup using Spanner.
Integrated inside the platform where her code resides, GitHub Actions automates the running of tests. Like this, other CI/CD systems will function.
Her application, Profile Service, is dependent on Spanner and uses Spanner emulator, a lightweight offline emulation of a Spanner database.
GitHub Activities: Workflow automation
GitHub Actions are the ideal choice for her automated integration tests since our service code is kept in a GitHub repository.
Your software development process may be automated using GitHub Actions, which is a component of a continuous integration and delivery (CI/CD) platform.
You may establish and carry out automatic actions that are triggered by code changes or scheduled events thanks to its seamless integration with GitHub repositories.
A local testing environment: The Spanner emulator
An very portable utility that operates entirely offline is the Spanner emulator. This eliminates the need for real Spanner instances and allows developers to test their apps against Spanner without paying any cloud fees. This enables quick development cycles and early integration problem discovery.
You should be aware of the following variations and restrictions between a real Spanner database and the Spanner emulator.
Integrity testing setup for the profile service
On Github, you can find the code for the example game application. The integration test for the profile service will be examined first, followed by the process that makes use of Github Actions to allow automated integration testing.
The profile-service main_test.go file contains the integration test for the profile service.
The sections listed below are included in this file:
- Launching Spanner as an emulator.
- Install the schema and any necessary test data in the database and Spanner instance.
- Install the Profile service.
- The actual exams.
- tidying up once the exams are over
- Launching the emulator for Spanner
- They use the test containers-go library as the Spanner emulator is set up as a container image.
This configures the emulator container so that may connect with it on mapped port 9010. In order to facilitate communication between the “emulator” container and any container or process having network access, a Docker network is used for networking.
It’s simple to wait until the container is ready before moving on to the next step thanks to the test containers-go package.
As soon as it’s prepared, you create a Golang struct, get the host information, and expose it as an operating system environment variable. These two will be used subsequently to configure the database and instance.
Install the Spanner package and database
Now that the emulator is operating, will must configure a test instance and database.
Let’s start by configuring the instance:
This creates the instance by using the Spanner instance golang library. The SPANNER_EMULATOR_HOST environment variable has to be set previously for this to function. If not, your Google Cloud project’s Spanner instance would be the one that the Spanner library would be searching for.
They need a schema file for the database setup. It is up to your processes where this schema file originates. During the’make profile-integration’ instructions in the Makefile, you create a copy of the master schema file in this instance. This enables me to get the most current schema pertinent to the profiles of the players.
They can handle schema adjustments in this method so that the emulator can understand them. Without the terminating semicolons, you must transform the schema file into an array of statements.
Launch the service for profiles
- Here, you are launching the profile service as an additional container that can interface with the simulator by using test containers go.
- executing the examinations
- Her integration tests connect to your profile service’s endpoints using the testify assert package.