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:
parent
bb7cab6ac7
commit
8040fee0d4
5 changed files with 62 additions and 3 deletions
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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...)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
1
go.mod
1
go.mod
|
|
@ -15,6 +15,7 @@ require (
|
|||
github.com/stretchr/testify v1.10.0
|
||||
golang.org/x/sys v0.30.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
sigs.k8s.io/yaml v1.4.0
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
|
|||
5
go.sum
5
go.sum
|
|
@ -143,6 +143,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
|
|||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
|
|
@ -237,8 +238,6 @@ github.com/opencontainers/selinux v1.11.1 h1:nHFvthhM0qY8/m+vfhJylliSshm8G1jJ2jD
|
|||
github.com/opencontainers/selinux v1.11.1/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec=
|
||||
github.com/osbuild/blueprint v1.5.0 h1:ca3C32Ltx+2P2eEZrI1fyutAInpwojl+NdQZKOrIR+Q=
|
||||
github.com/osbuild/blueprint v1.5.0/go.mod h1:0d3dlY8aSJ6jM6NHwBmJFF1VIySsp/GsDpcJQ0yrOqM=
|
||||
github.com/osbuild/images v0.149.0 h1:gAmgwbsSer16vX8tkOcXM2TFqzQ2tQUApSOwutt8Q5Q=
|
||||
github.com/osbuild/images v0.149.0/go.mod h1:ZiEO1WWKuRvPSaiXsmqn+7krAIZ+qXiiOfBQed0H7lY=
|
||||
github.com/osbuild/images v0.153.0 h1:NPPhtq/WWE5DK7psKFZ/cO4OSaRd+FYsYqPG48WBgzk=
|
||||
github.com/osbuild/images v0.153.0/go.mod h1:ZiEO1WWKuRvPSaiXsmqn+7krAIZ+qXiiOfBQed0H7lY=
|
||||
github.com/ostreedev/ostree-go v0.0.0-20210805093236-719684c64e4f h1:/UDgs8FGMqwnHagNDPGOlts35QkhAZ8by3DR7nMih7M=
|
||||
|
|
@ -504,3 +503,5 @@ gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
|
|||
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
|
||||
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue