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
16 changes: 8 additions & 8 deletions backup/internal/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

var execCommand = exec.Command

func buildRsyncCmd(job Job, simulate bool) *exec.Cmd {
args := []string{"-aiv", "--info=progress2"}
if job.Delete == nil || *job.Delete {
Expand All @@ -19,7 +21,7 @@ func buildRsyncCmd(job Job, simulate bool) *exec.Cmd {
if simulate {
args = append([]string{"--dry-run"}, args...)
}
return exec.Command("rsync", args...)
return execCommand("rsync", args...)
}

func ExecuteJob(job Job, simulate bool, logger *log.Logger) string {
Expand All @@ -31,13 +33,11 @@ func ExecuteJob(job Job, simulate bool, logger *log.Logger) string {
cmd := buildRsyncCmd(job, simulate)
fmt.Printf("Job: %s\n", job.Name)
fmt.Printf("Command: %s\n", strings.Join(cmd.Args, " "))
if !simulate {
out, err := cmd.CombinedOutput()
if err != nil {
logger.Printf("ERROR [%s]: %v\nOutput: %s", job.Name, err, string(out))
return "FAILURE"
}
logger.Printf("SUCCESS [%s]: %s", job.Name, string(out))
out, err := cmd.CombinedOutput()
if err != nil {
logger.Printf("ERROR [%s]: %v\nOutput: %s", job.Name, err, string(out))
return "FAILURE"
}
logger.Printf("SUCCESS [%s]: %s", job.Name, string(out))
return "SUCCESS"
}
65 changes: 54 additions & 11 deletions backup/internal/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"bytes"
"log"
"os/exec"
"strings"
"testing"
)
Expand All @@ -12,15 +13,36 @@ func boolPtr(b bool) *bool {
return &b
}

var capturedArgs []string

var mockExecCommand = func(name string, args ...string) *exec.Cmd {
capturedArgs = args // Capture arguments for assertions
if name == "rsync" {
if strings.Contains(strings.Join(args, " "), "/invalid/source/path") {
cmd := exec.Command("false") // Simulate failure for invalid paths
cmd.Args = append([]string{name}, args...)
return cmd
}
cmd := exec.Command("echo")
cmd.Args = append([]string{name}, args...)
return cmd
}
return exec.Command(name, args...)
}

func init() {
execCommand = mockExecCommand
}

func TestBuildRsyncCmd(t *testing.T) {
job := Job{
Source: "/home/user/Music/",
Target: "/target/user/music/home",
Delete: nil,
Exclusions: []string{"*.tmp", "node_modules/"},
}
dryRun := true
cmd := buildRsyncCmd(job, dryRun)
simulate := true
cmd := buildRsyncCmd(job, simulate)

expectedArgs := []string{
"rsync", "--dry-run", "-aiv", "--info=progress2", "--delete",
Expand All @@ -41,10 +63,10 @@ func TestExecuteJob(t *testing.T) {
Delete: nil,
Exclusions: []string{"*.tmp"},
}
dryRun := true
simulate := true
logger := log.New(&bytes.Buffer{}, "", log.LstdFlags)

status := ExecuteJob(job, dryRun, logger)
status := ExecuteJob(job, simulate, logger)
if status != "SUCCESS" {
t.Errorf("Expected status SUCCESS, got %s", status)
}
Expand All @@ -56,7 +78,7 @@ func TestExecuteJob(t *testing.T) {
Enabled: boolPtr(false),
}

status = ExecuteJob(disabledJob, dryRun, logger)
status = ExecuteJob(disabledJob, simulate, logger)
if status != "SKIPPED" {
t.Errorf("Expected status SKIPPED, got %s", status)
}
Expand All @@ -81,10 +103,10 @@ func TestJobSkippedEnabledTrue(t *testing.T) {
Target: "/mnt/backup1/test/",
Enabled: boolPtr(true),
}
dryRun := true
simulate := true
logger := log.New(&bytes.Buffer{}, "", log.LstdFlags)

status := ExecuteJob(job, dryRun, logger)
status := ExecuteJob(job, simulate, logger)
if status != "SUCCESS" {
t.Errorf("Expected status SUCCESS, got %s", status)
}
Expand All @@ -97,10 +119,10 @@ func TestJobSkippedEnabledFalse(t *testing.T) {
Target: "/mnt/backup1/disabled/",
Enabled: boolPtr(false),
}
dryRun := true
simulate := true
logger := log.New(&bytes.Buffer{}, "", log.LstdFlags)

status := ExecuteJob(disabledJob, dryRun, logger)
status := ExecuteJob(disabledJob, simulate, logger)
if status != "SKIPPED" {
t.Errorf("Expected status SKIPPED, got %s", status)
}
Expand All @@ -112,11 +134,32 @@ func TestJobSkippedEnabledOmitted(t *testing.T) {
Source: "/home/omitted/",
Target: "/mnt/backup1/omitted/",
}
dryRun := true
simulate := true
logger := log.New(&bytes.Buffer{}, "", log.LstdFlags)

status := ExecuteJob(job, dryRun, logger)
status := ExecuteJob(job, simulate, logger)
if status != "SUCCESS" {
t.Errorf("Expected status SUCCESS, got %s", status)
}
}

func TestExecuteJobWithMockedRsync(t *testing.T) {
// Reset capturedArgs before the test
capturedArgs = nil

job := Job{
Name: "test_job",
Source: "/home/test/",
Target: "/mnt/backup1/test/",
Delete: nil,
Exclusions: []string{"*.tmp"},
}
simulate := true
logger := log.New(&bytes.Buffer{}, "", log.LstdFlags)

_ = ExecuteJob(job, simulate, logger)

if len(capturedArgs) == 0 || capturedArgs[0] != "--dry-run" {
t.Errorf("Expected --dry-run flag, got %v", capturedArgs)
}
}