diff --git a/backup/internal/config.go b/backup/internal/config.go index 1e8effd..8c8f1e9 100644 --- a/backup/internal/config.go +++ b/backup/internal/config.go @@ -72,7 +72,7 @@ func SubstituteVariables(input string, variables map[string]string) string { return input } -func resolveConfig(cfg Config) Config { +func ResolveConfig(cfg Config) Config { resolvedCfg := cfg for i, job := range resolvedCfg.Jobs { resolvedCfg.Jobs[i].Source = SubstituteVariables(job.Source, cfg.Variables) @@ -191,7 +191,7 @@ func LoadResolvedConfig(configPath string) Config { log.Fatalf("Job validation failed: %v", err) } - resolvedCfg := resolveConfig(cfg) + resolvedCfg := ResolveConfig(cfg) err = ValidatePaths(resolvedCfg) if err != nil { diff --git a/backup/internal/helper.go b/backup/internal/helper.go index ad7f839..ce5d0da 100644 --- a/backup/internal/helper.go +++ b/backup/internal/helper.go @@ -15,13 +15,6 @@ type Path struct { Exclusions []string `yaml:"exclusions"` } -func GetConfigTitle(configPath string) string { - filename := filepath.Base(configPath) - filename = strings.TrimSuffix(filename, ".yaml") - - return filename -} - func NormalizePath(path string) string { return strings.TrimSuffix(strings.ReplaceAll(path, "//", "/"), "/") } @@ -29,25 +22,27 @@ func NormalizePath(path string) string { const LogFilePermission = 0644 const LogDirPermission = 0755 -func getLogPath(simulate bool, title string) string { - logPath := "logs/sync-" + time.Now().Format("2006-01-02T15-04-05") + "-" + GetConfigTitle(title) +func getLogPath(simulate bool, configPath string) string { + filename := filepath.Base(configPath) + filename = strings.TrimSuffix(filename, ".yaml") + logPath := "logs/sync-" + time.Now().Format("2006-01-02T15-04-05") + "-" + filename + if simulate { logPath += "-sim" } - err := os.MkdirAll(logPath, LogDirPermission) - if err != nil { - log.Fatalf("Failed to create log directory: %v", err) - } - return logPath } func CreateMainLogger(configPath string, simulate bool) (*log.Logger, string) { logPath := getLogPath(simulate, configPath) - overallLogPath := logPath + "/summary.log" + err := os.MkdirAll(logPath, LogDirPermission) + if err != nil { + log.Fatalf("Failed to create log directory: %v", err) + } + overallLogFile, err := os.OpenFile(overallLogPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, LogFilePermission) if err != nil { log.Fatalf("Failed to open overall log file: %v", err) diff --git a/backup/internal/test/config_test.go b/backup/internal/test/config_test.go index a75457e..a3ae015 100644 --- a/backup/internal/test/config_test.go +++ b/backup/internal/test/config_test.go @@ -332,3 +332,31 @@ func TestConfigString_ValidConfig(t *testing.T) { assert.Equal(t, expectedOutput, actualOutput) } + +func TestResolveConfig(t *testing.T) { + cfg := Config{ + Variables: map[string]string{ + "source_base": "/home/user", + "target_base": "/backup/user", + }, + Jobs: []Job{ + { + Name: "job1", + Source: "${source_base}/Documents", + Target: "${target_base}/Documents", + }, + { + Name: "job2", + Source: "${source_base}/Pictures", + Target: "${target_base}/Pictures", + }, + }, + } + + resolvedCfg := ResolveConfig(cfg) + + assert.Equal(t, "/home/user/Documents", resolvedCfg.Jobs[0].Source) + assert.Equal(t, "/backup/user/Documents", resolvedCfg.Jobs[0].Target) + assert.Equal(t, "/home/user/Pictures", resolvedCfg.Jobs[1].Source) + assert.Equal(t, "/backup/user/Pictures", resolvedCfg.Jobs[1].Target) +} diff --git a/backup/internal/test/helper_test.go b/backup/internal/test/helper_test.go index 03a7aa2..906bce4 100644 --- a/backup/internal/test/helper_test.go +++ b/backup/internal/test/helper_test.go @@ -28,17 +28,31 @@ func TestNormalizePath(t *testing.T) { func TestCreateMainLogger_Title_IsPresent(t *testing.T) { logger, logPath := CreateMainLogger("title", true) assert.Contains(t, logPath, "title") - assert.NotNil(t, logger, "Logger should not be nil") + assert.NotNil(t, logger) } func TestCreateMainLogger_IsSimulate_HasSimSuffix(t *testing.T) { logger, logPath := CreateMainLogger("", true) assert.Contains(t, logPath, "-sim") - assert.NotNil(t, logger, "Logger should not be nil") + assert.NotNil(t, logger) } func TestCreateMainLogger_NotSimulate_HasNoSimSuffix(t *testing.T) { logger, logPath := CreateMainLogger("", false) assert.NotContains(t, logPath, "-sim") - assert.NotNil(t, logger, "Logger should not be nil") + assert.NotNil(t, logger) +} + +func TestCreateLogPath_IsSimulate_ContainsTimestamp(t *testing.T) { + _, logPath := CreateMainLogger("", true) + // Check if the logPath contains a timestamp in the format '2006-01-02T15-04-05' + timestampRegex := `\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}` + assert.Regexp(t, timestampRegex, logPath) +} + +func TestCreateLogPath_NotSimulate_ContainsTimestamp(t *testing.T) { + _, logPath := CreateMainLogger("", false) + // Check if the logPath contains a timestamp in the format '2006-01-02T15-04-05' + timestampRegex := `\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}` + assert.Regexp(t, timestampRegex, logPath) } diff --git a/backup/internal/test/rsync_test.go b/backup/internal/test/rsync_test.go index c06e0d8..249864d 100644 --- a/backup/internal/test/rsync_test.go +++ b/backup/internal/test/rsync_test.go @@ -33,6 +33,25 @@ func TestArgumentsForJob(t *testing.T) { assert.Equal(t, strings.Join(expectedArgs, " "), strings.Join(args, " ")) } +func TestArgumentsForJob_WithLogPath_(t *testing.T) { + job := Job{ + Delete: false, + Source: "/home/user/Documents/", + Target: "/backup/user/documents", + Exclusions: []string{"*.log", "temp/"}, + } + args := ArgumentsForJob(job, "/var/log/rsync.log", false) + + expectedArgs := []string{ + "-aiv", "--stats", + "--log-file=/var/log/rsync.log", + "--exclude=*.log", "--exclude=temp/", + "/home/user/Documents/", "/backup/user/documents", + } + + assert.Equal(t, strings.Join(expectedArgs, " "), strings.Join(args, " ")) +} + func TestGetVersionInfo_Success(t *testing.T) { mockExec := NewMockExec(t) rsync := SharedCommand{