|
| 1 | +// Copyright 2026 NetApp, Inc. All Rights Reserved. |
| 2 | +package utils |
| 3 | + |
| 4 | +import ( |
| 5 | + "archive/tar" |
| 6 | + "compress/gzip" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCreateTarGz(t *testing.T) { |
| 14 | + // 1. Setup temporary source directory with files |
| 15 | + srcDir, err := os.MkdirTemp("", "archive_test_src") |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("Failed to create temp src dir: %v", err) |
| 18 | + } |
| 19 | + defer os.RemoveAll(srcDir) |
| 20 | + |
| 21 | + files := map[string]string{ |
| 22 | + "file1.txt": "Content of file 1", |
| 23 | + "subdir/file2.go": "package main\nfunc main() {}", |
| 24 | + } |
| 25 | + |
| 26 | + for path, content := range files { |
| 27 | + fullPath := filepath.Join(srcDir, path) |
| 28 | + if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil { |
| 29 | + t.Fatalf("Failed to create subdir for %s: %v", path, err) |
| 30 | + } |
| 31 | + if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil { |
| 32 | + t.Fatalf("Failed to write file %s: %v", path, err) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // 2. Define destination archive path |
| 37 | + tmpDest, err := os.MkdirTemp("", "archive_test_dest") |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("Failed to create temp dest dir: %v", err) |
| 40 | + } |
| 41 | + defer os.RemoveAll(tmpDest) |
| 42 | + |
| 43 | + destFile := filepath.Join(tmpDest, "archive.tar.gz") |
| 44 | + |
| 45 | + // 3. Run CreateTarGz |
| 46 | + if err := CreateTarGz(srcDir, destFile); err != nil { |
| 47 | + t.Fatalf("CreateTarGz failed: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + // 4. Verify Archive Contents |
| 51 | + f, err := os.Open(destFile) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("Failed to open archive: %v", err) |
| 54 | + } |
| 55 | + defer f.Close() |
| 56 | + |
| 57 | + gzr, err := gzip.NewReader(f) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Failed to create gzip reader: %v", err) |
| 60 | + } |
| 61 | + defer gzr.Close() |
| 62 | + |
| 63 | + tr := tar.NewReader(gzr) |
| 64 | + |
| 65 | + foundFiles := make(map[string]bool) |
| 66 | + for { |
| 67 | + header, err := tr.Next() |
| 68 | + if err == io.EOF { |
| 69 | + break |
| 70 | + } |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("Tar iterator error: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + // Check if file is in our expected list |
| 76 | + // Relative paths in archive might differ slightly (e.g. ./file1.txt vs file1.txt) |
| 77 | + // Our implementation uses filepath.Rel which typically produces clean relative paths |
| 78 | + if _, ok := files[header.Name]; ok { |
| 79 | + foundFiles[header.Name] = true |
| 80 | + |
| 81 | + // Verify content |
| 82 | + content, err := io.ReadAll(tr) |
| 83 | + if err != nil { |
| 84 | + t.Fatalf("Failed to read content for %s: %v", header.Name, err) |
| 85 | + } |
| 86 | + if string(content) != files[header.Name] { |
| 87 | + t.Errorf("Content mismatch for %s. Got %s, want %s", header.Name, string(content), files[header.Name]) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Verify all expected files were found |
| 93 | + for file := range files { |
| 94 | + if !foundFiles[file] { |
| 95 | + t.Errorf("File %s not found in archive", file) |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments