-
Notifications
You must be signed in to change notification settings - Fork 618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite cp #3323
Open
apostasie
wants to merge
2
commits into
containerd:main
Choose a base branch
from
apostasie:b-dev-cp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,746
−562
Open
Rewrite cp #3323
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package container | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
"gotest.tools/v3/icmd" | ||
|
||
"github.com/containerd/nerdctl/v2/pkg/containerutil" | ||
"github.com/containerd/nerdctl/v2/pkg/rootlessutil" | ||
"github.com/containerd/nerdctl/v2/pkg/testutil" | ||
) | ||
|
||
// This is a separate set of tests for cp specifically meant to test corner or extreme cases that do not fit in the normal testing rig | ||
// because of their complexity | ||
|
||
func TestCopyAcid(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("Travelling along volumes w/o read-only", func(t *testing.T) { | ||
t.Parallel() | ||
testID := testutil.Identifier(t) | ||
tempDir := t.TempDir() | ||
base := testutil.NewBase(t) | ||
base.Dir = tempDir | ||
|
||
sourceFile := filepath.Join(tempDir, "hostfile") | ||
sourceFileContent := []byte(testID) | ||
|
||
roContainer := testID + "-ro" | ||
rwContainer := testID + "-rw" | ||
|
||
setup := func() { | ||
base.Cmd("volume", "create", testID+"-1-ro").AssertOK() | ||
base.Cmd("volume", "create", testID+"-2-rw").AssertOK() | ||
base.Cmd("volume", "create", testID+"-3-rw").AssertOK() | ||
base.Cmd("run", "-d", "-w", containerCwd, "--name", roContainer, "--read-only", | ||
"-v", fmt.Sprintf("%s:%s:ro", testID+"-1-ro", "/vol1/dir1/ro"), | ||
"-v", fmt.Sprintf("%s:%s", testID+"-2-rw", "/vol2/dir2/rw"), | ||
testutil.CommonImage, "sleep", "Inf", | ||
).AssertOK() | ||
base.Cmd("run", "-d", "-w", containerCwd, "--name", rwContainer, | ||
"-v", fmt.Sprintf("%s:%s:ro", testID+"-1-ro", "/vol1/dir1/ro"), | ||
"-v", fmt.Sprintf("%s:%s", testID+"-3-rw", "/vol3/dir3/rw"), | ||
testutil.CommonImage, "sleep", "Inf", | ||
).AssertOK() | ||
|
||
base.Cmd("exec", rwContainer, "sh", "-euxc", "cd /vol3/dir3/rw; ln -s ../../../ relativelinktoroot").AssertOK() | ||
base.Cmd("exec", rwContainer, "sh", "-euxc", "cd /vol3/dir3/rw; ln -s / absolutelinktoroot").AssertOK() | ||
base.Cmd("exec", roContainer, "sh", "-euxc", "cd /vol2/dir2/rw; ln -s ../../../ relativelinktoroot").AssertOK() | ||
base.Cmd("exec", roContainer, "sh", "-euxc", "cd /vol2/dir2/rw; ln -s / absolutelinktoroot").AssertOK() | ||
// Create file on the host | ||
err := os.WriteFile(sourceFile, sourceFileContent, filePerm) | ||
assert.NilError(t, err) | ||
} | ||
|
||
tearDown := func() { | ||
base.Cmd("rm", "-f", roContainer).Run() | ||
base.Cmd("rm", "-f", rwContainer).Run() | ||
base.Cmd("volume", "rm", testID+"-1-ro").Run() | ||
base.Cmd("volume", "rm", testID+"-2-rw").Run() | ||
base.Cmd("volume", "rm", testID+"-3-rw").Run() | ||
} | ||
|
||
t.Cleanup(tearDown) | ||
tearDown() | ||
|
||
setup() | ||
|
||
expectedErr := containerutil.ErrTargetIsReadOnly.Error() | ||
if testutil.GetTarget() == testutil.Docker { | ||
expectedErr = "" | ||
} | ||
|
||
t.Run("Cannot copy into a read-only root", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
base.Cmd("cp", sourceFile, roContainer+":/").Assert(icmd.Expected{ | ||
ExitCode: 1, | ||
Err: expectedErr, | ||
}) | ||
}) | ||
|
||
t.Run("Cannot copy into a read-only mount, in a rw container", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
base.Cmd("cp", sourceFile, rwContainer+":/vol1/dir1/ro").Assert(icmd.Expected{ | ||
ExitCode: 1, | ||
Err: expectedErr, | ||
}) | ||
}) | ||
|
||
t.Run("Can copy into a read-write mount in a read-only container", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
base.Cmd("cp", sourceFile, roContainer+":/vol2/dir2/rw").Assert(icmd.Expected{ | ||
ExitCode: 0, | ||
}) | ||
}) | ||
|
||
t.Run("Traverse read-only locations to a read-write location", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
base.Cmd("cp", sourceFile, roContainer+":/vol1/dir1/ro/../../../vol2/dir2/rw").Assert(icmd.Expected{ | ||
ExitCode: 0, | ||
}) | ||
}) | ||
|
||
t.Run("Follow an absolute symlink inside a read-write mount to a read-only root", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
base.Cmd("cp", sourceFile, roContainer+":/vol2/dir2/rw/absolutelinktoroot").Assert(icmd.Expected{ | ||
ExitCode: 1, | ||
Err: expectedErr, | ||
}) | ||
}) | ||
|
||
t.Run("Follow am absolute symlink inside a read-write mount to a read-only mount", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
base.Cmd("cp", sourceFile, rwContainer+":/vol3/dir3/rw/absolutelinktoroot/vol1/dir1/ro").Assert(icmd.Expected{ | ||
ExitCode: 1, | ||
Err: expectedErr, | ||
}) | ||
}) | ||
|
||
t.Run("Follow a relative symlink inside a read-write location to a read-only root", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
base.Cmd("cp", sourceFile, roContainer+":/vol2/dir2/rw/relativelinktoroot").Assert(icmd.Expected{ | ||
ExitCode: 1, | ||
Err: expectedErr, | ||
}) | ||
}) | ||
|
||
t.Run("Follow a relative symlink inside a read-write location to a read-only mount", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
base.Cmd("cp", sourceFile, rwContainer+":/vol3/dir3/rw/relativelinktoroot/vol1/dir1/ro").Assert(icmd.Expected{ | ||
ExitCode: 1, | ||
Err: expectedErr, | ||
}) | ||
}) | ||
|
||
t.Run("Cannot copy into a HOST read-only location", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Root will just ignore the 000 permission on the host directory. | ||
if !rootlessutil.IsRootless() { | ||
t.Skip("This test does not work rootful") | ||
} | ||
|
||
err := os.MkdirAll(filepath.Join(tempDir, "rotest"), 0o000) | ||
assert.NilError(t, err) | ||
base.Cmd("cp", roContainer+":/etc/issue", filepath.Join(tempDir, "rotest")).Assert(icmd.Expected{ | ||
ExitCode: 1, | ||
Err: expectedErr, | ||
}) | ||
}) | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not shadow package name.