Skip to content

Commit

Permalink
chore: add testutil logging (#222)
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Zantow <[email protected]>
  • Loading branch information
kzantow authored Feb 16, 2024
1 parent 705198d commit 6171ee2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
12 changes: 9 additions & 3 deletions pkg/imagetest/fileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ func copyFile(t testing.TB, src, dst string) {
}
}

func fileOrDirExists(t testing.TB, filename string) bool {
func fileExists(t testing.TB, filename string) bool {
t.Helper()
_, err := os.Stat(filename)
return !os.IsNotExist(err)
s, err := os.Stat(filename)
return !os.IsNotExist(err) && !s.IsDir()
}

func dirExists(t testing.TB, filename string) bool {
t.Helper()
s, err := os.Stat(filename)
return !os.IsNotExist(err) && s.IsDir()
}

func dirHash(t testing.TB, root string) string {
Expand Down
33 changes: 28 additions & 5 deletions pkg/imagetest/image_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"path"
"path/filepath"
"strings"
"testing"

"github.com/logrusorgru/aurora"
Expand Down Expand Up @@ -65,7 +66,16 @@ func GetFixtureImage(t testing.TB, source, name string) *image.Image {
request := PrepareFixtureImage(t, source, name)

i, err := stereoscope.GetImage(context.TODO(), request)
require.NoError(t, err)
if err != nil {
// if request is docker-archive, get a bit of extra info about the tar path
info := ""
const dockerArchivePrefix = "docker-archive:"
if strings.HasPrefix(request, dockerArchivePrefix) {
tarPath := strings.TrimPrefix(request, dockerArchivePrefix)
info = fmt.Sprintf(" (%s)", fileInfo(tarPath))
}
t.Fatalf("error getting fixture image: '%s' '%s' with request '%s'%s: %v", source, name, request, info, err)
}
t.Cleanup(func() {
require.NoError(t, i.Cleanup())
})
Expand Down Expand Up @@ -163,15 +173,15 @@ func getFixtureImageTarPath(t testing.TB, fixtureName, tarStoreDir, tarFileName
tarPath := path.Join(tarStoreDir, tarFileName)

// create the cache dir if it does not already exist...
if !fileOrDirExists(t, CacheDir) {
if !dirExists(t, CacheDir) {
err := os.Mkdir(CacheDir, 0o755)
if err != nil {
t.Fatalf("could not create tar cache dir (%s): %+v", CacheDir, err)
}
}

// if the image tar does not exist, make it
if !fileOrDirExists(t, tarPath) {
if !fileExists(t, tarPath) {
if !isImageInDocker(fullImageName) {
contextPath := path.Join(testutils.TestFixturesDir, fixtureName)
buildDockerImage(t, contextPath, imageName, imageVersion)
Expand All @@ -181,11 +191,21 @@ func getFixtureImageTarPath(t testing.TB, fixtureName, tarStoreDir, tarFileName
if err != nil {
t.Fatal("could not save fixture image:", err)
}
} else {
t.Logf("using existing image tar: '%s' (%s)", tarPath, fileInfo(tarPath))
}

return tarPath
}

func fileInfo(filePath string) string {
s, err := os.Stat(filePath)
if os.IsNotExist(err) {
return "not exist"
}
return fmt.Sprintf("size: %v, modified: %v, mode: %v", s.Size(), s.ModTime(), s.Mode())
}

func GetFixtureImageTarPath(t testing.TB, name string) string {
imageName, imageVersion := getFixtureImageInfo(t, name)
tarFileName := fmt.Sprintf("%s-%s.tar", imageName, imageVersion)
Expand Down Expand Up @@ -270,6 +290,7 @@ func buildContainerdImage(t testing.TB, contextDir, name, tag string) {
}

func saveImage(t testing.TB, image, path string) error {
t.Logf("saveImage running: docker image save %s", image)
outfile, err := os.Create(path)
if err != nil {
t.Fatal("unable to create file for docker image tar:", err)
Expand Down Expand Up @@ -304,15 +325,15 @@ func getFixtureImageSIFPath(t testing.TB, fixtureName, sifStoreDir, sifFileName
sifPath := path.Join(sifStoreDir, sifFileName)

// create the cache dir if it does not already exist...
if !fileOrDirExists(t, CacheDir) {
if !dirExists(t, CacheDir) {
err := os.Mkdir(CacheDir, 0o755)
if err != nil {
t.Fatalf("could not create sif cache dir (%s): %+v", CacheDir, err)
}
}

// if the image sif does not exist, make it
if !fileOrDirExists(t, sifPath) {
if !fileExists(t, sifPath) {
if !isImageInDocker(fullImageName) {
contextPath := path.Join(testutils.TestFixturesDir, fixtureName)
buildDockerImage(t, contextPath, imageName, imageVersion)
Expand All @@ -321,6 +342,8 @@ func getFixtureImageSIFPath(t testing.TB, fixtureName, sifStoreDir, sifFileName
if err != nil {
t.Fatal("could not save fixture image:", err)
}
} else {
t.Logf("using existing sif archive: %s", sifPath)
}

return sifPath
Expand Down

0 comments on commit 6171ee2

Please sign in to comment.