Skip to content

Commit 6370510

Browse files
committed
update tests for systemd
1 parent 2baf748 commit 6370510

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

pkg/systemd/systemd_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package systemd
33
import (
44
"strings"
55
"testing"
6+
"text/template"
67
)
78

89
func TestRenderTemplate(t *testing.T) {
@@ -106,6 +107,34 @@ func TestRenderTemplate(t *testing.T) {
106107
}
107108
})
108109
}
110+
111+
// Test case for template execution failure
112+
t.Run("Template execution failure", func(t *testing.T) {
113+
// Save the original template
114+
originalTemplate := parsedTemplate
115+
116+
// Replace the template with one that will cause an execution error
117+
parsedTemplate, _ = template.New("errorTemplate").Parse("{{.NonexistentField}}")
118+
119+
fields := &Fields{
120+
ServiceID: "testservice",
121+
Script: "testscript",
122+
TagPrefix: "testprefix",
123+
Interval: "testinterval",
124+
User: "testuser",
125+
Group: "testgroup",
126+
}
127+
128+
_, err := RenderTemplate(fields)
129+
if err == nil {
130+
t.Errorf("RenderTemplate() expected error, got nil")
131+
} else if !strings.Contains(err.Error(), "failed to execute template") {
132+
t.Errorf("RenderTemplate() error = %v, expected 'failed to execute template' error", err)
133+
}
134+
135+
// Restore the original template
136+
parsedTemplate = originalTemplate
137+
})
109138
}
110139

111140
func TestValidateFields(t *testing.T) {

0 commit comments

Comments
 (0)