summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/cmd/uninstall.py
blob: abad976c8f4e914b91f9d69949a2ffd403739f0f (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)


import pytest

import llnl.util.tty as tty

import spack.cmd.uninstall
import spack.environment
import spack.store
from spack.main import SpackCommand, SpackCommandError

uninstall = SpackCommand("uninstall")
install = SpackCommand("install")


class MockArgs:
    def __init__(self, packages, all=False, force=False, dependents=False):
        self.packages = packages
        self.all = all
        self.force = force
        self.dependents = dependents
        self.yes_to_all = True


@pytest.mark.db
def test_multiple_matches(mutable_database):
    """Test unable to uninstall when multiple matches."""
    with pytest.raises(SpackCommandError):
        uninstall("-y", "mpileaks")


@pytest.mark.db
def test_installed_dependents(mutable_database):
    """Test can't uninstall when there are installed dependents."""
    with pytest.raises(SpackCommandError):
        uninstall("-y", "libelf")


@pytest.mark.db
def test_correct_installed_dependents(mutable_database):
    # Test whether we return the right dependents.

    # Take callpath from the database
    callpath = spack.store.STORE.db.query_local("callpath")[0]

    # Ensure it still has dependents and dependencies
    dependents = callpath.dependents(deptype=("run", "link"))
    dependencies = callpath.dependencies(deptype=("run", "link"))
    assert dependents and dependencies

    # Uninstall it, so it's missing.
    callpath.package.do_uninstall(force=True)

    # Retrieve all dependent hashes
    dependents = spack.cmd.uninstall.installed_dependents(dependencies)
    assert dependents

    dependent_hashes = [s.dag_hash() for s in dependents]
    set_dependent_hashes = set(dependent_hashes)

    # Assert uniqueness
    assert len(dependent_hashes) == len(set_dependent_hashes)

    # Ensure parents of callpath are listed
    assert all(s.dag_hash() in set_dependent_hashes for s in dependents)

    # Ensure callpath itself is not, since it was missing.
    assert callpath.dag_hash() not in set_dependent_hashes


@pytest.mark.db
def test_recursive_uninstall(mutable_database):
    """Test recursive uninstall."""
    uninstall("-y", "-a", "--dependents", "callpath")

    all_specs = spack.store.STORE.layout.all_specs()
    assert len(all_specs) == 9
    # query specs with multiple configurations
    mpileaks_specs = [s for s in all_specs if s.satisfies("mpileaks")]
    callpath_specs = [s for s in all_specs if s.satisfies("callpath")]
    mpi_specs = [s for s in all_specs if s.satisfies("mpi")]

    assert len(mpileaks_specs) == 0
    assert len(callpath_specs) == 0
    assert len(mpi_specs) == 3


@pytest.mark.db
@pytest.mark.regression("3690")
@pytest.mark.parametrize("constraint,expected_number_of_specs", [("dyninst", 8), ("libelf", 6)])
def test_uninstall_spec_with_multiple_roots(
    constraint, expected_number_of_specs, mutable_database
):
    uninstall("-y", "-a", "--dependents", constraint)

    all_specs = spack.store.STORE.layout.all_specs()
    assert len(all_specs) == expected_number_of_specs


@pytest.mark.db
@pytest.mark.parametrize("constraint,expected_number_of_specs", [("dyninst", 14), ("libelf", 14)])
def test_force_uninstall_spec_with_ref_count_not_zero(
    constraint, expected_number_of_specs, mutable_database
):
    uninstall("-f", "-y", constraint)

    all_specs = spack.store.STORE.layout.all_specs()
    assert len(all_specs) == expected_number_of_specs


