-
Notifications
You must be signed in to change notification settings - Fork 25
test(codec): add tests for raw and tar codecs #450
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
Merged
+227
−0
Merged
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,227 @@ | ||
| /* | ||
| * Copyright 2025 The CNAI 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 codec | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // --- Raw Codec Tests --- | ||
|
|
||
| func TestRawEncodeDecode(t *testing.T) { | ||
| t.Parallel() | ||
| dir := t.TempDir() | ||
| content := []byte("hello world raw codec test") | ||
|
|
||
| // Write source file. | ||
| srcPath := filepath.Join(dir, "input.bin") | ||
| require.NoError(t, os.WriteFile(srcPath, content, 0644)) | ||
|
|
||
| r := newRaw() | ||
|
|
||
| // Encode: should return a reader with the file's content. | ||
| reader, err := r.Encode(srcPath, dir) | ||
| require.NoError(t, err) | ||
| if c, ok := reader.(io.Closer); ok { | ||
| t.Cleanup(func() { _ = c.Close() }) | ||
| } | ||
|
|
||
| encoded, err := io.ReadAll(reader) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, content, encoded) | ||
|
|
||
| // Decode: write the encoded bytes to an output directory. | ||
| outputDir := filepath.Join(dir, "output") | ||
| require.NoError(t, os.MkdirAll(outputDir, 0755)) | ||
|
|
||
| desc := ocispec.Descriptor{Size: int64(len(content))} | ||
| err = r.Decode(outputDir, "decoded.bin", bytes.NewReader(encoded), desc) | ||
| require.NoError(t, err) | ||
|
|
||
| decoded, err := os.ReadFile(filepath.Join(outputDir, "decoded.bin")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, content, decoded) | ||
| } | ||
|
|
||
| func TestRawEncodeEmpty(t *testing.T) { | ||
| t.Parallel() | ||
| dir := t.TempDir() | ||
rishi-jat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| content := []byte{} | ||
|
|
||
| srcPath := filepath.Join(dir, "empty.bin") | ||
| require.NoError(t, os.WriteFile(srcPath, content, 0644)) | ||
|
|
||
| r := newRaw() | ||
|
|
||
| reader, err := r.Encode(srcPath, dir) | ||
| require.NoError(t, err) | ||
| if c, ok := reader.(io.Closer); ok { | ||
| t.Cleanup(func() { _ = c.Close() }) | ||
| } | ||
|
|
||
| encoded, err := io.ReadAll(reader) | ||
| require.NoError(t, err) | ||
| assert.Empty(t, encoded) | ||
| } | ||
|
|
||
| func TestRawDecodeInvalidInput(t *testing.T) { | ||
| t.Parallel() | ||
| dir := t.TempDir() | ||
rishi-jat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| r := newRaw() | ||
|
|
||
| // Decode from a reader that always errors; the error should propagate. | ||
| badReader := &errorReader{} | ||
| desc := ocispec.Descriptor{Size: 10} | ||
|
|
||
| err := r.Decode(dir, "out.bin", badReader, desc) | ||
| assert.Error(t, err) | ||
| } | ||
|
|
||
| // errorReader is a reader that always returns an error. | ||
| type errorReader struct{} | ||
|
|
||
| func (e *errorReader) Read([]byte) (int, error) { | ||
| return 0, io.ErrUnexpectedEOF | ||
| } | ||
|
|
||
| // --- Tar Codec Tests --- | ||
|
|
||
| func TestTarArchiveSingleFile(t *testing.T) { | ||
| t.Parallel() | ||
| srcDir := t.TempDir() | ||
rishi-jat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| content := []byte("single file content") | ||
|
|
||
| filePath := filepath.Join(srcDir, "file.txt") | ||
| require.NoError(t, os.WriteFile(filePath, content, 0644)) | ||
|
|
||
| c := newTar() | ||
|
|
||
| reader, err := c.Encode(filePath, srcDir) | ||
| require.NoError(t, err) | ||
| if c, ok := reader.(io.Closer); ok { | ||
| t.Cleanup(func() { _ = c.Close() }) | ||
| } | ||
|
|
||
| // Read the tar stream fully so it can be used for extraction. | ||
| tarData, err := io.ReadAll(reader) | ||
| require.NoError(t, err) | ||
| assert.NotEmpty(t, tarData) | ||
|
|
||
| // Extract and verify. | ||
| extractDir := t.TempDir() | ||
| desc := ocispec.Descriptor{} | ||
| err = c.Decode(extractDir, "file.txt", bytes.NewReader(tarData), desc) | ||
| require.NoError(t, err) | ||
|
|
||
| extracted, err := os.ReadFile(filepath.Join(extractDir, "file.txt")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, content, extracted) | ||
| } | ||
|
|
||
| func TestTarArchiveMultipleFiles(t *testing.T) { | ||
| t.Parallel() | ||
| srcDir := t.TempDir() | ||
rishi-jat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| files := map[string]string{ | ||
| "a.txt": "content of a", | ||
| "b.txt": "content of b", | ||
| "sub/c.txt": "content of c in sub", | ||
| } | ||
|
|
||
| for name, data := range files { | ||
| p := filepath.Join(srcDir, name) | ||
| require.NoError(t, os.MkdirAll(filepath.Dir(p), 0755)) | ||
| require.NoError(t, os.WriteFile(p, []byte(data), 0644)) | ||
| } | ||
|
|
||
| c := newTar() | ||
|
|
||
| // Archive the entire directory. | ||
| reader, err := c.Encode(srcDir, filepath.Dir(srcDir)) | ||
| require.NoError(t, err) | ||
| if c, ok := reader.(io.Closer); ok { | ||
| t.Cleanup(func() { _ = c.Close() }) | ||
| } | ||
|
|
||
| tarData, err := io.ReadAll(reader) | ||
| require.NoError(t, err) | ||
| assert.NotEmpty(t, tarData) | ||
|
|
||
| // Extract. | ||
| extractDir := t.TempDir() | ||
| desc := ocispec.Descriptor{} | ||
| err = c.Decode(extractDir, "", bytes.NewReader(tarData), desc) | ||
| require.NoError(t, err) | ||
|
|
||
| // The archive was created relative to filepath.Dir(srcDir), so the | ||
| // extracted tree includes the base name of srcDir as a prefix. | ||
| base := filepath.Base(srcDir) | ||
| for name, expected := range files { | ||
| got, err := os.ReadFile(filepath.Join(extractDir, base, name)) | ||
| require.NoError(t, err, "reading extracted file %s", name) | ||
| assert.Equal(t, expected, string(got)) | ||
| } | ||
| } | ||
|
|
||
| func TestTarExtractRoundtrip(t *testing.T) { | ||
| t.Parallel() | ||
| srcDir := t.TempDir() | ||
rishi-jat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| content := []byte("roundtrip data 1234567890") | ||
|
|
||
| require.NoError(t, os.WriteFile(filepath.Join(srcDir, "data.bin"), content, 0644)) | ||
|
|
||
| c := newTar() | ||
|
|
||
| // Encode. | ||
| reader, err := c.Encode(filepath.Join(srcDir, "data.bin"), srcDir) | ||
| require.NoError(t, err) | ||
| if c, ok := reader.(io.Closer); ok { | ||
| t.Cleanup(func() { _ = c.Close() }) | ||
| } | ||
|
|
||
| tarData, err := io.ReadAll(reader) | ||
| require.NoError(t, err) | ||
|
|
||
| // Decode. | ||
| extractDir := t.TempDir() | ||
| desc := ocispec.Descriptor{} | ||
| require.NoError(t, c.Decode(extractDir, "data.bin", bytes.NewReader(tarData), desc)) | ||
|
|
||
| got, err := os.ReadFile(filepath.Join(extractDir, "data.bin")) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, content, got) | ||
| } | ||
|
|
||
| func TestTarInvalidArchive(t *testing.T) { | ||
| t.Parallel() | ||
| c := newTar() | ||
rishi-jat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| extractDir := t.TempDir() | ||
| desc := ocispec.Descriptor{} | ||
|
|
||
| // Feed garbage data as a tar stream. | ||
| err := c.Decode(extractDir, "file.txt", strings.NewReader("this is not a tar"), desc) | ||
| assert.Error(t, err) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.