github.com/containerd/Containerd@v1.4.13/export_test.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package containerd
    18  
    19  import (
    20  	"archive/tar"
    21  	"bytes"
    22  	"io"
    23  	"runtime"
    24  	"testing"
    25  
    26  	"github.com/containerd/containerd/images/archive"
    27  	"github.com/containerd/containerd/platforms"
    28  )
    29  
    30  // TestExport exports testImage as a tar stream
    31  func TestExport(t *testing.T) {
    32  	// TODO: support windows
    33  	if testing.Short() || runtime.GOOS == "windows" {
    34  		t.Skip()
    35  	}
    36  	ctx, cancel := testContext(t)
    37  	defer cancel()
    38  
    39  	client, err := New(address)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	defer client.Close()
    44  
    45  	_, err = client.Fetch(ctx, testImage)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	wb := bytes.NewBuffer(nil)
    50  	err = client.Export(ctx, wb, archive.WithPlatform(platforms.Default()), archive.WithImage(client.ImageService(), testImage))
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	assertOCITar(t, bytes.NewReader(wb.Bytes()))
    55  }
    56  
    57  func assertOCITar(t *testing.T, r io.Reader) {
    58  	// TODO: add more assertion
    59  	tr := tar.NewReader(r)
    60  	foundOCILayout := false
    61  	foundIndexJSON := false
    62  	for {
    63  		h, err := tr.Next()
    64  		if err == io.EOF {
    65  			break
    66  		}
    67  		if err != nil {
    68  			t.Error(err)
    69  			continue
    70  		}
    71  		if h.Name == "oci-layout" {
    72  			foundOCILayout = true
    73  		}
    74  		if h.Name == "index.json" {
    75  			foundIndexJSON = true
    76  		}
    77  	}
    78  	if !foundOCILayout {
    79  		t.Error("oci-layout not found")
    80  	}
    81  	if !foundIndexJSON {
    82  		t.Error("index.json not found")
    83  	}
    84  }