aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/molecule/default/tests/test_lineinfile_strategy.py
blob: b75a656ef38bd0fe05fca605f1851b224cbb20c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import yaml


testinfra_hosts = ["prometheus"]


def read_file(host, path):
    target = host.file(path)
    assert target.exists
    return target


def read_yaml_file(host, path):
    return yaml.safe_load(read_file(host, path).content_string)


def test_lineinfile_bootstrap_adds_first_host_without_breaking_yaml(host):
    target = read_file(host, "/opt/lineinfile_first_host.yml")

    assert target.user == "prometheus"
    assert target.group == "prometheus"
    assert target.mode == 0o600
    assert read_yaml_file(host, "/opt/lineinfile_first_host.yml") == ["application:9100"]


def test_lineinfile_adds_subsequent_host_to_existing_yaml(host):
    target = read_file(host, "/opt/lineinfile_bootstrap.yml")

    assert target.content_string == (
        "- labels:\n"
        "    my: label\n"
        "  targets:\n"
        "  - existing:9100\n"
        "  - application:9100\n"
    )

    assert read_yaml_file(host, "/opt/lineinfile_bootstrap.yml") == [
        {"labels": {"my": "label"}, "targets": ["existing:9100", "application:9100"]}
    ]


def test_lineinfile_parallel_writes_keep_all_hosts_and_parse(host):
    hosts = sorted(read_yaml_file(host, "/opt/lineinfile_parallel.yml"))
    assert hosts == [
        "application2:9100",
        "application3:9100",
        "application4:9100",
        "application:9100",
    ]


def test_lineinfile_user_edited_file_stays_parseable(host):
    target = read_file(host, "/opt/lineinfile_user_edited.yml")

    assert target.content_string == (
        "# user comment\n"
        "- labels:\n"
        "    env: prod\n"
        "  targets:\n"
        "  - existing:9100\n"
        "  - application2:9100\n"
    )

    assert read_yaml_file(host, "/opt/lineinfile_user_edited.yml") == [
        {"labels": {"env": "prod"}, "targets": ["existing:9100", "application2:9100"]}
    ]


def test_lineinfile_removing_last_host_still_leaves_parseable_yaml(host):
    assert read_yaml_file(host, "/opt/lineinfile_remove_last.yml") == [
        {"labels": {"env": "stage"}, "targets": None}
    ]


def test_lineinfile_compatibility_simple_and_prefix_cases(host):
    assert read_yaml_file(host, "/opt/simple_target1.yml") == ["application"]
    assert read_yaml_file(host, "/opt/simple_target2.yml") == ["test1", "test2"]
    assert read_yaml_file(host, "/opt/simple_target3.yml") == ["application_AA"]
    assert read_yaml_file(host, "/opt/simple_target4.yml") == ["exporter_without_id"]
    assert read_yaml_file(host, "/opt/prefix_target1.yml") == ["application"]
    assert read_yaml_file(host, "/opt/prefix_target2.yml") == ["application"]
    assert read_yaml_file(host, "/opt/prefix/prefix_target3.yml") == ["application"]


def test_lineinfile_hooks_still_run(host):
    assert sorted(read_yaml_file(host, "/opt/hook_target.yml")) == [
        "application",
        "application2",
        "application3",
        "application4",
    ]

    assert host.file("/opt/hook1").exists
    assert host.file("/opt/hook2").content_string == "hello\nhello\nhello\nhello\n"