Skip to content

Commit

Permalink
Merge pull request #6658 from gabriel-samfira/use-temp-file-for-impor…
Browse files Browse the repository at this point in the history
…t-export-test

Use temp file for export/import test
  • Loading branch information
estesp committed Mar 10, 2022
2 parents b521429 + 80bc32f commit a25a84f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
20 changes: 16 additions & 4 deletions integration/client/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ package client

import (
"archive/tar"
"bytes"
"io"
"io/ioutil"
"os"
"testing"

. "github.com/containerd/containerd"
Expand All @@ -45,12 +46,23 @@ func TestExport(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wb := bytes.NewBuffer(nil)
err = client.Export(ctx, wb, archive.WithPlatform(platforms.Default()), archive.WithImage(client.ImageService(), testImage))
dstFile, err := ioutil.TempFile("", "export-import-test")
if err != nil {
t.Fatal(err)
}
assertOCITar(t, bytes.NewReader(wb.Bytes()))
defer func() {
dstFile.Close()
os.Remove(dstFile.Name())
}()

err = client.Export(ctx, dstFile, archive.WithPlatform(platforms.Default()), archive.WithImage(client.ImageService(), testImage))
if err != nil {
t.Fatal(err)
}

// Seet to begining of file before passing it to assertOCITar()
dstFile.Seek(0, 0)
assertOCITar(t, dstFile)
}

func assertOCITar(t *testing.T, r io.Reader) {
Expand Down
20 changes: 17 additions & 3 deletions integration/client/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"context"
"encoding/json"
"io"
"io/ioutil"
"os"

"math/rand"
"reflect"
Expand Down Expand Up @@ -74,18 +76,30 @@ func testExportImport(t *testing.T, imageName string) {
t.Fatal(err)
}

wb := bytes.NewBuffer(nil)
err = client.Export(ctx, wb, archive.WithPlatform(platforms.Default()), archive.WithImage(client.ImageService(), imageName))
dstFile, err := ioutil.TempFile("", "export-import-test")
if err != nil {
t.Fatal(err)
}
defer func() {
dstFile.Close()
os.Remove(dstFile.Name())
}()

err = client.Export(ctx, dstFile, archive.WithPlatform(platforms.Default()), archive.WithImage(client.ImageService(), imageName))
if err != nil {
t.Fatal(err)
}

client.ImageService().Delete(ctx, imageName)

// Seek to beginning of file in preparation for Import()
dstFile.Seek(0, 0)

opts := []ImportOpt{
WithImageRefTranslator(archive.AddRefPrefix("foo/bar")),
}
imgrecs, err := client.Import(ctx, bytes.NewReader(wb.Bytes()), opts...)

imgrecs, err := client.Import(ctx, dstFile, opts...)
if err != nil {
t.Fatalf("Import failed: %+v", err)
}
Expand Down

0 comments on commit a25a84f

Please sign in to comment.