-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathtest_alt.py
More file actions
324 lines (272 loc) · 11.5 KB
/
Copy pathtest_alt.py
File metadata and controls
324 lines (272 loc) · 11.5 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""Test alt"""
import os
import string
import py
import pytest
import utils
TEST_PATHS = [utils.ALT_FILE1, utils.ALT_FILE2, utils.ALT_DIR]
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize("yadm_alt", [True, False], ids=["alt", "worktree"])
@pytest.mark.parametrize(
"tracked,encrypt,exclude",
[
(False, False, False),
(True, False, False),
(False, True, False),
(False, True, True),
],
ids=["untracked", "tracked", "encrypted", "excluded"],
)
def test_alt_source(runner, paths, tracked, encrypt, exclude, yadm_alt):
"""Test yadm alt operates on all expected sources of alternates"""
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
utils.create_alt_files(
paths, "##default", tracked=tracked, encrypt=encrypt, exclude=exclude, yadm_alt=yadm_alt, yadm_dir=yadm_dir
)
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
assert run.success
assert run.err == ""
linked = utils.parse_alt_output(run.out)
basepath = yadm_dir.join("alt") if yadm_alt else paths.work
for link_path in TEST_PATHS:
source_file_content = link_path + "##default"
source_file = basepath.join(source_file_content)
link_file = paths.work.join(link_path)
if tracked or (encrypt and not exclude):
assert link_file.islink()
target = py.path.local(os.path.realpath(link_file))
if target.isfile():
assert link_file.read() == source_file_content
assert str(source_file) in linked
else:
assert link_file.join(utils.CONTAINED).read() == source_file_content
assert str(source_file) in linked
else:
assert not link_file.exists()
assert str(source_file) not in linked
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize("yadm_alt", [True, False], ids=["alt", "worktree"])
def test_relative_link(runner, paths, yadm_alt):
"""Confirm links created are relative"""
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
utils.create_alt_files(
paths, "##default", tracked=True, encrypt=False, exclude=False, yadm_alt=yadm_alt, yadm_dir=yadm_dir
)
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
assert run.success
assert run.err == ""
basepath = yadm_dir.join("alt") if yadm_alt else paths.work
for link_path in TEST_PATHS:
source_file_content = link_path + "##default"
source_file = basepath.join(source_file_content)
link_file = paths.work.join(link_path)
link = link_file.readlink()
relpath = os.path.relpath(source_file, start=os.path.dirname(link_file))
assert link == relpath
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize(
"suffix",
[
"##default",
"##default,e.txt",
"##default,extension.txt",
"##a.$tst_arch",
"##arch.$tst_arch",
"##o.$tst_sys",
"##os.$tst_sys",
"##d.$tst_distro",
"##distro.$tst_distro",
"##f.$tst_distro_family",
"##distro_family.$tst_distro_family",
"##c.$tst_class",
"##class.$tst_class",
"##h.$tst_host",
"##hostname.$tst_host",
"##u.$tst_user",
"##user.$tst_user",
],
)
def test_alt_conditions(runner, paths, tst_arch, tst_sys, tst_distro, tst_distro_family, tst_host, tst_user, suffix):
"""Test conditions supported by yadm alt"""
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
# set the class
tst_class = "testclass"
utils.set_local(paths, "class", tst_class + ".before")
utils.set_local(paths, "class", tst_class, add=True)
utils.set_local(paths, "class", tst_class + ".after", add=True)
suffix = string.Template(suffix).substitute(
tst_arch=tst_arch,
tst_sys=tst_sys,
tst_distro=tst_distro,
tst_distro_family=tst_distro_family,
tst_class=tst_class,
tst_host=tst_host,
tst_user=tst_user,
)
utils.create_alt_files(paths, suffix)
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
assert run.success
assert run.err == ""
linked = utils.parse_alt_output(run.out)
for link_path in TEST_PATHS:
source_file = link_path + suffix
assert paths.work.join(link_path).islink()
target = py.path.local(os.path.realpath(paths.work.join(link_path)))
if target.isfile():
assert paths.work.join(link_path).read() == source_file
assert str(paths.work.join(source_file)) in linked
else:
assert paths.work.join(link_path).join(utils.CONTAINED).read() == source_file
assert str(paths.work.join(source_file)) in linked
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize("kind", ["default", "", None, "envtpl", "j2cli", "j2", "esh"])
@pytest.mark.parametrize(
"label",
[
"t",
"template",
"yadm",
],
)
def test_alt_templates(runner, paths, kind, label):
"""Test templates supported by yadm alt"""
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
suffix = f"##{label}.{kind}"
if kind is None:
suffix = f"##{label}"
utils.create_alt_files(paths, suffix)
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "alt"])
assert run.success
assert run.err == ""
created = utils.parse_alt_output(run.out, linked=False)
for created_path in TEST_PATHS:
if created_path != utils.ALT_DIR:
source_file = created_path + suffix
assert paths.work.join(created_path).isfile()
assert paths.work.join(created_path).read().strip() == source_file
assert str(paths.work.join(source_file)) in created
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize("autoalt", [None, "true", "false"])
def test_auto_alt(runner, yadm_cmd, paths, autoalt):
"""Test auto alt"""
# set the value of auto-alt
if autoalt:
os.system(" ".join(yadm_cmd("config", "yadm.auto-alt", autoalt)))
utils.create_alt_files(paths, "##default")
run = runner(yadm_cmd("status"))
assert run.success
assert run.err == ""
linked = utils.parse_alt_output(run.out)
for link_path in TEST_PATHS:
source_file = link_path + "##default"
if autoalt == "false":
assert not paths.work.join(link_path).exists()
else:
assert paths.work.join(link_path).islink()
target = py.path.local(os.path.realpath(paths.work.join(link_path)))
if target.isfile():
assert paths.work.join(link_path).read() == source_file
# no linking output when run via auto-alt
assert str(paths.work.join(source_file)) not in linked
else:
assert paths.work.join(link_path).join(utils.CONTAINED).read() == source_file
# no linking output when run via auto-alt
assert str(paths.work.join(source_file)) not in linked
@pytest.mark.usefixtures("ds1_copy")
def test_stale_link_removal(runner, yadm_cmd, paths):
"""Stale links to alternative files are removed
This test ensures that when an already linked alternative becomes invalid
due to a change in class, the alternate link is removed.
"""
# set the class
tst_class = "testclass"
utils.set_local(paths, "class", tst_class)
# create files which match the test class
utils.create_alt_files(paths, f"##class.{tst_class}")
# run alt to trigger linking
run = runner(yadm_cmd("alt"))
assert run.success
assert run.err == ""
linked = utils.parse_alt_output(run.out)
# assert the proper linking has occurred
for stale_path in TEST_PATHS:
source_file = stale_path + "##class." + tst_class
assert paths.work.join(stale_path).islink()
target = py.path.local(os.path.realpath(paths.work.join(stale_path)))
if target.isfile():
assert paths.work.join(stale_path).read() == source_file
assert str(paths.work.join(source_file)) in linked
else:
assert paths.work.join(stale_path).join(utils.CONTAINED).read() == source_file
assert str(paths.work.join(source_file)) in linked
# change the class so there are no valid alternates
utils.set_local(paths, "class", "changedclass")
# run alt to trigger linking
run = runner(yadm_cmd("alt"))
assert run.success
assert run.err == ""
linked = utils.parse_alt_output(run.out)
# assert the linking is removed
for stale_path in TEST_PATHS:
source_file = stale_path + "##class." + tst_class
assert not paths.work.join(stale_path).exists()
assert str(paths.work.join(source_file)) not in linked
@pytest.mark.usefixtures("ds1_repo_copy")
def test_template_overwrite_symlink(runner, yadm_cmd, paths, tst_sys):
"""Remove symlinks before processing a template
If a symlink is in the way of the output of a template, the target of the
symlink will get the template content. To prevent this, the symlink should
be removed just before processing a template.
"""
target = paths.work.join(f"test_link##os.{tst_sys}")
target.write("target")
link = paths.work.join("test_link")
link.mksymlinkto(target, absolute=1)
template = paths.work.join("test_link##template.default")
template.write("test-data")
run = runner(yadm_cmd("add", target, template))
assert run.success
assert run.err == ""
assert run.out == ""
assert not link.islink()
assert target.read().strip() == "target"
assert link.read().strip() == "test-data"
@pytest.mark.usefixtures("ds1_copy")
@pytest.mark.parametrize("style", ["symlink", "template"])
def test_ensure_alt_path(runner, paths, style):
"""Test that directories are created before making alternates"""
yadm_dir, yadm_data = setup_standard_yadm_dir(paths)
suffix = "default" if style == "symlink" else "template"
filename = "a/b/c/file"
source = yadm_dir.join(f"alt/{filename}##{suffix}")
source.write("test-data", ensure=True)
run = runner([paths.pgm, "-Y", yadm_dir, "--yadm-data", yadm_data, "add", source])
assert run.success
assert run.err == ""
assert run.out == ""
assert paths.work.join(filename).read().strip() == "test-data"
@pytest.mark.usefixtures("ds1_repo_copy")
@pytest.mark.parametrize("readonly", [None, "true", "false"])
def test_template_readonly(runner, yadm_cmd, paths, tst_sys, readonly):
"""Remove write permission for template result file.
If the `yadm.template-read-only` configuration is not set to false,
the resulting file from processing a template should has no write permission.
"""
# set the value of template read-only
if readonly:
runner(yadm_cmd("config", "yadm.template-read-only", readonly))
utils.create_alt_files(paths, f"##template.default")
run = runner(yadm_cmd("alt"))
for stale_path in [utils.ALT_FILE1, utils.ALT_FILE2]:
write_perm_mask = os.stat(paths.work.join(stale_path)).st_mode & 0o222
if readonly == "false":
assert write_perm_mask > 0
else:
assert write_perm_mask == 0
def setup_standard_yadm_dir(paths):
"""Configure a yadm home within the work tree"""
std_yadm_dir = paths.work.mkdir(".config").mkdir("yadm")
std_yadm_data = paths.work.mkdir(".local").mkdir("share").mkdir("yadm")
std_yadm_data.join("repo.git").mksymlinkto(paths.repo, absolute=1)
std_yadm_dir.join("encrypt").mksymlinkto(paths.encrypt, absolute=1)
return std_yadm_dir, std_yadm_data