-
Notifications
You must be signed in to change notification settings - Fork 204
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
pkg/detach: fix broken Copy() detach sequence #2302
Draft
Luap99
wants to merge
1
commit into
containers:main
Choose a base branch
from
Luap99:detach
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.
+201
−31
Draft
Changes from all commits
Commits
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
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,132 @@ | ||
package detach | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
smallBytes = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} | ||
bigBytes = []byte(strings.Repeat("0F", 32*1024+30)) | ||
) | ||
|
||
func newCustomReader(buf *bytes.Buffer, readsize uint) *customReader { | ||
return &customReader{ | ||
inner: buf, | ||
readsize: readsize, | ||
} | ||
} | ||
|
||
type customReader struct { | ||
inner *bytes.Buffer | ||
readsize uint | ||
} | ||
|
||
func (c *customReader) Read(p []byte) (n int, err error) { | ||
return c.inner.Read(p[:min(int(c.readsize), len(p))]) | ||
} | ||
|
||
func FuzzCopy(f *testing.F) { | ||
for _, i := range []uint{1, 2, 3, 5, 10, 100, 200, 1000, 1024, 32 * 1024} { | ||
f.Add(i) | ||
} | ||
|
||
f.Fuzz(func(t *testing.T, readSize uint) { | ||
// 0 is not a valid read size | ||
if readSize == 0 { | ||
t.Skip() | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
from []byte | ||
expected []byte | ||
expectDetach bool | ||
keys []byte | ||
}{ | ||
{ | ||
name: "small copy", | ||
from: smallBytes, | ||
expected: smallBytes, | ||
keys: nil, | ||
}, | ||
{ | ||
name: "small copy with detach keys", | ||
from: smallBytes, | ||
expected: smallBytes, | ||
keys: []byte{'A', 'B'}, | ||
}, | ||
{ | ||
name: "big copy", | ||
from: bigBytes, | ||
expected: bigBytes, | ||
keys: nil, | ||
}, | ||
{ | ||
name: "big copy with detach keys", | ||
from: bigBytes, | ||
expected: bigBytes, | ||
keys: []byte{'A', 'B'}, | ||
}, | ||
{ | ||
name: "simple detach 1 key", | ||
from: append(smallBytes, 'A'), | ||
expected: smallBytes, | ||
expectDetach: true, | ||
keys: []byte{'A'}, | ||
}, | ||
{ | ||
name: "simple detach 2 keys", | ||
from: append(smallBytes, 'A', 'B'), | ||
expected: smallBytes, | ||
expectDetach: true, | ||
keys: []byte{'A', 'B'}, | ||
}, | ||
{ | ||
name: "simple detach 3 keys", | ||
from: append(smallBytes, 'A', 'B', 'C'), | ||
expected: smallBytes, | ||
expectDetach: true, | ||
keys: []byte{'A', 'B', 'C'}, | ||
}, | ||
{ | ||
name: "detach early", | ||
from: append(smallBytes, 'A', 'B', 'B', 'A'), | ||
expected: smallBytes, | ||
expectDetach: true, | ||
keys: []byte{'A', 'B'}, | ||
}, | ||
{ | ||
name: "detach with partial match", | ||
from: append(smallBytes, 'A', 'A', 'A', 'B'), | ||
expected: append(smallBytes, 'A', 'A'), | ||
expectDetach: true, | ||
keys: []byte{'A', 'B'}, | ||
}, | ||
{ | ||
name: "big buffer detach with partial match", | ||
from: append(bigBytes, 'A', 'A', 'A', 'B'), | ||
expected: append(bigBytes, 'A', 'A'), | ||
expectDetach: true, | ||
keys: []byte{'A', 'B'}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
dst := &bytes.Buffer{} | ||
src := newCustomReader(bytes.NewBuffer(tt.from), readSize) | ||
written, err := Copy(dst, src, tt.keys) | ||
if tt.expectDetach { | ||
assert.ErrorIs(t, err, ErrDetach) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
assert.Equal(t, dst.Len(), int(written), "bytes written matches buffer") | ||
assert.Equal(t, tt.expected, dst.Bytes(), "buffer matches") | ||
}) | ||
} | ||
}) | ||
} |
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.
I wonder if we shouldn't mention this in containers.conf - disabling the detach keys (almost never used) improving performance by a pretty significant bit could be relevant to folks.
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.
I would not make any claims without solid numbers. I have not measured this, the main reason I added this was to not have to deal with the case len(keys) == 0. The io.Copy() code has some interface logic to use better methods when available and logically then don't have to compare each byte * number of keys so yes one should assume it is much faster. However is it faster in the context of podman users. Maybe other overhead makes this pretty much not noticeable.