Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nahsi committed Dec 29, 2023
1 parent 39231ca commit 668ab12
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 55 deletions.
1 change: 0 additions & 1 deletion ansible/.githignore

This file was deleted.

3 changes: 3 additions & 0 deletions ansible/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
files/**
**.pyc
**/__pycache__/
65 changes: 23 additions & 42 deletions ansible/molecule/tests/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,45 @@
os.environ["MOLECULE_INVENTORY_FILE"]).get_hosts("all")


def test_directories(host):
d = host.file("/opt/molecule")
@pytest.mark.parametrize("directory", [
"/opt/nox/nox-1",
"/opt/nox/nox-2",
])
def test_nox_directories(host, directory):
d = host.file(directory)

assert d.is_directory
assert d.exists
assert d.user == "molecule"
assert d.mode == 509

assert d.user == "nox"
assert d.mode == 493

def test_files(host):
f = host.file("/opt/molecule/foo")
def test_nox_cleanup(host):
directory = host.file("/opt/nox/nox-0")
service = host.service("nox-@0")

assert f.is_file
assert f.exists
assert f.content == b'bar'
assert not service.is_running
assert not service.is_enabled
assert not directory.exists


@pytest.mark.parametrize("package", [
"jq",
"unzip",
"python3-dev"
])
def test_is_package_is_installed(host, package):
package = host.package(package)
def test_ipfs_download(host):
f = host.file("/opt/nox/ipfs")

assert package.is_installed
assert f.is_file
assert f.exists


@pytest.mark.parametrize("service", [
"docker",
"dnsmasq"
"nox-@1",
"nox-@2",
])
def test_service_is_running(host, service):
def test_nox_is_running(host, service):
service = host.service(service)

assert service.is_running
assert service.is_enabled


def test_user_cleanup(host):
assert not host.user("deleted").exists


def test_user_created(host):
assert host.user("molecule").exists
assert host.user("molecule").uid == 666
assert "docker" in host.user("molecule").groups


def test_sysctl(host):
assert host.sysctl("vm.swappiness") == 1


def test_dnsmasq_docker_bind(host):
assert host.socket("udp://172.17.0.1:53").is_listening


def test_limits(host):
l = host.run("ulimit -Sn")

assert l.stdout == '65536\n'
assert host.user("nox").exists
assert "nox" in host.user("nox").groups
4 changes: 4 additions & 0 deletions ansible/tasks/00-preflight.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
- nox_instances is not string
quiet: true

- name: convert all elements in nox_instances to strings
set_fact:
nox_instances: "{{ nox_instances | map('string') | list }}"

- name: check "nox_version" variable
tags: always
assert:
Expand Down
6 changes: 5 additions & 1 deletion ansible/tasks/01-cleanup.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- name: get list of all running nox instances
command: systemctl list-units --type=service --no-legend --no-pager nox-@*
register: _running_instances
changed_when: false

- name: extract running nox instance names
vars:
Expand All @@ -9,6 +10,10 @@
| list }}"
set_fact:
_cleanup_instances: "{{ _present_instances | difference(nox_instances) | default([]) }}"
changed_when: false

- debug:
msg: "{{ _cleanup_instances }}"

- name: cleanup nox instances
when: _cleanup_instances | length
Expand All @@ -27,7 +32,6 @@
file:
path: "{{ nox_dir }}/nox-{{ instance }}"
state: absent
mode: 0755
loop: "{{ _cleanup_instances }}"
loop_control:
loop_var: instance
Expand Down
6 changes: 3 additions & 3 deletions ansible/tasks/02-install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
run_once: true
delegate_to: localhost
file:
path: "{{ role_path }}/files/{{ _nox_version }}"
path: "{{ role_path }}/files/nox/{{ _nox_version }}"
state: directory

- name: download nox binary
get_url:
url: "{{ _nox_download_url }}/{{ _nox_bin }}"
dest: "{{ role_path }}/files/{{ _nox_version }}/{{ _nox_bin }}"
dest: "{{ role_path }}/files/nox/{{ _nox_version }}/{{ _nox_bin }}"
checksum: "{{ _nox_checksums }}"
register: _download_bin
until: _download_bin is succeeded
Expand All @@ -64,7 +64,7 @@
run_once: false
delegate_to: "{{ inventory_hostname }}"
copy:
src: "{{ role_path }}/files/{{ _nox_version }}/{{ _nox_bin }}"
src: "{{ role_path }}/files/nox/{{ _nox_version }}/{{ _nox_bin }}"
dest: "{{ nox_dir }}/nox-{{ instance }}/nox"
owner: "{{ nox_user }}"
group: "{{ nox_group }}"
Expand Down
14 changes: 7 additions & 7 deletions ansible/tasks/03-ipfs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
block:
- name: create IPFS directory on localhost
file:
path: "{{ role_path }}/files/ipfs-{{ _ipfs_version }}"
path: "{{ role_path }}/files/ipfs/{{ _ipfs_version }}/{{ _arch }}"
state: directory

- name: download IPFS archive
get_url:
url: "{{ _ipfs_download_url }}"
dest: "{{ role_path }}/files/ipfs-{{ _ipfs_version }}/{{ _ipfs_archive }}"
dest: "{{ role_path }}/files/ipfs/{{ _ipfs_version }}/{{ _ipfs_archive }}"
checksum: "{{ _ipfs_checksums }}"
register: _download_ipfs
until: _download_ipfs is succeeded
Expand All @@ -20,15 +20,15 @@

- name: extract IPFS binary
unarchive:
src: "{{ role_path }}/files/ipfs-{{ _ipfs_version }}/{{ _ipfs_archive }}"
dest: "{{ role_path }}/files/ipfs-{{ _ipfs_version }}"
src: "{{ role_path }}/files/ipfs/{{ _ipfs_version }}/{{ _ipfs_archive }}"
dest: "{{ role_path }}/files/ipfs/{{ _ipfs_version }}/{{ _arch }}"
extra_opts:
- "--transform"
- "s/^kubo/{{ _ipfs_bin }}/"
- "--strip-components=1"
- "kubo/ipfs"

- name: propagate IPFS binary
copy:
src: "{{ role_path }}/files/ipfs-{{ _ipfs_version }}/{{ _ipfs_bin }}"
src: "{{ role_path }}/files/ipfs/{{ _ipfs_version }}/{{ _arch }}/ipfs"
dest: "{{ nox_dir }}/ipfs"
owner: "{{ nox_user }}"
group: "{{ nox_group }}"
Expand Down
1 change: 0 additions & 1 deletion ansible/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ _ipfs_version: "{{ nox_ipfs_version | regex_replace('^(?!v)', 'v') }}"
_ipfs_archive: "kubo_{{ _ipfs_version }}_linux-{{ _arch }}.tar.gz"
_ipfs_download_url: "https://dist.ipfs.io/kubo/{{ _ipfs_version }}/{{ _ipfs_archive }}"
_ipfs_checksums: "sha512:{{ _ipfs_download_url + '.sha512' }}"
_ipfs_bin: "kubo-{{ _arch }}"

0 comments on commit 668ab12

Please sign in to comment.