Skip to content

Commit

Permalink
add tests on crontab GetEntries
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Nov 3, 2024
1 parent 5355cdc commit 7a097d1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
42 changes: 42 additions & 0 deletions crond/crontab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,45 @@ func TestParseEntry(t *testing.T) {
})
}
}

func TestGetEntries(t *testing.T) {
fs := afero.NewMemMapFs()
file := "/var/spool/cron/crontabs/user"

t.Run("no crontab file", func(t *testing.T) {
crontab := NewCrontab(nil).SetFile(file).SetFs(fs)
entries, err := crontab.GetEntries()
require.NoError(t, err)
assert.Nil(t, entries)
})

t.Run("empty crontab file", func(t *testing.T) {
require.NoError(t, afero.WriteFile(fs, file, []byte(""), 0600))
crontab := NewCrontab(nil).SetFile(file).SetFs(fs)
entries, err := crontab.GetEntries()
require.NoError(t, err)
assert.Nil(t, entries)
})

t.Run("crontab with no own section", func(t *testing.T) {
require.NoError(t, afero.WriteFile(fs, file, []byte("some other content\n"), 0600))
crontab := NewCrontab(nil).SetFile(file).SetFs(fs)
entries, err := crontab.GetEntries()
require.NoError(t, err)
assert.Nil(t, entries)
})

t.Run("crontab with own section", func(t *testing.T) {
content := "some other content\n" + startMarker + "00,30 * * * *\tcd workdir && /some/bin/resticprofile --no-ansi --config config.yaml run-schedule backup@profile\n" + endMarker + "more content\n"
require.NoError(t, afero.WriteFile(fs, file, []byte(content), 0600))
crontab := NewCrontab(nil).SetFile(file).SetFs(fs)
entries, err := crontab.GetEntries()
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, "config.yaml", entries[0].configFile)
assert.Equal(t, "profile", entries[0].profileName)
assert.Equal(t, "backup", entries[0].commandName)
assert.Equal(t, "workdir", entries[0].workDir)
assert.Equal(t, "/some/bin/resticprofile --no-ansi --config config.yaml run-schedule backup@profile", entries[0].commandLine)
})
}
20 changes: 19 additions & 1 deletion examples/v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ global:
priority: low
# restic-binary: ~/fake_restic
# legacy-arguments: true
scheduler: crontab:*:/tmp/crontab

groups:
full-backup:
description: Full Backup
continue-on-error: true
profiles:
- root
- src
- self
schedules:
backup:
at: "*:*"

profiles:
default:
description: Contains default parameters like repository and password file
Expand All @@ -26,11 +33,13 @@ profiles:
copy:
password-file: key
repository: "/Volumes/RAMDisk/{{ .Profile.Name }}-copy"

space:
description: Repository contains space
initialize: false
password-file: key
repository: "/Volumes/RAMDisk/with space"

documents:
inherit: default
backup:
Expand All @@ -41,6 +50,7 @@ profiles:
tag:
- dev
- "{{ .Profile.Name }}"

root:
backup:
schedule: "*:0,15,30,45"
Expand Down Expand Up @@ -89,6 +99,7 @@ profiles:
tag:
- dev
- "{{ .Profile.Name }}"

self:
force-inactive-lock: true
initialize: true
Expand Down Expand Up @@ -123,6 +134,7 @@ profiles:
initialize: true
schedule:
- "*:45"

prom:
force-inactive-lock: true
initialize: true
Expand All @@ -142,6 +154,7 @@ profiles:
- "{{ .Profile.Name }}"
# exclude:
# - examples/private

system:
initialize: true
no-cache: true
Expand All @@ -153,6 +166,7 @@ profiles:
schedule-permission: system
forget:
schedule: "weekly"

src:
backup:
check-before: true
Expand Down Expand Up @@ -183,11 +197,13 @@ profiles:
tag:
- dev
- "{{ .Profile.Name }}"

home:
inherit: default
# cache-dir: "${TMPDIR}.restic/"
backup:
source: "${HOME}/Projects"

stdin:
backup:
stdin: true
Expand All @@ -200,6 +216,7 @@ profiles:
tag:
- dev
- "{{ .Profile.Name }}"

dropbox:
initialize: false
inherit: default
Expand All @@ -208,6 +225,7 @@ profiles:
check-before: false
no-error-on-warning: true
source: "../../../../../Dropbox"

escape:
initialize: true
inherit: default
Expand Down

0 comments on commit 7a097d1

Please sign in to comment.