|
| 1 | +# Using the Terratest Docker Container |
| 2 | + |
| 3 | +Use the Terratest Docker container to run the suite of Terratest Go tests. For more information on Terratest, follow the [Documentation](https://terratest.gruntwork.io/docs/) page. The Terratest Docker image is used by the [Github Workflow](../../.github/workflows/default_plan_unit_tests.yml) as a required check before merging changes. |
| 4 | + |
| 5 | +## Prereqs |
| 6 | + |
| 7 | +- Docker [installed on your workstation](../../README.md#docker). |
| 8 | + |
| 9 | +## Preparation |
| 10 | + |
| 11 | +### Docker image |
| 12 | + |
| 13 | +Run the following command to create the `viya4-iac-gcp-terratest` Docker image using the provided [Dockerfile.terratest](../../Dockerfile.terratest) |
| 14 | + |
| 15 | +```bash |
| 16 | +docker build -t viya4-iac-gcp-terratest -f Dockerfile.terratest . |
| 17 | +``` |
| 18 | + |
| 19 | +The Docker image `viya4-iac-gcp-terratest` will contain Terraform and Go executables, as well as the required Go modules. The Docker entrypoint for the image is `go test`, and it accepts several optional command-line arguments. For more information about command-line arguments, see [Command-Line Arguments](#command-line-arguments). |
| 20 | + |
| 21 | +### Service Account Keyfile for Google Cloud Authentication |
| 22 | + |
| 23 | +Prepare a file with Google Cloud authentication information, as described in [Authenticating Terraform to access Google Cloud](./TerraformGCPAuthentication.md) and store it outside of this repository in a secure file, for example `$HOME/.viya4-tf-gcp-service-account.json`. |
| 24 | + |
| 25 | +#### Public Access Cidrs Environment File |
| 26 | + |
| 27 | +In order to run ```terraform apply``` integration tests, you will also need to define your ```TF_VAR_public_cidrs``` as described in [Admin Access](../CONFIG-VARS.md#admin-access), and create a file with the public access cidr values to use with container invocation. Store these values in [CIDR notation](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) outside of this repository in a secure file, such as `$HOME/.gcp_public_cidrs.env`. Protect that file with public access cidr values so that only you have Read access to it. Below is an example of what the file should look like. |
| 28 | + |
| 29 | +```bash |
| 30 | +TF_VAR_public_cidrs=["123.456.7.8/16", "98.76.54.32/32"] |
| 31 | +``` |
| 32 | + |
| 33 | +Now each time you invoke the container, specify the file with the [`--env-file`](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file) option to pass the Cidrs to the container. |
| 34 | + |
| 35 | +### Docker Volume Mounts |
| 36 | + |
| 37 | +To mount the current working directory, add the following argument to the docker run command: |
| 38 | +`--volume="$(pwd)":/viya4-iac-gcp` |
| 39 | +Note that the project must be mounted to the `/viya4-iac-gcp` directory. |
| 40 | + |
| 41 | +## Command-Line Arguments |
| 42 | + |
| 43 | +The `terratest_docker_entrypoint.sh` script supports several command-line arguments to customize the test execution. Here are the available options: |
| 44 | + |
| 45 | +* `-p, --package=PACKAGE`: The package to test. Default is './...' |
| 46 | +* `-r, --run=TEST`: The name of the test to run. Default is '.\*Plan.\*'. |
| 47 | +* `-v, --verbose`: Run the tests in verbose mode. |
| 48 | +* `-h, --help`: Display the help message. |
| 49 | + |
| 50 | +## Running Terratest Commands |
| 51 | + |
| 52 | +### Running the Plan Tests |
| 53 | + |
| 54 | +To run the suite of unit tests (only `terraform plan`), run the following Docker command: |
| 55 | + |
| 56 | +```bash |
| 57 | +# Run from the ./viya4-iac-gcp directory |
| 58 | +docker run --rm \ |
| 59 | + --volume $HOME/.viya4-tf-gcp-service-account.json:/.viya4-tf-gcp-service-account.json \ |
| 60 | + --volume "$(pwd)":/viya4-iac-gcp \ |
| 61 | + viya4-iac-gcp-terratest |
| 62 | +``` |
| 63 | + |
| 64 | +### Running the Apply Tests |
| 65 | + |
| 66 | +To run the suite of integration tests (only `terraform apply`), run the following Docker command: |
| 67 | + |
| 68 | +```bash |
| 69 | +# Run from the ./viya4-iac-gcp directory |
| 70 | +docker run --rm \ |
| 71 | + --volume $HOME/.viya4-tf-gcp-service-account.json:/.viya4-tf-gcp-service-account.json \ |
| 72 | + --env-file=$HOME/.gcp_public_cidrs.env \ |
| 73 | + --volume "$(pwd)":/viya4-iac-gcp \ |
| 74 | + viya4-iac-gcp-terratest \ |
| 75 | + -r=".*Apply.*" |
| 76 | +``` |
| 77 | + |
| 78 | +### Running a Specific Go Test |
| 79 | + |
| 80 | +To run a specific test, run the following Docker command with the `-r` option: |
| 81 | + |
| 82 | +```bash |
| 83 | +# Run from the ./viya4-iac-gcp directory |
| 84 | +docker run --rm \ |
| 85 | + --volume $HOME/.viya4-tf-gcp-service-account.json:/.viya4-tf-gcp-service-account.json \ |
| 86 | + --env-file=$HOME/.gcp_public_cidrs.env \ #env file for integration tests |
| 87 | + --volume "$(pwd)":/viya4-iac-gcp \ |
| 88 | + viya4-iac-gcp-terratest \ |
| 89 | + -r="YourTest" |
| 90 | +``` |
| 91 | +To run multiple tests, pass in a regex to the `-r` option - "TestName1|TestName2|TestName3" |
| 92 | + |
| 93 | +#### Running a Specific Integration Go Test |
| 94 | + |
| 95 | +To run a specific integration test, modify the main test runner function (e.g. YourIntegrationTestMainFunction) to define the test name you desire and run the following Docker command with the `-r` option: |
| 96 | + |
| 97 | +```bash |
| 98 | +# Run from the ./viya4-iac-gcp directory |
| 99 | +docker run --rm \ |
| 100 | + --volume $HOME/.viya4-tf-gcp-service-account.json:/.viya4-tf-gcp-service-account.json \ |
| 101 | + --env-file=$HOME/.gcp_public_cidrs.env \ |
| 102 | + --volume "$(pwd)":/viya4-iac-gcp \ |
| 103 | + viya4-iac-gcp-terratest \ |
| 104 | + -r="YourIntegrationTestMainFunction" |
| 105 | +``` |
| 106 | + |
| 107 | +### Running a Specific Go Package and Test |
| 108 | + |
| 109 | +If you want to specify the Go package and test name, run the following Docker command with the following options: |
| 110 | + |
| 111 | +```bash |
| 112 | +# Run from the ./viya4-iac-gcp directory |
| 113 | +docker run --rm \ |
| 114 | + --volume $HOME/.viya4-tf-gcp-service-account.json:/.viya4-tf-gcp-service-account.json \ |
| 115 | + --volume "$(pwd)":/viya4-iac-gcp \ |
| 116 | + viya4-iac-gcp-terratest \ |
| 117 | + -r="YourTest" \ |
| 118 | + -p="YourPackage" |
| 119 | +``` |
| 120 | + |
| 121 | +#### Running a Specific Integration Go Package and Test |
| 122 | + |
| 123 | +To run a specific integration Go package and test name, modify the main test runner function in the desired packaged to define the test name you want and run the following Docker command with the following options: |
| 124 | + |
| 125 | +```bash |
| 126 | +# Run from the ./viya4-iac-gcp directory |
| 127 | +docker run --rm \ |
| 128 | + --volume $HOME/.viya4-tf-gcp-service-account.json:/.viya4-tf-gcp-service-account.json \ |
| 129 | + --env-file=$HOME/.gcp_public_cidrs.env \ |
| 130 | + --volume "$(pwd)":/viya4-iac-gcp \ |
| 131 | + viya4-iac-gcp-terratest \ |
| 132 | + -r="YourIntegrationTestMainFunction" \ |
| 133 | + -p="YourPackage" |
| 134 | +``` |
| 135 | + |
| 136 | +### Running the Go Tests with verbose mode |
| 137 | + |
| 138 | +If you want to run the tests in verbose mode, run the Docker command with the `-v` option: |
| 139 | + |
| 140 | +```bash |
| 141 | +# Run from the ./viya4-iac-gcp directory |
| 142 | +docker run --rm \ |
| 143 | + --volume $HOME/.viya4-tf-gcp-service-account.json:/.viya4-tf-gcp-service-account.json \ |
| 144 | + --volume "$(pwd)":/viya4-iac-gcp \ |
| 145 | + viya4-iac-gcp-terratest -v |
| 146 | +``` |
| 147 | + |
| 148 | +### Accessing test run logs |
| 149 | + |
| 150 | +After you have started the Docker container, log files are created in the `./viya4-iac-gcp/test/test_output` directory. These files enable you to view the test results in XML format, as well as test logs that are generated by the terrratest_log_parser. |
0 commit comments