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 Nov 21, 2023
1 parent 28358fd commit b58a147
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 4 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 @@ -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()

Expand All @@ -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
}

0 comments on commit b58a147

Please sign in to comment.