| 1 |
This directory contains the image integration tests. |
| 2 |
|
| 3 |
# Tests |
| 4 |
Integration tests run on [Cloud Build](https://cloud.google.com/build). |
| 5 |
Run the test with `gcloud builds submit --config=test_{image_type}_cloudbuild.yaml` |
| 6 |
|
| 7 |
# Development |
| 8 |
When writing a test, determine whether it should target the hardened image, |
| 9 |
debug image, or both. Add it to the corresponding test `test_{image_type}_cloudbuild.yaml` |
| 10 |
file. |
| 11 |
|
| 12 |
If there need to be multiple scripts, please suffix the script with the test name in each script. |
| 13 |
|
| 14 |
For example, testing `new_feature` might use three scripts: |
| 15 |
`test_newfeature_initresource.sh`, `test_newfeature_validate.sh`, and `test_newfeature_cleanupresource.sh`. |
| 16 |
|
| 17 |
## Common Steps |
| 18 |
Hardened and debug tests will include common steps that do test setup and cleanup activities. |
| 19 |
They look like: |
| 20 |
|
| 21 |
```yaml |
| 22 |
- name: 'gcr.io/cloud-builders/gcloud' |
| 23 |
entrypoint: 'bash' |
| 24 |
env: |
| 25 |
- 'CLEANUP=$_CLEANUP' |
| 26 |
args: ['cleanup.sh'] |
| 27 |
``` |
| 28 |
|
| 29 |
* `create_vm.sh` creates a VM with the given image project, image name, and metadata. It then caches the VM name in the Cloud Build workspace. |
| 30 |
* `cleanup.sh` deletes the VM created in create_vm.sh. |
| 31 |
* `check_failure.sh` checks for a failure message in the status.txt file from a previous test step. This runs last due to Cloud Build exiting on previous step failures. |
| 32 |
|
| 33 |
## Data |
| 34 |
`data/` contains data that will be loaded as Metadata or onto the VM directly. |
| 35 |
|
| 36 |
## Utils |
| 37 |
Scripts in `util/` contain functions that can be sourced from other test scripts. |
| 38 |
|
| 39 |
* `read_serial.sh` contains a helper to pull the entire serial log for a VM. |
| 40 |
|
| 41 |
## Sharing Data Between Steps |
| 42 |
`/workspace` is used in Cloud Build as a scratch space for specific builds. Some conventions for Confidential Space tests: |
| 43 |
|
| 44 |
* `/workspace/status.txt` contains the success/failure message from test steps. |
| 45 |
`check_failure.sh` looks for a failed message in the step to determine whether |
| 46 |
the cloud build is successful. |
| 47 |
* `workspace/next_start.txt` is used when reading the serial logs. |
| 48 |
|
| 49 |
## Test Failures |
| 50 |
Due to the sequential/only-proceed-with-success nature of Cloud Build, tests |
| 51 |
with non-zero exit codes will cause subsequent steps to fail. This is |
| 52 |
problematic when cleanup of a VM or other resources do not occur. |
| 53 |
|
| 54 |
To avoid this issue, test assertions with non-zero exit codes should shell OR (`||`) the result |
| 55 |
and place a "Test failed" message in `/workspace/status.txt`. |
| 56 |
|
| 57 |
For example, `echo $SERIAL_OUTPUT | grep 'Expected output'` will fail and cancel |
| 58 |
the rest of the Cloud Build on not finding the string "Expected output" in the |
| 59 |
serial log. |
| 60 |
The test writer should modify this line to do: |
| 61 |
|
| 62 |
```bash |
| 63 |
echo $SERIAL_OUTPUT | grep 'Expected output' || echo 'TEST FAILED' > /workspace/status.txt |
| 64 |
# Optionally, for debugging: |
| 65 |
echo $SERIAL_OUTPUT > /workspace/status.txt |
| 66 |
``` |