Skip to content

Commit

Permalink
add compression test and fix linting/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ostempel committed Dec 3, 2024
1 parent e68bf66 commit adc8066
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 17 deletions.
6 changes: 0 additions & 6 deletions cmd/internal/backup/providers/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http"
"path/filepath"
"strconv"
"strings"

"errors"

Expand Down Expand Up @@ -155,11 +154,6 @@ func (b *BackupProviderGCP) DownloadBackup(ctx context.Context, version *provide

bucket := b.c.Bucket(b.config.BucketName)

downloadFileName := version.Name
if strings.Contains(downloadFileName, "/") {
downloadFileName = filepath.Base(downloadFileName)
}

r, err := bucket.Object(version.Name).Generation(gen).NewReader(ctx)
if err != nil {
return fmt.Errorf("backup not found: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/internal/backup/providers/gcp/gcp_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func Test_BackupProviderGCP(t *testing.T) {
require.NoError(t, err)

backupFile, err := fs.Open(backupPath)
require.NoError(t, err)
err = p.UploadBackup(ctx, backupFile, backupPath)
require.NoError(t, err)

Expand Down Expand Up @@ -155,6 +156,7 @@ func Test_BackupProviderGCP(t *testing.T) {
require.NotNil(t, latestVersion)

outputfile, err := fs.Create("outputfile")
require.NoError(t, err)

err = p.DownloadBackup(ctx, latestVersion, outputfile)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/internal/backup/providers/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func Test_BackupProviderLocal(t *testing.T) {
require.NoError(t, err)

infile, err := fs.Open(backupPath)
require.NoError(t, err)
err = p.UploadBackup(ctx, infile, backupPath)
require.NoError(t, err)

Expand Down Expand Up @@ -132,6 +133,7 @@ func Test_BackupProviderLocal(t *testing.T) {
require.NotNil(t, latestVersion)

outputfile, err := fs.Create("outputfile")
require.NoError(t, err)

err = p.DownloadBackup(ctx, latestVersion, outputfile)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions cmd/internal/backup/providers/s3/s3_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func Test_BackupProviderS3(t *testing.T) {
require.NoError(t, err)

backupFile, err := fs.Open(backupPath)
require.NoError(t, err)

err = p.UploadBackup(ctx, backupFile, backupPath)
require.NoError(t, err)
Expand Down Expand Up @@ -150,6 +151,7 @@ func Test_BackupProviderS3(t *testing.T) {
require.NotNil(t, latestVersion)

outputfile, err := fs.Create("outputfile")
require.NoError(t, err)

err = p.DownloadBackup(ctx, latestVersion, outputfile)
require.NoError(t, err)
Expand Down
57 changes: 57 additions & 0 deletions cmd/internal/compress/compress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package compress

import (
"context"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)

func TestCompress(t *testing.T) {
ctx := context.Background()
fsys := afero.NewMemMapFs()

compressor, err := New(&CompressorConfig{Method: "targz", FS: fsys})
require.NoError(t, err)

// Prepare input for compression
input, err := fsys.Create("compress")
require.NoError(t, err)

err = afero.WriteFile(fsys, input.Name(), []byte("This is the content of the file"), 0600)
require.NoError(t, err)

// Compress files
output, err := fsys.Create("compress.targz")
require.NoError(t, err)

files, err := compressor.BuildFilesForCompression("compress", input.Name())
require.NoError(t, err)

err = compressor.Compress(ctx, output, files)
require.NoError(t, err)

// Prepare input for decompression
inputDecompressed, err := fsys.Open("compress.targz")
require.NoError(t, err)

err = fsys.Remove("compress")
require.NoError(t, err)

// Decompress files
err = compressor.Decompress(ctx, inputDecompressed, "./")
require.NoError(t, err)

outputDecompressed, err := fsys.Open("compress")
require.NoError(t, err)

cleartext, err := afero.ReadFile(fsys, outputDecompressed.Name())
require.NoError(t, err)

println("cleartext: ")
println(string(cleartext))

require.Equal(t, []byte("This is the content of the file"), (cleartext))

}
33 changes: 22 additions & 11 deletions cmd/internal/encryption/encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@ func TestEncrypter(t *testing.T) {

//Prepare input for encryption
input, err := fs.Create("encrypt")
require.NoError(t, err)
output, err := fs.Create("encrypt.aes")
require.NoError(t, err)
cleartextInput := []byte("This is the content of the file")
err = afero.WriteFile(fs, input.Name(), cleartextInput, 0600)
require.NoError(t, err)

//Encrypt files
err = e.Encrypt(input, output)
require.NoError(t, err)
encryptedText, err := afero.ReadFile(fs, output.Name())
require.NoError(t, err)

require.Equal(t, input.Name()+Suffix, output.Name())
require.NotEqual(t, cleartextInput, encryptedText)

//Prepare input for decryption
inputDecrypted, err := fs.Open("encrypt.aes")
require.NoError(t, err)
outputDecrypted, err := fs.Create("decrypted")
require.NoError(t, err)

//Decrypt files
err = e.Decrypt(inputDecrypted, outputDecrypted)
Expand Down Expand Up @@ -71,15 +77,20 @@ func TestEncrypter(t *testing.T) {
err = e.Decrypt(inputBigDec, outputBigDec)
require.NoError(t, err)

fs.Remove(input.Name())
fs.Remove(output.Name())

fs.Remove(inputDecrypted.Name())
fs.Remove(outputDecrypted.Name())

fs.Remove(inputBigEnc.Name())
fs.Remove(outputBigEnc.Name())

fs.Remove(inputBigDec.Name())
fs.Remove(outputBigDec.Name())
err = fs.Remove(input.Name())
require.NoError(t, err)
err = fs.Remove(output.Name())
require.NoError(t, err)
err = fs.Remove(inputDecrypted.Name())
require.NoError(t, err)
err = fs.Remove(outputDecrypted.Name())
require.NoError(t, err)
err = fs.Remove(inputBigEnc.Name())
require.NoError(t, err)
err = fs.Remove(outputBigEnc.Name())
require.NoError(t, err)
err = fs.Remove(inputBigDec.Name())
require.NoError(t, err)
err = fs.Remove(outputBigDec.Name())
require.NoError(t, err)
}

0 comments on commit adc8066

Please sign in to comment.