From 318d59203a47219e4fa3302f6bf606cf1d9ea206 Mon Sep 17 00:00:00 2001 From: ostempel Date: Tue, 3 Dec 2024 15:07:17 +0100 Subject: [PATCH] add compression test and fix linting/tests --- cmd/internal/backup/providers/gcp/gcp.go | 6 -- .../providers/gcp/gcp_integration_test.go | 2 + .../backup/providers/local/local_test.go | 2 + .../providers/s3/s3_integration_test.go | 2 + cmd/internal/compress/compress_test.go | 57 +++++++++++++++++++ cmd/internal/encryption/encryption.go | 4 +- cmd/internal/encryption/encryption_test.go | 29 ++++++---- 7 files changed, 82 insertions(+), 20 deletions(-) create mode 100644 cmd/internal/compress/compress_test.go diff --git a/cmd/internal/backup/providers/gcp/gcp.go b/cmd/internal/backup/providers/gcp/gcp.go index 86c2f31..865e9ad 100644 --- a/cmd/internal/backup/providers/gcp/gcp.go +++ b/cmd/internal/backup/providers/gcp/gcp.go @@ -8,7 +8,6 @@ import ( "net/http" "path/filepath" "strconv" - "strings" "errors" @@ -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) diff --git a/cmd/internal/backup/providers/gcp/gcp_integration_test.go b/cmd/internal/backup/providers/gcp/gcp_integration_test.go index f823121..4bee3fb 100644 --- a/cmd/internal/backup/providers/gcp/gcp_integration_test.go +++ b/cmd/internal/backup/providers/gcp/gcp_integration_test.go @@ -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) @@ -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) diff --git a/cmd/internal/backup/providers/local/local_test.go b/cmd/internal/backup/providers/local/local_test.go index d02bd6b..4baf31b 100644 --- a/cmd/internal/backup/providers/local/local_test.go +++ b/cmd/internal/backup/providers/local/local_test.go @@ -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) @@ -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) diff --git a/cmd/internal/backup/providers/s3/s3_integration_test.go b/cmd/internal/backup/providers/s3/s3_integration_test.go index ea1371a..e6d896b 100644 --- a/cmd/internal/backup/providers/s3/s3_integration_test.go +++ b/cmd/internal/backup/providers/s3/s3_integration_test.go @@ -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) @@ -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) diff --git a/cmd/internal/compress/compress_test.go b/cmd/internal/compress/compress_test.go new file mode 100644 index 0000000..66c950c --- /dev/null +++ b/cmd/internal/compress/compress_test.go @@ -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)) + +} diff --git a/cmd/internal/encryption/encryption.go b/cmd/internal/encryption/encryption.go index 6b836ba..1c3ed4d 100644 --- a/cmd/internal/encryption/encryption.go +++ b/cmd/internal/encryption/encryption.go @@ -80,10 +80,8 @@ func (e *Encrypter) Decrypt(inputReader io.Reader, outputWriter io.Writer) error return err } - // Erstelle einen Puffer, der den gesamten Input speichert + // needs to be a buffer to read the IV and message length var buf bytes.Buffer - - // Kopiere die Daten aus inputReader in den Puffer _, err = io.Copy(&buf, inputReader) if err != nil { return err diff --git a/cmd/internal/encryption/encryption_test.go b/cmd/internal/encryption/encryption_test.go index 186f1af..cebb22f 100644 --- a/cmd/internal/encryption/encryption_test.go +++ b/cmd/internal/encryption/encryption_test.go @@ -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) @@ -71,15 +77,16 @@ 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(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(outputBigDec.Name()) + require.NoError(t, err) }