-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_svc.py
171 lines (143 loc) · 6.73 KB
/
test_svc.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import json
import pytest
from commands.svc import Mgr
@pytest.mark.ci
@pytest.mark.usefixtures("osvc_path_tests")
@pytest.mark.usefixtures("has_euid_0")
class TestPurgeLocal:
@staticmethod
def test_object_is_not_recreated_after_local_purge():
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create"]) == 0
assert Mgr()(argv=["-s", svcname, "ls"]) == 0
assert Mgr()(argv=["-s", svcname, "purge", "--local"]) == 0
assert Mgr()(argv=["-s", svcname, "ls"]) > 0
@pytest.mark.ci
@pytest.mark.usefixtures("osvc_path_tests")
@pytest.mark.usefixtures("has_euid_0")
class TestCreateWithKw:
@staticmethod
def test_create_id_refused_when_config_is_not_valid_env():
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create", "--kw", "env.foo={bar}", "--kw", "env.bar={foo}"]) > 0
assert Mgr()(argv=["-s", svcname, "ls"]) > 0
@staticmethod
def test_create_is_accepted_when_config_has_valid_env():
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create", "--kw", "env.foo={bar}", "--kw", "env.baz={foo}"]) == 0
assert Mgr()(argv=["-s", svcname, "ls"]) == 0
@staticmethod
def test_create_correctly_set_env_keywords(capsys):
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create", "--kw", "env.foo=00BAB10C", "--kw", "env.bar=BADDCAFE"]) == 0
for kw, value in {"env.foo": "00BAB10C", "env.bar": "BADDCAFE"}.items():
capsys.readouterr()
assert Mgr()(argv=["-s", svcname, "get", "--kw", kw]) == 0
assert capsys.readouterr().out.strip() == value
@pytest.mark.ci
@pytest.mark.usefixtures("osvc_path_tests")
@pytest.mark.usefixtures("has_euid_0")
class TestSet:
@staticmethod
def test_refuse_dry_run():
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create", "--kw", "env.foo=bar"]) == 0
assert Mgr()(argv=["-s", svcname, "set", "--kw", "env.foo=dry_run", "--dry-run", "--local"]) == 1
@staticmethod
def test_update_service_config(tmp_file, capture_stdout):
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create", "--kw", "env.foo=bar"]) == 0
assert Mgr()(argv=["-s", svcname, "set", "--kw", "env.foo=BAR", "--local"]) == 0
with capture_stdout(tmp_file):
assert Mgr()(argv=["-s", svcname, "print", "config", "--format", "json"]) == 0
assert json.load(open(tmp_file, "r"))["env"]["foo"] == "BAR"
@staticmethod
def test_set_invalid_env_values_is_refused():
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create"]) == 0
assert Mgr()(argv=["-s", svcname, "set", "--kw", "env.foo={bar}", "--kw", "env.bar={foo}"]) > 0
@staticmethod
def test_set_valid_env_values_is_accepted():
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create"]) == 0
assert Mgr()(argv=["-s", svcname, "set", "--kw", "env.foo={bar}", "--kw", "env.bar=something"]) == 0
@pytest.mark.ci
@pytest.mark.usefixtures("osvc_path_tests")
@pytest.mark.usefixtures("has_euid_0")
class TestUnset:
@staticmethod
def test_refuse_dry_run():
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create", "--kw", "env.foo=bar"]) == 0
assert Mgr()(argv=["-s", svcname, "unset", "--kw", "env.foo", "--dry-run", "--local"]) == 1
@staticmethod
def test_update_config(tmp_file, capture_stdout):
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create", "--kw", "env.foo=bar"]) == 0
assert Mgr()(argv=["-s", svcname, "unset", "--kw", "env.foo", "--local"]) == 0
with capture_stdout(tmp_file):
assert Mgr()(argv=["-s", svcname, "print", "config", "--format", "json"]) == 0
assert "foo" not in json.load(open(tmp_file, "r"))["env"]
@pytest.mark.ci
@pytest.mark.usefixtures("osvc_path_tests")
@pytest.mark.usefixtures("has_euid_0")
class TestDeleteRid:
@staticmethod
def test_remove_section(tmp_file, capture_stdout):
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create", "--kw", "env.foo=bar"]) == 0
assert Mgr()(argv=["-s", svcname, "delete", "--rid", "env", "--local"]) == 0
with capture_stdout(tmp_file):
assert Mgr()(argv=["-s", svcname, "print", "config", "--format", "json"]) == 0
assert "env" not in json.load(open(tmp_file, "r"))
@staticmethod
def test_refuse_dry_run():
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create", "--kw", "env.foo=bar"]) == 0
assert Mgr()(argv=["-s", svcname, "delete", "--rid", "env", "--dry-run", "--local"]) == 1
@pytest.mark.ci
@pytest.mark.usefixtures("osvc_path_tests")
@pytest.mark.usefixtures("has_euid_0")
class TestDelete:
@staticmethod
def test_delete_service():
svcname = "a-service"
assert Mgr()(argv=["-s", svcname, "create", "--kw", "env.foo=bar"]) == 0
assert Mgr()(argv=["-s", svcname, "ls"]) == 0
assert Mgr()(argv=["-s", svcname, "delete", "--local"]) == 0
assert Mgr()(argv=["-s", svcname, "ls"]) == 1
@staticmethod
def test_refuse_dry_run():
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create", "--kw", "env.foo=bar"]) == 0
assert Mgr()(argv=["-s", svcname, "delete", "--dry-run", "--local"]) == 1
assert Mgr()(argv=["-s", svcname, "ls"]) == 0
@pytest.mark.ci
@pytest.mark.usefixtures("osvc_path_tests")
@pytest.mark.usefixtures("has_euid_0")
class TestUpdate:
@staticmethod
def test_it_updates_config(mock_sysname, tmp_file, capture_stdout):
svcname = "pytest"
mock_sysname("Linux")
assert Mgr()(argv=["-s", svcname, "create"]) == 0
assert Mgr()(argv=["-s", svcname, "update", "--resource", '{"rid": "fs#1", "type": "flag"}', "--local"]) == 0
with capture_stdout(tmp_file):
assert Mgr()(argv=["-s", svcname, "print", "config", "--format", "json"]) == 0
assert json.load(open(tmp_file, "r"))["fs#1"] == {"type": "flag"}
@staticmethod
def test_refuse_dry_run():
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create"]) == 0
assert Mgr()(argv=["-s", svcname, "update", "--resource", '{"comment": "Comment"}', "--local",
"--dry-run"]) == 1
@staticmethod
def test_resource_option_is_not_preserved_on_next_mgr_call():
assert Mgr()(argv=["-s", "svc1", "create"]) == 0
assert Mgr()(argv=["-s", "svc1", "update", "--resource", '{"comment": "foo"}', "--local"]) == 0
assert Mgr()(argv=["-s", "svc2", "create"]) == 0
@staticmethod
def test_update_allowed_without_resource():
svcname = "pytest"
assert Mgr()(argv=["-s", svcname, "create"]) == 0
assert Mgr()(argv=["-s", svcname, "update", "--local"]) == 0