Skip to content

Commit

Permalink
Adapt CI workflow for molecule 5 (#12)
Browse files Browse the repository at this point in the history
* Adapt CI workflow for molecule 5

* Fix tests
  • Loading branch information
boutetnico authored Apr 27, 2023
1 parent 8c0f650 commit f82a3f7
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 47 deletions.
6 changes: 6 additions & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
# .ansible-lint

exclude_paths:
- .github
- .pre-commit-config.yaml
9 changes: 8 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: sudo apt-get remove --purge -y ansible

- name: Install dependencies
run: pip install ansible ansible-lint docker flake8 molecule molecule-docker pytest-testinfra
run: pip install ansible ansible-lint docker molecule molecule-plugins[docker] pytest-testinfra black

- name: Display versions
run: |
Expand All @@ -34,6 +34,13 @@ jobs:
ansible --version
molecule --version
- name: Lint code
run: |
ansible-lint
black --check .
env:
PY_COLORS: "1"

- name: Run Molecule
run: molecule test
env:
Expand Down
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
13 changes: 0 additions & 13 deletions .yamllint

This file was deleted.

1 change: 1 addition & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
- name: Run geoipupdate
ansible.builtin.command: geoipupdate -f {{ geoipupdate_config_file }}
changed_when: false
7 changes: 1 addition & 6 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
dependency:
name: galaxy
enabled: False
enabled: false

driver:
name: docker
Expand All @@ -16,11 +16,6 @@ platforms:
- name: ansible-role-geoipupdate-ubuntu-2204
image: ubuntu:22.04

lint: |
yamllint .
ansible-lint
flake8
provisioner:
name: ansible
ansible_args:
Expand Down
55 changes: 34 additions & 21 deletions molecule/default/tests/test_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@
import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
os.environ["MOLECULE_INVENTORY_FILE"]
).get_hosts("all")


@pytest.mark.parametrize('name', [
('cron'),
])
@pytest.mark.parametrize(
"name",
[
("cron"),
],
)
def test_packages_are_installed(host, name):
package = host.package(name)
assert package.is_installed


@pytest.mark.parametrize('username,groupname,path', [
('root', 'root', '/usr/local/bin/geoipupdate'),
('root', 'root', '/usr/local/etc/GeoIP.conf'),
])
@pytest.mark.parametrize(
"username,groupname,path",
[
("root", "root", "/usr/local/bin/geoipupdate"),
("root", "root", "/usr/local/etc/GeoIP.conf"),
],
)
def test_geoipupdate_files(host, username, groupname, path):
config = host.file(path)
assert config.exists
Expand All @@ -28,10 +35,13 @@ def test_geoipupdate_files(host, username, groupname, path):
assert config.group == groupname


@pytest.mark.parametrize('username,groupname,path', [
('root', 'root', '/usr/local/share/GeoIP/GeoLite2-Country.mmdb'),
('root', 'root', '/usr/local/share/GeoIP/GeoLite2-City.mmdb'),
])
@pytest.mark.parametrize(
"username,groupname,path",
[
("root", "root", "/usr/local/share/GeoIP/GeoLite2-Country.mmdb"),
("root", "root", "/usr/local/share/GeoIP/GeoLite2-City.mmdb"),
],
)
def test_geoip_database_files_exist(host, username, groupname, path):
file = host.file(path)
assert file.exists
Expand All @@ -40,16 +50,19 @@ def test_geoip_database_files_exist(host, username, groupname, path):
assert file.group == groupname


@pytest.mark.parametrize('file,job', [
(
'geoipupdate',
'@weekly root /usr/local/bin/geoipupdate -f /usr/local/etc/GeoIP.conf'
),
])
@pytest.mark.parametrize(
"file,job",
[
(
"geoipupdate",
"@weekly root /usr/local/bin/geoipupdate -f /usr/local/etc/GeoIP.conf",
),
],
)
def test_cron_files_exist(host, file, job):
cron_file = host.file('/etc/cron.d/' + file)
cron_file = host.file("/etc/cron.d/" + file)
assert cron_file.exists
assert cron_file.is_file
assert cron_file.user == 'root'
assert cron_file.group == 'root'
assert cron_file.user == "root"
assert cron_file.group == "root"
assert cron_file.contains(job)
4 changes: 2 additions & 2 deletions tasks/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- name: Ensure database directory exists
ansible.builtin.file:
group: root
mode: 0755
mode: "0755"
name: "{{ geoipupdate_database_directory }}"
owner: root
state: directory
Expand All @@ -11,7 +11,7 @@
ansible.builtin.template:
dest: "{{ geoipupdate_config_file }}"
group: root
mode: 0640
mode: "0640"
owner: root
src: GeoIP.conf.j2
notify: Run geoipupdate
Expand Down
9 changes: 5 additions & 4 deletions tasks/setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
- name: Download geoipupdate tarball
ansible.builtin.get_url:
dest: /tmp
mode: 0640
url: https://github.com/maxmind/geoipupdate/releases/download/v{{ geoipupdate_version }}/geoipupdate_{{ geoipupdate_version }}_linux_{{ geoipupdate_arch }}.tar.gz
mode: "0640"
url: "https://github.com/maxmind/geoipupdate/releases/download/v{{ geoipupdate_version }}\
/geoipupdate_{{ geoipupdate_version }}_linux_{{ geoipupdate_arch }}.tar.gz"

- name: Unarchive geoipupdate tarball
ansible.builtin.unarchive:
copy: false
dest: /tmp
mode: 0750
mode: "0750"
src: /tmp/geoipupdate_{{ geoipupdate_version }}_linux_{{ geoipupdate_arch }}.tar.gz

- name: Copy geoipupdate binary to its install path
ansible.builtin.copy:
dest: "{{ geoipupdate_path }}"
group: root
mode: 0755
mode: "0755"
owner: root
remote_src: true
src: /tmp/geoipupdate_{{ geoipupdate_version }}_linux_{{ geoipupdate_arch }}/geoipupdate
Expand Down

0 comments on commit f82a3f7

Please sign in to comment.