forked from easybuilders/easybuild-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.py
279 lines (215 loc) · 10.6 KB
/
package.py
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
# #
# Copyright 2015-2025 Ghent University
#
# This file is part of EasyBuild,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
# with support of Ghent University (http://ugent.be/hpc),
# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be),
# Flemish Research Foundation (FWO) (http://www.fwo.be/en)
# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en).
#
# https://github.com/easybuilders/easybuild
#
# EasyBuild is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation v2.
#
# EasyBuild is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with EasyBuild. If not, see <http://www.gnu.org/licenses/>.
# #
"""
Unit tests for packaging support.
@author: Kenneth Hoste (Ghent University)
"""
import os
import re
import stat
import sys
from test.framework.utilities import EnhancedTestCase, TestLoaderFiltered, init_config
from unittest import TextTestRunner
from easybuild.framework.easyconfig.easyconfig import EasyConfig
from easybuild.tools.config import get_package_naming_scheme, log_path
from easybuild.tools.build_log import EasyBuildError
from easybuild.tools.filetools import adjust_permissions, read_file, write_file
from easybuild.tools.package.utilities import ActivePNS, avail_package_naming_schemes, check_pkg_support, package
from easybuild.tools.version import VERSION as EASYBUILD_VERSION
FPM_OUTPUT_FILE = 'fpm_mocked.out'
# purposely using non-bash script, to detect issues with shebang line being ignored (run_shell_cmd with use_bash=False)
MOCKED_FPM = """#!/usr/bin/env python
import os, sys
def verbose(msg):
with open('%(fpm_output_file)s', 'a') as fp:
fp.write(msg + '\\n')
description, iteration, name, source, target, url, version, workdir = '', '', '', '', '', '', '', ''
excludes = []
verbose(' '.join(sys.argv[1:]))
idx = 1
while idx < len(sys.argv):
if sys.argv[idx] == '--workdir':
idx += 1
workdir = sys.argv[idx]
verbose('workdir'); verbose(workdir)
elif sys.argv[idx] == '--name':
idx += 1
name = sys.argv[idx]
elif sys.argv[idx] == '--version':
idx += 1
version = sys.argv[idx]
verbose('version'); verbose(version)
elif sys.argv[idx] == '--description':
idx += 1
description = sys.argv[idx]
elif sys.argv[idx] == '--url':
idx += 1
url = sys.argv[idx]
elif sys.argv[idx] == '--iteration':
idx += 1
iteration = sys.argv[idx]
elif sys.argv[idx] == '-t':
idx += 1
target = sys.argv[idx]
elif sys.argv[idx] == '-s':
idx += 1
source = sys.argv[idx]
elif sys.argv[idx] == '--exclude':
idx += 1
excludes.append(sys.argv[idx])
elif sys.argv[idx].startswith('--'):
verbose("got an unhandled option: " + sys.argv[idx] + ' ' + sys.argv[idx+1])
idx += 1
else:
installdir = sys.argv[idx]
modulefile = sys.argv[idx+1]
break
idx += 1
pkgfile = os.path.join(workdir, name + '-' + version + '.' + iteration + '.' + target)
with open(pkgfile, 'w') as fp:
fp.write('thisisan' + target + '\\n')
fp.write(' '.join(sys.argv[1:]) + '\\n')
fp.write("STARTCONTENTS of installdir " + installdir + ':\\n')
find_cmd = 'find ' + installdir + ' ' + ''.join([" -not -path /" + x + ' ' for x in excludes])
verbose("trying: " + find_cmd)
fp.write(find_cmd + '\\n')
fp.write('ENDCONTENTS\\n')
fp.write("Contents of module file " + modulefile + ':')
fp.write('modulefile: ' + modulefile + '\\n')
#modtxt = open(modulefile).read()
#fp.write(modtxt + '\\n')
fp.write("I found excludes " + ' '.join(excludes) + '\\n')
fp.write("DESCRIPTION: " + description + '\\n')
"""
def mock_fpm(tmpdir):
"""Put mocked version of fpm command in place in specified tmpdir."""
# put mocked 'fpm' command in place, just for testing purposes
fpm = os.path.join(tmpdir, 'fpm')
write_file(fpm, MOCKED_FPM % {'fpm_output_file': os.path.join(tmpdir, FPM_OUTPUT_FILE)})
adjust_permissions(fpm, stat.S_IXUSR, add=True)
# also put mocked rpmbuild in place
rpmbuild = os.path.join(tmpdir, 'rpmbuild')
write_file(rpmbuild, '#!/bin/bash') # only needs to be there, doesn't need to actually do something...
adjust_permissions(rpmbuild, stat.S_IXUSR, add=True)
os.environ['PATH'] = '%s:%s' % (tmpdir, os.environ['PATH'])
class PackageTest(EnhancedTestCase):
"""Tests for packaging support."""
pnsNames = ['EasyBuildDebFriendlyPNS', 'EasyBuildPNS']
def test_avail_package_naming_schemes(self):
"""Test avail_package_naming_schemes()"""
self.assertEqual(sorted(avail_package_naming_schemes().keys()), self.pnsNames)
def test_check_pkg_support(self):
"""Test check_pkg_support()."""
# clear $PATH to make sure fpm/rpmbuild can not be found
os.environ['PATH'] = ''
self.assertErrorRegex(EasyBuildError, "Selected packaging tool 'fpm' not found", check_pkg_support)
for binary in ['fpm', 'rpmbuild']:
binpath = os.path.join(self.test_prefix, binary)
write_file(binpath, '#!/bin/bash')
adjust_permissions(binpath, stat.S_IXUSR, add=True)
os.environ['PATH'] = self.test_prefix
# no errors => support check passes
check_pkg_support()
def test_active_pns(self):
"""Test use of ActivePNS."""
for pns_type in self.pnsNames:
os.environ['EASYBUILD_PACKAGE_NAMING_SCHEME'] = pns_type
init_config(build_options={'silent': True})
topdir = os.path.dirname(os.path.abspath(__file__))
test_easyconfigs = os.path.join(topdir, 'easyconfigs', 'test_ecs')
test_ec = os.path.join(test_easyconfigs, 'o', 'OpenMPI', 'OpenMPI-2.1.2-GCC-6.4.0-2.28.eb')
ec = EasyConfig(test_ec, validate=False)
pns = ActivePNS()
self.assertEqual(pns.name(ec), 'OpenMPI-2.1.2-GCC-6.4.0-2.28')
self.assertEqual(pns.release(ec), '1')
if get_package_naming_scheme() == "EasyBuildPNS":
# default: EasyBuild package naming scheme, pkg release 1
self.assertEqual(pns.version(ec), 'eb-%s' % EASYBUILD_VERSION)
elif get_package_naming_scheme() == "EasyBuildDebFriendlyPNS":
# default: EasyBuild deb friendly package naming scheme, pkg release 1
self.assertEqual(pns.version(ec), '%s-eb' % EASYBUILD_VERSION)
def test_package(self):
"""Test package function."""
self.mock_stdout(True)
build_options = {
'package_tool_options': '--foo bar',
'silent': True,
}
init_config(build_options=build_options)
topdir = os.path.dirname(os.path.abspath(__file__))
test_easyconfigs = os.path.join(topdir, 'easyconfigs', 'test_ecs')
ec = EasyConfig(os.path.join(test_easyconfigs, 't', 'toy', 'toy-0.0-gompi-2018a-test.eb'), validate=False)
mock_fpm(self.test_prefix)
# import needs to be done here, since test easyblocks are only included later
from easybuild.easyblocks.toy import EB_toy
easyblock = EB_toy(ec)
# build & install first
easyblock.run_all_steps(False)
# write a dummy log and report file to make sure they don't get packaged
logfile = os.path.join(easyblock.installdir, log_path(), "logfile.log")
write_file(logfile, "I'm a logfile")
reportfile = os.path.join(easyblock.installdir, log_path(), "report.md")
write_file(reportfile, "I'm a reportfile")
# package using default packaging configuration (FPM to build RPM packages)
pkgdir = package(easyblock)
pkgfile = os.path.join(pkgdir, 'toy-0.0-gompi-2018a-test-eb-%s.1.rpm' % EASYBUILD_VERSION)
fpm_output = read_file(os.path.join(self.test_prefix, FPM_OUTPUT_FILE))
pkgtxt = read_file(pkgfile)
self.assertTrue(os.path.isfile(pkgfile), "Found %s" % pkgfile)
# check whether extra packaging options were passed down
regex = re.compile("^got an unhandled option: --foo bar$", re.M)
self.assertTrue(regex.search(fpm_output), "Pattern '%s' found in: %s" % (regex.pattern, fpm_output))
pkgtxt = read_file(pkgfile)
pkgtxt_regex = re.compile("STARTCONTENTS of installdir %s" % easyblock.installdir)
self.assertTrue(pkgtxt_regex.search(pkgtxt), "Pattern '%s' found in: %s" % (pkgtxt_regex.pattern, pkgtxt))
no_logfiles_regex = re.compile(r'STARTCONTENTS.*\.(log|md)$.*ENDCONTENTS', re.DOTALL | re.MULTILINE)
res = no_logfiles_regex.search(pkgtxt)
self.assertFalse(res, "Pattern not '%s' found in: %s" % (no_logfiles_regex.pattern, pkgtxt))
toy_txt = read_file(os.path.join(test_easyconfigs, 't', 'toy', 'toy-0.0-gompi-2018a-test.eb'))
replace_str = '''description = """Toy C program, 100% toy. Now with `backticks'\n'''
replace_str += '''and newlines"""'''
toy_txt = re.sub('description = .*', replace_str, toy_txt)
toy_file = os.path.join(self.test_prefix, 'toy-test-description.eb')
write_file(toy_file, toy_txt)
regex = re.compile(r"""`backticks'""")
self.assertTrue(regex.search(toy_txt), "Pattern '%s' found in: %s" % (regex.pattern, toy_txt))
ec_desc = EasyConfig(toy_file, validate=False)
easyblock_desc = EB_toy(ec_desc)
easyblock_desc.run_all_steps(False)
pkgdir = package(easyblock_desc)
pkgfile = os.path.join(pkgdir, 'toy-0.0-gompi-2018a-test-eb-%s.1.rpm' % EASYBUILD_VERSION)
self.assertTrue(os.path.isfile(pkgfile))
pkgtxt = read_file(pkgfile)
regex_pkg = re.compile(r"""DESCRIPTION:.*`backticks'.*""")
self.assertTrue(regex_pkg.search(pkgtxt), "Pattern '%s' not found in: %s" % (regex_pkg.pattern, pkgtxt))
regex_pkg = re.compile(r"""DESCRIPTION:.*\nand newlines""", re.MULTILINE)
self.assertTrue(regex_pkg.search(pkgtxt), "Pattern '%s' not found in: %s" % (regex_pkg.pattern, pkgtxt))
self.mock_stdout(False)
def suite():
""" returns all the testcases in this module """
return TestLoaderFiltered().loadTestsFromTestCase(PackageTest, sys.argv[1:])
if __name__ == '__main__':
res = TextTestRunner(verbosity=1).run(suite())
sys.exit(len(res.failures))