Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iac-test module mvp #41

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions plugins/action/common/nac_iac_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import absolute_import, division, print_function


__metaclass__ = type

from ansible import constants as C
from ansible.utils.display import Display
from ansible.plugins.action import ActionBase

import os
import iac_test.robot_writer
import iac_test.pabot

display = Display()

class ActionModule(ActionBase):

def run(self, tmp=None, task_vars=None):
results = super(ActionModule, self).run(tmp, task_vars)
results['failed'] = False
results['msg'] = None

data = self._task.args.get('data')
templates = self._task.args.get('templates')
filters = self._task.args.get('filters', "")
tests = self._task.args.get('tests', "")
output = self._task.args.get('output')
include = self._task.args.get('include', [])
exclude = self._task.args.get('exclude', [])
render_only = self._task.args.get('render_only', "")
dry_run = self._task.args.get('dry_run', "")

if output and not os.path.exists(output):
display.warning(f"The provided directory {output} does not appear to exist. Is it a directory?")

writer = iac_test.robot_writer.RobotWriter(data, filters, tests, include, exclude)
writer.write(templates, output)
if not render_only:
iac_test.pabot.run_pabot(output, include, exclude, dry_run)

results["changed"] = True

return results
121 changes: 121 additions & 0 deletions plugins/modules/iac_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from __future__ import absolute_import, division, print_function

__metaclass__ = type

ANSIBLE_METADATA = {"metadata_version": "0.1", "status": ["preview"]}

import os

from ansible.module_utils.basic import AnsibleModule

import iac_test.robot_writer
import iac_test.pabot

import sys
from io import StringIO


def run_module():
module_args = dict(
data=dict(type="list", required=True),
templates=dict(type="str", required=True),
filters=dict(type="str", default="", required=False),
tests=dict(type="str", default="", required=False),
output=dict(type="str", required=True),
include=dict(type="list", default=[], required=False),
exclude=dict(type="list", default=[], required=False),
render_only=dict(type="bool", default=False, required=False),
dry_run=dict(type="bool", default=False, required=False)
)

result = dict(changed=False)

module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)

# import epdb; epdb.set_trace()

data = module.params["data"]
templates = module.params["templates"]
filters = module.params["filters"]
tests = module.params["tests"]
output = module.params["output"]
include = module.params["include"]
exclude = module.params["exclude"]
render_only = module.params["render_only"]
dry_run = module.params["dry_run"]

paths = data
paths.append(templates)
if filters:
paths.append(filters)
if tests:
paths.append(tests)

for path in paths:
if not os.path.exists(path):
module.fail_json(msg=f"The provided directory {path} does not appear to exist!")

for path in paths:
if not os.listdir(path):
module.fail_json(msg=f"The provided directory {path} exists but appears to be empty!")

#### Option #1 ####
#### iac-test class object & methods ####

# writer = iac_test.robot_writer.RobotWriter(data, filters, tests, include, exclude)
# writer.write(templates, output)
# if not render_only:
# iac_test.pabot.run_pabot(outpiac_testut, include, exclude, dry_run)

#### Option #2 ####
#### iac-test cmd line wrapper ####

options = []

for item in data:
options.append("--data")
options.append(item)
options.append("--templates")
options.append(templates)
if filters:
options.append("--filters")
options.append(filters)
if tests:
options.append("--tests")
options.append(tests)
options.append("--output")
options.append(output)
if include:
for item in include:
options.append("--include")
options.append(item)
if exclude:
for item in exclude:
options.append("--exclude")
options.append(item)
if render_only:
options.append("--render-only")
if dry_run:
options.append("--dry-run")

command = ["iac-test"]
command.extend(options)
result["command"] = command

rc, _, _ = module.run_command(command)

if rc > 0 and rc < 251:
result["failed_tests"] = rc

result["rc"] = rc
result["changed"] = True

module.exit_json(**result)


def main():
run_module()


if __name__ == "__main__":
main()
38 changes: 38 additions & 0 deletions roles/test_ndfc_deploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Role Name
=========

A brief description of the role goes here.

Requirements
------------

Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.

Role Variables
--------------

A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.

Dependencies
------------

A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.

Example Playbook
----------------

Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:

- hosts: servers
roles:
- { role: username.rolename, x: 42 }

License
-------

BSD

Author Information
------------------

An optional section for the role authors to include contact information, or a website (HTML is not allowed).
2 changes: 2 additions & 0 deletions roles/test_ndfc_deploy/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# defaults file for test_ndfc_deploy
Empty file.
2 changes: 2 additions & 0 deletions roles/test_ndfc_deploy/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# handlers file for test_ndfc_deploy
52 changes: 52 additions & 0 deletions roles/test_ndfc_deploy/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)

# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker

# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)

min_ansible_version: 2.1

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

#
# Provide a list of supported platforms, and for each platform a list of versions.
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
# To view available platforms and versions (or releases), visit:
# https://galaxy.ansible.com/api/v1/platforms/
#
# platforms:
# - name: Fedora
# versions:
# - all
# - 25
# - name: SomePlatform
# versions:
# - all
# - 1.0
# - 7
# - 99.99

galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.

dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
25 changes: 25 additions & 0 deletions roles/test_ndfc_deploy/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
# tasks file for test_ndfc_deploy

# Force Actions To Run

# - name: Render NDFC Robot Resource File
# cisco.nac_dc_vxlan.template_local:
# src: 'ndfc_common.robot'
# dest: ""
# lstrip_blocks: yes

- name: Create Output Directory
ansible.builtin.file:
path: "{{ playbook_dir }}/output/"
state: directory
mode: '0755'

- name: Run Tests
# cisco.nac_dc_vxlan.common.nac_iac_test:
cisco.nac_dc_vxlan.iac_test:
data:
- "{{ playbook_dir }}/host_vars/{{ MD.fabric.global.name }}"
templates: "{{ role_path }}/templates"
output: "{{ playbook_dir }}/output"
delegate_to: localhost
Empty file.
2 changes: 2 additions & 0 deletions roles/test_ndfc_deploy/tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
localhost

5 changes: 5 additions & 0 deletions roles/test_ndfc_deploy/tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- test_ndfc_deploy
2 changes: 2 additions & 0 deletions roles/test_ndfc_deploy/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# vars file for test_ndfc_deploy
Loading