55import shutil
66import stat
77import sys
8+ import tempfile
9+ from unittest import mock
810
911# pylint: disable=protected-access, missing-docstring, invalid-name, line-too-long
1012# imports
@@ -33,7 +35,12 @@ def setUp(self):
3335 "Version" : "v1r1, v2r2" ,
3436 }
3537 },
36- "CEs" : {"grid1.example.com" : {"GridCEType" : "cetype1" , "Site" : "site.example.com" }},
38+ "CEs" : {
39+ "grid1.example.com" : {
40+ "GridCEType" : "cetype1" ,
41+ "Site" : "site.example.com" ,
42+ }
43+ },
3744 "DefaultSetup" : "TestSetup" ,
3845 },
3946 fp ,
@@ -62,14 +69,33 @@ def tearDown(self):
6269 shutil .rmtree ("ReplacementCode" )
6370 except OSError :
6471 pass
72+ try :
73+ shutil .rmtree ("etc" )
74+ except OSError :
75+ pass
76+ try :
77+ os .remove (fileProd )
78+ except OSError :
79+ pass
80+ try :
81+ shutil .rmtree ("ReplacementCode" )
82+ except OSError :
83+ pass
6584
6685
6786class CommandsTestCase (PilotTestCase ):
6887 """Test case for each pilot command"""
6988
7089 def test_InitJSON (self ):
7190 """Test the pilot.json and command line parsing"""
72- sys .argv [1 :] = ["--Name" , "grid1.example.com" , "--commandOptions" , "a=1,b=2" , "-Z" , "c=3" ]
91+ sys .argv [1 :] = [
92+ "--Name" ,
93+ "grid1.example.com" ,
94+ "--commandOptions" ,
95+ "a=1,b=2" ,
96+ "-Z" ,
97+ "c=3" ,
98+ ]
7399 pp = PilotParams ()
74100
75101 self .assertEqual (pp .commands , ["x" , "y" , "z" ])
@@ -93,7 +119,13 @@ def test_InitJSON(self):
93119 self .assertEqual (pp .commandOptions ["b" ], "2" )
94120 self .assertEqual (pp .commandOptions ["c" ], "3" )
95121
96- sys .argv [1 :] = ["--Name" , "grid1.example.com" , "--commandOptions=a = 1, b=2" , "-Z" , " c=3" ] # spaces and '=''
122+ sys .argv [1 :] = [
123+ "--Name" ,
124+ "grid1.example.com" ,
125+ "--commandOptions=a = 1, b=2" ,
126+ "-Z" ,
127+ " c=3" ,
128+ ] # spaces and '=''
97129 pp = PilotParams ()
98130
99131 self .assertEqual (pp .commandOptions ["a" ], "1" )
@@ -105,6 +137,42 @@ def test_CheckWorkerNode(self):
105137 pp = PilotParams ()
106138 cwn = CheckWorkerNode (pp )
107139 self .assertEqual (cwn .execute (), None )
140+ with open ("pilot.out" ) as po :
141+ s = po .read ()
142+ self .assertTrue ("OS Release =" in s )
143+
144+ def test_CheckWorkerNode_osrelease_unavailable (self ):
145+ """Test CheckWorkerNode when os-release is unavailable"""
146+ pp = PilotParams ()
147+ cwn = CheckWorkerNode (pp )
148+
149+ with mock .patch ("pilotCommands.os.path.isfile" ) as mock_isfile :
150+ mock_isfile .return_value = False
151+
152+ cwn .execute ()
153+
154+ with open ("pilot.out" ) as po :
155+ s = po .read ()
156+ self .assertNotIn ("OS Release =" , s )
157+
158+ def test_CheckWorkerNode_osrelease_blocked (self ):
159+ """Test CheckWorkerNode when os-release access is blocked"""
160+ pp = PilotParams ()
161+ cwn = CheckWorkerNode (pp )
162+
163+ original_open = open
164+
165+ def selective_open (path , * args , ** kwargs ):
166+ if path in ["/etc/os-release" , "/usr/lib/os-release" ]:
167+ raise IOError ("Permission denied" )
168+ return original_open (path , * args , ** kwargs )
169+
170+ with mock .patch ("pilotCommands.open" , side_effect = selective_open ):
171+ cwn .execute ()
172+
173+ with open ("pilot.out" ) as po :
174+ s = po .read ()
175+ self .assertNotIn ("OS Release =" , s )
108176
109177 def test_ConfigureSite (self ):
110178 """Test ConfigureSite command"""
0 commit comments