From 3576a53c01b7b53d6940002a9e926bf944f68571 Mon Sep 17 00:00:00 2001 From: Ondra Machacek Date: Wed, 28 Feb 2024 11:49:31 +0100 Subject: [PATCH] Add tests Signed-off-by: Ondra Machacek --- .github/workflows/test.yml | 30 +++++++++++ Makefile | 3 ++ tests/expectations/mock.json | 51 +++++++++++++++++++ tests/integration/inventory | 2 + tests/integration/targets/init.sh | 5 ++ .../targets/prepare_rest/tasks/main.yml | 26 ++++++++++ tests/integration/targets/security/run.yml | 15 ++++++ tests/integration/targets/security/runme.sh | 3 ++ .../targets/security/tasks/main.yml | 4 ++ tests/integration/targets/vars.yml | 6 +++ 10 files changed, 145 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100644 Makefile create mode 100644 tests/expectations/mock.json create mode 100644 tests/integration/inventory create mode 100644 tests/integration/targets/init.sh create mode 100644 tests/integration/targets/prepare_rest/tasks/main.yml create mode 100644 tests/integration/targets/security/run.yml create mode 100755 tests/integration/targets/security/runme.sh create mode 100644 tests/integration/targets/security/tasks/main.yml create mode 100644 tests/integration/targets/vars.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..daaf5ec9 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,30 @@ +--- +name: Ansible Test +on: # yamllint disable-line rule:truthy + pull_request: + merge_group: + branches: + - main + push: + branches: + - main +permissions: + contents: read +jobs: + ansible_integration_test: + runs-on: ubuntu-latest + steps: + - name: Update pip, git + run: | + set -euxo pipefail + sudo apt-get update + + - name: Checkout repo + uses: actions/checkout@v4 + with: + path: ansible_collections/redhat_cop/vmware_ops + + - name: Run + run: | + make integration + working-directory: ansible_collections/redhat_cop/vmware_ops diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..311e6cd7 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +integration: + ansible-test integration --no-temp-workdir + diff --git a/tests/expectations/mock.json b/tests/expectations/mock.json new file mode 100644 index 00000000..904c13d6 --- /dev/null +++ b/tests/expectations/mock.json @@ -0,0 +1,51 @@ +[ +{ + "httpRequest": { + "method": "POST", + "path": "/rest/com/vmware/cis/session" + }, + "httpResponse": { + "statusCode": 200, + "body": {"value": "72300ca9ff16c5743fa0a6328c8570ce"} + } +}, +{ + "httpRequest": { + "method": "PUT", + "path": "/api/appliance/access/consolecli" + }, + "httpResponse": { + "statusCode": 200, + "headers": { + "Content-Type": "application/json" + }, + "body": "{}" + } +}, +{ + "httpRequest": { + "method": "GET", + "path": "/api/appliance/access/consolecli" + }, + "httpResponse": { + "statusCode": 200, + "headers": { + "Content-Type": "application/json" + }, + "body": "true" + } +}, +{ + "httpRequest": { + "method" : "GET", + "path" : "/api/appliance/networking/firewall/inbound" + }, + "httpResponse": { + "statusCode": 200, + "headers": { + "Content-Type": "application/json" + }, + "body": [{}] + } +} +] diff --git a/tests/integration/inventory b/tests/integration/inventory new file mode 100644 index 00000000..7c937f87 --- /dev/null +++ b/tests/integration/inventory @@ -0,0 +1,2 @@ +[testgroup] +testhost ansible_connection="local" ansible_pipelining="yes" ansible_python_interpreter="/usr/bin/python3" diff --git a/tests/integration/targets/init.sh b/tests/integration/targets/init.sh new file mode 100644 index 00000000..cd18c79e --- /dev/null +++ b/tests/integration/targets/init.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +# shellcheck disable=SC2155,SC2086 + +BASE_DIR=$(dirname "${BASH_SOURCE[0]}") +export ANSIBLE_ROLES_PATH=${BASE_DIR} diff --git a/tests/integration/targets/prepare_rest/tasks/main.yml b/tests/integration/targets/prepare_rest/tasks/main.yml new file mode 100644 index 00000000..70f0e481 --- /dev/null +++ b/tests/integration/targets/prepare_rest/tasks/main.yml @@ -0,0 +1,26 @@ +--- +- name: Install dependencies + ansible.builtin.pip: + name: + - requests + - aiohttp + +- name: Run rest + community.docker.docker_container: + name: vmwarest + image: mockserver/mockserver + state: started + recreate: yes + exposed_ports: + - 1080 + ports: + - 1080:1080 + +- name: Pause + ansible.builtin.pause: + seconds: 10 + +- name: Put data to mockserver + ansible.builtin.command: + cmd: "curl -v -X PUT --data @../../../expectations/mock.json http://localhost:1080/mockserver/expectation" + diff --git a/tests/integration/targets/security/run.yml b/tests/integration/targets/security/run.yml new file mode 100644 index 00000000..f9d8f8bf --- /dev/null +++ b/tests/integration/targets/security/run.yml @@ -0,0 +1,15 @@ +- hosts: localhost + gather_facts: no + collections: + - community.general + vars_files: + - ../vars.yml + + tasks: + - name: Vcsim + ansible.builtin.import_role: + name: prepare_rest + + - name: Import drs rule role + ansible.builtin.import_role: + name: security diff --git a/tests/integration/targets/security/runme.sh b/tests/integration/targets/security/runme.sh new file mode 100755 index 00000000..a4c36631 --- /dev/null +++ b/tests/integration/targets/security/runme.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +source ../init.sh +exec ansible-playbook run.yml diff --git a/tests/integration/targets/security/tasks/main.yml b/tests/integration/targets/security/tasks/main.yml new file mode 100644 index 00000000..ac9b6caa --- /dev/null +++ b/tests/integration/targets/security/tasks/main.yml @@ -0,0 +1,4 @@ +--- +- name: Run + ansible.builtin.import_role: + name: redhat_cop.vmware_ops.security diff --git a/tests/integration/targets/vars.yml b/tests/integration/targets/vars.yml new file mode 100644 index 00000000..018baf98 --- /dev/null +++ b/tests/integration/targets/vars.yml @@ -0,0 +1,6 @@ +security_hostname: "127.0.0.1:1080" +security_username: "user" +security_password: "pass" +security_validate_certs: false + +security_consolecli_enabled: true