|
| 1 | +package tar |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "maps" |
| 7 | + "math" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "path" |
| 11 | + "slices" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/kubernetes-csi/csi-driver-nfs/pkg/nfs" |
| 16 | + "golang.org/x/mod/sumdb/dirhash" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + code packApi = '0' |
| 21 | + cli packApi = '1' |
| 22 | +) |
| 23 | + |
| 24 | +type packApi byte |
| 25 | + |
| 26 | +const archiveFileExt = ".tar.gz" |
| 27 | + |
| 28 | +func TestPackUnpack(t *testing.T) { |
| 29 | + inputPath := t.TempDir() |
| 30 | + generateFileSystem(t, inputPath) |
| 31 | + |
| 32 | + outputPath := t.TempDir() |
| 33 | + |
| 34 | + // produced file names (without extensions) have a suffix, |
| 35 | + // which determine the last operation: |
| 36 | + // "0" means that it was produced from code |
| 37 | + // "1" means that it was produced from CLI |
| 38 | + // e.g.: "testdata011.tar.gz" - was packed from code, |
| 39 | + // then unpacked from cli and packed again from cli |
| 40 | + |
| 41 | + pathsBySuffix := make(map[string]string) |
| 42 | + |
| 43 | + // number of pack/unpack operations |
| 44 | + opNum := 4 |
| 45 | + |
| 46 | + // generate all operation combinations |
| 47 | + fileNum := int(math.Pow(2, float64(opNum))) |
| 48 | + for i := 0; i < fileNum; i++ { |
| 49 | + binStr := fmt.Sprintf("%b", i) |
| 50 | + |
| 51 | + // left-pad with zeroes |
| 52 | + binStr = strings.Repeat("0", opNum-len(binStr)) + binStr |
| 53 | + |
| 54 | + // copy slices to satisfy type system |
| 55 | + ops := make([]packApi, opNum) |
| 56 | + for opIdx := 0; opIdx < opNum; opIdx++ { |
| 57 | + ops[opIdx] = packApi(binStr[opIdx]) |
| 58 | + } |
| 59 | + |
| 60 | + // produce folders and archives |
| 61 | + produce(t, pathsBySuffix, inputPath, outputPath, ops...) |
| 62 | + } |
| 63 | + |
| 64 | + // compare all unpacked directories |
| 65 | + paths := slices.Collect(maps.Values(pathsBySuffix)) |
| 66 | + assertUnpackedFilesEqual(t, inputPath, paths) |
| 67 | +} |
| 68 | + |
| 69 | +func produce( |
| 70 | + t *testing.T, |
| 71 | + results map[string]string, |
| 72 | + inputDirPath string, |
| 73 | + outputDirPath string, |
| 74 | + ops ...packApi, |
| 75 | +) { |
| 76 | + baseName := path.Base(inputDirPath) |
| 77 | + |
| 78 | + for i := 0; i < len(ops); i++ { |
| 79 | + packing := i%2 == 0 |
| 80 | + |
| 81 | + srcPath := inputDirPath |
| 82 | + if i > 0 { |
| 83 | + prevSuffix := string(ops[:i]) |
| 84 | + srcPath = path.Join(outputDirPath, baseName+prevSuffix) |
| 85 | + if !packing { |
| 86 | + srcPath += archiveFileExt |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + suffix := string(ops[:i+1]) |
| 91 | + dstPath := path.Join(outputDirPath, baseName+suffix) |
| 92 | + if packing { |
| 93 | + dstPath += archiveFileExt |
| 94 | + } |
| 95 | + |
| 96 | + if _, ok := results[suffix]; ok { |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + switch { |
| 101 | + case packing && ops[i] == code: |
| 102 | + // packing from code |
| 103 | + if err := nfs.TarPack(srcPath, dstPath, true); err != nil { |
| 104 | + t.Fatalf("packing '%s' with TarPack into '%s': %v", srcPath, dstPath, err) |
| 105 | + } |
| 106 | + case packing && ops[i] == cli: |
| 107 | + // packing from CLI |
| 108 | + if out, err := exec.Command("tar", "-C", srcPath, "-czvf", dstPath, ".").CombinedOutput(); err != nil { |
| 109 | + t.Log("TAR OUTPUT:", string(out)) |
| 110 | + t.Fatalf("packing '%s' with tar into '%s': %v", srcPath, dstPath, err) |
| 111 | + } |
| 112 | + case !packing && ops[i] == code: |
| 113 | + // unpacking from code |
| 114 | + if err := nfs.TarUnpack(srcPath, dstPath, true); err != nil { |
| 115 | + t.Fatalf("unpacking '%s' with TarUnpack into '%s': %v", srcPath, dstPath, err) |
| 116 | + } |
| 117 | + case !packing && ops[i] == cli: |
| 118 | + // unpacking from CLI |
| 119 | + // tar requires destination directory to exist |
| 120 | + if err := os.MkdirAll(dstPath, 0755); err != nil { |
| 121 | + t.Fatalf("making dir '%s' for unpacking with tar: %v", dstPath, err) |
| 122 | + } |
| 123 | + if out, err := exec.Command("tar", "-xzvf", srcPath, "-C", dstPath).CombinedOutput(); err != nil { |
| 124 | + t.Log("TAR OUTPUT:", string(out)) |
| 125 | + t.Fatalf("unpacking '%s' with tar into '%s': %v", srcPath, dstPath, err) |
| 126 | + } |
| 127 | + default: |
| 128 | + t.Fatalf("unknown suffix: %s", string(ops[i])) |
| 129 | + } |
| 130 | + |
| 131 | + results[suffix] = dstPath |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func assertUnpackedFilesEqual(t *testing.T, originalDir string, paths []string) { |
| 136 | + originalDirHash, err := dirhash.HashDir(originalDir, "_", dirhash.DefaultHash) |
| 137 | + if err != nil { |
| 138 | + t.Fatal("failed hashing original dir ", err) |
| 139 | + } |
| 140 | + |
| 141 | + for _, p := range paths { |
| 142 | + if strings.HasSuffix(p, archiveFileExt) { |
| 143 | + // archive, not a directory |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + // unpacked directory |
| 148 | + hs, err := dirhash.HashDir(p, "_", dirhash.DefaultHash) |
| 149 | + if err != nil { |
| 150 | + t.Fatal("failed hashing dir ", err) |
| 151 | + } |
| 152 | + |
| 153 | + if hs != originalDirHash { |
| 154 | + t.Errorf("expected '%s' to have the same hash as '%s', got different", originalDir, p) |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func generateFileSystem(t *testing.T, inputPath string) { |
| 160 | + // empty directory |
| 161 | + if err := os.MkdirAll(path.Join(inputPath, "empty_dir"), 0755); err != nil { |
| 162 | + t.Fatalf("generating empty directory: %v", err) |
| 163 | + } |
| 164 | + |
| 165 | + // deep empty directories |
| 166 | + deepEmptyDirPath := path.Join(inputPath, "deep_empty_dir", strings.Repeat("/0/1/2", 20)) |
| 167 | + if err := os.MkdirAll(deepEmptyDirPath, 0755); err != nil { |
| 168 | + t.Fatalf("generating deep empty directory '%s': %v", deepEmptyDirPath, err) |
| 169 | + } |
| 170 | + |
| 171 | + // empty file |
| 172 | + f, err := os.Create(path.Join(inputPath, "empty_file")) |
| 173 | + if err != nil { |
| 174 | + t.Fatalf("generating empty file: %v", err) |
| 175 | + } |
| 176 | + f.Close() |
| 177 | + |
| 178 | + // big (100MB) file |
| 179 | + bigFilePath := path.Join(inputPath, "big_file") |
| 180 | + for i := byte(0); i < 100; i++ { |
| 181 | + // write 1MB |
| 182 | + err := os.WriteFile(bigFilePath, bytes.Repeat([]byte{i}, 1024*1024), 0755) |
| 183 | + if err != nil { |
| 184 | + t.Fatalf("generating empty file: %v", err) |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments