Skip to main content

Use Ansible to test containers in OpenShift 4

Ansible provides an easy and flexible way to test containers in your OpenShift 4 clusters.
Antique toolbox with wooden tools

Image by bluebudgie from Pixabay

Are you developing or maintaining containers to run in OpenShift 4 environments? If so, you will want to test the container to be sure that it is working correctly.

Red Hat provides standalone containers that are designed to work in OpenShift 4 environments, but you still have to be sure the container is running. It is good to test containers within the continuous integration (CI) process. Red Hat provides processes to prepare OpenShift environments for its customers, so they just need to deliver a test case that ensures each container is working correctly. You don't have to be a Red Hat customer, however, to create a test. Here's how you can do it, whether you're a Red Hat customer or working on your own.

Create a test

To test the container under an OpenShift 4 environment, you must provide information about where to download the container, how to deploy it, and how to test it. In this example, I'm using a RHEL with PostgreSQL 10 container.

For example, you can specify the test like this:

registry_redhat_io: "rhscl/postgresql-10-rhel7"
tag_name: "postgresql:10-el7"
deployment: "oc new-app postgresql:10-el7~ \
     --name new-postgresql \
     --context-dir examples/extending-image/ \
     -e POSTGRESQL_USER=user \
     -e POSTGRESQL_PASSWORD=password"
pod_name: "new-postgresql"
add_route: true
test_exec_command: "./files/"
expected_exec_result: "FINE"
check_curl_output: “SOMETHING from curl output”
scl_url: "postgresql-container"
is_name: "postgresql"

Here is what each field means:

  • registry_redhat_io: The image in with the namespace (in this case, rhscl)
  • tag_name: Specifies the image's tag_name
  • deployment: The command for how the image is deployed into the OpenShift 4 environment
  • pod_name: The name of the pod in the OpenShift namespace
  • add_route: Specifies if the route should be exposed or not (by default, it is not)
  • test_exec_command: The file that performs the test
  • expected_exec_result: The expected result of the test from test_exec_command "SOMETHING"
  • check_curl_output: Part of the content from the output of the curl command
  • scl_url: Specifies the repository name in the case of containers
  • is_name: Specifies the container's imagestream

Add a test to the general OpenShift 4 test suite

I used an Ansible test suite from the Software Collections project that can handle OpenShift requirements such as running containers. A simple way to access the test suite is to file a pull request in the GitHub repository.

If you do not want to share the test, you can still add and run it with our test suite.

The steps are:

  1. Clone the ansible-tests repository:
    $ git clone
  2. Change to the cloned repository:
    $ cd ansible-tests
  3. Add your testing suite into the vars directory.  PostgreSQL 10 is already present.
  4. Add your container test suite to this file.

[ Learn How to check deployment health on Red Hat OpenShift. ]

As soon as you create the testing file that you do not want to share, you must add it to a directory.

Run a test

If you have already installed an OpenShift 4 cluster, then testing your container is easy. Use the following steps.

  1. Download the latest version of the OpenShift 4 client for your system from the mirror.
  2. Next, extract the file using tar:
    $ tar -xzvf <FILE>
  3. Download the kubeconfig file. To connect to the OpenShift 4 cluster, you must get the kubeconfig file. Ask your OpenShift 4 cluster administrator for the file. When you have the kubeconfig file available on your system, run:
    $ export KUBECONFIG=<path_to_kubeconfig>/kubeconfig
  4. Run your test in your OpenShift 4 environment.
    1. Switch to the cloned repository:
      $ cd ansible-tests
    2. Finally, execute the test:
      $ make ocp4-tests EXT_TEST=<your_name>

Test your container

You can add tests for your container by following the steps in this article. By running your tests in an OpenShift 4 cluster, you don't have to worry about the cluster's installation and configuration if an administrator sets it up for you. If you want to learn more about OpenShift, visit Red Hat's Hybrid Cloud blog.

Topics:   Ansible   OpenShift   Testing  
Author’s photo

Petr Hracek

Petr is a Senior Software Engineer at Red Hat. He is a contributor to various projects that focus on containers, OpenShift 4, and CI/CD delivery. His motto is: Whatever we can automate and where we can save time for users, let's do it. More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.