summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/modules.py
blob: 135cd028e3d75f0abaeb87cd62679d55fecbeabc (plain) (blame)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
##############################################################################
# Copyright (c) 2013-2016, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the LICENSE file for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program 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 terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
import collections
from contextlib import contextmanager

import StringIO
import spack.modules
from spack.test.mock_packages_test import MockPackagesTest

FILE_REGISTRY = collections.defaultdict(StringIO.StringIO)


# Monkey-patch open to write module files to a StringIO instance
@contextmanager
def mock_open(filename, mode):
    if not mode == 'w':
        raise RuntimeError(
            'test.modules : unexpected opening mode for monkey-patched open')

    FILE_REGISTRY[filename] = StringIO.StringIO()

    try:
        yield FILE_REGISTRY[filename]
    finally:
        handle = FILE_REGISTRY[filename]
        FILE_REGISTRY[filename] = handle.getvalue()
        handle.close()


configuration_autoload_direct = {
    'enable': ['tcl'],
    'tcl': {
        'all': {
            'autoload': 'direct'
        }
    }
}

configuration_autoload_all = {
    'enable': ['tcl'],
    'tcl': {
        'all': {
            'autoload': 'all'
        }
    }
}

configuration_prerequisites_direct = {
    'enable': ['tcl'],
    'tcl': {
        'all': {
            'prerequisites': 'direct'
        }
    }
}

configuration_prerequisites_all = {
    'enable': ['tcl'],
    'tcl': {
        'all': {
            'prerequisites': 'all'
        }
    }
}

configuration_alter_environment = {
    'enable': ['tcl'],
    'tcl': {
        'all': {
            'filter': {'environment_blacklist': ['CMAKE_PREFIX_PATH']}
        },
        'platform=test target=x86_64': {
            'environment': {
                'set': {'FOO': 'foo'},
                'unset': ['BAR']
            }
        },
        'platform=test target=x86_32': {
            'load': ['foo/bar']
        }
    }
}

configuration_blacklist = {
    'enable': ['tcl'],
    'tcl': {
        'whitelist': ['zmpi'],
        'blacklist': ['callpath', 'mpi'],
        'all': {
            'autoload': 'direct'
        }
    }
}

configuration_conflicts = {
    'enable': ['tcl'],
    'tcl': {
        'naming_scheme': '{name}/{version}-{compiler.name}',
        'all': {
            'conflict': ['{name}', 'intel/14.0.1']
        }
    }
}

configuration_wrong_conflicts = {
    'enable': ['tcl'],
    'tcl': {
        'naming_scheme': '{name}/{version}-{compiler.name}',
        'all': {
            'conflict': ['{name}/{compiler.name}']
        }
    }
}

configuration_suffix = {
    'enable': ['tcl'],
    'tcl': {
        'mpileaks': {
            'suffixes': {
                '+debug': 'foo',
                '~debug': 'bar'
            }
        }
    }
}


class HelperFunctionsTests(MockPackagesTest):

    def test_update_dictionary_extending_list(self):
        target = {
            'foo': {
                'a': 1,
                'b': 2,
                'd': 4
            },
            'bar': [1, 2, 4],
            'baz': 'foobar'
        }
        update = {
            'foo': {
                'c': 3,
            },
            'bar': [3],
            'baz': 'foobaz',
            'newkey': {
                'd': 4
            }
        }
        spack.modules.update_dictionary_extending_lists(target, update)
        self.assertTrue(len(target) == 4)
        self.assertTrue(len(target['foo']) == 4)
        self.assertTrue(len(target['bar']) == 4)
        self.assertEqual(target['baz'], 'foobaz')

    def test_inspect_path(self):
        env = spack.modules.inspect_path('/usr')
        names = [item.name for item in env]
        self.assertTrue('PATH' in names)
        self.assertTrue('LIBRARY_PATH' in names)
        self.assertTrue('LD_LIBRARY_PATH' in names)
        self.assertTrue('CPATH' in names)


