Merge pull request #6658 from gabriel-samfira/use-temp-file-for-import-export-test
Use temp file for export/import test
This commit is contained in:
commit
a25a84f39a
@ -18,8 +18,9 @@ package client
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
. "github.com/containerd/containerd"
|
||||
@ -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) {
|
||||
|
@ -21,6 +21,8 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"math/rand"
|
||||
"reflect"
|
||||
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user