1 // Copyright (C) 2021 Simon Ruderich
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
24 "ruderich.org/simon/safcm"
25 "ruderich.org/simon/safcm/testutil"
28 func TestLoadTemplates(t *testing.T) {
29 cwd, err := os.Getwd()
35 err = os.Chdir("../testdata/project")
40 allHosts, err := LoadHosts()
44 allGroups, err := LoadGroups(&Config{}, allHosts)
48 host := "host1.example.org"
49 groups, err := ResolveHostGroups(host, allGroups,
50 []string{"detected_amd64", "detected_linux"})
57 exp map[string]*safcm.File
69 map[string]*safcm.File{
72 Mode: fs.ModeDir | 0755,
78 Mode: fs.ModeDir | 0755,
94 Data: []byte(`Welcome to Host ONE
97 This is GNU/Linux host
112 Path: "/etc/rc.local",
116 Data: []byte("#!/bin/sh\n"),
118 "/etc/resolv.conf": {
119 Path: "/etc/resolv.conf",
123 Data: []byte("nameserver ::1\n"),
127 Mode: fs.ModeSymlink | 0777,
130 Data: []byte("doesnt-exist"),
137 "templates-invalid-group",
138 map[string]*safcm.File{
141 Mode: fs.ModeDir | 0755,
147 Mode: fs.ModeDir | 0755,
157 {{if .InGroup "invalid-group"}}
163 fmt.Errorf("templates-invalid-group/templates.yaml: template: templates-invalid-group/files/etc/motd:2:5: executing \"templates-invalid-group/files/etc/motd\" at <.InGroup>: error calling InGroup: group \"invalid-group\" does not exist"),
166 "templates-invalid-host",
167 map[string]*safcm.File{
170 Mode: fs.ModeDir | 0755,
176 Mode: fs.ModeDir | 0755,
186 {{if .IsHost "invalid-host"}}
192 fmt.Errorf("templates-invalid-host/templates.yaml: template: templates-invalid-host/files/etc/motd:2:5: executing \"templates-invalid-host/files/etc/motd\" at <.IsHost>: error calling IsHost: host \"invalid-host\" does not exist"),
195 "templates-invalid-path",
197 fmt.Errorf("templates-invalid-path/templates.yaml: \"/etc/motd\" does not exist in files/"),
200 "templates-invalid-template",
201 map[string]*safcm.File{
204 Mode: fs.ModeDir | 0755,
210 Mode: fs.ModeDir | 0755,
219 Data: []byte("{{\n"),
222 fmt.Errorf("templates-invalid-template/templates.yaml: invalid template: templates-invalid-template/files/etc/motd:2: unclosed action started at templates-invalid-template/files/etc/motd:1"),
225 "templates-invalid-type",
226 map[string]*safcm.File{
229 Mode: fs.ModeDir | 0755,
235 Mode: fs.ModeDir | 0755,
247 fmt.Errorf("templates-invalid-type/templates.yaml: \"/etc\" is not a regular file"),
251 for _, tc := range tests {
252 t.Run(tc.group, func(t *testing.T) {
253 // Use LoadFiles() so we work on real data and don't
254 // make any mistakes generating it
255 files, err := LoadFiles(tc.group)
257 t.Fatalf("err = %#v, want nil", err)
259 err = LoadTemplates(tc.group, files,
260 host, groups, allHosts, allGroups)
262 testutil.AssertEqual(t, "res", files, tc.exp)
263 testutil.AssertErrorEqual(t, "err", err, tc.expErr)