github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/image_save_linux_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 main
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"fmt"
    23  	"os"
    24  	"os/exec"
    25  	"path/filepath"
    26  	"strings"
    27  	"testing"
    28  
    29  	"github.com/containerd/nerdctl/pkg/testutil"
    30  
    31  	"gotest.tools/v3/assert"
    32  )
    33  
    34  func TestSave(t *testing.T) {
    35  	base := testutil.NewBase(t)
    36  	base.Cmd("pull", testutil.AlpineImage).AssertOK()
    37  	archiveTarPath := filepath.Join(t.TempDir(), "a.tar")
    38  	base.Cmd("save", "-o", archiveTarPath, testutil.AlpineImage).AssertOK()
    39  	rootfsPath := filepath.Join(t.TempDir(), "rootfs")
    40  	err := extractDockerArchive(archiveTarPath, rootfsPath)
    41  	assert.NilError(t, err)
    42  	etcOSReleasePath := filepath.Join(rootfsPath, "/etc/os-release")
    43  	etcOSReleaseBytes, err := os.ReadFile(etcOSReleasePath)
    44  	assert.NilError(t, err)
    45  	etcOSRelease := string(etcOSReleaseBytes)
    46  	t.Logf("read %q, extracted from %q", etcOSReleasePath, testutil.AlpineImage)
    47  	t.Log(etcOSRelease)
    48  	assert.Assert(t, strings.Contains(etcOSRelease, "Alpine"))
    49  }
    50  
    51  func extractDockerArchive(archiveTarPath, rootfsPath string) error {
    52  	if err := os.MkdirAll(rootfsPath, 0755); err != nil {
    53  		return err
    54  	}
    55  	workDir, err := os.MkdirTemp("", "extract-docker-archive")
    56  	if err != nil {
    57  		return err
    58  	}
    59  	defer os.RemoveAll(workDir)
    60  	if err := extractTarFile(workDir, archiveTarPath); err != nil {
    61  		return err
    62  	}
    63  	manifestJSONPath := filepath.Join(workDir, "manifest.json")
    64  	manifestJSONBytes, err := os.ReadFile(manifestJSONPath)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	var mani DockerArchiveManifestJSON
    69  	if err := json.Unmarshal(manifestJSONBytes, &mani); err != nil {
    70  		return err
    71  	}
    72  	if len(mani) > 1 {
    73  		return fmt.Errorf("multi-image archive cannot be extracted: contains %d images", len(mani))
    74  	}
    75  	if len(mani) < 1 {
    76  		return errors.New("invalid archive")
    77  	}
    78  	ent := mani[0]
    79  	for _, l := range ent.Layers {
    80  		layerTarPath := filepath.Join(workDir, l)
    81  		if err := extractTarFile(rootfsPath, layerTarPath); err != nil {
    82  			return err
    83  		}
    84  	}
    85  	return nil
    86  }
    87  
    88  type DockerArchiveManifestJSON []DockerArchiveManifestJSONEntry
    89  
    90  type DockerArchiveManifestJSONEntry struct {
    91  	Config   string
    92  	RepoTags []string
    93  	Layers   []string
    94  }
    95  
    96  func extractTarFile(dirPath, tarFilePath string) error {
    97  	cmd := exec.Command("tar", "Cxf", dirPath, tarFilePath)
    98  	if out, err := cmd.CombinedOutput(); err != nil {
    99  		return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err)
   100  	}
   101  	return nil
   102  }