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
2 changes: 1 addition & 1 deletion config/jsonschema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestJsonSchemaValidation(t *testing.T) {

extensionMatcher := regexp.MustCompile(`\.(conf|toml|yaml|json)$`)
version2Matcher := regexp.MustCompile(`"version":\s*"2`)
exclusions := regexp.MustCompile(`[\\/](rsyslogd\.conf|utf.*\.conf)$`)
exclusions := regexp.MustCompile(`[\\/](rsyslogd\.conf|utf.*\.conf|drop-in-example\.conf)$`)
testCount := 0

err := filepath.Walk("../../examples/", func(filename string, info fs.FileInfo, err error) error {
Expand Down
2 changes: 2 additions & 0 deletions examples/drop-in-example.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Service]
Environment=RESTIC_PASSWORD_FILE=%d/restic-repo-password
2 changes: 1 addition & 1 deletion examples/linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ longrun:
self:
inherit: default
status-file: /tmp/status.json
# systemd-drop-in-files: [ drop-in-example.conf ]
systemd-drop-in-files: [ drop-in-example.conf ]
backup:
extended-status: true
source: ./
Expand Down
14 changes: 14 additions & 0 deletions systemd/drop_ins.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

import (
"bufio"
"errors"
"fmt"
"io"
"io/fs"
"path/filepath"
"regexp"
"strings"
Expand All @@ -23,6 +25,18 @@
return fmt.Sprintf("%s.resticprofile.conf", strings.TrimSuffix(basename, ext))
}

func (u Unit) DropInFileExists(file string) bool {
if _, err := u.fs.Stat(file); err != nil {
if errors.Is(err, fs.ErrNotExist) {
clog.Errorf("drop-in file %q not found", file)
return false
}
clog.Error(err)
return false

Check warning on line 35 in systemd/drop_ins.go

View check run for this annotation

Codecov / codecov/patch

systemd/drop_ins.go#L34-L35

Added lines #L34 - L35 were not covered by tests
}
return true
}

func (u Unit) IsTimerDropIn(file string) bool {
if f, err := u.fs.Open(file); err == nil {
defer func() { _ = f.Close() }()
Expand Down
68 changes: 68 additions & 0 deletions systemd/drop_ins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//go:build !darwin && !windows

package systemd

import (
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDropInFileExists(t *testing.T) {
t.Parallel()

tests := []struct {
name string
file string
setupFs func() afero.Fs
expectedValue bool
}{
{
name: "file exists",
file: "/path/to/file",
setupFs: func() afero.Fs {
testFs := afero.NewMemMapFs()
_, err := testFs.Create("/path/to/file")
require.NoError(t, err)
return testFs
},
expectedValue: true,
},
{
name: "directory",
file: "/path/to/dir",
setupFs: func() afero.Fs {
testFs := afero.NewMemMapFs()
err := testFs.Mkdir("/path/to/dir", 0755)
require.NoError(t, err)
return testFs
},
expectedValue: true,
},
{
name: "file does not exist",
file: "/path/to/nonexistent",
setupFs: func() afero.Fs {
testFs := afero.NewMemMapFs()
return testFs
},
expectedValue: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
testFs := tc.setupFs()

unit := Unit{
fs: testFs,
}

result := unit.DropInFileExists(tc.file)
assert.Equal(t, tc.expectedValue, result)
})
}
}
6 changes: 4 additions & 2 deletions systemd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,11 @@ func (u Unit) Generate(config Config) error {
_ = u.fs.Chown(filePathName, u.user.Uid, u.user.Gid)
}

existingFiles := collect.All(config.DropInFiles, u.DropInFileExists)

dropIns := map[string][]string{
GetTimerFileDropInDir(config.Title, config.SubTitle): collect.All(config.DropInFiles, u.IsTimerDropIn),
GetServiceFileDropInDir(config.Title, config.SubTitle): collect.All(config.DropInFiles, collect.Not(u.IsTimerDropIn)),
GetTimerFileDropInDir(config.Title, config.SubTitle): collect.All(existingFiles, u.IsTimerDropIn),
GetServiceFileDropInDir(config.Title, config.SubTitle): collect.All(existingFiles, collect.Not(u.IsTimerDropIn)),
}
for dropInDir, dropInFiles := range dropIns {
dropInDir = filepath.Join(systemdUserDir, dropInDir)
Expand Down