Skip to content

Commit ffe5768

Browse files
committed
ansible test integration action
1 parent 94af415 commit ffe5768

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: ansible-aws-test-provider
2+
description: create file cloud-config-aws.ini in order to run ansible-test
3+
inputs:
4+
collection_path:
5+
description: Path to the AWS collection to create file in.
6+
required: true
7+
ansible_core_ci_key:
8+
description: ansible core ci key
9+
required: true
10+
stage:
11+
description: session stage
12+
default: "prod"
13+
session_id:
14+
description: aws session identifier
15+
default: ${{ github.head_ref }}
16+
17+
outputs:
18+
configuration_file:
19+
description: aws session details
20+
value: ${{ inputs.collection_path }}/tests/integration/cloud-config-aws.ini
21+
22+
runs:
23+
using: composite
24+
steps:
25+
- name: Set up Python '3.9'
26+
uses: actions/setup-python@v4
27+
with:
28+
python-version: "3.9"
29+
30+
- name: install python required modules
31+
run: pip install requests
32+
shell: bash
33+
34+
- name: create aws session file
35+
run: |
36+
python3 ${{ github.action_path }}/create_aws_session.py
37+
shell: bash
38+
env:
39+
ANSIBLE_CORE_CI_KEY: ${{ inputs.ansible_core_ci_key }}
40+
ANSIBLE_CORE_CI_STAGE: "prod"
41+
ANSIBLE_TEST_CLOUD_CONFIG_FILE: "${{ inputs.collection_path }}/tests/integration/cloud-config-aws.ini"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/python
2+
"""Script to request new aws session using ansible_core_ci_key."""
3+
4+
import json
5+
import logging
6+
import os
7+
import random
8+
import sys
9+
10+
import requests
11+
12+
13+
FORMAT = "[%(asctime)s] - %(message)s"
14+
logging.basicConfig(format=FORMAT)
15+
logger = logging.getLogger("create_aws_session")
16+
logger.setLevel(logging.DEBUG)
17+
18+
19+
def main() -> None:
20+
"""Request new aws session credentials.
21+
22+
:raises ValueError: when ANSIBLE_CORE_CI_KEY environment variable is missing or empty
23+
"""
24+
ansible_core_ci_key = os.environ.get("ANSIBLE_CORE_CI_KEY") or ""
25+
ansible_core_ci_stage = os.environ.get("ANSIBLE_CORE_CI_STAGE") or "prod"
26+
headers = {"Content-Type": "application/json"}
27+
data = {
28+
"config": {"platform": "aws", "version": "sts"},
29+
"auth": {
30+
"remote": {
31+
"key": ansible_core_ci_key,
32+
"nonce": None,
33+
}
34+
},
35+
"threshold": 1,
36+
}
37+
if ansible_core_ci_key == "":
38+
logger.error("Empty or missing environment variable 'ANSIBLE_CORE_CI_KEY'")
39+
raise ValueError("ANSIBLE_CORE_CI_KEY environment variable is empty or missing")
40+
logger.info("data -> %s", json.dumps(data).replace(ansible_core_ci_key, "*******"))
41+
session_id = "".join(random.choice("0123456789abcdef") for _ in range(32))
42+
endpoint_url = (
43+
f"https://ansible-core-ci.testing.ansible.com/{ansible_core_ci_stage}/aws/{session_id}"
44+
)
45+
logger.info("Endpoint URL -> '%s'", endpoint_url)
46+
response = requests.put(endpoint_url, data=json.dumps(data), headers=headers, timeout=10)
47+
logger.info("Status: [%d]", response.status_code)
48+
if response.status_code != 200:
49+
logger.info("Response: %s", response.json())
50+
logger.error("Request failed with [%s]", response.json().get("errorMessage"))
51+
sys.exit(1)
52+
53+
# create ansible-test credential file
54+
credentials = response.json().get("aws").get("credentials")
55+
cloud_config_file = os.environ.get("ANSIBLE_TEST_CLOUD_CONFIG_FILE") or "cloud-config-aws.ini"
56+
access_key = credentials.get("access_key")
57+
secret_key = credentials.get("secret_key")
58+
session_token = credentials.get("session_token")
59+
aws_credentials = [
60+
"[default]",
61+
f"aws_access_key: {access_key}",
62+
f"aws_secret_key: {secret_key}",
63+
f"security_token: {session_token}",
64+
"aws_region: us-east-1",
65+
"ec2_access_key: {{ aws_access_key }}",
66+
"ec2_secret_key: {{ aws_secret_key }}",
67+
"ec2_region: {{ aws_region }}",
68+
]
69+
logger.info("writing aws credentials into file => %s", cloud_config_file)
70+
with open(cloud_config_file, mode="w", encoding="utf-8") as file_writer:
71+
file_writer.write("\n".join(aws_credentials))
72+
73+
74+
if __name__ == "__main__":
75+
main()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: ansible test integration
2+
description: configure cloud environment and run ansible-test integration tests
3+
inputs:
4+
collection_path:
5+
description: Relative path where to run `ansible-test integration` command.
6+
required: true
7+
python_version:
8+
description: Python version to use to run integration tests
9+
required: true
10+
ansible_version:
11+
description: ansible-core version to use to run integration tests
12+
required: true
13+
ansible_test_targets:
14+
description: Integration tests targets
15+
required: false
16+
ansible_test_environment:
17+
description: list of environment variables to set when running ansible-test
18+
required: false
19+
20+
runs:
21+
using: composite
22+
steps:
23+
- name: Set up Python ${{ inputs.python_version }}
24+
uses: actions/setup-python@v4
25+
with:
26+
python-version: ${{ inputs.python_version }}
27+
28+
- name: Install wheel now for faster builds
29+
run: python3 -m pip install wheel --upgrade
30+
shell: bash
31+
32+
- name: Install ansible-core (${{ inputs.ansible_version }})
33+
run: python3 -m pip install https://github.com/ansible/ansible/archive/${{ inputs.ansible_version }}.tar.gz --disable-pip-version-check
34+
shell: bash
35+
36+
- name: Disable selinux with selinux_please_lie_to_me
37+
run: |
38+
python3 -m pip uninstall -y selinux
39+
python3 -m pip install selinux_please_lie_to_me
40+
shell: bash
41+
42+
- name: Install collection python requirements
43+
run: python3 -m pip install -r requirements.txt -r test-requirements.txt
44+
shell: bash
45+
working-directory: ${{ inputs.collection_path }}
46+
47+
- name: Set environment variables
48+
run: echo "${{ inputs.ansible_test_environment }}" >> $GITHUB_ENV
49+
shell: bash
50+
if: inputs.ansible_test_environment != ''
51+
52+
- name: Run integration tests
53+
run: >-
54+
ansible-test integration
55+
--diff
56+
--no-temp-workdir
57+
--color
58+
--skip-tags False
59+
--retry-on-error
60+
--continue-on-error
61+
--python ${{ inputs.python_version }}
62+
-v
63+
${{ inputs.ansible_test_targets }}
64+
shell: bash
65+
working-directory: ${{ inputs.collection_path }}

0 commit comments

Comments
 (0)