Server-Side Swift: Testing on Linux
In this tutorial, you’ll test your server-side Swift apps on Linux, learning the differences between testing on macOS and Linux, and how to use Docker and Docker Compose. By Christian Weinberger.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Server-Side Swift: Testing on Linux
10 mins
Generating a Dockerfile
Well, you’ve already used Linux for the PostgreSQL test database using Docker! So you can also use Docker to run your tests in a Linux environment. In the top level project starter directory, create a new file called Dockerfile (with no extension). Open the file in a text editor and add the following:
# 1
FROM swift:5.3
# 2
WORKDIR /package
# 3
COPY . ./
# 4
CMD ["swift", "test", "--enable-test-discovery"]
The Dockerfile:
- Uses the official Swift 5.3 image.
- Sets the working directory to /package.
- Copies the contents of the current directory into /package in the container.
- Fetches the dependencies and cleans up the project’s build artifacts.
- Sets the default command to
swift test --enable-test-discovery
. This will also fetch dependencies and clean up the project’s build artifacts.
Using Docker Compose
As described earlier, the tests need a PostgreSQL database to run. By default, Docker containers can’t see each other. However, Docker has a tool, Docker Compose, designed to link different containers for testing and running apps. Create a new file called docker-compose.yml in the top level starter project directory. Open the file in an editor and add the following:
# 1
version: '3'
# 2
services:
# 3
til-app:
# 4
depends_on:
- postgres
# 5
build: .
# 6
environment:
- DATABASE_HOST=postgres
- DATABASE_PORT=5432
# 7
postgres:
# 8
image: "postgres"
# 9
environment:
- POSTGRES_DB=vapor-test
- POSTGRES_USER=vapor_username
- POSTGRES_PASSWORD=vapor_password
This code:
- Specifies the Docker Compose version.
- Defines the services for this app.
- Defines a service for the TIL app.
- Sets a dependency on the Postgres container, so Docker Compose starts the Postgres container first.
- Builds the Dockerfile in the current directory — the Dockerfile you just created.
- Injects the
DATABASE_HOST
andDATABASE_PORT
environment variable. Docker Compose has an internal DNS resolver. This
allows thetil-app
container to connect to thepostgres
container with the hostnamepostgres
and port5432
. - Defines a service for the Postgres container.
- Uses the standard Postgres image.
- Sets the same environment variables as were used at the start of the tutorial for the test database.
To test your app on Linux, in Terminal, type the following:
# 1
docker-compose build
# 2
docker-compose up --abort-on-container-exit
This:
- Builds the different Docker containers.
- Spins up the different containers and run the tests.
--abort-on-container-exit
tells Docker Compose to stop thepostgres
container when thetil-app
container stops. Thepostgres
container used for this test is different from, and doesn’t conflict with, the one you ran earlier for testing on macOS.
When the tests finish running, you’ll see the output in Terminal with all tests passing:
swift test --enable-test-discovery
. Your Linux machine will need PostgreSQL (as configured in the Docker example above) and the following packages installed: libssl-dev, libz-dev and pkg-config, which you can install via sudo apt install libssl-dev libz-dev pkg-config
.
To clean up, in Terminal, run the following commands from the root folder of your project:
# 1
docker-compose down
# 2
docker-compose rm
These commands:
- Stop all containers and networks created by
docker-compose
. - Delete the previously created containers now that they are stopped.
Where to Go From Here?
Writing tests and running them on the platforms you’ll use in production is important. It gives you confidence your code will work when you deploy your application. Having a good test suite allows you to evolve and adapt your apps quickly.
In this tutorial, you’ve learned how to test your server-side Swift apps on Linux to ensure that they work correctly. Using Docker and Docker Compose makes testing on Linux simple. You can add different dependencies and services as needed, such as Redis or other databases.
Feel free to take a look at the final project using the Download Materials button at the top or bottom of this tutorial. If you have any questions, join us on the forums below!