Use temp file for export/import test

Using a bytes buffer for this test increases the memory usage on Windows
to over 3 GB. Using a temporary file as a destination for the image
keeps memory usage at a reasonable level.

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2022-03-10 11:28:02 +02:00
parent b521429b67
commit 80bc32f069
No known key found for this signature in database
GPG Key ID: 7D073DCC2C074CB5
2 changed files with 33 additions and 7 deletions

View File

@ -18,8 +18,9 @@ package client
import ( import (
"archive/tar" "archive/tar"
"bytes"
"io" "io"
"io/ioutil"
"os"
"testing" "testing"
. "github.com/containerd/containerd" . "github.com/containerd/containerd"
@ -45,12 +46,23 @@ func TestExport(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
wb := bytes.NewBuffer(nil) dstFile, err := ioutil.TempFile("", "export-import-test")
err = client.Export(ctx, wb, archive.WithPlatform(platforms.Default()), archive.WithImage(client.ImageService(), testImage))
if err != nil { if err != nil {
t.Fatal(err) 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) { func assertOCITar(t *testing.T, r io.Reader) {

View File

@ -21,6 +21,8 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil"
"os"
"math/rand" "math/rand"
"reflect" "reflect"
@ -74,18 +76,30 @@ func testExportImport(t *testing.T, imageName string) {
t.Fatal(err) t.Fatal(err)
} }
wb := bytes.NewBuffer(nil) dstFile, err := ioutil.TempFile("", "export-import-test")
err = client.Export(ctx, wb, archive.WithPlatform(platforms.Default()), archive.WithImage(client.ImageService(), imageName)) 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
client.ImageService().Delete(ctx, imageName) client.ImageService().Delete(ctx, imageName)
// Seek to beginning of file in preparation for Import()
dstFile.Seek(0, 0)
opts := []ImportOpt{ opts := []ImportOpt{
WithImageRefTranslator(archive.AddRefPrefix("foo/bar")), 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 { if err != nil {
t.Fatalf("Import failed: %+v", err) t.Fatalf("Import failed: %+v", err)
} }