This is a cache of https://docs.openshift.com/enterprise/3.1/creating_images/s2i_testing.html. It is a snapshot of the page at 2024-11-21T04:35:49.360+0000.
Testing <strong>s2i</strong> Images | Creating Images | OpenShift Enterprise 3.1
×

Overview

As an Source-to-Image (s2i) builder image author, you can test your s2i image locally and use the OpenShift Enterprise build system for automated testing and continuous integration.

Check the s2i Requirements topic to learn more about the s2i architecture before proceeding.

As described in the s2i Requirements topic, s2i requires the assemble and run scripts to be present in order to successfully execute the s2i build. Providing the save-artifacts script reuses the build artifacts, and providing the usage script ensures that usage information is printed to console when someone runs the Docker image outside of the s2i.

The goal of testing an s2i image is to make sure that all of these described commands work properly, even if the base Docker image has changed or the tooling used by the commands was updated.

Testing Requirements

The standard location for the test script is test/run. This script is invoked by the OpenShift Enterprise s2i image builder and it could be a simple Bash script or a static Go binary.

The test/run script performs the s2i build, so you must have the s2i binary available in your $PATH. If required, follow the installation instructions in the s2i README.

s2i combines the application source code and builder image, so in order to test it you need a sample application source to verify that the source successfully transforms into a runnable Docker image. The sample application should be simple, but it should exercise the crucial steps of assemble and run scripts.

Generating Scripts and Tools

The s2i tooling comes with powerful generation tools to speed up the process of creating a new s2i image. The s2i create command produces all the necessary s2i scripts and testing tools along with the Makefile:

$ s2i create <image name> <destination directory>

The generated test/run script must be adjusted to be useful, but it provides a good starting point to begin developing.

The test/run script produced by the s2i create command requires that the sample application sources are inside the test/test-app directory.

Testing Locally

The easiest way to run the s2i image tests locally is to use the generated Makefile. If you did not use the s2i create command, you can copy the following Makefile template and replace the IMAGE_NAME parameter with your image name.

Example 1. Sample Makefile
IMAGE_NAME = openshift/ruby-20-centos7

build:
	docker build -t $(IMAGE_NAME) .

.PHONY: test
test:
	docker build -t $(IMAGE_NAME)-candidate .
	IMAGE_NAME=$(IMAGE_NAME)-candidate test/run

Basic Testing Workflow

The test script assumes you have already built the image you want to test. If required, first build the s2i image using:

$ docker build -t <BUILDER_IMAGE_NAME>

The following steps describe the default workflow to test s2i image builders:

  1. Verify the usage script is working:

    $ docker run <BUILDER_IMAGE_NAME> .

  2. Build the image:

    $ s2i build file:///path-to-sample-app <BUILDER_IMAGE_NAME> <OUTPUT_APPLICATION_IMAGE_NAME>

  3. Optionally, if you support save-artifacts, execute step 2 once again to verify that saving and restoring artifacts works properly.

  4. Run the container:

    $ docker run <OUTPUT_APPLICATION_IMAGE_NAME>

  5. Verify the container is running and the application is responding.

Executing these steps is generally enough to tell if the builder image is working as expected.

Using OpenShift Enterprise Build for Automated Testing

Another way you can execute the s2i image tests is to use the OpenShift Enterprise platform itself as a continuous integration system. The OpenShift Enterprise platform is capable of building Docker images and is highly customizable.

To set up an s2i image builder continuous integration system, define a Custom build and use the openshift/sti-image-builder image. This image executes all the steps mentioned in the Basic Testing Workflow section and creates a new s2i builder image.

Example 2. Sample CustomBuild
kind: "BuildConfig"
apiVersion: "v1"
metadata:
  name: "ruby-20-centos7-build"
spec:
  triggers:
    - type: "GitHub"
      github:
        secret: "secret101"
  source:
    type: "Git"
    git:
      uri: "git://github.com/sclorg/s2i-ruby-container.git"
  strategy:
    type: "Custom"
    customStrategy:
      from:
        kind: "DockerImage"
        name: "openshift/sti-image-builder"
      env:
        - name: "IMAGE_NAME"
          value: "openshift/ruby-20-centos7"
        - name: "CONTEXT_DIR"
          value: "/2.0/"
      exposeDockerSocket: true
  output:
    to:
      kind: "ImageStreamTag"
      name: "ruby-20-centos7:latest"

You can use the oc create command to create this BuildConfig. After you create the BuildConfig, you can start the build using the following command:

$ oc start-build ruby-20-centos7-build

If your OpenShift Enterprise instance is hosted on a public IP address, the build can be triggered each time you push into your s2i builder image GitHub repository. See webhook triggers for more information.

You can also use the CustomBuild to trigger a rebuild of your application based on the s2i image you updated. See image change triggers for more information.