Skip to content

Commit

Permalink
Merge pull request #6396 from allenxu404/archive-ut
Browse files Browse the repository at this point in the history
Add unit test for pkg/archive
  • Loading branch information
blackpiglet authored Jun 16, 2023
2 parents e3e0ce3 + 41ab949 commit d3785e5
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/6396-allenxu404
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add unit tests for pkg/archive
2 changes: 1 addition & 1 deletion pkg/archive/extractor.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 the Velero contributors.
Copyright the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
151 changes: 151 additions & 0 deletions pkg/archive/extractor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
Copyright The Velero Contributors.
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 archive

import (
"archive/tar"
"compress/gzip"
"io"
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/vmware-tanzu/velero/pkg/test"
"github.com/vmware-tanzu/velero/pkg/util/filesystem"
)

func TestUnzipAndExtractBackup(t *testing.T) {
tests := []struct {
name string
files []string
IsTarball bool
wantErr bool
}{
{
name: "when the format of backup file is invalid, an error is returned",
files: []string{},
IsTarball: false,
wantErr: true,
},
{
name: "when the backup tarball is empty, the function should work correctly and returns no error",
files: []string{},
IsTarball: true,
wantErr: false,
},
{
name: "when the backup tarball includes a mix of items, the function should work correctly and returns no error",
files: []string{
"root-dir/resources/namespace/cluster/example.json",
"root-dir/resources/pods/namespaces/example.json",
"root-dir/metadata/version",
},
IsTarball: true,
wantErr: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ext := NewExtractor(test.NewLogger(), test.NewFakeFileSystem())
var fileName string
var err error
if tc.IsTarball {
fileName, err = createArchive(tc.files, ext.fs)
} else {
fileName, err = createRegular(ext.fs)
}
require.NoError(t, err)

file, err := ext.fs.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0644)
require.NoError(t, err)

_, err = ext.UnzipAndExtractBackup(file.(io.Reader))
if tc.wantErr && (err == nil) {
t.Errorf("%s: wanted error but got nil", tc.name)
}

if !tc.wantErr && (err != nil) {
t.Errorf("%s: wanted no error but got err: %v", tc.name, err)
}
})
}
}

func createArchive(files []string, fs filesystem.Interface) (string, error) {
outName := "output.tar.gz"
out, err := fs.Create(outName)
if err != nil {
return outName, err
}
defer out.Close()
gw := gzip.NewWriter(out)
defer gw.Close()
tw := tar.NewWriter(gw)
defer tw.Close()

// Iterate over files and add them to the tar archive
for _, file := range files {
err := addToArchive(tw, file, fs)
if err != nil {
return outName, err
}
}

return outName, nil
}

func addToArchive(tw *tar.Writer, filename string, fs filesystem.Interface) error {
// Create the file
file, err := fs.Create(filename)
if err != nil {
return err
}
defer file.Close()

// Get FileInfo about size, mode, etc.
info, err := fs.Stat(filename)
if err != nil {
return err
}

// Create a tar Header from the FileInfo data
header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}

header.Name = filename
err = tw.WriteHeader(header)
if err != nil {
return err
}

return nil
}

func createRegular(fs filesystem.Interface) (string, error) {
outName := "output"
out, err := fs.Create(outName)
if err != nil {
return outName, err
}
defer out.Close()

return outName, nil
}
2 changes: 1 addition & 1 deletion pkg/archive/filesystem.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 the Velero contributors.
Copyright the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
40 changes: 39 additions & 1 deletion pkg/archive/filesystem_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 the Velero contributors.
Copyright the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,10 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/test"
)

func TestGetItemFilePath(t *testing.T) {
Expand Down Expand Up @@ -47,3 +51,37 @@ func TestGetItemFilePath(t *testing.T) {
res = GetVersionedItemFilePath("", "resource", "", "item", "")
assert.Equal(t, "resources/resource/cluster/item.json", res)
}

func TestGetScopeDir(t *testing.T) {
res := GetScopeDir("")
assert.Equal(t, velerov1api.ClusterScopedDir, res)

res = GetScopeDir("test-namespace")
assert.Equal(t, velerov1api.NamespaceScopedDir, res)
}

func TestUnmarshal(t *testing.T) {
fs := test.NewFakeFileSystem()
filePath := "pod.json"
fileContent := `{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "example-pod"
},
"spec": {
"containers": [{
"name": "example-container",
"image": "example-image"
}]
}
}`
out, err := fs.Create(filePath)
require.NoError(t, err)

_, err = out.Write([]byte(fileContent))
require.NoError(t, err)

_, err = Unmarshal(fs, filePath)
require.NoError(t, err)
}

0 comments on commit d3785e5

Please sign in to comment.