-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor module to chaosterraform
- Loading branch information
1 parent
c5cf881
commit 70de17c
Showing
8 changed files
with
167 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
title: Example of chaostoolkit-terraform control | ||
description: Use chaosterraform to deploy terraform code | ||
|
||
controls: | ||
- name: chaosterraform | ||
provider: | ||
type: python | ||
module: chaosterraform.control | ||
arguments: | ||
silent: false | ||
|
||
steady-state-hypothesis: | ||
title: empty | ||
probes: [] | ||
|
||
method: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
terraform { | ||
required_providers { | ||
docker = { | ||
source = "kreuzwerker/docker" | ||
version = "3.0.2" | ||
} | ||
} | ||
} | ||
|
||
provider "docker" { | ||
host = "unix:///var/run/docker.sock" | ||
} | ||
|
||
# Pulls the image | ||
resource "docker_image" "nginx" { | ||
name = "nginx:latest" | ||
} | ||
|
||
# Create a container | ||
resource "docker_container" "foo" { | ||
image = docker_image.nginx.image_id | ||
name = "foo" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from unittest.mock import patch | ||
from chaosterraform import driver | ||
|
||
|
||
class MockShellResponse: | ||
def __init__(self, returncode=0, text=""): | ||
self.returncode = returncode | ||
self.stdout = text | ||
|
||
|
||
@patch("chaosterraform.driver.os.path") | ||
def test_chdir(mocked_path): | ||
mocked_path.exists.return_value = True | ||
|
||
driver.Terraform.instance_ = None | ||
tf_driver = driver.Terraform(chdir="../testfolder/one") | ||
|
||
assert tf_driver._terraform == ["terraform", "-chdir=../testfolder/one"] | ||
|
||
|
||
@patch("subprocess.run") | ||
def test_init_default_args(mocked_run): | ||
mocked_run.return_value = MockShellResponse() | ||
driver.Terraform.instance_ = None | ||
tf_driver = driver.Terraform(silent=True) | ||
tf_driver.terraform_init() | ||
|
||
mocked_run.assert_called_once_with( | ||
["terraform", "init"], | ||
shell=False, | ||
capture_output=True, | ||
text=False, | ||
) | ||
|
||
|
||
@patch("subprocess.run") | ||
def test_apply_default_args(mocked_run): | ||
mocked_run.return_value = MockShellResponse() | ||
driver.Terraform.instance_ = None | ||
tf_driver = driver.Terraform() | ||
tf_driver.apply() | ||
|
||
mocked_run.assert_called_once_with( | ||
["terraform", "apply", "-auto-approve"], | ||
shell=False, | ||
capture_output=False, | ||
text=False, | ||
) | ||
|
||
|
||
@patch("subprocess.run") | ||
def test_apply_verbose(mocked_run): | ||
mocked_run.return_value = MockShellResponse() | ||
driver.Terraform.instance_ = None | ||
tf_driver = driver.Terraform(silent=True) | ||
tf_driver.apply() | ||
|
||
mocked_run.assert_called_once_with( | ||
["terraform", "apply", "-auto-approve"], | ||
shell=False, | ||
capture_output=True, | ||
text=False, | ||
) | ||
|
||
|
||
@patch("subprocess.run") | ||
def test_destroy_default_args(mocked_run): | ||
driver.Terraform.instance_ = None | ||
tf_driver = driver.Terraform() | ||
tf_driver.destroy() | ||
|
||
mocked_run.assert_called_once_with( | ||
["terraform", "destroy", "-auto-approve"], | ||
shell=False, | ||
capture_output=False, | ||
text=False, | ||
) | ||
|
||
|
||
@patch("subprocess.run") | ||
def test_output_strings(mocked_run): | ||
return_text = '{"expected":"value","one": "1","boolean":true,"number":999}' | ||
mocked_run.return_value = MockShellResponse(text=return_text) | ||
driver.Terraform.instance_ = None | ||
tf_driver = driver.Terraform() | ||
result = tf_driver.output() | ||
|
||
mocked_run.assert_called_once_with( | ||
["terraform", "output", "-json"], | ||
shell=False, | ||
capture_output=True, | ||
text=True, | ||
) | ||
assert result.get("expected") == "value" | ||
assert result.get("one") == "1" | ||
assert result.get("boolean") is True | ||
assert result.get("number") == 999 |