Skip to content

Commit

Permalink
image digest
Browse files Browse the repository at this point in the history
  • Loading branch information
praiskup committed Sep 2, 2024
1 parent df00cfa commit ac0733b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
11 changes: 11 additions & 0 deletions mock/docs/buildroot-lock-schema-1.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@
"packages"
]
},
"bootstrap": {
"description": "The object that describes the Mock bootstrap chroot. Optional, only provided when bootstrap (image) is used.",
"type": "object",
"additionalProperties": false,
"properties": {
"image_digest": {
"description": "Digest got by the 'podman image inspect --format {{ .Digest }}' command, sha256 string",
"type": "string"
}
}
},
"config": {
"description": "A set of important Mock configuration options used when the buildroot was generated (Mock's internal)",
"type": "object",
Expand Down
12 changes: 12 additions & 0 deletions mock/py/mockbuild/plugins/buildroot_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import os

from mockbuild.podman import Podman
from mockbuild.installed_packages import query_packages, query_packages_location

requires_api_version = "1.1"
Expand Down Expand Up @@ -92,6 +93,17 @@ def _executor(cmd):
if cfg_option in self.buildroot.config:
data["config"][cfg_option] = self.buildroot.config[cfg_option]

if "bootstrap_image" in data["config"]:
# Optional object, only if bootstrap image used (we still
# produce lockfiles even if these are useless for isolated
# builds).
podman = Podman(self.buildroot,
data["config"]["bootstrap_image"])
digest = podman.get_image_digest()
data["bootstrap"] = {
"image_digest": digest,
}

with open(out_file, "w", encoding="utf-8") as fdlist:
fdlist.write(json.dumps(data, indent=4, sort_keys=True) + "\n")
finally:
Expand Down
16 changes: 16 additions & 0 deletions mock/py/mockbuild/podman.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ def mounted_image(self):
subprocess.run(cmd_umount, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, check=True)

def get_image_digest(self):
"""
Get the "sha256:..." string for the image we work with.
"""
check = [self.podman_binary, "image", "inspect", self.image,
"--format", "{{ .Digest }}"]
result = subprocess.run(check, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, check=False,
encoding="utf8")
if result.returncode:
raise BootstrapError(f"Can't get {self.image} podman image digest")
result = result.stdout.strip()
if len(result.splitlines()) != 1:
raise BootstrapError(f"The digest of {self.image} image is not a single-line string")
return result

@traceLog()
def cp(self, destination, tar_cmd):
""" copy content of container to destination directory """
Expand Down
12 changes: 10 additions & 2 deletions mock/tests/test_buildroot_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import tempfile
from unittest import TestCase
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch
import jsonschema

from mockbuild.plugins.buildroot_lock import init
Expand Down Expand Up @@ -61,6 +61,9 @@
'version': '42',
}]
},
"bootstrap": {
"image_digest": "sha256:ba1067bef190fbe88f085bd019464a8c0803b7cd1e3f",
},
'config': {
'bootstrap_image': 'foo',
'bootstrap_image_ready': True,
Expand Down Expand Up @@ -96,7 +99,12 @@ def _call_method(plugins, buildroot):
# obtain the hook method, and call it
plugins.add_hook.assert_called_once()
_, method = plugins.add_hook.call_args[0]
method()

podman_obj = MagicMock()
podman_obj.get_image_digest.return_value = EXPECTED_OUTPUT["bootstrap"]["image_digest"]
podman_cls = MagicMock(return_value=podman_obj)
with patch("mockbuild.plugins.buildroot_lock.Podman", side_effect=podman_cls):
method()


def test_nonexisting_file_in_repo():
Expand Down

0 comments on commit ac0733b

Please sign in to comment.