aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/molecule/default/tests
diff options
context:
space:
mode:
authorColin Wilk <colin@wilk.cx>2026-07-21 19:58:10 +0200
committerColin Wilk <colin@wilk.cx>2026-07-21 19:58:10 +0200
commitc33948c219032977779a7ef6d82c16e84f37b81a (patch)
treef5a897d933639cdf97da2564d976610259115159 /molecule/default/tests
parent0055971afd5524f0df00fc8d2203a80f828885f7 (diff)
downloadansible-role-prometheus-target-c33948c219032977779a7ef6d82c16e84f37b81a.tar.gz
ansible-role-prometheus-target-c33948c219032977779a7ef6d82c16e84f37b81a.zip
test: expand Molecule coverage for exporter behavior
Cover lineinfile and YAML behavior for default exporters, skipped defaults, exporters without IDs, duplicate definitions, path and host overrides, path-prefix precedence, and inherited label overrides. Verify YAML handling of null documents, multiple files, labeled and unlabeled groups, and duplicate targets. Add test coverage for command and shell handlers using run-once, per-host, disabled, and non-become configurations.
Diffstat (limited to 'molecule/default/tests')
-rw-r--r--molecule/default/tests/test_lineinfile_strategy.py38
-rw-r--r--molecule/default/tests/test_yaml_strategy.py51
2 files changed, 89 insertions, 0 deletions
diff --git a/molecule/default/tests/test_lineinfile_strategy.py b/molecule/default/tests/test_lineinfile_strategy.py
index b75a656..770459e 100644
--- a/molecule/default/tests/test_lineinfile_strategy.py
+++ b/molecule/default/tests/test_lineinfile_strategy.py
@@ -92,3 +92,41 @@ def test_lineinfile_hooks_still_run(host):
assert host.file("/opt/hook1").exists
assert host.file("/opt/hook2").content_string == "hello\nhello\nhello\nhello\n"
+
+
+def test_lineinfile_exporter_defaults_overrides_and_duplicates(host):
+ assert read_yaml_file(host, "/opt/lineinfile_defaults.yml") == [
+ "inherited-default",
+ "appended-default",
+ ]
+ assert read_yaml_file(host, "/opt/lineinfile_no_id.yml") == ["no-id"]
+ assert read_yaml_file(host, "/opt/lineinfile_override.yml") == ["item-override"]
+ assert read_yaml_file(host, "/opt/lineinfile-prefix/default-prefix.yml") == [
+ "prefixed-default"
+ ]
+
+
+def test_lineinfile_skip_default_exporters(host):
+ assert read_file(host, "/opt/lineinfile_skipped.yml").content_string == ""
+
+
+def test_handler_run_once_and_per_host_modes(host):
+ for application in ["application", "application2", "application3", "application4"]:
+ assert host.file(f"/tmp/command-handler-{application}").exists
+
+ assert host.file("/tmp/shell-handler-run-once").exists
+
+
+def test_disabled_handlers_do_not_run(host):
+ assert not host.file("/tmp/disabled-command-handler").exists
+ assert not host.file("/tmp/disabled-shell-handler").exists
+
+
+def test_future_lineinfile_removal_fixture_is_untouched(host):
+ assert read_yaml_file(host, "/opt/lineinfile_state_removal.yml") == [
+ {
+ "labels": {"job": "shared"},
+ "targets": ["keep:9100", "remove:9100"],
+ },
+ {"labels": {"job": "remove_last"}, "targets": ["remove:9200"]},
+ ]
diff --git a/molecule/default/tests/test_yaml_strategy.py b/molecule/default/tests/test_yaml_strategy.py
index 7ba834e..c3d0dd9 100644
--- a/molecule/default/tests/test_yaml_strategy.py
+++ b/molecule/default/tests/test_yaml_strategy.py
@@ -92,6 +92,12 @@ def test_yaml_output_remains_parseable_after_all_operations(host):
"/opt/yaml_branch_matrix.yml",
"/opt/yaml_missing_labeled.yml",
"/opt/yaml_missing_unlabeled.yml",
+ "/opt/yaml_defaults.yml",
+ "/opt/yaml_no_id.yml",
+ "/opt/yaml_override.yml",
+ "/opt/yaml_null.yml",
+ "/opt/yaml-prefix/default-prefix.yml",
+ "/opt/yaml_state_removal.yml",
]:
assert read_yaml_file(host, path) is not None
@@ -154,3 +160,48 @@ def test_yaml_branch_matrix_creates_missing_file_groups_for_labeled_and_unlabele
assert read_yaml_file(host, "/opt/yaml_missing_unlabeled.yml") == [
{"targets": ["application4:9601"]}
]
+
+
+def test_yaml_exporter_defaults_merge_labels_and_deduplicate(host):
+ groups = read_yaml_file(host, "/opt/yaml_defaults.yml")
+
+ assert get_group_by_labels(
+ groups, {"job": "inherited", "environment": "staging"}
+ )["targets"] == ["application:9700"]
+ assert get_group_by_labels(groups, {"job": "default"})["targets"] == [
+ "appended-default:9701"
+ ]
+
+
+def test_yaml_exporter_without_id_and_item_overrides(host):
+ assert read_yaml_file(host, "/opt/yaml_no_id.yml") == [
+ {"labels": {"job": "no-id"}, "targets": ["no-id:9700"]}
+ ]
+ assert read_yaml_file(host, "/opt/yaml_override.yml") == [
+ {"labels": {"job": "override"}, "targets": ["item-override:9700"]}
+ ]
+ assert read_yaml_file(host, "/opt/yaml-prefix/default-prefix.yml") == [
+ {"labels": {"job": "prefixed"}, "targets": ["prefixed-default:9702"]}
+ ]
+
+
+def test_yaml_null_document_is_initialized(host):
+ assert read_yaml_file(host, "/opt/yaml_null.yml") == [
+ {"targets": ["from-null:9700"]}
+ ]
+
+
+def test_yaml_skip_default_exporters(host):
+ target = host.file("/opt/yaml_skipped.yml")
+ assert target.exists
+ assert target.content_string == ""
+
+
+def test_future_yaml_removal_fixture_is_untouched(host):
+ assert read_yaml_file(host, "/opt/yaml_state_removal.yml") == [
+ {
+ "labels": {"job": "shared"},
+ "targets": ["keep:9100", "remove:9100"],
+ },
+ {"labels": {"job": "remove_last"}, "targets": ["remove:9200"]},
+ ]