Skip to content

Commit bd4157d

Browse files
authored
Merge pull request #277 from fstagni/redhat_release__not
Avoid silent failure when checking the worker node: /etc/redhat-release
2 parents a3edb9a + 08cfd0c commit bd4157d

File tree

2 files changed

+84
-12
lines changed

2 files changed

+84
-12
lines changed

Pilot/pilotCommands.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,19 @@ def execute(self):
115115
self.log.info("Host FQDN = %s" % socket.getfqdn())
116116
self.log.info("WorkingDir = %s" % self.pp.workingDir) # this could be different than rootPath
117117

118-
fileName = "/etc/redhat-release"
119-
if os.path.exists(fileName):
120-
with open(fileName, "r") as f:
121-
self.log.info("RedHat Release = %s" % f.read().strip())
122-
123-
fileName = "/etc/lsb-release"
124-
if os.path.isfile(fileName):
125-
with open(fileName, "r") as f:
126-
self.log.info("Linux release:\n%s" % f.read().strip())
118+
for fileName in ["/etc/os-release", "/usr/lib/os-release"]:
119+
if os.path.isfile(fileName):
120+
try:
121+
with open(fileName, "r") as f:
122+
for line in f:
123+
line = line.strip()
124+
if line.startswith(("NAME=", "VERSION=", "PRETTY_NAME=")):
125+
self.log.info(
126+
"OS Release = %s" % line.split("=", 1)[1].strip('"')
127+
)
128+
break
129+
except (OSError, IOError):
130+
self.log.debug("Could not read %s" % fileName)
127131

128132
fileName = "/proc/cpuinfo"
129133
if os.path.exists(fileName):

Pilot/tests/Test_Pilot.py

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import shutil
66
import stat
77
import 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

6786
class 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

Comments
 (0)