Skip to content

Commit

Permalink
Tests: new tool to build a no-history image
Browse files Browse the repository at this point in the history
Script crafts a custom image with multiple layers but
no history; used in tests/bud.bats:bud-implicit-no-history test.

Signed-off-by: Ed Santiago <santiago@redhat.com>
  • Loading branch information
edsantiago committed Apr 22, 2024
1 parent 292afc5 commit f5f31d3
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions tests/make-nohistory-image
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/bash
#
# make-no-history-image - craft a custom image used in bud-implicit-no-history test
#
# See https://github.com/containers/buildah/pull/5473/files
#
set -e

# Name of the resulting image. (Never pushed automatically).
# If image build is successful, a human can push it to quay.
IMAGE=quay.io/libpod/buildah-testimage-nohistory:$(date +%Y%m%d)

function createrandom() {
outfile=${1?Missing OUTFILE arg to createrandom()}
if [[ ! -e $outfile ]]; then
dd if=/dev/urandom bs=1 count=${2:-256} of=$outfile status=none
fi
}

workdir=$(mktemp -d)

# First layer is a copy of this script
# FIXME: find some way to add a label or annotation or something to indicate that
cp $0 $workdir/file01
cd $workdir

mkdir -p oci/blobs/sha256

# Build an image config and image manifest in parallel
# FIXME: find some way to create images for other arches?
echo '{"architecture": "amd64", "os": "linux", "rootfs": {"type": "layers", "diff_ids": [' > config.json
echo '{"schemaVersion": 2, "mediaType": "application/vnd.oci.image.manifest.v1+json", "layers": [' > manifest.json

# Create some layers
for layer in $(seq 16) ; do
# Content for the layer
createrandom file$layer $((RANDOM+1024))
# Layer blob
tar -cf layer$layer.tar file$layer
# Get the layer blob's digest and size
diffid=$(sha256sum layer$layer.tar | awk '{print $1}')
diffsize=$(wc -c layer$layer.tar | awk '{print $1}')
# Link the blob into where an OCI layout would put it.
ln layer$layer.tar oci/blobs/sha256/$diffid
# Try to keep the resulting files at least kind of readable.
if test $layer -gt 1 ; then
echo "," >> config.json
echo "," >> manifest.json
fi

# Add the layer to the config blob's list of diffIDs for its rootfs.
echo -n ' "sha256:'${diffid}'"' >> config.json
# Add the layer blob to the manifest's list of blobs.
echo -n ' {"mediaType": "application/vnd.oci.image.layer.v1.tar", "digest": "sha256:'${diffid}'", "size": '${diffsize}'}' >> manifest.json
done

# Finish the diffID and layer blob lists.
echo >> config.json
echo >> manifest.json
# Finish the config blob with some boilerplate stuff.
echo ']}, "config": { "Cmd": ["/bin/sh"], "Env": [ "PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin" ]}}' >> config.json

# Compute the CONFIG BLOB'S digest and size, so that we can list it in the manifest.
configsize=$(wc -c config.json | awk '{print $1}')
configdigest=$(sha256sum config.json | awk '{print $1}')
# Finish the manifest with information about the config blob.
echo '], "config": { "mediaType": "application/vnd.oci.image.config.v1+json", "digest": "sha256:'${configdigest}'", "size": '${configsize}'}}' >> manifest.json

# Compute the MANIFEST'S digest and size, so that we can list it in the OCI layout index.
manifestsize=$(wc -c manifest.json | awk '{print $1}')
manifestdigest=$(sha256sum manifest.json | awk '{print $1}')
# Link the config blob and manifest into where an OCI layout would put them.
ln config.json oci/blobs/sha256/$configdigest
ln manifest.json oci/blobs/sha256/$manifestdigest
# Write the layout index with just the one image manifest in it.
echo '{"schemaVersion": 2, "manifests": [ {"mediaType": "application/vnd.oci.image.manifest.v1+json", "digest": "sha256:'${manifestdigest}'", "size": '${manifestsize}' } ]}' > oci/index.json
# Write the "this is an OCI layout directory" identifier.
echo '{"imageLayoutVersion":"1.0.0"}' > oci/oci-layout

# Import the image from the OCI layout into buildah's normal storage.
skopeo copy oci:oci containers-storage:$IMAGE

# Double-check that the image has no history, which is what we wanted to get
# out of all of this.
inspect_history=$(buildah inspect --format '{{.History}}' $IMAGE)
if [[ "$inspect_history" != '[]' ]]; then
echo "base image generated for test had history field that was not an empty slice:" >&2
echo "$inspect_history" >&2
exit 1
fi

# Worked. Clean up working directory
cd /
rm -rf $workdir

echo
echo "You may now run:"
echo " skopeo copy containers-storage:$IMAGE docker://$IMAGE"

0 comments on commit f5f31d3

Please sign in to comment.