Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Oct 4, 2024
1 parent 2641dc9 commit 6288717
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 23 deletions.
8 changes: 6 additions & 2 deletions examples/linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ global:
priority: low
systemd-unit-template: sample.service
prevent-sleep: false
ionice: true
ionice-class: 3
ionice-level: 7
nice: 19

default:
password-file: key
Expand Down Expand Up @@ -57,12 +61,12 @@ test2:
backup:
source: ./
schedule: "*:05,20,35,50"
schedule-permission: system
schedule-permission: user
schedule-log: backup-test2.log
run-after: "chown -R $SUDO_USER $HOME/.cache/restic /tmp/backup"
check:
schedule: "*-*-2"
schedule-permission: system
schedule-permission: user
schedule-log: check-test2.log

test3:
Expand Down
9 changes: 8 additions & 1 deletion examples/sample.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ OnFailure=unit-status-mail@%n.service
Type=notify
WorkingDirectory={{ .WorkingDirectory }}
ExecStart={{ .CommandLine }}
{{ if .Nice }}Nice={{ .Nice }}{{ end }}
{{ if .Nice }}Nice={{ .Nice }}
{{ end -}}
{{ if .CPUSchedulingPolicy }}CPUSchedulingPolicy={{ .CPUSchedulingPolicy }}
{{ end -}}
{{ if .IOSchedulingClass }}IOSchedulingClass={{ .IOSchedulingClass }}
{{ end -}}
{{ if .IOSchedulingPriority }}IOSchedulingPriority={{ .IOSchedulingPriority }}
{{ end -}}
{{ range .Environment -}}
Environment="{{ . }}"
{{ end -}}
2 changes: 1 addition & 1 deletion schedule/handler_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (h *HandlerSystemd) CreateJob(job *Config, schedules []*calendar.Event, per
}

err := systemd.Generate(systemd.Config{
CommandLine: job.Command + " --no-prio " + strings.Join(job.Arguments, " "),
CommandLine: job.Command + " " + strings.Join(append([]string{"--no-prio"}, job.Arguments...), " "),
Environment: job.Environment,
WorkingDirectory: job.WorkingDirectory,
Title: job.ProfileName,
Expand Down
34 changes: 20 additions & 14 deletions schedule/scheduler_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func NewSchedulerConfig(global *config.Global) SchedulerConfig {
} else {
return SchedulerCrond{}
}

case constants.SchedulerCrontab:
if len(resource) > 0 {
if user, location, found := strings.Cut(resource, ":"); found {
Expand All @@ -88,33 +89,38 @@ func NewSchedulerConfig(global *config.Global) SchedulerConfig {
} else {
panic(fmt.Errorf("invalid schedule %q, no crontab file was specified, expecting \"%s: filename\"", scheduler, scheduler))
}

case constants.SchedulerLaunchd:
return SchedulerLaunchd{}

case constants.SchedulerSystemd:
scheduler := SchedulerSystemd{
UnitTemplate: global.SystemdUnitTemplate,
TimerTemplate: global.SystemdTimerTemplate,
Nice: global.Nice,
}
if global.IONice {
scheduler.IONiceClass = global.IONiceClass
scheduler.IONiceLevel = global.IONiceLevel
}
return scheduler
return getSchedulerSystemdDefaultConfig(global)

case constants.SchedulerWindows:
return SchedulerWindows{}

default:
return SchedulerDefaultOS{
defaults: []SchedulerConfig{
SchedulerSystemd{
UnitTemplate: global.SystemdUnitTemplate,
TimerTemplate: global.SystemdTimerTemplate,
},
getSchedulerSystemdDefaultConfig(global),
},
}
}
}

func getSchedulerSystemdDefaultConfig(global *config.Global) SchedulerSystemd {
scheduler := SchedulerSystemd{
UnitTemplate: global.SystemdUnitTemplate,
TimerTemplate: global.SystemdTimerTemplate,
Nice: global.Nice,
}
if global.IONice {
scheduler.IONiceClass = global.IONiceClass
scheduler.IONiceLevel = global.IONiceLevel
}
return scheduler
}

var (
_ SchedulerConfig = SchedulerDefaultOS{}
_ SchedulerConfig = SchedulerCrond{}
Expand Down
51 changes: 46 additions & 5 deletions schedule/scheduler_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,53 @@ func TestOsDefaultConfig(t *testing.T) {
}

func TestSystemdConfig(t *testing.T) {
g := &config.Global{
Scheduler: constants.SchedulerSystemd,
SystemdTimerTemplate: "timer.tpl",
SystemdUnitTemplate: "unit.tpl",
testCases := []struct {
global *config.Global
expected SchedulerSystemd
}{
{
global: &config.Global{
Scheduler: constants.SchedulerSystemd,
SystemdTimerTemplate: "timer.tpl",
SystemdUnitTemplate: "unit.tpl",
},
expected: SchedulerSystemd{TimerTemplate: "timer.tpl", UnitTemplate: "unit.tpl"},
},
{
global: &config.Global{
Scheduler: constants.SchedulerSystemd,
SystemdTimerTemplate: "timer.tpl",
SystemdUnitTemplate: "unit.tpl",
IONiceClass: 3,
IONiceLevel: 5,
},
expected: SchedulerSystemd{TimerTemplate: "timer.tpl", UnitTemplate: "unit.tpl"},
},
{
global: &config.Global{
Scheduler: constants.SchedulerSystemd,
SystemdTimerTemplate: "timer.tpl",
SystemdUnitTemplate: "unit.tpl",
IONice: true,
IONiceClass: 3,
IONiceLevel: 5,
},
expected: SchedulerSystemd{TimerTemplate: "timer.tpl", UnitTemplate: "unit.tpl", IONiceClass: 3, IONiceLevel: 5},
},
{
global: &config.Global{
Scheduler: constants.SchedulerSystemd,
SystemdTimerTemplate: "timer.tpl",
SystemdUnitTemplate: "unit.tpl",
Nice: 12,
},
expected: SchedulerSystemd{TimerTemplate: "timer.tpl", UnitTemplate: "unit.tpl", Nice: 12},
},
}

for _, tc := range testCases {
assert.Equal(t, tc.expected, NewSchedulerConfig(tc.global))
}
assert.Equal(t, SchedulerSystemd{TimerTemplate: "timer.tpl", UnitTemplate: "unit.tpl"}, NewSchedulerConfig(g))
}

func TestCrondConfig(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions systemd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,22 @@ func TestGeneratePriorityFields(t *testing.T) {
contains: []string{"IOSchedulingClass=2\n", "IOSchedulingPriority=7\n"},
notContains: []string{"CPUSchedulingPolicy=", "Nice="},
},
{
config: Config{
CommandLine: "commandLine",
WorkingDirectory: "workdir",
Title: jobName,
SubTitle: jobCommand,
Schedules: []string{"daily"},
UnitType: SystemUnit,
Priority: "background",
Nice: 11,
IOSchedulingClass: 3,
IOSchedulingPriority: 7,
},
contains: []string{"CPUSchedulingPolicy=idle\n", "Nice=11\n", "IOSchedulingClass=3\n", "IOSchedulingPriority=7\n"},
notContains: []string{},
},
}

systemdDir := GetSystemDir()
Expand Down

0 comments on commit 6288717

Please sign in to comment.