Skip to content

Commit

Permalink
add integration tests for the plan_stash module
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Feb 2, 2024
1 parent f5857bd commit fea7b57
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 27 deletions.
1 change: 0 additions & 1 deletion plugins/action/plan_stash.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: Contributors to the Ansible project
Expand Down
29 changes: 3 additions & 26 deletions plugins/modules/plan_stash.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: Contributors to the Ansible project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# Copyright (c) 2024, Aubin Bikouo <abikouo>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

DOCUMENTATION = r"""
---
Expand Down Expand Up @@ -47,27 +47,10 @@
"""

import base64
import keyword

from ansible.module_utils.basic import AnsibleModule


def _is_valid_variable_name(var_name: str) -> bool:
if not isinstance(var_name, str):
return False

if not var_name.isascii():
return False

if not var_name.isidentifier():
return False

if keyword.iskeyword(var_name):
return False

return True


def main() -> None:
module = AnsibleModule(
argument_spec=dict(
Expand All @@ -92,12 +75,6 @@ def main() -> None:
if not data:
module.fail_json(msg="The following file '{0}' is empty.".format(terrafom_plan_file))

if not _is_valid_variable_name(var_name):
module.fail_json(
msg="The variable name '%s' is not valid. Variables must start with a letter or underscore character, and contain only "
"letters, numbers and underscores." % var_name
)

# encode binary data
try:
encoded_data = base64.b64encode(data)
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/targets/plan_stash/files/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
terraform {
required_providers {
random = {
source = "hashicorp/random"
version = "3.6.0"
}
}
}

resource "random_string" "random" {
length = 16
special = true
}
62 changes: 62 additions & 0 deletions tests/integration/targets/plan_stash/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
- name: Create temporary directory to work in
ansible.builtin.tempfile:
state: directory
suffix: .tfplan
register: test_dir

- name: Run tests
block:
- name: Copy terraform configuration into working directory
copy:
src: main.tf
dest: "{{ test_dir.path }}"

- name: Run terraform plan
cloud.terraform.terraform:
force_init: true
project_path: "{{ test_dir.path }}"
plan_file: "{{ test_dir.path }}/terraform_plan_file.tfplan"
check_mode: true

- set_fact:
plan_file_b64encoded: "{{ lookup('file', test_dir.path + '/terraform_plan_file.tfplan') | b64encode }}"

- name: Save the terraform plan into stats
cloud.terraform.plan_stash:
path: "{{ test_dir.path }}/terraform_plan_file.tfplan"
per_host: true
register: plan_stash

- name: Ensure terraform plan file has been saved as expected
ansible.builtin.assert:
that:
- plan_stash is not changed
- '"ansible_stats" in plan_stash'
- '"data" in plan_stash.ansible_stats'
- '"tfplan" in plan_stash.ansible_stats.data'
- plan_stash.ansible_stats.data.tfplan == plan_file_b64encoded
- '"per_host" in plan_stash.ansible_stats'
- plan_stash.ansible_stats.per_host

- name: Save the terraform plan into a custom variable name
cloud.terraform.plan_stash:
path: "{{ test_dir.path }}/terraform_plan_file.tfplan"
var_name: "terraform_plan_custom_variable"
register: plan_stash

- name: Ensure terraform plan file has been saved as expected
ansible.builtin.assert:
that:
- plan_stash is not changed
- '"ansible_stats" in plan_stash'
- '"data" in plan_stash.ansible_stats'
- '"terraform_plan_custom_variable" in plan_stash.ansible_stats.data'
- plan_stash.ansible_stats.data.terraform_plan_custom_variable == plan_file_b64encoded
- '"per_host" in plan_stash.ansible_stats'
- not plan_stash.ansible_stats.per_host

always:
- name: Delete temporary directory
ansible.builtin.file:
path: "{{ test_dir.path }}"
state: absent

0 comments on commit fea7b57

Please sign in to comment.