-
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.
- Loading branch information
Showing
7 changed files
with
261 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
- name: converge | ||
hosts: instance | ||
any_errors_fatal: false | ||
|
||
environment: | ||
NETRC: '' | ||
|
||
roles: | ||
- role: ansible-automysqlbackup |
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,3 @@ | ||
--- | ||
|
||
automysqlbackup_source: git |
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,52 @@ | ||
--- | ||
|
||
role_name_check: 1 | ||
|
||
dependency: | ||
name: galaxy | ||
|
||
driver: | ||
name: docker | ||
|
||
platforms: | ||
- name: instance | ||
image: "bodsch/ansible-${DISTRIBUTION:-debian:12}" | ||
command: ${MOLECULE_DOCKER_COMMAND:-""} | ||
docker_host: "${DOCKER_HOST:-unix://run/docker.sock}" | ||
privileged: true | ||
pre_build_image: true | ||
cgroupns_mode: host | ||
volumes: | ||
- /sys/fs/cgroup:/sys/fs/cgroup:rw | ||
- /var/lib/containerd | ||
tmpfs: | ||
- /run | ||
- /tmp | ||
|
||
provisioner: | ||
name: ansible | ||
ansible_args: | ||
- --diff | ||
- -v | ||
config_options: | ||
defaults: | ||
deprecation_warnings: true | ||
stdout_callback: yaml | ||
gathering: smart | ||
fact_caching: jsonfile | ||
fact_caching_timeout: 8640 | ||
fact_caching_connection: ansible_facts | ||
|
||
scenario: | ||
test_sequence: | ||
- destroy | ||
- dependency | ||
- syntax | ||
- create | ||
- prepare | ||
- converge | ||
- verify | ||
- destroy | ||
|
||
verifier: | ||
name: testinfra |
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,49 @@ | ||
--- | ||
|
||
- name: information | ||
hosts: all | ||
gather_facts: true | ||
|
||
pre_tasks: | ||
- name: arch- / artixlinux | ||
when: | ||
- ansible_distribution | lower == 'archlinux' or | ||
ansible_os_family | lower == 'artix linux' | ||
block: | ||
- name: update pacman system | ||
ansible.builtin.command: | | ||
pacman --refresh --sync --sysupgrade --noconfirm | ||
register: pacman | ||
changed_when: pacman.rc != 0 | ||
failed_when: pacman.rc != 0 | ||
|
||
- name: create depends service | ||
ansible.builtin.copy: | ||
mode: 0755 | ||
dest: /etc/init.d/net | ||
content: | | ||
#!/usr/bin/openrc-run | ||
true | ||
when: | ||
- ansible_os_family | lower == 'artix linux' | ||
|
||
- name: make sure python3-apt is installed (only debian based) | ||
ansible.builtin.package: | ||
name: | ||
- python3-apt | ||
state: present | ||
when: | ||
- ansible_os_family | lower == 'debian' | ||
|
||
- name: update package cache | ||
become: true | ||
ansible.builtin.package: | ||
update_cache: true | ||
|
||
- name: environment | ||
ansible.builtin.debug: | ||
msg: | ||
- "os family : {{ ansible_distribution }} ({{ ansible_os_family }})" | ||
- "distribution version : {{ ansible_distribution_major_version }}" | ||
- "ansible version : {{ ansible_version.full }}" | ||
- "python version : {{ ansible_python.version.major }}.{{ ansible_python.version.minor }}" |
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,126 @@ | ||
# coding: utf-8 | ||
from __future__ import unicode_literals | ||
|
||
from ansible.parsing.dataloader import DataLoader | ||
from ansible.template import Templar | ||
|
||
import json | ||
import pytest | ||
import os | ||
|
||
import testinfra.utils.ansible_runner | ||
|
||
HOST = 'instance' | ||
|
||
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( | ||
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts(HOST) | ||
|
||
|
||
def pp_json(json_thing, sort=True, indents=2): | ||
if type(json_thing) is str: | ||
print(json.dumps(json.loads(json_thing), sort_keys=sort, indent=indents)) | ||
else: | ||
print(json.dumps(json_thing, sort_keys=sort, indent=indents)) | ||
return None | ||
|
||
|
||
def base_directory(): | ||
""" | ||
""" | ||
cwd = os.getcwd() | ||
|
||
if 'group_vars' in os.listdir(cwd): | ||
directory = "../.." | ||
molecule_directory = "." | ||
else: | ||
directory = "." | ||
molecule_directory = f"molecule/{os.environ.get('MOLECULE_SCENARIO_NAME')}" | ||
|
||
return directory, molecule_directory | ||
|
||
|
||
def read_ansible_yaml(file_name, role_name): | ||
""" | ||
""" | ||
read_file = None | ||
|
||
for e in ["yml", "yaml"]: | ||
test_file = f"{file_name}.{e}" | ||
if os.path.isfile(test_file): | ||
read_file = test_file | ||
break | ||
|
||
return f"file={read_file} name={role_name}" | ||
|
||
|
||
@pytest.fixture() | ||
def get_vars(host): | ||
""" | ||
parse ansible variables | ||
- defaults/main.yml | ||
- vars/main.yml | ||
- vars/${DISTRIBUTION}.yaml | ||
- molecule/${MOLECULE_SCENARIO_NAME}/group_vars/all/vars.yml | ||
""" | ||
base_dir, molecule_dir = base_directory() | ||
distribution = host.system_info.distribution | ||
operation_system = None | ||
|
||
if distribution in ['debian', 'ubuntu']: | ||
operation_system = "debian" | ||
elif distribution in ['redhat', 'ol', 'centos', 'rocky', 'almalinux']: | ||
operation_system = "redhat" | ||
elif distribution in ['arch', 'artix']: | ||
operation_system = f"{distribution}linux" | ||
|
||
# print(" -> {} / {}".format(distribution, os)) | ||
# print(" -> {}".format(base_dir)) | ||
|
||
file_defaults = read_ansible_yaml(f"{base_dir}/defaults/main", "role_defaults") | ||
file_vars = read_ansible_yaml(f"{base_dir}/vars/main", "role_vars") | ||
file_distibution = read_ansible_yaml(f"{base_dir}/vars/{operation_system}", "role_distibution") | ||
file_molecule = read_ansible_yaml(f"{molecule_dir}/group_vars/all/vars", "test_vars") | ||
# file_host_molecule = read_ansible_yaml("{}/host_vars/{}/vars".format(base_dir, HOST), "host_vars") | ||
|
||
defaults_vars = host.ansible("include_vars", file_defaults).get("ansible_facts").get("role_defaults") | ||
vars_vars = host.ansible("include_vars", file_vars).get("ansible_facts").get("role_vars") | ||
distibution_vars = host.ansible("include_vars", file_distibution).get("ansible_facts").get("role_distibution") | ||
molecule_vars = host.ansible("include_vars", file_molecule).get("ansible_facts").get("test_vars") | ||
# host_vars = host.ansible("include_vars", file_host_molecule).get("ansible_facts").get("host_vars") | ||
|
||
ansible_vars = defaults_vars | ||
ansible_vars.update(vars_vars) | ||
ansible_vars.update(distibution_vars) | ||
ansible_vars.update(molecule_vars) | ||
# ansible_vars.update(host_vars) | ||
|
||
templar = Templar(loader=DataLoader(), variables=ansible_vars) | ||
result = templar.template(ansible_vars, fail_on_undefined=False) | ||
|
||
return result | ||
|
||
|
||
def local_facts(host): | ||
""" | ||
return local facts | ||
""" | ||
return host.ansible("setup").get("ansible_facts").get("ansible_local").get("automysqlbackup") | ||
|
||
|
||
@pytest.mark.parametrize("dirs", [ | ||
"/etc/automysqlbackup" | ||
]) | ||
def test_directories(host, dirs): | ||
d = host.file(dirs) | ||
assert d.is_directory | ||
assert d.exists | ||
|
||
|
||
@pytest.mark.parametrize("files", [ | ||
"/etc/automysqlbackup/automysqlbackup.conf", | ||
"/usr/local/bin/automysqlbackup", | ||
]) | ||
def test_files(host, files): | ||
f = host.file(files) | ||
assert f.exists | ||
assert f.is_file |
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