From d6e7e2ffecf84fbb95866492794132c940a69c70 Mon Sep 17 00:00:00 2001 From: Mahendra Paipuri Date: Sat, 24 Feb 2024 18:09:59 +0100 Subject: [PATCH] Fix more failing tests Signed-off-by: Mahendra Paipuri --- .../alternative/tests/test_alternative.py | 38 ++++----- .../molecule/default/tests/test_default.py | 81 ++++++++----------- .../molecule/latest/tests/test_latest.py | 35 +++----- roles/nvidia_dcgm_exporter/tasks/install.yml | 5 +- 4 files changed, 66 insertions(+), 93 deletions(-) diff --git a/roles/nvidia_dcgm_exporter/molecule/alternative/tests/test_alternative.py b/roles/nvidia_dcgm_exporter/molecule/alternative/tests/test_alternative.py index d05ff71..e3137c0 100644 --- a/roles/nvidia_dcgm_exporter/molecule/alternative/tests/test_alternative.py +++ b/roles/nvidia_dcgm_exporter/molecule/alternative/tests/test_alternative.py @@ -3,29 +3,31 @@ import os import testinfra.utils.ansible_runner +import pytest testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') -def test_directories(host): - dirs = [] - for dir in dirs: - d = host.file(dir) - assert not d.exists - - -# def test_service(host): -# s = host.service("dcgm_exporter") -# try: -# assert s.is_running -# except AssertionError: -# # Capture service logs -# journal_output = host.run('journalctl -u dcgm_exporter --since "1 hour ago"') -# print("\n==== journalctl -u dcgm_exporter Output ====\n") -# print(journal_output) -# print("\n============================================\n") -# raise # Re-raise the original assertion error +@pytest.mark.parametrize("dir", [ + "/etc/dcgm_exporter", +]) +def test_directories(host, dir): + d = host.file(dir) + assert d.is_directory + assert d.exists + + +@pytest.mark.parametrize("file", [ + "/etc/systemd/system/dcgm_exporter.service", + "/usr/local/bin/dcgm_exporter", + "/etc/dcgm_exporter/counters.csv", + "/etc/dcgm_exporter/config.yaml", +]) +def test_files(host, file): + f = host.file(file) + assert f.exists + assert f.is_file def test_protecthome_property(host): diff --git a/roles/nvidia_dcgm_exporter/molecule/default/tests/test_default.py b/roles/nvidia_dcgm_exporter/molecule/default/tests/test_default.py index fe3b59e..5469e54 100644 --- a/roles/nvidia_dcgm_exporter/molecule/default/tests/test_default.py +++ b/roles/nvidia_dcgm_exporter/molecule/default/tests/test_default.py @@ -3,46 +3,44 @@ import os import testinfra.utils.ansible_runner +import pytest testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') -def test_directories(host): - dirs = [ - "/etc/dcgm_exporter" - ] - for dir in dirs: - d = host.file(dir) - assert d.is_directory - assert d.exists - - -def test_files(host): - files = [ - "/etc/systemd/system/dcgm_exporter.service", - "/usr/local/bin/dcgm_exporter", - "/etc/dcgm_exporter/counters.csv" - ] - for file in files: - f = host.file(file) - assert f.exists - assert f.is_file - - -def test_permissions_didnt_change(host): - dirs = [ - "/etc", - "/root", - "/usr", - "/var" - ] - for file in dirs: - f = host.file(file) - assert f.exists - assert f.is_directory - assert f.user == "root" - assert f.group == "root" +@pytest.mark.parametrize("dir", [ + "/etc/dcgm_exporter", +]) +def test_directories(host, dir): + d = host.file(dir) + assert d.is_directory + assert d.exists + + +@pytest.mark.parametrize("file", [ + "/etc/systemd/system/dcgm_exporter.service", + "/usr/local/bin/dcgm_exporter", + "/etc/dcgm_exporter/counters.csv" +]) +def test_files(host, file): + f = host.file(file) + assert f.exists + assert f.is_file + + +@pytest.mark.parametrize("dir", [ + "/etc", + "/root", + "/usr", + "/var", +]) +def test_permissions_didnt_change(host, dir): + f = host.file(dir) + assert f.exists + assert f.is_directory + assert f.user == "root" + assert f.group == "root" def test_user(host): @@ -52,19 +50,6 @@ def test_user(host): assert host.user("dcgmexp").home == "/" -# def test_service(host): -# s = host.service("dcgm_exporter") -# try: -# assert s.is_running -# except AssertionError: -# # Capture service logs -# journal_output = host.run('journalctl -u dcgm_exporter --since "1 hour ago"') -# print("\n==== journalctl -u dcgm_exporter Output ====\n") -# print(journal_output) -# print("\n============================================\n") -# raise # Re-raise the original assertion error - - def test_protecthome_property(host): s = host.service("dcgm_exporter") p = s.systemd_properties diff --git a/roles/nvidia_dcgm_exporter/molecule/latest/tests/test_latest.py b/roles/nvidia_dcgm_exporter/molecule/latest/tests/test_latest.py index c638fc1..6a69d8b 100644 --- a/roles/nvidia_dcgm_exporter/molecule/latest/tests/test_latest.py +++ b/roles/nvidia_dcgm_exporter/molecule/latest/tests/test_latest.py @@ -9,6 +9,15 @@ os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') +@pytest.mark.parametrize("dir", [ + "/etc/dcgm_exporter", +]) +def test_directories(host, dir): + d = host.file(dir) + assert d.is_directory + assert d.exists + + @pytest.mark.parametrize("files", [ "/etc/systemd/system/dcgm_exporter.service", "/usr/local/bin/dcgm_exporter" @@ -17,29 +26,3 @@ def test_files(host, files): f = host.file(files) assert f.exists assert f.is_file - - -def test_directories(host): - dirs = [] - for dir in dirs: - d = host.file(dir) - assert d.is_directory - assert d.exists - - -# def test_service(host): -# s = host.service("dcgm_exporter") -# try: -# assert s.is_running -# except AssertionError: -# # Capture service logs -# journal_output = host.run('journalctl -u dcgm_exporter --since "1 hour ago"') -# print("\n==== journalctl -u dcgm_exporter Output ====\n") -# print(journal_output) -# print("\n============================================\n") -# raise # Re-raise the original assertion error - - -# def test_socket(host): -# s = host.socket("tcp://0.0.0.0:9010") -# assert s.is_listening diff --git a/roles/nvidia_dcgm_exporter/tasks/install.yml b/roles/nvidia_dcgm_exporter/tasks/install.yml index c0d4dd6..874e1b5 100644 --- a/roles/nvidia_dcgm_exporter/tasks/install.yml +++ b/roles/nvidia_dcgm_exporter/tasks/install.yml @@ -18,7 +18,6 @@ when: dcgm_exporter_system_user != "root" - name: Get binary - run_once: true when: - dcgm_exporter_binary_local_dir | length == 0 - not dcgm_exporter_skip_install @@ -27,6 +26,7 @@ block: - name: Download golang to local folder become: false + run_once: true ansible.builtin.get_url: url: "{{ _golang_download_url }}" dest: /tmp/golang-{{ golang_version }}.linux-{{ go_arch }}.tar.gz @@ -40,6 +40,7 @@ - name: Unpack golang become: false + run_once: true ansible.builtin.unarchive: src: /tmp/golang-{{ golang_version }}.linux-{{ go_arch }}.tar.gz dest: /tmp @@ -49,6 +50,7 @@ - name: Clone dcgm_exporter repository to local folder become: false + run_once: true ansible.builtin.git: repo: "{{ dcgm_exporter_git_url }}" dest: /tmp/dcgm_exporter @@ -58,6 +60,7 @@ - name: Compile dcgm-exporter binary become: false + run_once: true community.general.make: chdir: /tmp/dcgm_exporter target: binary