cmd/describe: test that all images can be described

Add an unit test to verify that the `describe` command can describe all
image types. I've recently ran into issue when describing ostree-based
and `*-simplified-installer` image types.

Add new dependecy for working with YAML - sigs.k8s.io/yaml.

This is what we will probably converge to, since our currently used YAML
library is unmaintained.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
This commit is contained in:
Tomáš Hozza 2025-06-23 14:26:43 +02:00 committed by Simon de Vlieger
parent bb7cab6ac7
commit 8040fee0d4
5 changed files with 62 additions and 3 deletions

View file

@ -2,13 +2,17 @@ package main_test
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sigs.k8s.io/yaml"
testrepos "github.com/osbuild/images/test/data/repositories"
"github.com/osbuild/image-builder-cli/cmd/image-builder"
main "github.com/osbuild/image-builder-cli/cmd/image-builder"
)
func TestDescribeImage(t *testing.T) {
@ -58,3 +62,34 @@ packages:
`
assert.Equal(t, expectedOutput, buf.String())
}
func TestDescribeImageAll(t *testing.T) {
restore := main.MockNewRepoRegistry(testrepos.New)
defer restore()
allImages, err := main.GetAllImages(nil)
require.NoError(t, err)
require.NotEmpty(t, allImages)
for _, res := range allImages {
t.Run(fmt.Sprintf("%s/%s/%s", res.Distro.Name(), res.Arch.Name(), res.ImgType.Name()), func(t *testing.T) {
var buf bytes.Buffer
err = main.DescribeImage(&res, &buf)
require.NoError(t, err)
// check that the first line of the output contains the "@WARNING" message
lines := strings.Split(buf.String(), "\n")
require.NotEmpty(t, lines)
require.Equal(t, "@WARNING - the output format is not stable yet and may change", lines[0])
// the rest of the output should contain a valid YAML representation of the image
describeOutput := strings.Join(lines[1:], "\n")
var imgDef main.DescribeImgYAML
err := yaml.Unmarshal([]byte(describeOutput), &imgDef)
require.NoError(t, err)
require.Equal(t, res.Distro.Name(), imgDef.Distro)
require.Equal(t, res.Arch.Name(), imgDef.Arch)
require.Equal(t, res.ImgType.Name(), imgDef.Type)
})
}
}

View file

@ -13,6 +13,7 @@ import (
var (
GetOneImage = getOneImage
GetAllImages = getAllImages
Run = run
FindDistro = findDistro
DescribeImage = describeImage
@ -20,6 +21,8 @@ var (
BasenameFor = basenameFor
)
type DescribeImgYAML describeImgYAML
func MockOsArgs(new []string) (restore func()) {
saved := os.Args
os.Args = append([]string{"argv0"}, new...)

View file

@ -76,3 +76,22 @@ func getOneImage(distroName, imgTypeStr, archStr string, repoOpts *repoOptions)
return nil, fmt.Errorf("internal error: found %v results for %q %q %q", len(filteredResults), distroName, imgTypeStr, archStr)
}
}
// getAllImages returns all images matching the filter expressions.
func getAllImages(repoOpts *repoOptions, filterExprs ...string) ([]imagefilter.Result, error) {
if repoOpts == nil {
repoOpts = &repoOptions{}
}
imageFilter, err := newImageFilterDefault(repoOpts.DataDir, repoOpts.ExtraRepos)
if err != nil {
return nil, err
}
filteredResults, err := imageFilter.Filter(filterExprs...)
if err != nil {
return nil, err
}
return filteredResults, nil
}