From 641abe1e8d71fa880b4578fb1d0613ad1269d771 Mon Sep 17 00:00:00 2001 From: Martin Zihlmann Date: Fri, 26 Jun 2026 22:10:46 +0100 Subject: [PATCH] mz843: generate integration tar fixtures on the fly --- .gitignore | 7 +- integration/context/tars/file.bz2 | Bin 196 -> 0 bytes integration/context/tars/file.tar | Bin 10240 -> 0 bytes integration/context/tars/file.tar.gz | Bin 199 -> 0 bytes integration/context/tars/sys.tar.gz | Bin 166 -> 0 bytes integration/fixtures.go | 167 +++++++++++++++++++++++++++ integration/integration_test.go | 4 + 7 files changed, 177 insertions(+), 1 deletion(-) delete mode 100644 integration/context/tars/file.bz2 delete mode 100644 integration/context/tars/file.tar delete mode 100644 integration/context/tars/file.tar.gz delete mode 100644 integration/context/tars/sys.tar.gz create mode 100644 integration/fixtures.go diff --git a/.gitignore b/.gitignore index 99b2a42cc..47528aa80 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,9 @@ BUILD.bazel .idea/** *.iml .vagrant -.vscode/ \ No newline at end of file +.vscode/ +# Generated by integration TestMain (generateTarFixtures), never commit binary archives +integration/context/tars/file.tar +integration/context/tars/file.tar.gz +integration/context/tars/file.bz2 +integration/context/tars/sys.tar.gz diff --git a/integration/context/tars/file.bz2 b/integration/context/tars/file.bz2 deleted file mode 100644 index 53c13149a1a2a8e4f309d9c4f1920a8eec06f199..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 196 zcmV;#06YIeT4*^jL0KkKSt*-N;{X6?e}uu1AP7JK|9}Yr00DO9-mn0G00=MuyIK;c z&;vjKXbl5E(x0jxpa1{>01`+>Q_-k;fB*n`n+tHlwC-Gx6Os}}a;B#-Drt1cg+>ww z5ENu?2%qXhZUC2F;7J-Fk}QfOwL*!5DvT*ARKsiI0^yuwVp3_uc-MmuffC&yAA{-a yu&Z;>@rr;*w+Wj)X`MKHJ3nP)!rB-ho|uFQJuF^gP_c-Re;0B^I8cx&n@;0WB=1||~uvs?kcOkxS zv7d&`1LsWU#q_-P-4L5`Z0bw_M$&mlY>P2!~DCNHjU@vpR?Uf_Rv%e zJm#{iR;*EAYsNy|s`*dP9QgC#EO+GGS*-6}rz`FdaFLYXv$6lP`o(J%>$iL?v%4N9 ztvfTme%1+#n50uiFN2GFgV*>4*Zj`gm!G^#F4+8A^qX5AaWcEM`F}OIa&@2O_3S@| w{4rMFckbWjUH|ymzokKLMq7`~2(Q0azjFWYuObY{AbuBP=Ei7Q1`P%V06MT)5C8xG diff --git a/integration/context/tars/sys.tar.gz b/integration/context/tars/sys.tar.gz deleted file mode 100644 index d61da594905b599186542270a78e8f1f55e92269..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 166 zcmb2|=3rR=cuh0|^V>^~T!#zk diff --git a/integration/fixtures.go b/integration/fixtures.go new file mode 100644 index 000000000..d64dfcf73 --- /dev/null +++ b/integration/fixtures.go @@ -0,0 +1,167 @@ +/* +Copyright 2026 OSS Container Tools + +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 integration + +import ( + "archive/tar" + "bytes" + "compress/gzip" + "fmt" + "os" + "os/exec" + "path/filepath" + "runtime" + "time" +) + +var fixtureModTime = time.Unix(1600000000, 0) + +type tarEntry struct { + name string + typeflag byte + mode int64 + content string +} + +func buildTar(entries []tarEntry) ([]byte, error) { + var buf bytes.Buffer + tw := tar.NewWriter(&buf) + for _, e := range entries { + hdr := &tar.Header{ + Name: e.name, + Typeflag: e.typeflag, + Mode: e.mode, + ModTime: fixtureModTime, + } + if e.typeflag == tar.TypeReg { + hdr.Size = int64(len(e.content)) + } + err := tw.WriteHeader(hdr) + if err != nil { + return nil, err + } + if e.typeflag == tar.TypeReg { + _, err = tw.Write([]byte(e.content)) + if err != nil { + return nil, err + } + } + } + err := tw.Close() + if err != nil { + return nil, err + } + return buf.Bytes(), nil +} + +func gzipBytes(in []byte) ([]byte, error) { + var buf bytes.Buffer + zw := gzip.NewWriter(&buf) + _, err := zw.Write(in) + if err != nil { + return nil, err + } + err = zw.Close() + if err != nil { + return nil, err + } + return buf.Bytes(), nil +} + +// bzip2Bytes shells out to bzip2 because the standard library has no bzip2 writer. +func bzip2Bytes(in []byte) ([]byte, error) { + cmd := exec.Command("bzip2", "-c") + cmd.Stdin = bytes.NewReader(in) + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + return nil, fmt.Errorf("bzip2: %w", err) + } + return out.Bytes(), nil +} + +// generateTarFixtures writes the tar archives consumed by the ADD integration +// tests into context/tars, so the repository holds no committed binary archives. +func generateTarFixtures() error { + _, ex, _, _ := runtime.Caller(0) + dir := filepath.Join(filepath.Dir(ex), "context", "tars") + err := os.MkdirAll(dir, 0o755) + if err != nil { + return err + } + + plain, err := buildTar([]tarEntry{ + {name: "./", typeflag: tar.TypeDir, mode: 0o755}, + {name: "./uncompressedFile", typeflag: tar.TypeReg, mode: 0o644}, + }) + if err != nil { + return err + } + + gzipped, err := buildTar([]tarEntry{ + {name: "./", typeflag: tar.TypeDir, mode: 0o755}, + {name: "./compressedFile", typeflag: tar.TypeReg, mode: 0o644, content: "compressed\n"}, + }) + if err != nil { + return err + } + + bzipped, err := buildTar([]tarEntry{ + {name: "./", typeflag: tar.TypeDir, mode: 0o755}, + {name: "./bzCompressedFile", typeflag: tar.TypeReg, mode: 0o644, content: "bzip\n"}, + }) + if err != nil { + return err + } + + sys, err := buildTar([]tarEntry{ + {name: "sys/", typeflag: tar.TypeDir, mode: 0o755}, + {name: "sys/fs/", typeflag: tar.TypeDir, mode: 0o755}, + {name: "sys/fs/foo", typeflag: tar.TypeReg, mode: 0o644}, + }) + if err != nil { + return err + } + + gzippedGz, err := gzipBytes(gzipped) + if err != nil { + return err + } + sysGz, err := gzipBytes(sys) + if err != nil { + return err + } + bzippedBz, err := bzip2Bytes(bzipped) + if err != nil { + return err + } + + fixtures := map[string][]byte{ + "file.tar": plain, + "file.tar.gz": gzippedGz, + "file.bz2": bzippedBz, + "sys.tar.gz": sysGz, + } + for name, data := range fixtures { + err = os.WriteFile(filepath.Join(dir, name), data, 0o644) + if err != nil { + return err + } + } + return nil +} diff --git a/integration/integration_test.go b/integration/integration_test.go index 86248ee00..043e5df0a 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -121,6 +121,10 @@ func TestMain(m *testing.M) { } config = initIntegrationTestConfig() + if err = generateTarFixtures(); err != nil { + fmt.Println("Couldn't generate tar fixtures", err) + os.Exit(1) + } if allDockerfiles, err = FindDockerFiles(dockerfilesPath, config.dockerfilesPattern); err != nil { fmt.Println("Coudn't create map of dockerfiles", err) os.Exit(1)