PostgreSQL backup tool with Point-In-Time-Recovery and restore verification
TypeScript backupbackupsdatabasedatabase-backupdevopsdockergolangkubernetesmariadbmongodbmysqlpgpostgrespostgresqls3self-hostedsystem-administrationtoolsweb-ui
FEATURE (storages): Add test for local storages
Rostislav Dugin089500fb3e7a
diff --git a/backend/assets/file_for_storage_text.txt b/backend/assets/file_for_storage_text.txtnew file mode 100644index 00000000..75c6536b--- /dev/null+++ b/backend/assets/file_for_storage_text.txt@@ -0,0 +1 @@+This is test text\ No newline at end of filediff --git a/backend/internal/config/config.go b/backend/internal/config/config.goindex 934eb82a..5957bef3 100644--- a/backend/internal/config/config.go+++ b/backend/internal/config/config.go@@ -28,6 +28,8 @@ type EnvVariables struct {DataFolder stringTempFolder string++ TestFilePath string}var (@@ -118,5 +120,7 @@ func loadEnvVariables() {env.DataFolder = filepath.Join(filepath.Dir(backendRoot), "postgresus-data", "data")env.TempFolder = filepath.Join(filepath.Dir(backendRoot), "postgresus-data", "temp")+ env.TestFilePath = filepath.Join(backendRoot, "assets", "file_for_storage_text.txt")+log.Info("Environment variables loaded successfully!")}diff --git a/backend/internal/features/storages/model_test.go b/backend/internal/features/storages/model_test.gonew file mode 100644index 00000000..5beb7a30--- /dev/null+++ b/backend/internal/features/storages/model_test.go@@ -0,0 +1,89 @@+package storages++import (+ "bytes"+ "io"+ "os"+ "postgresus-backend/internal/config"+ local_storage "postgresus-backend/internal/features/storages/storages/local"+ "testing"++ "github.com/google/uuid"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++func Test_Storage_BasicOperations(t *testing.T) {+ testCases := []struct {+ name string+ storage StorageFileSaver+ }{+ {+ name: "LocalStorage",+ storage: &local_storage.LocalStorage{StorageID: uuid.New()},+ },+ }++ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ t.Run("Test_TestConnection_ConnectionSucceeds", func(t *testing.T) {+ err := tc.storage.TestConnection()+ assert.NoError(t, err, "TestConnection should succeed")+ })++ t.Run("Test_TestValidation_ValidationSucceeds", func(t *testing.T) {+ err := tc.storage.Validate()+ assert.NoError(t, err, "Validate should succeed")+ })++ t.Run("Test_TestSaveAndGetFile_ReturnsCorrectContent", func(t *testing.T) {+ testData, err := os.ReadFile(config.GetEnv().TestFilePath)+ require.NoError(t, err, "Should be able to read test file")++ fileID := uuid.New()++ err = tc.storage.SaveFile(fileID, bytes.NewReader(testData))+ require.NoError(t, err, "SaveFile should succeed")++ file, err := tc.storage.GetFile(fileID)+ assert.NoError(t, err, "GetFile should succeed")+ defer file.Close()++ content, err := io.ReadAll(file)+ assert.NoError(t, err, "Should be able to read file")+ assert.Equal(t, testData, content, "File content should match the original")+ })++ t.Run("Test_TestDeleteFile_RemovesFileFromDisk", func(t *testing.T) {+ // Read test file+ testData, err := os.ReadFile(config.GetEnv().TestFilePath)+ require.NoError(t, err, "Should be able to read test file")++ // Generate a unique file ID+ fileID := uuid.New()++ // Save file first+ err = tc.storage.SaveFile(fileID, bytes.NewReader(testData))+ require.NoError(t, err, "SaveFile should succeed")++ // Delete file+ err = tc.storage.DeleteFile(fileID)+ assert.NoError(t, err, "DeleteFile should succeed")++ // Try to get the deleted file+ file, err := tc.storage.GetFile(fileID)+ assert.Error(t, err, "GetFile should fail for non-existent file")+ if file != nil {+ file.Close()+ }+ })++ t.Run("Test_TestDeleteNonExistentFile_DoesNotError", func(t *testing.T) {+ // Try to delete a non-existent file+ nonExistentID := uuid.New()+ err := tc.storage.DeleteFile(nonExistentID)+ assert.NoError(t, err, "DeleteFile should not error for non-existent file")+ })+ })+ }+}diff --git a/backend/internal/features/storages/storages/local/model_test.go b/backend/internal/features/storages/storages/local/model_test.gonew file mode 100644index 00000000..5d6d540e--- /dev/null+++ b/backend/internal/features/storages/storages/local/model_test.go@@ -0,0 +1,75 @@+package local_storage++import (+ "bytes"+ "io"+ "os"+ "postgresus-backend/internal/config"+ "testing"++ "github.com/google/uuid"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"+)++func Test_LocalStorage_BasicOperations(t *testing.T) {+ storage := &LocalStorage{+ StorageID: uuid.New(),+ }++ t.Run("Test_TestConnection_ConnectionSucceeds", func(t *testing.T) {+ err := storage.TestConnection()+ assert.NoError(t, err, "TestConnection should succeed")+ })++ t.Run("Test_TestValidation_ValidationSucceeds", func(t *testing.T) {+ err := storage.Validate()+ assert.NoError(t, err, "Validate should succeed")+ })++ t.Run("Test_TestSaveAndGetFile_ReturnsCorrectContent", func(t *testing.T) {+ testData, err := os.ReadFile(config.GetEnv().TestFilePath)+ require.NoError(t, err, "Should be able to read test file")++ fileID := uuid.New()++ err = storage.SaveFile(fileID, bytes.NewReader(testData))+ require.NoError(t, err, "SaveFile should succeed")++ file, err := storage.GetFile(fileID)+ assert.NoError(t, err, "GetFile should succeed")+ defer file.Close()++ content, err := io.ReadAll(file)+ assert.NoError(t, err, "Should be able to read file")+ assert.Equal(t, testData, content, "File content should match the original")+ })++ t.Run("Test_TestDeleteFile_RemovesFileFromDisk", func(t *testing.T) {+ // Read test file+ testData, err := os.ReadFile(config.GetEnv().TestFilePath)+ require.NoError(t, err, "Should be able to read test file")++ // Generate a unique file ID+ fileID := uuid.New()++ // Save file first+ err = storage.SaveFile(fileID, bytes.NewReader(testData))+ require.NoError(t, err, "SaveFile should succeed")++ // Delete file+ err = storage.DeleteFile(fileID)+ assert.NoError(t, err, "DeleteFile should succeed")++ file, err := storage.GetFile(fileID)+ assert.Error(t, err, "GetFile should fail for non-existent file")+ assert.Nil(t, file, "File should be nil")+ })++ t.Run("Test_TestDeleteNonExistentFile_DoesNotError", func(t *testing.T) {+ // Try to delete a non-existent file+ nonExistentID := uuid.New()+ err := storage.DeleteFile(nonExistentID)+ assert.NoError(t, err, "DeleteFile should not error for non-existent file")+ })+}