@pytest.mark.db
def test_force_uninstall_and_reinstall_by_hash(mutable_database):
    """Test forced uninstall and reinstall of old specs."""
    # this is the spec to be removed
    callpath_spec = spack.store.STORE.db.query_one("callpath ^mpich")
    dag_hash = callpath_spec.dag_hash()

    # ensure can look up by hash and that it's a dependent of mpileaks
    def validate_callpath_spec(installed):
        assert installed is True or installed is False

        specs = spack.store.STORE.db.get_by_hash(dag_hash, installed=installed)
        assert len(specs) == 1 and specs[0] == callpath_spec

        specs = spack.store.STORE.db.get_by_hash(dag_hash[:7], installed=installed)
        assert len(specs) == 1 and specs[0] == callpath_spec

        specs = spack.store.STORE.db.get_by_hash(dag_hash, installed=any)
        assert len(specs) == 1 and specs[0] == callpath_spec

        specs = spack.store.STORE.db.get_by_hash(dag_hash[:7], installed=any)
        assert len(specs) == 1 and specs[0] == callpath_spec

        specs = spack.store.STORE.db.get_by_hash(dag_hash, installed=not installed)
        assert specs is None

        specs = spack.store.STORE.db.get_by_hash(dag_hash[:7], installed=not installed)
        assert specs is None

        mpileaks_spec = spack.store.STORE.db.query_one("mpileaks ^mpich")
        assert callpath_spec in mpileaks_spec

        spec = spack.store.STORE.db.query_one("callpath ^mpich", installed=installed)
        assert spec == callpath_spec

        spec = spack.store.STORE.db.query_one("callpath ^mpich", installed=any)
        assert spec == callpath_spec

        spec = spack.store.STORE.db.query_one("callpath ^mpich", installed=not installed)
        assert spec is None

    validate_callpath_spec(True)

    uninstall("-y", "-f", "callpath ^mpich")

    # ensure that you can still look up by hash and see deps, EVEN though
    # the callpath spec is missing.
    validate_callpath_spec(False)

    # BUT, make sure that the removed callpath spec is not in queries
    def db_specs():
        all_specs = spack.store.STORE.layout.all_specs()
        return (
            all_specs,
            [s for s in all_specs if s.satisfies("mpileaks")],
            [s for s in all_specs if s.satisfies("callpath")],
            [s for s in all_specs if s.satisfies("mpi")],
        )

    all_specs, mpileaks_specs, callpath_specs, mpi_specs = db_specs()
    total_specs = len(all_specs)
    assert total_specs == 14
    assert len(mpileaks_specs) == 3
    assert len(callpath_specs) == 2
    assert len(mpi_specs) == 3

    # Now, REINSTALL the spec and make sure everything still holds
    install("--fake", "/%s" % dag_hash[:7])

    validate_callpath_spec(True)

    all_specs, mpileaks_specs, callpath_specs, mpi_specs = db_specs()
    assert len(all_specs) == total_specs + 1  # back to total_specs+1
    assert len(mpileaks_specs) == 3
    assert len(callpath_specs) == 3  # back to 3
    assert len(mpi_specs) == 3


@pytest.mark.db
@pytest.mark.regression("15773")
def test_in_memory_consistency_when_uninstalling(mutable_database, monkeypatch):
    """Test that uninstalling doesn't raise warnings"""

    def _warn(*args, **kwargs):
        raise RuntimeError("a warning was triggered!")

    monkeypatch.setattr(tty, "warn", _warn)
    # Now try to uninstall and check this doesn't trigger warnings
    uninstall("-y", "-a")


