github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_cp_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  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/containerd/nerdctl/pkg/testutil"
    26  	"gotest.tools/v3/assert"
    27  )
    28  
    29  func TestComposeCopy(t *testing.T) {
    30  	base := testutil.NewBase(t)
    31  
    32  	var dockerComposeYAML = fmt.Sprintf(`
    33  version: '3.1'
    34  
    35  services:
    36    svc0:
    37      image: %s
    38      command: "sleep infinity"
    39  `, testutil.CommonImage)
    40  
    41  	comp := testutil.NewComposeDir(t, dockerComposeYAML)
    42  	defer comp.CleanUp()
    43  	projectName := comp.ProjectName()
    44  	t.Logf("projectName=%q", projectName)
    45  
    46  	base.ComposeCmd("-f", comp.YAMLFullPath(), "up", "-d").AssertOK()
    47  	defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK()
    48  
    49  	// gernetate test file
    50  	srcDir := t.TempDir()
    51  	srcFile := filepath.Join(srcDir, "test-file")
    52  	srcFileContent := []byte("test-file-content")
    53  	err := os.WriteFile(srcFile, srcFileContent, 0o644)
    54  	assert.NilError(t, err)
    55  
    56  	// test copy to service
    57  	destPath := "/dest-no-exist-no-slash"
    58  	base.ComposeCmd("-f", comp.YAMLFullPath(), "cp", srcFile, "svc0:"+destPath).AssertOK()
    59  
    60  	// test copy from service
    61  	destFile := filepath.Join(srcDir, "test-file2")
    62  	base.ComposeCmd("-f", comp.YAMLFullPath(), "cp", "svc0:"+destPath, destFile).AssertOK()
    63  
    64  	destFileContent, err := os.ReadFile(destFile)
    65  	assert.NilError(t, err)
    66  	assert.DeepEqual(t, srcFileContent, destFileContent)
    67  
    68  }