From 7cc530fbbfb517ec8c5f742e7030a92c46916850 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 +-- pkg/driver/distro/distro.go | 6 +- 3 files changed, 128 insertions(+), 5 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 00000000..e82aaafd --- /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 fd4df65f..5f18f0d0 100644 --- a/internal/utils/extract.go +++ b/internal/utils/extract.go @@ -106,14 +106,15 @@ func stripComponents(headerName string, stripComponents int) string { return filepath.Clean(strings.Join(names[stripComponents:], "/")) } -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() @@ -122,9 +123,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 } diff --git a/pkg/driver/distro/distro.go b/pkg/driver/distro/distro.go index 17ac5bf8..2114b2c2 100644 --- a/pkg/driver/distro/distro.go +++ b/pkg/driver/distro/distro.go @@ -35,6 +35,7 @@ import ( "github.com/falcosecurity/falcoctl/internal/utils" drivertype "github.com/falcosecurity/falcoctl/pkg/driver/type" + "github.com/falcosecurity/falcoctl/pkg/oci" "github.com/falcosecurity/falcoctl/pkg/output" ) @@ -319,7 +320,10 @@ func downloadKernelSrc(ctx context.Context, return env, err } - _, err = utils.ExtractTarGz(resp.Body, fullKernelDir, stripComponents) + // todo(loresuso,fededp): use oci.Asset as the artifact type + // oci.Asset is generic enough to be used for kernel sources but we might want to find + // a better way to handle this. + _, err = utils.ExtractTarGz(resp.Body, fullKernelDir, oci.Asset, stripComponents) if err != nil { return env, err }