|
1 |
| -= Creating a custom integration test |
| 1 | += Creating a custom integration test |
| 2 | + |
| 3 | +In {ProductName}, you can create your own integration tests to run on all components of a given application before they are deployed. |
| 4 | + |
| 5 | +.Procedure |
| 6 | + |
| 7 | +To create any custom test, complete the following steps: |
| 8 | + |
| 9 | +. In your preferred IDE, create a Tekton pipeline in a `.yaml` file. |
| 10 | + |
| 11 | +. Within that pipeline, create tasks, which define the actual steps of the test that {ProductName} executes against images before deploying them. |
| 12 | + |
| 13 | +. Commit the `.yaml` file to a GitHub repo and add it as an integration test in {ProductName}. |
| 14 | + |
| 15 | +.Procedure with example |
| 16 | + |
| 17 | +To create a custom test that checks that your app serves the text “Hello world!”, complete the following steps: |
| 18 | + |
| 19 | +. In your preferred IDE, create a new `.yaml` file, with a name of your choosing. |
| 20 | + |
| 21 | +. Define a new Tekton pipeline. The following example is the beginning of a pipeline that uses `curl` to check that the app serves the text “Hello world!”. |
| 22 | + |
| 23 | ++ |
| 24 | +Example pipeline file: |
| 25 | + |
| 26 | ++ |
| 27 | +[source] |
| 28 | +---- |
| 29 | +kind: Pipeline |
| 30 | +apiVersion: tekton.dev/v1beta1 |
| 31 | +metadata: |
| 32 | + name: example-pipeline |
| 33 | +spec: |
| 34 | + params: |
| 35 | + - description: 'Snapshot of the application' |
| 36 | + name: SNAPSHOT |
| 37 | + default: '{"components": [{"name":"test-app", "containerImage": "quay.io/example/repo:latest"}]}' |
| 38 | + type: string |
| 39 | + tasks: |
| 40 | +---- |
| 41 | + |
| 42 | +. In the `.pipeline.spec` path, declare a new task. |
| 43 | + |
| 44 | ++ |
| 45 | +Example task declaration: |
| 46 | + |
| 47 | ++ |
| 48 | +[source] |
| 49 | +---- |
| 50 | +tasks: |
| 51 | + - name: task-1 |
| 52 | + description: Placeholder task that prints the Snapshot and outputs standard TEST_OUTPUT |
| 53 | + params: |
| 54 | + - name: SNAPSHOT |
| 55 | + value: $(params.SNAPSHOT) |
| 56 | + taskSpec: |
| 57 | + params: |
| 58 | + - name: SNAPSHOT |
| 59 | + results: |
| 60 | + - name: TEST_OUTPUT |
| 61 | + description: Test output |
| 62 | + steps: |
| 63 | + - image: registry.redhat.io/openshift4/ose-cli:latest |
| 64 | + env: |
| 65 | + - name: SNAPSHOT |
| 66 | + value: $(params.SNAPSHOT) |
| 67 | + script: | |
| 68 | + dnf -y install jq |
| 69 | +
|
| 70 | + echo -e "Example test task for the Snapshot:\n ${SNAPSHOT}" |
| 71 | + // Run custom tests for the given Snapshot here |
| 72 | + // After the tests finish, record the overall result in the RESULT variable |
| 73 | + RESULT="SUCCESS" |
| 74 | +
|
| 75 | + // Output the standardized TEST_OUTPUT result in JSON form |
| 76 | + TEST_OUTPUT=$(jq -rc --arg date $(date +%s) --arg RESULT "${RESULT}" --null-input \ |
| 77 | + '{result: $RESULT, timestamp: $date, failures: 0, successes: 1, warnings: 0}') |
| 78 | + echo -n "${TEST_OUTPUT}" | tee $(results.TEST_OUTPUT.path) |
| 79 | +
|
| 80 | +---- |
| 81 | + |
| 82 | +. Save the `.yaml` file. |
| 83 | + |
| 84 | +.. If you haven’t already, commit this file to a GitHub repository that {ProductName} can access. |
| 85 | + |
| 86 | ++ |
| 87 | +Complete example file: |
| 88 | + |
| 89 | ++ |
| 90 | +[source] |
| 91 | +---- |
| 92 | +kind: Pipeline |
| 93 | +apiVersion: tekton.dev/v1beta1 |
| 94 | +metadata: |
| 95 | + name: example-pipeline |
| 96 | +spec: |
| 97 | + params: |
| 98 | + - description: 'Snapshot of the application' |
| 99 | + name: SNAPSHOT |
| 100 | + default: '{"components": [{"name":"test-app", "containerImage": "quay.io/example/repo:latest"}]}' |
| 101 | + type: string |
| 102 | + - description: 'Namespace where the application is running' |
| 103 | + name: NAMESPACE |
| 104 | + default: "default" |
| 105 | + type: string |
| 106 | + - description: 'Expected output' |
| 107 | + name: EXPECTED_OUTPUT |
| 108 | + default: "Hello World!" |
| 109 | + type: string |
| 110 | + tasks: |
| 111 | + - name: task-1 |
| 112 | + description: Placeholder task that prints the Snapshot and outputs standard TEST_OUTPUT |
| 113 | + params: |
| 114 | + - name: SNAPSHOT |
| 115 | + value: $(params.SNAPSHOT) |
| 116 | + taskSpec: |
| 117 | + params: |
| 118 | + - name: SNAPSHOT |
| 119 | + results: |
| 120 | + - name: TEST_OUTPUT |
| 121 | + description: Test output |
| 122 | + steps: |
| 123 | + - image: registry.redhat.io/openshift4/ose-cli:latest |
| 124 | + env: |
| 125 | + - name: SNAPSHOT |
| 126 | + value: $(params.SNAPSHOT) |
| 127 | + script: | |
| 128 | + dnf -y install jq |
| 129 | + echo -e "Example test task for the Snapshot:\n ${SNAPSHOT}" |
| 130 | + // Run custom tests for the given Snapshot here |
| 131 | + // After the tests finish, record the overall result in the RESULT variable |
| 132 | + RESULT="SUCCESS" |
| 133 | +
|
| 134 | + // Output the standardized TEST_OUTPUT result in JSON form |
| 135 | + TEST_OUTPUT=$(jq -rc --arg date $(date +%s) --arg RESULT "${RESULT}" --null-input \ |
| 136 | + '{result: $RESULT, timestamp: $date, failures: 0, successes: 1, warnings: 0}') |
| 137 | + echo -n "${TEST_OUTPUT}" | tee $(results.TEST_OUTPUT.path) |
| 138 | +---- |
| 139 | + |
| 140 | +. Add your new custom test as an integration test in {ProductName}. |
| 141 | + |
| 142 | +.. For additional instructions on adding an integration test, see Adding an integration test. |
| 143 | + |
| 144 | +.Data injected into the PipelineRun of the integration test |
| 145 | + |
| 146 | +When you create a custom integration test, {ProductName} automatically adds certain parameters and labels to the PipelineRun of the integration test. This section explains what those parameters and labels are, and how they can help you. |
| 147 | + |
| 148 | +Parameters: |
| 149 | + |
| 150 | +* *`SNAPSHOT`*: contains the snapshot of the whole application as a JSON string. This JSON string provides useful information about the test, such as which components {ProductName} is testing, and what git repository and commit {ProductName} is using to build those components. For information about snapshot JSON string, see link:https://github.com/konflux-ci/integration-examples/blob/main/examples/snapshot_json_string_example[an example snapshot JSON string]. |
| 151 | +
|
| 152 | +Labels: |
| 153 | + |
| 154 | +* *`appstudio.openshift.io/application`*: contains the name of the application. |
| 155 | +
|
| 156 | +* *`appstudio.openshift.io/component`*: contains the name of the component. |
| 157 | +
|
| 158 | +* *`appstudio.openshift.io/snapshot`*: contains the name of the snapshot. |
| 159 | +
|
| 160 | +* *`test.appstudio.openshift.io/optional`*: contains the optional flag, which specifies whether or not components must pass the integration test before deployment. |
| 161 | +
|
| 162 | +* *`test.appstudio.openshift.io/scenario`*: contains the name of the integration test (this label ends with "scenario," because each test is technically a custom resource called an `IntegrationTestScenario`). |
| 163 | +
|
| 164 | +
|
| 165 | +.Verification |
| 166 | + |
| 167 | +After adding the integration test to an application, you need to trigger a new build of its components to make {ProductName} run the integration test. Make a commit to the GitHub repositories of your components to trigger a new build. |
| 168 | + |
| 169 | +When the new build is finished, complete the following steps in the {ProductName} console: |
| 170 | + |
| 171 | +. Go to the *Integration tests* tab and select the highlighted name of your test. |
| 172 | + |
| 173 | +. Go to the *Pipeline runs* tab of that test and select the most recent run. |
| 174 | + |
| 175 | +. On the *Details* page, see if the test succeeded for that component. Select the other tabs to view more details. |
| 176 | + |
| 177 | +.. If you used our example script, switch to the *Logs* tab and verify that the test printed “Hello world!”. |
0 commit comments