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 (
"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) {

View File

@ -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)
}