diff --git a/.github/workflows/test-test.yaml b/.github/workflows/test-test.yaml index f74dd8f4..52570509 100644 --- a/.github/workflows/test-test.yaml +++ b/.github/workflows/test-test.yaml @@ -19,17 +19,68 @@ jobs: - name: Test uses: ./terraform-test id: test + env: + TERRAFORM_VERSION: 1.10.0 + with: + path: tests/workflows/test-test/local + + - name: Check Passed + env: + FAILURE_REASON: ${{ steps.test.outputs.failure-reason }} + JUNIT_XML_PATH: ${{ steps.test.outputs.junit-xml-path }} + run: | + if [[ "$FAILURE_REASON" != "" ]]; then + echo "::error:: failure-reason not set correctly" + exit 1 + fi + + if [[ "$JUNIT_XML_PATH" != "" ]]; then + echo "::error:: junit-xml-path should not be set" + exit 1 + fi + + junit: + runs-on: ubuntu-24.04 + name: Junit support + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Test + uses: ./terraform-test + id: test + env: + TERRAFORM_VERSION: 1.11.0 with: path: tests/workflows/test-test/local - name: Check Passed env: FAILURE_REASON: ${{ steps.test.outputs.failure-reason }} + JUNIT_XML_PATH: ${{ steps.test.outputs.junit-xml-path }} run: | if [[ "$FAILURE_REASON" != "" ]]; then echo "::error:: failure-reason not set correctly" exit 1 fi + + if [[ "$JUNIT_XML_PATH" == "" ]]; then + echo "::error:: junit-xml-path should be set" + exit 1 + fi + + # Check the output looks right + if [[ ! -f "$JUNIT_XML_PATH" ]]; then + echo "::error:: junit-xml-path does not point to a file" + exit 1 + fi + + if [[ "$(grep -c ' str: @@ -95,6 +95,7 @@ class Output: type: str = None aliases: list[str] = field(default_factory=list) meta_output: bool = False + available_in: list[Type[Terraform] | Type[OpenTofu]] = field(default_factory=lambda: [Terraform, OpenTofu]) def markdown(self, tool: Tool) -> str: if self.meta_output: @@ -182,6 +183,7 @@ def assert_ordering(self): "plan_path", "json_plan_path", "text_plan_path", + "junit-xml-path", "to_add", "failure-reason", "lock-info", @@ -231,6 +233,8 @@ def markdown(self, tool: Tool) -> str: s += text_chunk(self.outputs_intro) for output in self.outputs: + if tool not in output.available_in: + continue s += text_chunk(output.markdown(tool)) if self.environment_variables: @@ -273,10 +277,10 @@ def action_yaml(self, tool: Tool) -> str: s += '\n' - if [output for output in self.outputs if not output.meta_output]: + if [output for output in self.outputs if not output.meta_output and tool in output.available_in]: s += 'outputs:\n' - for output in self.outputs: + for output in (output for output in self.outputs if not output.meta_output and tool in output.available_in): if output.meta_output: continue diff --git a/docs-gen/actions/test.py b/docs-gen/actions/test.py index 1095f683..c597daba 100644 --- a/docs-gen/actions/test.py +++ b/docs-gen/actions/test.py @@ -12,6 +12,7 @@ from inputs.var_file import var_file from inputs.variables import variables from outputs.failure_reason import failure_reason +from outputs.junit_xml import junit_xml_path test = Action( 'test', @@ -31,6 +32,7 @@ var_file ], outputs=[ + junit_xml_path, dataclasses.replace(failure_reason, description=''' When the job outcome is `failure`, this output may be set. The value may be one of: diff --git a/docs-gen/outputs/junit_xml.py b/docs-gen/outputs/junit_xml.py new file mode 100644 index 00000000..3dff3895 --- /dev/null +++ b/docs-gen/outputs/junit_xml.py @@ -0,0 +1,14 @@ +from action import Output, Terraform + +junit_xml_path = Output( + name='junit-xml-path', + type='string', + description=''' +A test report in JUnit XML format. + +The path is relative to the Actions workspace. + +This will only be available when using Terraform 1.11.0 or later. +''', + available_in = [Terraform] +) diff --git a/image/entrypoints/test.sh b/image/entrypoints/test.sh index 565c2ae6..2fba424e 100755 --- a/image/entrypoints/test.sh +++ b/image/entrypoints/test.sh @@ -26,6 +26,10 @@ function set-test-args() { TEST_ARGS="$TEST_ARGS -filter=$file" done fi + + if [[ "$TOOL_COMMAND_NAME" == "terraform" && $TERRAFORM_VER_MAJOR -ge 1 && $TERRAFORM_VER_MINOR -ge 11 ]]; then + TEST_ARGS="$TEST_ARGS -junit-xml=$STEP_TMP_DIR/test-result.xml" + fi } function test() { @@ -45,6 +49,12 @@ function test() { cat "$STEP_TMP_DIR/terraform_test.stderr" + if [[ -f "$STEP_TMP_DIR/test-result.xml" ]]; then + mkdir -p "$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR" + cp "$STEP_TMP_DIR/test-result.xml" "$GITHUB_WORKSPACE/$WORKSPACE_TMP_DIR/test-result.xml" + set_output junit-xml-path "$WORKSPACE_TMP_DIR/test-result.xml" + fi + if [[ $TEST_EXIT -eq 0 ]]; then # Workaround a bit of stupidity in the terraform test command if grep -q "Success! 0 passed, 0 failed." "$STEP_TMP_DIR/terraform_test.stdout"; then diff --git a/terraform-test/README.md b/terraform-test/README.md index b47e2b3b..8aba6d28 100644 --- a/terraform-test/README.md +++ b/terraform-test/README.md @@ -81,6 +81,16 @@ If the tests fail, the job will stop with a failure status. ## Outputs +* `junit-xml-path` + + A test report in JUnit XML format. + + The path is relative to the Actions workspace. + + This will only be available when using Terraform 1.11.0 or later. + + - Type: string + * `failure-reason` When the job outcome is `failure`, this output may be set. The value may be one of: diff --git a/terraform-test/action.yaml b/terraform-test/action.yaml index 6d1e1e86..c4881c93 100644 --- a/terraform-test/action.yaml +++ b/terraform-test/action.yaml @@ -31,6 +31,13 @@ inputs: required: false outputs: + junit-xml-path: + description: | + A test report in JUnit XML format. + + The path is relative to the Actions workspace. + + This will only be available when using Terraform 1.11.0 or later. failure-reason: description: | When the job outcome is `failure`, this output may be set. The value may be one of: