1"""
2Tests for the cron state
3"""
4
5import logging
6import subprocess
7
8import pytest
9import salt.utils.platform
10
11log = logging.getLogger(__name__)
12
13
14@pytest.fixture
15def cron_account():
16    with pytest.helpers.create_account() as system_account:
17        try:
18            yield system_account
19        finally:
20            command = ["crontab", "-u", system_account.username, "-r"]
21            if salt.utils.platform.is_freebsd():
22                command.append("-f")
23            subprocess.run(command, check=False)
24
25
26@pytest.mark.slow_test
27@pytest.mark.skip_on_windows
28@pytest.mark.skip_if_not_root
29@pytest.mark.skip_if_binaries_missing("crontab")
30def test_managed(cron_account, salt_cli, salt_minion, base_env_state_tree_root_dir):
31    """
32    file.managed
33    """
34    cron_contents = (
35        "# Lines below here are managed by Salt, do not edit\n@hourly touch"
36        " /tmp/test-file\n"
37    )
38    expected = (
39        "--- \n+++ \n@@ -1 +1,2 @@\n-\n+# Lines below here are managed by Salt, do not"
40        " edit\n+@hourly touch /tmp/test-file\n"
41    )
42    with pytest.helpers.temp_file(
43        "issue-46881/cron", cron_contents, base_env_state_tree_root_dir
44    ):
45        ret = salt_cli.run(
46            "state.single",
47            "cron.file",
48            name="salt://issue-46881/cron",
49            user=cron_account.username,
50            minion_tgt=salt_minion.id,
51        )
52    assert ret.exitcode == 0, ret
53    state = ret.json["cron_|-salt://issue-46881/cron_|-salt://issue-46881/cron_|-file"]
54    assert "changes" in state
55    assert "diff" in state["changes"]
56    assert state["changes"]["diff"] == expected
57