# Note: I want to use https://docs.pytest.org/en/7.1.x/how-to/skipping.html#skip-all-test-functions-of-a-class-or-module
# the style formatter insists on separating these two lines.
@pytest.mark.not_on_windows("Envs unsupported on Windows")
class TestUninstallFromEnv:
    """Tests an installation with two environments e1 and e2, which each have
    shared package installations:

    e1 has diamond-link-left -> diamond-link-bottom

    e2 has diamond-link-right -> diamond-link-bottom
    """

    env = SpackCommand("env")
    add = SpackCommand("add")
    concretize = SpackCommand("concretize")
    find = SpackCommand("find")

    @pytest.fixture(scope="function")
    def environment_setup(
        self, mutable_mock_env_path, config, mock_packages, mutable_database, install_mockery
    ):
        TestUninstallFromEnv.env("create", "e1")
        e1 = spack.environment.read("e1")
        with e1:
            TestUninstallFromEnv.add("diamond-link-left")
            TestUninstallFromEnv.add("diamond-link-bottom")
            TestUninstallFromEnv.concretize()
            install("--fake")

        TestUninstallFromEnv.env("create", "e2")
        e2 = spack.environment.read("e2")
        with e2:
            TestUninstallFromEnv.add("diamond-link-right")
            TestUninstallFromEnv.add("diamond-link-bottom")
            TestUninstallFromEnv.concretize()
            install("--fake")
        yield "environment_setup"
        TestUninstallFromEnv.env("rm", "e1", "-y")
        TestUninstallFromEnv.env("rm", "e2", "-y")

    def test_basic_env_sanity(self, environment_setup):
        for env_name in ["e1", "e2"]:
            e = spack.environment.read(env_name)
            with e:
                for _, concretized_spec in e.concretized_specs():
                    assert concretized_spec.package.installed

    def test_uninstall_force_dependency_shared_between_envs(self, environment_setup):
        """If you "spack uninstall -f --dependents diamond-link-bottom" from
        e1, then all packages should be uninstalled (but not removed) from
        both e1 and e2.
        """
        e1 = spack.environment.read("e1")
        with e1:
            uninstall("-f", "-y", "--dependents", "diamond-link-bottom")

            # The specs should still be in the environment, since
            # --remove was not specified
            assert set(root.name for (root, _) in e1.concretized_specs()) == set(
                ["diamond-link-left", "diamond-link-bottom"]
            )

            for _, concretized_spec in e1.concretized_specs():
                assert not concretized_spec.package.installed

        # Everything in e2 depended on diamond-link-bottom, so should also
        # have been uninstalled. The roots should be unchanged though.
        e2 = spack.environment.read("e2")
        with e2:
            assert set(root.name for (root, _) in e2.concretized_specs()) == set(
                ["diamond-link-right", "diamond-link-bottom"]
            )
            for _, concretized_spec in e2.concretized_specs():
                assert not concretized_spec.package.installed

    def test_uninstall_remove_dependency_shared_between_envs(self, environment_setup):
        """If you "spack uninstall --dependents --remove diamond-link-bottom" from
        e1, then all packages are removed from e1 (it is now empty);
        diamond-link-left is also uninstalled (since only e1 needs it) but
        diamond-link-bottom is not uninstalled (since e2 needs it).
        """
        e1 = spack.environment.read("e1")
        with e1:
            dtdiamondleft = next(
                concrete
                for (_, concrete) in e1.concretized_specs()
                if concrete.name == "diamond-link-left"
            )
            output = uninstall("-y", "--dependents", "--remove", "diamond-link-bottom")
            assert "The following specs will be removed but not uninstalled" in output
            assert not list(e1.roots())
            assert not dtdiamondleft.package.installed

        # Since -f was not specified, all specs in e2 should still be installed
        # (and e2 should be unchanged)
        e2 = spack.environment.read("e2")
        with e2:
            assert set(root.name for (root, _) in e2.concretized_specs()) == set(
                ["diamond-link-right", "diamond-link-bottom"]
            )
            for _, concretized_spec in e2.concretized_specs():
                assert concretized_spec.package.installed

    def test_uninstall_dependency_shared_between_envs_fail(self, environment_setup):
        """If you "spack uninstall --dependents diamond-link-bottom" from
        e1 (without --remove or -f), then this should fail (this is needed by
        e2).
        """
        e1 = spack.environment.read("e1")
        with e1:
            output = uninstall("-y", "--dependents", "diamond-link-bottom", fail_on_error=False)
            assert "There are still dependents." in output
            assert "use `spack env remove`" in output

        # The environment should be unchanged and nothing should have been
        # uninstalled
        assert set(root.name for (root, _) in e1.concretized_specs()) == set(
            ["diamond-link-left", "diamond-link-bottom"]
        )
        for _, concretized_spec in e1.concretized_specs():
            assert concretized_spec.package.installed

    def test_uninstall_force_and_remove_dependency_shared_between_envs(self, environment_setup):
        """If you "spack uninstall -f --dependents --remove diamond-link-bottom" from
        e1, then all packages should be uninstalled and removed from e1.
        All packages will also be uninstalled from e2, but the roots will
        remain unchanged.
        """
        e1 = spack.environment.read("e1")
        with e1:
            dtdiamondleft = next(
                concrete
                for (_, concrete) in e1.concretized_specs()
                if concrete.name == "diamond-link-left"
            )
            uninstall("-f", "-y", "--dependents", "--remove", "diamond-link-bottom")
            assert not list(e1.roots())
            assert not dtdiamondleft.package.installed

        e2 = spack.environment.read("e2")
        with e2:
            assert set(root.name for (root, _) in e2.concretized_specs()) == set(
                ["diamond-link-right", "diamond-link-bottom"]
            )
            for _, concretized_spec in e2.concretized_specs():
                assert not concretized_spec.package.installed

    def test_uninstall_keep_dependents_dependency_shared_between_envs(self, environment_setup):
        """If you "spack uninstall -f --remove diamond-link-bottom" from
        e1, then diamond-link-bottom should be uninstalled, which leaves
        "dangling" references in both environments, since
        diamond-link-left and diamond-link-right both need it.
        """
        e1 = spack.environment.read("e1")
        with e1:
            dtdiamondleft = next(
                concrete
                for (_, concrete) in e1.concretized_specs()
                if concrete.name == "diamond-link-left"
            )
            uninstall("-f", "-y", "--remove", "diamond-link-bottom")
            # diamond-link-bottom was removed from the list of roots (note that
            # it would still be installed since diamond-link-left depends on it)
            assert set(x.name for x in e1.roots()) == set(["diamond-link-left"])
            assert dtdiamondleft.package.installed

        e2 = spack.environment.read("e2")
        with e2:
            assert set(root.name for (root, _) in e2.concretized_specs()) == set(
                ["diamond-link-right", "diamond-link-bottom"]
            )
            dtdiamondright = next(
                concrete
                for (_, concrete) in e2.concretized_specs()
                if concrete.name == "diamond-link-right"
            )
            assert dtdiamondright.package.installed
            dtdiamondbottom = next(
                concrete
                for (_, concrete) in e2.concretized_specs()
                if concrete.name == "diamond-link-bottom"
            )
            assert not dtdiamondbottom.package.installed