Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

BUILDAH_BINARY=${BUILDAH_BINARY:-$(dirname ${BASH_SOURCE})/../buildah}
TESTSDIR=${TESTSDIR:-$(dirname ${BASH_SOURCE})}
STORAGE_DRIVER=${STORAGE_DRIVER:-vfs}

function setup() {
suffix=$(dd if=/dev/urandom bs=12 count=1 status=none | base64 | tr +/ _.)
TESTDIR=${BATS_TMPDIR}/tmp.${suffix}
rm -fr ${TESTDIR}
mkdir -p ${TESTDIR}/{root,runroot}
REPO=${TESTDIR}/root
}

function buildimgtype() {
Expand Down Expand Up @@ -44,9 +44,9 @@ function createrandom() {
}

function buildah() {
${BUILDAH_BINARY} --debug --root ${TESTDIR}/root --runroot ${TESTDIR}/runroot --storage-driver vfs "$@"
${BUILDAH_BINARY} --debug --root ${TESTDIR}/root --runroot ${TESTDIR}/runroot --storage-driver ${STORAGE_DRIVER} "$@"
}

function imgtype() {
./imgtype -root ${TESTDIR}/root -runroot ${TESTDIR}/runroot -storage-driver vfs "$@"
./imgtype -root ${TESTDIR}/root -runroot ${TESTDIR}/runroot -storage-driver ${STORAGE_DRIVER} "$@"
}
78 changes: 55 additions & 23 deletions tests/imgtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"

"github.com/Sirupsen/logrus"
Expand All @@ -16,10 +17,15 @@ import (
)

func main() {
if buildah.InitReexec() {
return
}

expectedManifestType := ""
expectedConfigType := ""

storeOptions := storage.DefaultStoreOptions
debug := flag.Bool("debug", false, "turn on debug logging")
root := flag.String("root", storeOptions.GraphRoot, "storage root directory")
runroot := flag.String("runroot", storeOptions.RunRoot, "storage runtime directory")
driver := flag.String("storage-driver", storeOptions.GraphDriverName, "storage driver")
Expand All @@ -29,6 +35,10 @@ func main() {
showm := flag.Bool("show-manifest", false, "output the manifest JSON")
showc := flag.Bool("show-config", false, "output the configuration JSON")
flag.Parse()
logrus.SetLevel(logrus.ErrorLevel)
if debug != nil && *debug {
logrus.SetLevel(logrus.DebugLevel)
}
switch *mtype {
case buildah.OCIv1ImageManifest:
expectedManifestType = *mtype
Expand All @@ -40,8 +50,9 @@ func main() {
expectedManifestType = ""
expectedConfigType = ""
default:
logrus.Fatalf("unknown -expected-manifest-type value, expected either %q or %q or %q",
logrus.Errorf("unknown -expected-manifest-type value, expected either %q or %q or %q",
buildah.OCIv1ImageManifest, buildah.Dockerv2ImageManifest, "*")
return
}
if root != nil {
storeOptions.GraphRoot = *root
Expand All @@ -65,10 +76,17 @@ func main() {
}
store, err := storage.GetStore(storeOptions)
if err != nil {
logrus.Fatalf("error opening storage: %v", err)
logrus.Errorf("error opening storage: %v", err)
return
}
defer store.Shutdown(false)

errors := false
defer func() {
store.Shutdown(false)
if errors {
os.Exit(1)
}
}()
for _, image := range args {
oImage := v1.Image{}
dImage := docker.V2Image{}
Expand All @@ -79,60 +97,74 @@ func main() {

ref, err := is.Transport.ParseStoreReference(store, image)
if err != nil {
logrus.Fatalf("error parsing reference %q: %v", image, err)
}

src, err := ref.NewImageSource(systemContext, []string{expectedManifestType})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test/check no longer needed? I don't see it in the new version.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code for handling an error result from ParseStoreReference? The diff is assuming one if err != nil is equivalent to another, but we should still be handling that case in the proposed version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't referring to the Transport.ParseStoreReference(), but the call to ref.NewImageSource on line 85 in the old code. I didn't see that elsewhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. Realized that we can grab the manifest contents from the Image object, so there's no need to create an ImageSource object to read it when we already have an Image object.

if err != nil {
logrus.Fatalf("error opening source image %q: %v", image, err)
}
defer src.Close()

manifest, manifestType, err := src.GetManifest()
if err != nil {
logrus.Fatalf("error reading manifest from %q: %v", image, err)
logrus.Errorf("error parsing reference %q: %v", image, err)
errors = true
continue
}

img, err := ref.NewImage(systemContext)
if err != nil {
logrus.Fatalf("error opening image %q: %v", image, err)
logrus.Errorf("error opening image %q: %v", image, err)
errors = true
continue
}
defer img.Close()

config, err := img.ConfigBlob()
if err != nil {
logrus.Fatalf("error reading configuration from %q: %v", image, err)
logrus.Errorf("error reading configuration from %q: %v", image, err)
errors = true
continue
}

manifest, manifestType, err := img.Manifest()
if err != nil {
logrus.Errorf("error reading manifest from %q: %v", image, err)
errors = true
continue
}

switch expectedManifestType {
case buildah.OCIv1ImageManifest:
err = json.Unmarshal(manifest, &oManifest)
if err != nil {
logrus.Fatalf("error parsing manifest from %q: %v", image, err)
logrus.Errorf("error parsing manifest from %q: %v", image, err)
errors = true
continue
}
err = json.Unmarshal(config, &oImage)
if err != nil {
logrus.Fatalf("error parsing config from %q: %v", image, err)
logrus.Errorf("error parsing config from %q: %v", image, err)
errors = true
continue
}
manifestType = v1.MediaTypeImageManifest
configType = oManifest.Config.MediaType
case buildah.Dockerv2ImageManifest:
err = json.Unmarshal(manifest, &dManifest)
if err != nil {
logrus.Fatalf("error parsing manifest from %q: %v", image, err)
logrus.Errorf("error parsing manifest from %q: %v", image, err)
errors = true
continue
}
err = json.Unmarshal(config, &dImage)
if err != nil {
logrus.Fatalf("error parsing config from %q: %v", image, err)
logrus.Errorf("error parsing config from %q: %v", image, err)
errors = true
continue
}
manifestType = dManifest.MediaType
configType = dManifest.Config.MediaType
}
if expectedManifestType != "" && manifestType != expectedManifestType {
logrus.Fatalf("expected manifest type %q in %q, got %q", expectedManifestType, image, manifestType)
logrus.Errorf("expected manifest type %q in %q, got %q", expectedManifestType, image, manifestType)
errors = true
continue
}
if expectedConfigType != "" && configType != expectedConfigType {
logrus.Fatalf("expected config type %q in %q, got %q", expectedConfigType, image, configType)
logrus.Errorf("expected config type %q in %q, got %q", expectedConfigType, image, configType)
errors = true
continue
}
if showm != nil && *showm {
fmt.Println(string(manifest))
Expand Down