-
Notifications
You must be signed in to change notification settings - Fork 727
Expand file tree
/
Copy pathservice_sysv_render_test.go
More file actions
79 lines (74 loc) · 2.44 KB
/
Copy pathservice_sysv_render_test.go
File metadata and controls
79 lines (74 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//go:build linux
package service
import (
"strings"
"testing"
)
// renderSysv mirrors the data map built in (*sysv).Install and renders the
// given script template through the engine, so the render can be exercised
// without touching the filesystem.
func renderSysv(script string, c *Config, path, logDir string) (string, error) {
data := map[string]any{
"Description": c.Description,
"DisplayName": c.DisplayName,
"Name": c.Name,
"Path": path,
"Arguments": c.Arguments,
"UserName": c.UserName,
"ChRoot": c.ChRoot,
"WorkingDirectory": c.WorkingDirectory,
"LogDirectory": logDir,
"EnvVars": envVars(c.EnvVars, func(k, v string) string { return "export " + k + "=" + v }),
}
return renderTemplate(script, data, tfs)
}
func TestSysvRenderBuiltin(t *testing.T) {
c := &Config{
Name: "svc",
DisplayName: "svc display",
Description: "A test",
Arguments: []string{"-a", "with space"},
}
out, err := renderSysv(sysvScript, c, "/usr/bin/svc", "/var/log")
if err != nil {
t.Fatalf("render built-in sysv script: %v", err)
}
for _, want := range []string{"svc display", `"-a" "with space"`, "/usr/bin/svc", "/var/log"} {
if !strings.Contains(out, want) {
t.Errorf("built-in sysv script output missing %q", want)
}
}
}
// TestSysvRenderCustomScriptConfigKeys is a regression test for Name,
// UserName and ChRoot going missing from the sysv template data. Before the
// template engine change these were reachable through the embedded *Config,
// and custom SysvScript templates use them: a script rendering
// {{UserName|cmd}} into a start-stop-daemon --chuid clause silently loses
// its privilege drop when the key is absent.
func TestSysvRenderCustomScriptConfigKeys(t *testing.T) {
const custom = `#!/bin/sh
NAME={{Name|cmd}}
{{if UserName}}CHUID="--chuid {{UserName|cmd}}"{{end}}
{{if ChRoot}}CHROOT="--chroot {{ChRoot|cmd}}"{{end}}
exec {{Path}} {{range Arguments}} {{.|cmd}}{{end}}
`
c := &Config{
Name: "svc",
UserName: "svcuser",
ChRoot: "/jail",
Arguments: []string{"run"},
}
out, err := renderSysv(custom, c, "/usr/bin/svc", "/var/log")
if err != nil {
t.Fatalf("render custom sysv script: %v", err)
}
for _, want := range []string{
`NAME="svc"`,
`CHUID="--chuid "svcuser""`,
`CHROOT="--chroot "/jail""`,
} {
if !strings.Contains(out, want) {
t.Errorf("custom sysv script output missing %q\ngot:\n%s", want, out)
}
}
}