1
1
import os
2
+ import shutil
2
3
import tempfile
3
4
4
5
import pytest
@@ -30,21 +31,26 @@ def test_find_rpm_untracked(current_actor_context):
30
31
files = ["/etc/crypto-policies/config" ]
31
32
assert find_rpm_untracked (files ) == []
32
33
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 ]
37
41
38
42
# not existing files are ignored
39
43
files = [NOFILE ]
40
44
assert find_rpm_untracked (files ) == []
41
45
42
46
# 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 ]
45
49
# 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 )
48
54
49
55
50
56
def test_read_current_policy ():
@@ -63,43 +69,53 @@ def test_read_current_policy():
63
69
64
70
65
71
def test_read_policy_dirs (current_actor_context ):
66
- with tempfile .TemporaryDirectory () as dir1 :
72
+ # python2 compatibility :/
73
+ dirpath = tempfile .mkdtemp ()
74
+
75
+ try :
67
76
# empty
68
- files = read_policy_dirs ([dir1 ], CustomCryptoPolicy , ".pol" )
77
+ files = read_policy_dirs ([dirpath ], CustomCryptoPolicy , ".pol" )
69
78
assert files == []
70
79
71
80
# 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 :
74
83
f .write ('test' )
75
- files = read_policy_dirs ([dir1 ], CustomCryptoPolicy , ".pol" )
84
+ files = read_policy_dirs ([dirpath ], CustomCryptoPolicy , ".pol" )
76
85
assert files == []
77
- files = read_policy_dirs ([dir1 ], CustomCryptoPolicyModule , ".mpol" )
86
+ files = read_policy_dirs ([dirpath ], CustomCryptoPolicyModule , ".mpol" )
78
87
assert files == [CustomCryptoPolicyModule (name = "policy" , path = path1 )]
79
88
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" )
82
94
assert files == []
83
- files = read_policy_dirs ([dir1 , dir2 ], CustomCryptoPolicyModule , ".mpol" )
95
+ files = read_policy_dirs ([dirpath , dirpath2 ], CustomCryptoPolicyModule , ".mpol" )
84
96
assert files == [CustomCryptoPolicyModule (name = "policy" , path = path1 )]
85
97
86
98
# 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 :
89
101
f .write ('test2' )
90
102
# 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 :
93
105
f .write ('test3' )
94
106
95
- files = read_policy_dirs ([dir1 , dir2 ], dict , ".pol" )
107
+ files = read_policy_dirs ([dirpath , dirpath2 ], dict , ".pol" )
96
108
assert len (files ) == 2
97
109
assert dict (name = "mypolicy" , path = path2 ) in files
98
110
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" )
100
112
assert files == [CustomCryptoPolicyModule (name = "policy" , path = path1 )]
113
+ finally :
114
+ shutil .rmtree (dirpath2 )
101
115
102
- files = read_policy_dirs ([dir1 ], CustomCryptoPolicy , ".pol" )
116
+ files = read_policy_dirs ([dirpath ], CustomCryptoPolicy , ".pol" )
103
117
assert files == []
104
- files = read_policy_dirs ([dir1 ], CustomCryptoPolicyModule , ".mpol" )
118
+ files = read_policy_dirs ([dirpath ], CustomCryptoPolicyModule , ".mpol" )
105
119
assert files == [CustomCryptoPolicyModule (name = "policy" , path = path1 )]
120
+ finally :
121
+ shutil .rmtree (dirpath )
0 commit comments