Skip to content

Commit

Permalink
add opentofu
Browse files Browse the repository at this point in the history
  • Loading branch information
Nilsty committed Aug 6, 2024
1 parent a0ad3a6 commit a401f9b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
uses: hashicorp/setup-terraform@v3
with:
terraform_version: "1.5.7"

- name: Install OpenTofu
uses: opentofu/setup-opentofu@v1

- name: Run tests
run: |
Expand Down
18 changes: 13 additions & 5 deletions TerraformLibrary/terraformlibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ class TerraformLibrary:
with any terraform script.
"""
def __init__(self, executable="terraform"):
"""
The TerraformLibrary can either use the terraform executable (default) or can be configured
to run OpenTofu instead by setting the executable to `tofu`.
| ***** Settings *****
| Library TerraformLibrary executable=tofu
"""
self.exec = executable

def _run_command(self, command: str, include_stderr: bool = False):
process = subprocess.run(
Expand All @@ -92,7 +100,7 @@ def terraform_init(self, script_path: str):
Returns the return code and the output of the terraform command.
"""
command = f"terraform -chdir={script_path} init -no-color"
command = f"{self.exec} -chdir={script_path} init -no-color"
rc, output = self._run_command(command, include_stderr=True)
return rc, output

Expand All @@ -108,7 +116,7 @@ def terraform_plan(self, script_path: str):
Returns the return code and the output of the terraform command.
"""
command = f"terraform -chdir={script_path} plan -no-color -input=false"
command = f"{self.exec} -chdir={script_path} plan -no-color -input=false"
rc, output = self._run_command(command, include_stderr=True)
return rc, output

Expand All @@ -124,7 +132,7 @@ def terraform_apply(self, script_path: str):
Returns the return code and the output of the terraform command.
"""
command = f"terraform -chdir={script_path} apply -auto-approve -no-color -input=false"
command = f"{self.exec} -chdir={script_path} apply -auto-approve -no-color -input=false"
rc, output = self._run_command(command, include_stderr=True)
return rc, output

Expand All @@ -140,7 +148,7 @@ def terraform_destroy(self, script_path: str):
Returns the return code and the output of the terraform command.
"""
command = f"terraform -chdir={script_path} destroy -auto-approve -no-color -input=false"
command = f"{self.exec} -chdir={script_path} destroy -auto-approve -no-color -input=false"
rc, output = self._run_command(command, include_stderr=True)
return rc, output

Expand Down Expand Up @@ -172,7 +180,7 @@ def get_terraform_state(self, script_path: str):
| Should Be Equal As Strings | ${output["values"]["root_module"]["resources"][0]["name"]} | name of the first resource |
"""
command = f"terraform -chdir={script_path} show --json"
command = f"{self.exec} -chdir={script_path} show --json"
rc, output = self._run_command(command)
output_json = json.loads(output)
return output_json
39 changes: 39 additions & 0 deletions atest/atest-opentofu.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
*** Settings ***
Library TerraformLibrary executable=tofu

*** Variables ***
${TESTDATA} ${CURDIR}/testdata

*** Test Cases ***
Run Terraform Init
${rc} ${output} Terraform Init ${TESTDATA}/simple
Should Be Equal As Integers ${rc} 0
Should Contain ${output} has been successfully initialized!

Run Terraform Plan
Set TF Var my_var test_value
${rc} ${output} Terraform Plan ${TESTDATA}/simple
Should Be Equal As Integers ${rc} 0
Should Contain ${output} Plan: 1 to add, 0 to change, 0 to destroy.
Should Contain ${output} + my_output = "test_value"

Run Terraform Apply
${rc} ${output} Terraform Apply ${TESTDATA}/simple
Should Be Equal As Integers ${rc} 0
Should Contain ${output} Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Should Contain ${output} my_output = "test_value"

Inspect Terraform State
${output} Get Terraform State ${TESTDATA}/simple
Should Be Equal As Strings ${output["values"]["root_module"]["resources"][0]["name"]} foo

Run Terraform Destroy
${rc} ${output} Terraform Destroy ${TESTDATA}/simple
Should Be Equal As Integers ${rc} 0
Should Contain ${output} Destroy complete! Resources: 1 destroyed.
Should Contain ${output} - my_output = "test_value" -> null

Terraform Error Is Raised
${rc} ${output} Terraform Plan ${TESTDATA}/tf-error
Should Be Equal As Integers ${rc} 1
Should Contain ${output} Error: Reference to undeclared input variable
2 changes: 1 addition & 1 deletion atest/atest.robot → atest/atest-terraform.robot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ${TESTDATA} ${CURDIR}/testdata
Run Terraform Init
${rc} ${output} Terraform Init ${TESTDATA}/simple
Should Be Equal As Integers ${rc} 0
Should Contain ${output} Terraform has been successfully initialized!
Should Contain ${output} has been successfully initialized!

Run Terraform Plan
Set TF Var my_var test_value
Expand Down
Loading

0 comments on commit a401f9b

Please sign in to comment.