Skip to content

Commit 764eacd

Browse files
authored
adding a test to the agent compression to ensure subfolders and empty py files are including in upload (#598)
1 parent ff3cd49 commit 764eacd

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

pkg/agentfs/tar_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,93 @@ func TestUploadTarballDotfiles(t *testing.T) {
219219

220220
require.Empty(t, expectedFiles, "some expected files were not found in tar: %v", expectedFiles)
221221
}
222+
223+
func TestUploadTarballDeepDirectories(t *testing.T) {
224+
tmpDir, err := os.MkdirTemp("", "tarball-test-*")
225+
require.NoError(t, err)
226+
defer os.RemoveAll(tmpDir)
227+
228+
dirs := []string{
229+
"level1",
230+
"level1/level2",
231+
"level1/level2/level3",
232+
"level1/level2/level3/level4",
233+
}
234+
235+
for _, dir := range dirs {
236+
err = os.MkdirAll(filepath.Join(tmpDir, dir), 0755)
237+
require.NoError(t, err)
238+
initPath := filepath.Join(tmpDir, dir, "__init__.py")
239+
err = os.WriteFile(initPath, []byte(""), 0644)
240+
require.NoError(t, err)
241+
}
242+
243+
files := []struct {
244+
path string
245+
content string
246+
}{
247+
{filepath.Join(tmpDir, "root.txt"), "root file"},
248+
{filepath.Join(tmpDir, "level1", "level1.txt"), "level 1 file"},
249+
{filepath.Join(tmpDir, "level1", "level2", "level2.txt"), "level 2 file"},
250+
{filepath.Join(tmpDir, "level1", "level2", "level3", "level3.txt"), "level 3 file"},
251+
{filepath.Join(tmpDir, "level1", "level2", "level3", "level4", "level4.txt"), "level 4 file"},
252+
}
253+
254+
for _, f := range files {
255+
err = os.WriteFile(f.path, []byte(f.content), 0644)
256+
require.NoError(t, err)
257+
}
258+
259+
var tarBuffer bytes.Buffer
260+
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
261+
_, err := io.Copy(&tarBuffer, r.Body)
262+
require.NoError(t, err)
263+
w.WriteHeader(http.StatusOK)
264+
}))
265+
defer mockServer.Close()
266+
267+
err = UploadTarball(tmpDir, mockServer.URL, []string{})
268+
require.NoError(t, err)
269+
270+
contents := readTarContents(t, tarBuffer.Bytes())
271+
272+
for _, dir := range dirs {
273+
found := false
274+
for _, content := range contents {
275+
if content.Name == dir+"/" && content.IsDir {
276+
found = true
277+
break
278+
}
279+
}
280+
require.True(t, found, "directory not found in tar: %s", dir)
281+
}
282+
283+
for _, f := range files {
284+
found := false
285+
relPath, err := filepath.Rel(tmpDir, f.path)
286+
require.NoError(t, err)
287+
for _, content := range contents {
288+
if content.Name == relPath {
289+
found = true
290+
require.Equal(t, int64(len(f.content)), content.Size, "incorrect file size for %s", relPath)
291+
require.False(t, content.IsDir, "file marked as directory: %s", relPath)
292+
break
293+
}
294+
}
295+
require.True(t, found, "file not found in tar: %s", relPath)
296+
}
297+
298+
for _, dir := range dirs {
299+
initPath := filepath.Join(dir, "__init__.py")
300+
found := false
301+
for _, content := range contents {
302+
if content.Name == initPath {
303+
found = true
304+
require.Equal(t, int64(0), content.Size, "incorrect file size for %s", initPath)
305+
require.False(t, content.IsDir, "file marked as directory: %s", initPath)
306+
break
307+
}
308+
}
309+
require.True(t, found, "__init__.py not found in tar: %s", initPath)
310+
}
311+
}

0 commit comments

Comments
 (0)