Skip to content

Commit 2bc3a5c

Browse files
committed
ScanCryptoPolicies: Adjust to Python2 compatibility
Even though it will never run on RHEL 7. This can be safely reverted after we will drop support for Python2 Signed-off-by: Jakub Jelen <[email protected]>
1 parent 73099e2 commit 2bc3a5c

File tree

2 files changed

+57
-41
lines changed

2 files changed

+57
-41
lines changed

repos/system_upgrade/common/actors/scancryptopolicies/libraries/scancryptopolicies.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111
'/usr/share/crypto-policies/policies/modules',)
1212

1313

14-
def read_current_policy(file):
15-
if not os.path.exists(file):
14+
def read_current_policy(filename):
15+
if not os.path.exists(filename):
1616
# NOTE(pstodulk) just seatbelt, I do not expect the file is not present
1717
# skipping tests
1818
raise StopActorExecutionError(
19-
'File not found: {}'.format(file),
19+
'File not found: {}'.format(filename),
2020
details={'details:': 'Cannot check the current set crypto policies.'}
2121
)
2222
current = 'DEFAULT'
23-
with open(file) as fp:
23+
with open(filename) as fp:
2424
current = fp.read().strip()
2525
return current
2626

2727

28-
def _get_name_from_file(file):
28+
def _get_name_from_file(filename):
2929
"""This is just stripping the path and the extension"""
30-
base = os.path.basename(file)
30+
base = os.path.basename(filename)
3131
return os.path.splitext(base)[0]
3232

3333

@@ -44,10 +44,10 @@ def find_rpm_untracked(files):
4444

4545
# return only untracked files from the list
4646
out = []
47-
for file in files:
48-
exp = "file {} is not owned by any package".format(file)
47+
for filename in files:
48+
exp = "file {} is not owned by any package".format(filename)
4949
if exp in res['stdout']:
50-
out.append(file)
50+
out.append(filename)
5151
return out
5252

5353

@@ -56,17 +56,17 @@ def read_policy_dirs(dirs, obj, extension):
5656
files = []
5757
# find all policy files
5858
for d in dirs:
59-
for file in os.listdir(d):
60-
file = os.path.join(d, file)
61-
if not os.path.isfile(file) or not file.endswith(extension):
59+
for filename in os.listdir(d):
60+
filepath = os.path.join(d, filename)
61+
if not os.path.isfile(filepath) or not filepath.endswith(extension):
6262
continue
63-
files.append(file)
63+
files.append(filepath)
6464
# now, check which are not tracked by RPM:
6565
files = find_rpm_untracked(files)
6666
out = []
67-
for file in files:
68-
name = _get_name_from_file(file)
69-
out.append(obj(name=name, path=file))
67+
for filename in files:
68+
name = _get_name_from_file(filename)
69+
out.append(obj(name=name, path=filename))
7070

7171
return out
7272

repos/system_upgrade/common/actors/scancryptopolicies/tests/unit_test_scancryptopolicies.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import shutil
23
import tempfile
34

45
import pytest
@@ -30,21 +31,26 @@ def test_find_rpm_untracked(current_actor_context):
3031
files = ["/etc/crypto-policies/config"]
3132
assert find_rpm_untracked(files) == []
3233

33-
# the tempfile is not tracked by RPM
34-
with tempfile.NamedTemporaryFile(delete=False) as f:
35-
files = [f.name]
36-
assert find_rpm_untracked(files) == [f.name]
34+
# python2 compatibility :/
35+
dirpath = tempfile.mkdtemp()
36+
37+
try:
38+
# the tempfile is not tracked by RPM
39+
files = [dirpath]
40+
assert find_rpm_untracked(files) == [dirpath]
3741

3842
# not existing files are ignored
3943
files = [NOFILE]
4044
assert find_rpm_untracked(files) == []
4145

4246
# combinations should yield expected results too
43-
files = ["/tmp", f.name, NOFILE]
44-
assert find_rpm_untracked(files) == [f.name]
47+
files = ["/tmp", dirpath, NOFILE]
48+
assert find_rpm_untracked(files) == [dirpath]
4549
# regardless the order
46-
files = [NOFILE, f.name, "/tmp"]
47-
assert find_rpm_untracked(files) == [f.name]
50+
files = [NOFILE, dirpath, "/tmp"]
51+
assert find_rpm_untracked(files) == [dirpath]
52+
finally:
53+
shutil.rmtree(dirpath)
4854

