summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/mock_packages_test.py
blob: e1acc32cb712eec24cd2066a4865a8cd10ad514e (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
##############################################################################
# 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 os
import shutil
import tempfile
import unittest

import spack
import spack.config
from llnl.util.filesystem import mkdirp
from ordereddict_backport import OrderedDict
from spack.repository import RepoPath
from spack.spec import Spec

platform = spack.architecture.platform()

linux_os_name = 'debian'
linux_os_version = '6'

if platform.name == 'linux':
    linux_os = platform.operating_system("default_os")
    linux_os_name = linux_os.name
    linux_os_version = linux_os.version

mock_compiler_config = """\
compilers:
- compiler:
    spec: clang@3.3
    operating_system: {0}{1}
    paths:
      cc: /path/to/clang
      cxx: /path/to/clang++
      f77: None
      fc: None
    modules: 'None'
- compiler:
    spec: gcc@4.5.0
    operating_system: {0}{1}
    paths:
      cc: /path/to/gcc
      cxx: /path/to/g++
      f77: None
      fc: None
    modules: 'None'
- compiler:
    spec: clang@3.3
    operating_system: CNL10
    paths:
      cc: /path/to/clang
      cxx: /path/to/clang++
      f77: None
      fc: None
    modules: 'None'
- compiler:
    spec: clang@3.3
    operating_system: SuSE11
    paths:
      cc: /path/to/clang
      cxx: /path/to/clang++
      f77: None
      fc: None
    modules: 'None'
- compiler:
    spec: clang@3.3
    operating_system: redhat6
    paths:
      cc: /path/to/clang
      cxx: /path/to/clang++
      f77: None
      fc: None
    modules: 'None'
- compiler:
    spec: clang@3.3
    operating_system: yosemite
    paths:
      cc: /path/to/clang
      cxx: /path/to/clang++
      f77: None
      fc: None
    modules: 'None'
- compiler:
    paths:
      cc: /path/to/gcc
      cxx: /path/to/g++
      f77: /path/to/gfortran
      fc: /path/to/gfortran
    operating_system: CNL10
    spec: gcc@4.5.0
    modules: 'None'
- compiler:
    paths:
      cc: /path/to/gcc
      cxx: /path/to/g++
      f77: /path/to/gfortran
      fc: /path/to/gfortran
    operating_system: SuSE11
    spec: gcc@4.5.0
    modules: 'None'
- compiler:
    paths:
      cc: /path/to/gcc
      cxx: /path/to/g++
      f77: /path/to/gfortran
      fc: /path/to/gfortran
    operating_system: redhat6
    spec: gcc@4.5.0
    modules: 'None'
- compiler:
    paths:
      cc: /path/to/gcc
      cxx: /path/to/g++
      f77: /path/to/gfortran
      fc: /path/to/gfortran
    operating_system: yosemite
    spec: gcc@4.5.0
    modules: 'None'
- compiler:
    paths:
      cc: /path/to/gcc
      cxx: /path/to/g++
      f77: /path/to/gfortran
      fc: /path/to/gfortran
    operating_system: elcapitan
    spec: gcc@4.5.0
    modules: 'None'
- compiler:
    spec: clang@3.3
    operating_system: elcapitan
    paths:
      cc: /path/to/clang
      cxx: /path/to/clang++
      f77: None
      fc: None
    modules: 'None'
""".format(linux_os_name, linux_os_version)

mock_packages_config = """\
packages:
  externaltool:
    buildable: False
    paths:
      externaltool@1.0%gcc@4.5.0: /path/to/external_tool
  externalvirtual:
    buildable: False
    paths:
      externalvirtual@2.0%clang@3.3: /path/to/external_virtual_clang
      externalvirtual@1.0%gcc@4.5.0: /path/to/external_virtual_gcc
  externalmodule:
    buildable: False
    modules:
      externalmodule@1.0%gcc@4.5.0: external-module
"""

class MockPackagesTest(unittest.TestCase):
    def initmock(self):
        # Use the mock packages database for these tests.  This allows
        # us to set up contrived packages that don't interfere with
        # real ones.
        self.db = RepoPath(spack.mock_packages_path)
        spack.repo.swap(self.db)

        spack.config.clear_config_caches()
        self.real_scopes = spack.config.config_scopes

        # Mock up temporary configuration directories
        self.temp_config = tempfile.mkdtemp()
        self.mock_site_config = os.path.join(self.temp_config, 'site')
        self.mock_user_config = os.path.join(self.temp_config, 'user')
        mkdirp(self.mock_site_config)
        mkdirp(self.mock_user_config)
        for confs in [('compilers.yaml', mock_compiler_config), ('packages.yaml', mock_packages_config)]:
            conf_yaml = os.path.join(self.mock_site_config, confs[0])
            with open(conf_yaml, 'w') as f:
                f.write(confs[1])

        # TODO: Mocking this up is kind of brittle b/c ConfigScope
        # TODO: constructor modifies config_scopes.  Make it cleaner.
        spack.config.config_scopes = OrderedDict()
        spack.config.ConfigScope('site', self.mock_site_config)
        spack.config.ConfigScope('user', self.mock_user_config)

        # Keep tests from interfering with the actual module path.
        self.real_share_path = spack.share_path
        spack.share_path = tempfile.mkdtemp()

        # Store changes to the package's dependencies so we can
        # restore later.
        self.saved_deps = {}


    def set_pkg_dep(self, pkg_name, spec):
        """Alters dependence information for a package.

        Adds a dependency on <spec> to pkg.
        Use this to mock up constraints.
        """
        spec = Spec(spec)

        # Save original dependencies before making any changes.
        pkg = spack.repo.get(pkg_name)
        if pkg_name not in self.saved_deps:
            self.saved_deps[pkg_name] = (pkg, pkg.dependencies.copy())

        # Change dep spec
        pkg.dependencies[spec.name] = { Spec(pkg_name) : spec }


    def cleanmock(self):
        """Restore the real packages path after any test."""
        spack.repo.swap(self.db)
        spack.config.config_scopes = self.real_scopes
        shutil.rmtree(self.temp_config, ignore_errors=True)
        spack.config.clear_config_caches()

        # Restore dependency changes that happened during the test
        for pkg_name, (pkg, deps) in self.saved_deps.items():
            pkg.dependencies.clear()
            pkg.dependencies.update(deps)

        shutil.rmtree(spack.share_path, ignore_errors=True)
        spack.share_path = self.real_share_path


    def setUp(self):
        self.initmock()


    def tearDown(self):
        self.cleanmock()