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
|
# Copyright 2013-2024 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)
# These are slow integration tests that do concretization, install, tarballing
# and compression. They still use an in-memory OCI registry.
import hashlib
import json
import os
from contextlib import contextmanager
import spack.environment as ev
import spack.oci.opener
from spack.binary_distribution import gzip_compressed_tarfile
from spack.main import SpackCommand
from spack.oci.image import Digest, ImageReference, default_config, default_manifest
from spack.oci.oci import blob_exists, get_manifest_and_config, upload_blob, upload_manifest
from spack.test.oci.mock_registry import DummyServer, InMemoryOCIRegistry, create_opener
buildcache = SpackCommand("buildcache")
mirror = SpackCommand("mirror")
env = SpackCommand("env")
install = SpackCommand("install")
@contextmanager
def oci_servers(*servers: DummyServer):
old_opener = spack.oci.opener.urlopen
spack.oci.opener.urlopen = create_opener(*servers).open
yield
spack.oci.opener.urlopen = old_opener
def test_buildcache_push_command(mutable_database, disable_parallel_buildcache_push):
with oci_servers(InMemoryOCIRegistry("example.com")):
mirror("add", "oci-test", "oci://example.com/image")
# Push the package(s) to the OCI registry
buildcache("push", "--update-index", "oci-test", "mpileaks^mpich")
# Remove mpileaks from the database
matches = mutable_database.query_local("mpileaks^mpich")
assert len(matches) == 1
spec = matches[0]
spec.package.do_uninstall()
# Reinstall mpileaks from the OCI registry
buildcache("install", "--unsigned", "mpileaks^mpich")
# Now it should be installed again
assert spec.installed
# And let's check that the bin/mpileaks executable is there
assert os.path.exists(os.path.join(spec.prefix, "bin", "mpileaks"))
def test_buildcache_tag(
install_mockery, mock_fetch, mutable_mock_env_path, disable_parallel_buildcache_push
):
"""Tests whether we can create an OCI image from a full environment with multiple roots."""
env("create", "test")
with ev.read("test"):
install("--add", "libelf")
install("--add", "trivial-install-test-package")
registry = InMemoryOCIRegistry("example.com")
with oci_servers(registry):
mirror("add", "oci-test", "oci://example.com/image")
with ev.read("test"):
buildcache("push", "--tag", "full_env", "oci-test")
name = ImageReference.from_string("example.com/image:full_env")
with ev.read("test") as e:
specs = e.all_specs()
manifest, config = get_manifest_and_config(name)
# without a base image, we should have one layer per spec
assert len(manifest["layers"]) == len(specs)
# Now create yet another tag, but with just a single selected spec as root. This should
# also test the case where Spack doesn't have to upload any binaries, it just has to create
# a new tag.
libelf = next(s for s in specs if s.name == "libelf")
with ev.read("test"):
# Get libelf spec
buildcache("push", "--tag", "single_spec", "oci-test", libelf.format("libelf{/hash}"))
name = ImageReference.from_string("example.com/image:single_spec")
manifest, config = get_manifest_and_config(name)
assert len(manifest["layers"]) == 1
def test_buildcache_push_with_base_image_command(
mutable_database, tmpdir, disable_parallel_buildcache_push
):
"""Test that we can push a package with a base image to an OCI registry.
This test is a bit involved, cause we have to create a small base image."""
registry_src = InMemoryOCIRegistry("src.example.com")
registry_dst = InMemoryOCIRegistry("dst.example.com")
base_image = ImageReference.from_string("src.example.com/my-base-image:latest")
with oci_servers(registry_src, registry_dst):
mirror("add", "oci-test", "oci://dst.example.com/image")
# TODO: simplify creation of images...
# We create a rootfs.tar.gz, a config file and a manifest file,
# and upload those.
config, manifest = default_config(architecture="amd64", os="linux"), default_manifest()
# Create a small rootfs
rootfs = tmpdir.join("rootfs")
rootfs.ensure(dir=True)
rootfs.join("bin").ensure(dir=True)
rootfs.join("bin", "sh").ensure(file=True)
# Create a tarball of it.
tarball = tmpdir.join("base.tar.gz")
with gzip_compressed_tarfile(tarball) as (tar, tar_gz_checksum, tar_checksum):
tar.add(rootfs, arcname=".")
tar_gz_digest = Digest.from_sha256(tar_gz_checksum.hexdigest())
tar_digest = Digest.from_sha256(tar_checksum.hexdigest())
# Save the config file
config["rootfs"]["diff_ids"] = [str(tar_digest)]
config_file = tmpdir.join("config.json")
with open(config_file, "w") as f:
f.write(json.dumps(config))
config_digest = Digest.from_sha256(
hashlib.sha256(open(config_file, "rb").read()).hexdigest()
)
# Register the layer in the manifest
manifest["layers"].append(
{
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
"digest": str(tar_gz_digest),
"size": tarball.size(),
}
)
manifest["config"]["digest"] = str(config_digest)
manifest["config"]["size"] = config_file.size()
# Upload the layer and config file
upload_blob(base_image, tarball, tar_gz_digest)
upload_blob(base_image, config_file, config_digest)
# Upload the manifest
upload_manifest(base_image, manifest)
# END TODO
# Finally... use it as a base image
buildcache("push", "--base-image", str(base_image), "oci-test", "mpileaks^mpich")
# Figure out what tag was produced
tag = next(tag for _, tag in registry_dst.manifests.keys() if tag.startswith("mpileaks-"))
assert tag is not None
# Fetch the manifest and config
dst_image = ImageReference.from_string(f"dst.example.com/image:{tag}")
retrieved_manifest, retrieved_config = get_manifest_and_config(dst_image)
# Check that the base image layer is first.
assert retrieved_manifest["layers"][0]["digest"] == str(tar_gz_digest)
assert retrieved_config["rootfs"]["diff_ids"][0] == str(tar_digest)
# And also check that we have layers for each link-run dependency
matches = mutable_database.query_local("mpileaks^mpich")
assert len(matches) == 1
spec = matches[0]
num_runtime_deps = len(list(spec.traverse(root=True, deptype=("link", "run"))))
# One base layer + num_runtime_deps
assert len(retrieved_manifest["layers"]) == 1 + num_runtime_deps
# And verify that all layers including the base layer are present
for layer in retrieved_manifest["layers"]:
assert blob_exists(dst_image, digest=Digest.from_string(layer["digest"]))
|