Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backup/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 10 additions & 15 deletions backup/internal/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,34 @@ 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, "//", "/"), "/")
}

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)
Expand Down
28 changes: 28 additions & 0 deletions backup/internal/test/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
20 changes: 17 additions & 3 deletions backup/internal/test/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
19 changes: 19 additions & 0 deletions backup/internal/test/rsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading