summaryrefslogtreecommitdiff
path: root/lib/spack/spack/test/cmd/verify.py
blob: 1293df78e21d7f92b73e89eef6c4d4153c40aae1 (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
# 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)

"""Tests for the `spack verify` command"""
import os

import llnl.util.filesystem as fs

import spack.spec
import spack.store
import spack.util.spack_json as sjson
import spack.verify
from spack.main import SpackCommand

verify = SpackCommand("verify")
install = SpackCommand("install")


def test_single_file_verify_cmd(tmpdir):
    # Test the verify command interface to verifying a single file.
    filedir = os.path.join(str(tmpdir), "a", "b", "c", "d")
    filepath = os.path.join(filedir, "file")
    metadir = os.path.join(str(tmpdir), spack.store.STORE.layout.metadata_dir)

    fs.mkdirp(filedir)
    fs.mkdirp(metadir)

    with open(filepath, "w") as f:
        f.write("I'm a file")

    data = spack.verify.create_manifest_entry(filepath)

    manifest_file = os.path.join(metadir, spack.store.STORE.layout.manifest_file_name)

    with open(manifest_file, "w") as f:
        sjson.dump({filepath: data}, f)

    results = verify("-f", filepath, fail_on_error=False)
    print(results)
    assert not results

    os.utime(filepath, (0, 0))
    with open(filepath, "w") as f:
        f.write("I changed.")

    results = verify("-f", filepath, fail_on_error=False)

    expected = ["hash"]
    mtime = os.stat(filepath).st_mtime
    if mtime != data["time"]:
        expected.append("mtime")

    assert results
    assert filepath in results
    assert all(x in results for x in expected)

    results = verify("-fj", filepath, fail_on_error=False)
    res = sjson.load(results)
    assert len(res) == 1
    errors = res.pop(filepath)
    assert sorted(errors) == sorted(expected)


def test_single_spec_verify_cmd(
    tmpdir, mock_packages, mock_archive, mock_fetch, config, install_mockery
):
    # Test the verify command interface to verify a single spec
    install("libelf")
    s = spack.spec.Spec("libelf").concretized()
    prefix = s.prefix
    hash = s.dag_hash()

    results = verify("/%s" % hash, fail_on_error=False)
    assert not results

    new_file = os.path.join(prefix, "new_file_for_verify_test")
    with open(new_file, "w") as f:
        f.write("New file")

    results = verify("/%s" % hash, fail_on_error=False)
    assert new_file in results
    assert "added" in results

    results = verify("-j", "/%s" % hash, fail_on_error=False)
    res = sjson.load(results)
    assert len(res) == 1
    assert res[new_file] == ["added"]