1"""
2Simple Smoke Tests for Connected Proxy Minion
3"""
4import logging
5import os
6
7import pytest
8
9log = logging.getLogger(__name__)
10
11
12@pytest.fixture(scope="module", autouse=True)
13def salt_delta_proxy(salt_delta_proxy):
14    """
15    Create some dummy proxy minions for testing
16    """
17    for proxy in [salt_delta_proxy.id, "dummy_proxy_one", "dummy_proxy_two"]:
18        cachefile = os.path.join(
19            salt_delta_proxy.config["cachedir"], "dummy-proxy-{}.cache".format(proxy)
20        )
21        if os.path.exists(cachefile):
22            os.unlink(cachefile)
23        return salt_delta_proxy
24
25
26@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
27def test_can_it_ping(salt_cli, proxy_id):
28    """
29    Ensure the proxy can ping
30    """
31    ret = salt_cli.run("test.ping", minion_tgt=proxy_id)
32    assert ret.json is True
33
34
35@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
36def test_list_pkgs(salt_cli, proxy_id):
37    """
38    Package test 1, really just tests that the virtual function capability
39    is working OK.
40    """
41    ret = salt_cli.run("pkg.list_pkgs", minion_tgt=proxy_id)
42    assert "coreutils" in ret.json
43    assert "apache" in ret.json
44    assert "redbull" in ret.json
45
46
47@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
48def test_install_pkgs(salt_cli, proxy_id):
49    """
50    Package test 2, really just tests that the virtual function capability
51    is working OK.
52    """
53
54    ret = salt_cli.run("pkg.install", "thispkg", minion_tgt=proxy_id)
55    assert ret.json["thispkg"] == "1.0"
56
57    ret = salt_cli.run("pkg.list_pkgs", minion_tgt=proxy_id)
58
59    assert ret.json["apache"] == "2.4"
60    assert ret.json["redbull"] == "999.99"
61    assert ret.json["thispkg"] == "1.0"
62
63
64@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
65def test_remove_pkgs(salt_cli, proxy_id):
66    ret = salt_cli.run("pkg.remove", "apache", minion_tgt=proxy_id)
67    assert "apache" not in ret.json
68
69
70@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
71def test_upgrade(salt_cli, proxy_id):
72    ret = salt_cli.run("pkg.upgrade", minion_tgt=proxy_id)
73    assert ret.json["coreutils"]["new"] == "2.0"
74    assert ret.json["redbull"]["new"] == "1000.99"
75
76
77@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
78def test_service_list(salt_cli, proxy_id):
79    ret = salt_cli.run("service.list", minion_tgt=proxy_id)
80    assert "ntp" in ret.json
81
82
83@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
84def test_service_stop(salt_cli, proxy_id):
85    ret = salt_cli.run("service.stop", "ntp", minion_tgt=proxy_id)
86    ret = salt_cli.run("service.status", "ntp", minion_tgt=proxy_id)
87    assert ret.json is False
88
89
90@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
91def test_service_start(salt_cli, proxy_id):
92    ret = salt_cli.run("service.start", "samba", minion_tgt=proxy_id)
93    ret = salt_cli.run("service.status", "samba", minion_tgt=proxy_id)
94    assert ret.json is True
95
96
97@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
98def test_service_get_all(salt_cli, proxy_id):
99    ret = salt_cli.run("service.get_all", minion_tgt=proxy_id)
100    assert ret.json
101    assert "samba" in ret.json
102
103
104@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
105def test_grains_items(salt_cli, proxy_id):
106    ret = salt_cli.run("grains.items", minion_tgt=proxy_id)
107    assert ret.json["kernel"] == "proxy"
108    assert ret.json["kernelrelease"] == "proxy"
109
110
111@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
112def test_state_apply(salt_cli, tmp_path, base_env_state_tree_root_dir, proxy_id):
113    test_file = tmp_path / "testfile"
114    core_state = """
115    {}:
116      file:
117        - managed
118        - source: salt://testfile
119        - makedirs: true
120        """.format(
121        test_file
122    )
123
124    with pytest.helpers.temp_file("core.sls", core_state, base_env_state_tree_root_dir):
125        ret = salt_cli.run("state.apply", "core", minion_tgt=proxy_id)
126        for value in ret.json.values():
127            assert value["result"] is True
128
129
130@pytest.mark.slow_test
131@pytest.mark.parametrize("proxy_id", ["dummy_proxy_one", "dummy_proxy_two"])
132def test_state_highstate(salt_cli, tmp_path, base_env_state_tree_root_dir, proxy_id):
133    test_file = tmp_path / "testfile"
134    top_sls = """
135    base:
136      '*':
137        - core
138        """
139
140    core_state = """
141    {}:
142      file:
143        - managed
144        - source: salt://testfile
145        - makedirs: true
146        """.format(
147        test_file
148    )
149
150    with pytest.helpers.temp_file(
151        "top.sls", top_sls, base_env_state_tree_root_dir
152    ), pytest.helpers.temp_file("core.sls", core_state, base_env_state_tree_root_dir):
153        ret = salt_cli.run("state.highstate", minion_tgt=proxy_id)
154        for value in ret.json.values():
155            assert value["result"] is True
156