Skip to content

Commit

Permalink
test: compressing of files and dirs
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Susini <[email protected]>
  • Loading branch information
loresuso committed Dec 13, 2023
1 parent 5df4a99 commit 7cc530f
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 5 deletions.
116 changes: 116 additions & 0 deletions internal/utils/compress_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
11 changes: 7 additions & 4 deletions internal/utils/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
}
6 changes: 5 additions & 1 deletion pkg/driver/distro/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 7cc530f

Please sign in to comment.