4955

5056
def test_read_current_policy():
@@ -63,43 +69,53 @@ def test_read_current_policy():
6369

6470

6571
def test_read_policy_dirs(current_actor_context):
66-
with tempfile.TemporaryDirectory() as dir1:
72+
# python2 compatibility :/
73+
dirpath = tempfile.mkdtemp()
74+
75+
try:
6776
# empty
68-
files = read_policy_dirs([dir1], CustomCryptoPolicy, ".pol")
77+
files = read_policy_dirs([dirpath], CustomCryptoPolicy, ".pol")
6978
assert files == []
7079

7180
# first policy module
72-
path1 = os.path.join(dir1, "policy.mpol")
73-
with open(path1, "x") as f:
81+
path1 = os.path.join(dirpath, "policy.mpol")
82+
with open(path1, "w") as f:
7483
f.write('test')
75-
files = read_policy_dirs([dir1], CustomCryptoPolicy, ".pol")
84+
files = read_policy_dirs([dirpath], CustomCryptoPolicy, ".pol")
7685
assert files == []
77-
files = read_policy_dirs([dir1], CustomCryptoPolicyModule, ".mpol")
86+
files = read_policy_dirs([dirpath], CustomCryptoPolicyModule, ".mpol")
7887
assert files == [CustomCryptoPolicyModule(name="policy", path=path1)]
7988

80-
with tempfile.TemporaryDirectory() as dir2:
81-
files = read_policy_dirs([dir1], CustomCryptoPolicy, ".pol")
89+
# python2 compatibility :/
90+
dirpath2 = tempfile.mkdtemp()
91+
92+
try:
93+
files = read_policy_dirs([dirpath], CustomCryptoPolicy, ".pol")
8294
assert files == []
83-
files = read_policy_dirs([dir1, dir2], CustomCryptoPolicyModule, ".mpol")
95+
files = read_policy_dirs([dirpath, dirpath2], CustomCryptoPolicyModule, ".mpol")
8496
assert files == [CustomCryptoPolicyModule(name="policy", path=path1)]
8597

8698
# first policy file
87-
path2 = os.path.join(dir2, "mypolicy.pol")
88-
with open(path2, "x") as f:
99+
path2 = os.path.join(dirpath2, "mypolicy.pol")
100+
with open(path2, "w") as f:
89101
f.write('test2')
90102
# second policy file
91-
path3 = os.path.join(dir2, "other.pol")
92-
with open(path3, "x") as f:
103+
path3 = os.path.join(dirpath2, "other.pol")
104+
with open(path3, "w") as f:
93105
f.write('test3')
94106

95-
files = read_policy_dirs([dir1, dir2], dict, ".pol")
107+
files = read_policy_dirs([dirpath, dirpath2], dict, ".pol")
96108
assert len(files) == 2
97109
assert dict(name="mypolicy", path=path2) in files
98110
assert dict(name="other", path=path3) in files
99-
files = read_policy_dirs([dir1, dir2], CustomCryptoPolicyModule, ".mpol")
111+
files = read_policy_dirs([dirpath, dirpath2], CustomCryptoPolicyModule, ".mpol")
100112
assert files == [CustomCryptoPolicyModule(name="policy", path=path1)]
113+
finally:
114+
shutil.rmtree(dirpath2)
101115

102-
files = read_policy_dirs([dir1], CustomCryptoPolicy, ".pol")
116+
files = read_policy_dirs([dirpath], CustomCryptoPolicy, ".pol")
103117
assert files == []
104-
files = read_policy_dirs([dir1], CustomCryptoPolicyModule, ".mpol")
118+
files = read_policy_dirs([dirpath], CustomCryptoPolicyModule, ".mpol")
105119
assert files == [CustomCryptoPolicyModule(name="policy", path=path1)]
120+
finally:
121+
shutil.rmtree(dirpath)

0 commit comments

Comments
 (0)