class TclTests(MockPackagesTest):

    def setUp(self):
        super(TclTests, self).setUp()
        self.configuration_obj = spack.modules.CONFIGURATION
        spack.modules.open = mock_open
        # Make sure that a non-mocked configuration will trigger an error
        spack.modules.CONFIGURATION = None

    def tearDown(self):
        del spack.modules.open
        spack.modules.CONFIGURATION = self.configuration_obj
        super(TclTests, self).tearDown()

    def get_modulefile_content(self, spec):
        spec.concretize()
        generator = spack.modules.TclModule(spec)
        generator.write()
        content = FILE_REGISTRY[generator.file_name].split('\n')
        return content

    def test_simple_case(self):
        spack.modules.CONFIGURATION = configuration_autoload_direct
        spec = spack.spec.Spec('mpich@3.0.4')
        content = self.get_modulefile_content(spec)
        self.assertTrue('module-whatis "mpich @3.0.4"' in content)
        self.assertRaises(TypeError, spack.modules.dependencies,
                          spec, 'non-existing-tag')

    def test_autoload(self):
        spack.modules.CONFIGURATION = configuration_autoload_direct
        spec = spack.spec.Spec('mpileaks')
        content = self.get_modulefile_content(spec)
        self.assertEqual(len([x for x in content if 'is-loaded' in x]), 2)
        self.assertEqual(len([x for x in content if 'module load ' in x]), 2)

        spack.modules.CONFIGURATION = configuration_autoload_all
        spec = spack.spec.Spec('mpileaks')
        content = self.get_modulefile_content(spec)
        self.assertEqual(len([x for x in content if 'is-loaded' in x]), 5)
        self.assertEqual(len([x for x in content if 'module load ' in x]), 5)

    def test_prerequisites(self):
        spack.modules.CONFIGURATION = configuration_prerequisites_direct
        spec = spack.spec.Spec('mpileaks arch=x86-linux')
        content = self.get_modulefile_content(spec)
        self.assertEqual(len([x for x in content if 'prereq' in x]), 2)

        spack.modules.CONFIGURATION = configuration_prerequisites_all
        spec = spack.spec.Spec('mpileaks arch=x86-linux')
        content = self.get_modulefile_content(spec)
        self.assertEqual(len([x for x in content if 'prereq' in x]), 5)

    def test_alter_environment(self):
        spack.modules.CONFIGURATION = configuration_alter_environment
        spec = spack.spec.Spec('mpileaks platform=test target=x86_64')
        content = self.get_modulefile_content(spec)
        self.assertEqual(
            len([x
                 for x in content
                 if x.startswith('prepend-path CMAKE_PREFIX_PATH')]), 0)
        self.assertEqual(
            len([x for x in content if 'setenv FOO "foo"' in x]), 1)
        self.assertEqual(len([x for x in content if 'unsetenv BAR' in x]), 1)

        spec = spack.spec.Spec('libdwarf %clang platform=test target=x86_32')
        content = self.get_modulefile_content(spec)
        self.assertEqual(
            len([x
                 for x in content
                 if x.startswith('prepend-path CMAKE_PREFIX_PATH')]), 0)
        self.assertEqual(
            len([x for x in content if 'setenv FOO "foo"' in x]), 0)
        self.assertEqual(len([x for x in content if 'unsetenv BAR' in x]), 0)
        self.assertEqual(
            len([x for x in content if 'is-loaded foo/bar' in x]), 1)
        self.assertEqual(
            len([x for x in content if 'module load foo/bar' in x]), 1)

    def test_blacklist(self):
        spack.modules.CONFIGURATION = configuration_blacklist
        spec = spack.spec.Spec('mpileaks ^zmpi')
        content = self.get_modulefile_content(spec)
        self.assertEqual(len([x for x in content if 'is-loaded' in x]), 1)
        self.assertEqual(len([x for x in content if 'module load ' in x]), 1)
        spec = spack.spec.Spec('callpath arch=x86-linux')
        # Returns a StringIO instead of a string as no module file was written
        self.assertRaises(AttributeError, self.get_modulefile_content, spec)
        spec = spack.spec.Spec('zmpi arch=x86-linux')
        content = self.get_modulefile_content(spec)
        self.assertEqual(len([x for x in content if 'is-loaded' in x]), 1)
        self.assertEqual(len([x for x in content if 'module load ' in x]), 1)

    def test_conflicts(self):
        spack.modules.CONFIGURATION = configuration_conflicts
        spec = spack.spec.Spec('mpileaks')
        content = self.get_modulefile_content(spec)
        self.assertEqual(
            len([x for x in content if x.startswith('conflict')]), 2)
        self.assertEqual(
            len([x for x in content if x == 'conflict mpileaks']), 1)
        self.assertEqual(
            len([x for x in content if x == 'conflict intel/14.0.1']), 1)

        spack.modules.CONFIGURATION = configuration_wrong_conflicts
        self.assertRaises(SystemExit, self.get_modulefile_content, spec)

    def test_suffixes(self):
        spack.modules.CONFIGURATION = configuration_suffix
        spec = spack.spec.Spec('mpileaks+debug arch=x86-linux')
        spec.concretize()
        generator = spack.modules.TclModule(spec)
        self.assertTrue('foo' in generator.use_name)

        spec = spack.spec.Spec('mpileaks~debug arch=x86-linux')
        spec.concretize()
        generator = spack.modules.TclModule(spec)
        self.assertTrue('bar' in generator.use_name)


configuration_dotkit = {
    'enable': ['dotkit'],
    'dotkit': {
        'all': {
            'prerequisites': 'direct'
        }
    }
}


class DotkitTests(MockPackagesTest):

    def setUp(self):
        super(DotkitTests, self).setUp()
        self.configuration_obj = spack.modules.CONFIGURATION
        spack.modules.open = mock_open
        # Make sure that a non-mocked configuration will trigger an error
        spack.modules.CONFIGURATION = None

    def tearDown(self):
        del spack.modules.open
        spack.modules.CONFIGURATION = self.configuration_obj
        super(DotkitTests, self).tearDown()

    def get_modulefile_content(self, spec):
        spec.concretize()
        generator = spack.modules.Dotkit(spec)
        generator.write()
        content = FILE_REGISTRY[generator.file_name].split('\n')
        return content

    def test_dotkit(self):
        spack.modules.CONFIGURATION = configuration_dotkit
        spec = spack.spec.Spec('mpileaks arch=x86-linux')
        content = self.get_modulefile_content(spec)
        self.assertTrue('#c spack' in content)
        self.assertTrue('#d mpileaks @2.3' in content)