Skip to content

Commit c3add93

Browse files
ignore missing drop-in files to avoid creating an empty configuration (#502)
* ignore missing drop-in files to avoid creating an empty configuration * add unit tests
1 parent 88fcedb commit c3add93

6 files changed

Lines changed: 90 additions & 4 deletions

File tree

config/jsonschema/schema_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func TestJsonSchemaValidation(t *testing.T) {
147147

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

153153
err := filepath.Walk("../../examples/", func(filename string, info fs.FileInfo, err error) error {

examples/drop-in-example.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[Service]
2+
Environment=RESTIC_PASSWORD_FILE=%d/restic-repo-password

examples/linux.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ longrun:
9898
self:
9999
inherit: default
100100
status-file: /tmp/status.json
101-
# systemd-drop-in-files: [ drop-in-example.conf ]
101+
systemd-drop-in-files: [ drop-in-example.conf ]
102102
backup:
103103
extended-status: true
104104
source: ./

systemd/drop_ins.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ package systemd
44

55
import (
66
"bufio"
7+
"errors"
78
"fmt"
89
"io"
10+
"io/fs"
911
"path/filepath"
1012
"regexp"
1113
"strings"
@@ -23,6 +25,18 @@ func getOwnedName(basename string) string {
2325
return fmt.Sprintf("%s.resticprofile.conf", strings.TrimSuffix(basename, ext))
2426
}
2527

28+
func (u Unit) DropInFileExists(file string) bool {
29+
if _, err := u.fs.Stat(file); err != nil {
30+
if errors.Is(err, fs.ErrNotExist) {
31+
clog.Errorf("drop-in file %q not found", file)
32+
return false
33+
}
34+
clog.Error(err)
35+
return false
36+
}
37+
return true
38+
}
39+
2640
func (u Unit) IsTimerDropIn(file string) bool {
2741
if f, err := u.fs.Open(file); err == nil {
2842
defer func() { _ = f.Close() }()

systemd/drop_ins_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//go:build !darwin && !windows
2+
3+
package systemd
4+
5+
import (
6+
"testing"
7+
8+
"github.com/spf13/afero"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestDropInFileExists(t *testing.T) {
14+
t.Parallel()
15+
16+
tests := []struct {
17+
name string
18+
file string
19+
setupFs func() afero.Fs
20+
expectedValue bool
21+
}{
22+
{
23+
name: "file exists",
24+
file: "/path/to/file",
25+
setupFs: func() afero.Fs {
26+
testFs := afero.NewMemMapFs()
27+
_, err := testFs.Create("/path/to/file")
28+
require.NoError(t, err)
29+
return testFs
30+
},
31+
expectedValue: true,
32+
},
33+
{
34+
name: "directory",
35+
file: "/path/to/dir",
36+
setupFs: func() afero.Fs {
37+
testFs := afero.NewMemMapFs()
38+
err := testFs.Mkdir("/path/to/dir", 0755)
39+
require.NoError(t, err)
40+
return testFs
41+
},
42+
expectedValue: true,
43+
},
44+
{
45+
name: "file does not exist",
46+
file: "/path/to/nonexistent",
47+
setupFs: func() afero.Fs {
48+
testFs := afero.NewMemMapFs()
49+
return testFs
50+
},
51+
expectedValue: false,
52+
},
53+
}
54+
55+
for _, tc := range tests {
56+
t.Run(tc.name, func(t *testing.T) {
57+
t.Parallel()
58+
testFs := tc.setupFs()
59+
60+
unit := Unit{
61+
fs: testFs,
62+
}
63+
64+
result := unit.DropInFileExists(tc.file)
65+
assert.Equal(t, tc.expectedValue, result)
66+
})
67+
}
68+
}

systemd/generate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,11 @@ func (u Unit) Generate(config Config) error {
220220
_ = u.fs.Chown(filePathName, u.user.Uid, u.user.Gid)
221221
}
222222

223+
existingFiles := collect.All(config.DropInFiles, u.DropInFileExists)
224+
223225
dropIns := map[string][]string{
224-
GetTimerFileDropInDir(config.Title, config.SubTitle): collect.All(config.DropInFiles, u.IsTimerDropIn),
225-
GetServiceFileDropInDir(config.Title, config.SubTitle): collect.All(config.DropInFiles, collect.Not(u.IsTimerDropIn)),
226+
GetTimerFileDropInDir(config.Title, config.SubTitle): collect.All(existingFiles, u.IsTimerDropIn),
227+
GetServiceFileDropInDir(config.Title, config.SubTitle): collect.All(existingFiles, collect.Not(u.IsTimerDropIn)),
226228
}
227229
for dropInDir, dropInFiles := range dropIns {
228230
dropInDir = filepath.Join(systemdUserDir, dropInDir)

0 commit comments

Comments
 (0)