Skip to content

Commit e176223

Browse files
authored
feat: initial testing framework (PSKD-1682) (#265)
* feat: initial testing framework Signed-off-by: chjmil <[email protected]> * chore: comment out workflow Signed-off-by: chjmil <[email protected]> * docs: update CONTRIBUTING.md Signed-off-by: chjmil <[email protected]> * feat: automated test workflow Signed-off-by: chjmil <[email protected]> * chore: fix working directory Signed-off-by: chjmil <[email protected]> * chore: fix working directory Signed-off-by: chjmil <[email protected]> * fix: workflow paths Signed-off-by: chjmil <[email protected]> * test: remove apply tests Signed-off-by: chjmil <[email protected]> --------- Signed-off-by: chjmil <[email protected]>
1 parent a3121af commit e176223

21 files changed

+1877
-4
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright © 2025, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
name: Default Plan Unit Tests
5+
on:
6+
push:
7+
branches: ['**'] # '*' will cause the workflow to run on all commits to all branches.
8+
9+
jobs:
10+
go-tests:
11+
name: Default Plan Unit Tests
12+
runs-on: ubuntu-latest
13+
environment: terraformSecrets
14+
steps:
15+
- name: Checkout Repository
16+
uses: actions/checkout@v4
17+
- name: Build Docker Image
18+
run: docker build -t viya4-iac-gcp:terratest -f Dockerfile.terratest .
19+
- name: Construct Credential File
20+
run: |
21+
go run create_credentials_file.go > /dev/null || true
22+
env:
23+
GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
24+
GCP_PRIVATE_KEY_ID: ${{ secrets.GCP_PRIVATE_KEY_ID }}
25+
GCP_PRIVATE_KEY: ${{ secrets.GCP_PRIVATE_KEY }}
26+
GCP_CLIENT_EMAIL: ${{ secrets.GCP_CLIENT_EMAIL }}
27+
GCP_CLIENT_ID: ${{ secrets.GCP_CLIENT_ID }}
28+
GCP_CLIENT_CERT_URL: ${{ secrets.GCP_CLIENT_CERT_URL }}
29+
working-directory: test
30+
- name: Run Tests
31+
run: |
32+
docker run \
33+
-v $(pwd)/test/.viya4-tf-gcp-service-account.json:/.viya4-tf-gcp-service-account.json \
34+
-v $(pwd):/viya4-iac-gcp \
35+
viya4-iac-gcp:terratest -v

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ terraform.tfvars
88
.terraform.tfstate.lock.info
99
*.lock.hcl
1010
*.swp
11+
test/testoutput/*

CONTRIBUTING.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# How to Contribute
2-
We'd love to accept your patches and contributions to this project.
3-
We just ask that you follow our contribution guidelines when you do.
2+
This project is community-driven, and we'd love to accept your patches and contributions.
3+
We just ask that you follow our contribution guidelines when you do. Refer
4+
to the [Contributor Handbook](https://sassoftware.github.io/contributor-handbook.html)
5+
for guidance.
46

57
## Contributor License Agreement
68
Contributions to this project must be accompanied by a signed [Contributor Agreement](ContributorAgreement.txt).
7-
You (or your employer) retain the copyright to your contribution; this simply grants us permission to use and redistribute your contributions as part of the project.
9+
You (or your employer) retain the copyright to your contribution; this agreement simply grants
10+
us permission to use and redistribute your contributions as part of the project.
811

912
## Code Reviews
1013
All submissions to this project—including submissions from project members—require
@@ -13,6 +16,16 @@ tests, integration tests, and security scans.
1316

1417
## Pull Request Requirement
1518

19+
### Automated Tests
20+
All contributors are expected to include appropriate tests to ensure code quality
21+
and maintainability. This may include unit and/or integration tests as applicable
22+
to the scope of the changes. We have a developed a Golang testing framework using
23+
[Terratest](https://terratest.gruntwork.io/) for unit tests and are in the process
24+
of developing integration tests. Please refer to our [Testing Philosopy](./docs/user/TestingPhilosophy.md)
25+
documentation for more information on our testing framework. If you need additional
26+
help and guidance, we are happy to help you navigate it by providing continuous
27+
collaboration within the pull request.
28+
1629
### Conventional Commits
1730
All pull requests must follow the [Conventional Commit](https://www.conventionalcommits.org/en/v1.0.0/)
1831
standard for commit messages. This helps maintain a consistent and meaningful
@@ -51,4 +64,4 @@ To ensure that all submissions meet our security and quality standards, we perfo
5164
security scans using internal SAS infrastructure. Contributions might be subjected
5265
to security scans before they can be accepted. Reporting of any Common Vulnerabilities
5366
and Exposures (CVEs) that are detected is not available in this project at this
54-
time.
67+
time.

Dockerfile.terratest

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
ARG GCP_CLI_VERSION=513.0.0
2+
3+
FROM google/cloud-sdk:$GCP_CLI_VERSION-alpine AS gcpcli
4+
FROM golang:1.24
5+
6+
# Install terraform from apt repository, terratest_log_parser, jq
7+
RUN \
8+
apt-get update \
9+
&& apt-get install -y jq lsb-release \
10+
&& wget -O - https://apt.releases.hashicorp.com/gpg \
11+
| gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg \
12+
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" \
13+
| tee /etc/apt/sources.list.d/hashicorp.list \
14+
&& apt update \
15+
&& apt install terraform \
16+
&& ssh-keygen -f ~/.ssh/id_rsa -P "" \
17+
&& go install github.com/gruntwork-io/terratest/cmd/terratest_log_parser@latest
18+
19+
WORKDIR /viya4-iac-gcp/test
20+
21+
# Copy gcloud from the google/cloud-sdk image
22+
COPY --from=gcpcli /google-cloud-sdk /google-cloud-sdk
23+
ENV PATH="/google-cloud-sdk/bin:${PATH}"
24+
25+
# Copy the test directory so it can install the go modules
26+
# during the docker build rather than the docker run
27+
COPY ./test ./
28+
RUN go mod tidy
29+
30+
ENTRYPOINT ["/viya4-iac-gcp/test/terratest_docker_entrypoint.sh"]

docs/user/TerratestDockerUsage.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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

Comments
 (0)