bib/internal/setup: use fake podman binary for testing

Create a mock podman executable script and add it at the top of PATH.
Use it to generate output for the container tag validation.
This commit is contained in:
bstrausser 2024-06-18 17:27:56 -04:00 committed by Simon de Vlieger
parent 3cd7df7b13
commit c74c402d74

View file

@ -2,6 +2,7 @@ package setup_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"runtime"
@ -29,13 +30,17 @@ func TestValidateCanRunTargetArchUnsupportedCanary(t *testing.T) {
assert.Contains(t, logbuf.String(), `level=warning msg="cannot check architecture support for unsupported-arch: no canary binary found"`)
}
func makeFakeCanary(t *testing.T, content string) {
func makeFakeBinary(t *testing.T, binary, content string) {
tmpdir := t.TempDir()
t.Setenv("PATH", os.Getenv("PATH")+":"+tmpdir)
err := os.WriteFile(filepath.Join(tmpdir, "bib-canary-fakearch"), []byte(content), 0o755)
t.Setenv("PATH", tmpdir+":"+os.Getenv("PATH"))
err := os.WriteFile(filepath.Join(tmpdir, binary), []byte(content), 0o755)
assert.NoError(t, err)
}
func makeFakeCanary(t *testing.T, content string) {
makeFakeBinary(t, "bib-canary-fakearch", content)
}
func TestValidateCanRunTargetArchHappy(t *testing.T) {
var logbuf bytes.Buffer
logrus.SetOutput(&logbuf)
@ -62,12 +67,47 @@ func TestValidateCanRunTargetArchUnexpectedOutput(t *testing.T) {
assert.ErrorContains(t, err, `internal error: unexpected output`)
}
func TestValidateTags(t *testing.T) {
imageref := "quay.io/centos-bootc/centos-bootc:stream9"
err := setup.ValidateHasContainerTags(imageref)
assert.NoError(t, err)
var (
fakePodmanOutputCentosBootc = `map[containers.bootc:1 io.buildah.version:1.29.1 org.opencontainers.image.version:stream9.20240319.0 ostree.bootable:true ostree.commit:97d619eae2a5474a9c363c78e3ad6caec14acba54a0b077c7cb69d00a4f800a5 ostree.final-diffid:sha256:12787d84fa137cd5649a9005efe98ec9d05ea46245fdc50aecb7dd007f2035b1 ostree.linux:5.14.0-430.el9.x86_64 redhat.compose-id:CentOS-Stream-9-20240304.d.0 redhat.id:centos redhat.version-id:9 rpmostree.inputhash:a5c67fd4e9465e47e01922171c6ab8edf261d2d381e590b5cd7fed81ea8d4dbe]`
badImageRef := "quay.io/centos/centos:stream9"
err = setup.ValidateHasContainerTags(badImageRef)
assert.ErrorContains(t, err, "image quay.io/centos/centos:stream9 is not a bootc image")
fakePodmanOutputCentos = `map[io.buildah.version:1.33.7 org.label-schema.build-date:20240618 org.label-schema.license:GPLv2 org.label-schema.name:CentOS Stream 9 Base Image org.label-schema.schema-version:1.0 org.label-schema.vendor:CentOS]`
emptyPodmanOutput = `map[]`
)
func TestValidateTags(t *testing.T) {
for _, tc := range []struct {
imageref string
fakeOutput string
expectedErr string
}{
{
"quay.io/centos-bootc/centos-bootc:stream9",
fakePodmanOutputCentosBootc,
"",
},
{
"quay.io/centos/centos:stream9",
fakePodmanOutputCentos,
"image quay.io/centos/centos:stream9 is not a bootc image",
},
{
"fake/image",
emptyPodmanOutput,
"image fake/image is not a bootc image",
},
} {
podmanArgsFile := filepath.Join(t.TempDir(), "args.txt")
fakePodman := fmt.Sprintf(`#!/bin/sh -e
echo "$@" > '%s'
echo '%s'
`, podmanArgsFile, tc.fakeOutput)
makeFakeBinary(t, "podman", fakePodman)
err := setup.ValidateHasContainerTags(tc.imageref)
if tc.expectedErr == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tc.expectedErr)
}
}
}