From 2a89cb6739821cda2ff10d2587492db205876795 Mon Sep 17 00:00:00 2001 From: bstrausser Date: Thu, 13 Jun 2024 19:27:45 -0400 Subject: [PATCH] bib/internal/setup: validate container tags early Check that the container has the expected bootc tags early and fail if they are missing. --- bib/internal/setup/setup.go | 15 +++++++++++++++ bib/internal/setup/setup_test.go | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/bib/internal/setup/setup.go b/bib/internal/setup/setup.go index 16da129..66d068c 100644 --- a/bib/internal/setup/setup.go +++ b/bib/internal/setup/setup.go @@ -6,6 +6,7 @@ import ( "os/exec" "path/filepath" "runtime" + "strings" "golang.org/x/sys/unix" @@ -148,3 +149,17 @@ func validateCanRunTargetArch(targetArch string) error { return nil } + +func ValidateHasContainerTags(imgref string) error { + output, err := exec.Command("podman", "image", "inspect", imgref, "--format", "{{.Labels}}").Output() + if err != nil { + return fmt.Errorf("failed inspect image: %w", util.OutputErr(err)) + } + + tags := string(output) + if !strings.Contains(tags, "containers.bootc:1") { + return fmt.Errorf("image %s is not a bootc image", imgref) + } + + return nil +} diff --git a/bib/internal/setup/setup_test.go b/bib/internal/setup/setup_test.go index bb57848..375cf78 100644 --- a/bib/internal/setup/setup_test.go +++ b/bib/internal/setup/setup_test.go @@ -61,3 +61,13 @@ func TestValidateCanRunTargetArchUnexpectedOutput(t *testing.T) { err := setup.ValidateCanRunTargetArch("fakearch") 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) + + 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") +}