From b58a1477e356f7d1aaaf5412c933fe02cf01ad79 Mon Sep 17 00:00:00 2001 From: Lorenzo Susini Date: Tue, 21 Nov 2023 11:14:27 +0000 Subject: [PATCH] test: compressing of files and dirs Signed-off-by: Lorenzo Susini --- internal/utils/compress_test.go | 116 ++++++++++++++++++++++++++++++++ internal/utils/extract.go | 11 +-- 2 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 internal/utils/compress_test.go diff --git a/internal/utils/compress_test.go b/internal/utils/compress_test.go new file mode 100644 index 000000000..e82aaafd1 --- /dev/null +++ b/internal/utils/compress_test.go @@ -0,0 +1,116 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright (C) 2023 The Falco 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 utils + +import ( + "fmt" + "os" + "path/filepath" + "testing" +) + +const ( + filename1 = "file1" + filename2 = "file2" +) + +func TestCreateTarGzArchiveFile(t *testing.T) { + dir := t.TempDir() + f1, err := os.Create(filepath.Join(dir, filename1)) + if err != nil { + t.Fatalf(err.Error()) + } + defer f1.Close() + + tarball, err := CreateTarGzArchive(filepath.Join(dir, filename1)) + if err != nil { + t.Fatalf(err.Error()) + } + defer os.Remove(tarball) + + file, err := os.Open(tarball) + if err != nil { + t.Fatalf(err.Error()) + } + + paths, err := listHeaders(file) + fmt.Println(paths) + if err != nil { + t.Fatalf(err.Error()) + } + + if len(paths) != 1 { + t.Fatalf("Expected 1 path, got %d", len(paths)) + } + + base := filepath.Base(paths[0]) + if base != filename1 { + t.Errorf("Expected file1, got %s", base) + } +} + +func TestCreateTarGzArchiveDir(t *testing.T) { + // Test that we can compress directories + dir := t.TempDir() + + // add some files + f1, err := os.Create(filepath.Join(dir, filename1)) + if err != nil { + t.Fatalf(err.Error()) + } + defer f1.Close() + f2, err := os.Create(filepath.Join(dir, filename2)) + if err != nil { + t.Fatalf(err.Error()) + } + defer f2.Close() + + tarball, err := CreateTarGzArchive(dir) + if err != nil { + t.Fatalf(err.Error()) + } + defer os.Remove(tarball) + + file, err := os.Open(tarball) + if err != nil { + t.Fatalf(err.Error()) + } + defer file.Close() + + paths, err := listHeaders(file) + if err != nil { + t.Fatalf(err.Error()) + } + + if len(paths) != 3 { + t.Fatalf("Expected 3 paths, got %d", len(paths)) + } + + p := filepath.Base(paths[0]) + if p != filepath.Base(dir) { + t.Errorf("Expected %s, got %s", filepath.Base(dir), p) + } + + p = filepath.Base(paths[1]) + if p != filename1 { + t.Errorf("Expected file1, got %s", p) + } + + p = filepath.Base(paths[2]) + if p != filename2 { + t.Errorf("Expected file2, got %s", p) + } +} diff --git a/internal/utils/extract.go b/internal/utils/extract.go index cc3e6cccb..8d41a82a7 100644 --- a/internal/utils/extract.go +++ b/internal/utils/extract.go @@ -90,14 +90,15 @@ func ExtractTarGz(gzipStream io.Reader, destDir string, artifactType oci.Artifac return files, nil } -func listHeaders(gzipStream io.Reader) { +func listHeaders(gzipStream io.Reader) ([]string, error) { uncompressedStream, err := gzip.NewReader(gzipStream) if err != nil { - return + return nil, err } tarReader := tar.NewReader(uncompressedStream) + var files []string for { header, err := tarReader.Next() @@ -106,9 +107,11 @@ func listHeaders(gzipStream io.Reader) { } if err != nil { - return + return nil, err } - fmt.Println(header.Name) + files = append(files, header.Name) } + + return files, nil }