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
103 Path: "/etc/rc.local",
107 Data: []byte("#!/bin/sh\n"),
109 "/etc/resolv.conf": {
110 Path: "/etc/resolv.conf",
114 Data: []byte("nameserver ::1\n"),
118 Mode: fs.ModeSymlink | 0777,
121 Data: []byte("doesnt-exist"),
128 "templates-invalid-group",
129 map[string]*safcm.File{
132 Mode: fs.ModeDir | 0755,
138 Mode: fs.ModeDir | 0755,
148 {{if .InGroup "invalid-group"}}
154 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"),
157 "templates-invalid-host",
158 map[string]*safcm.File{
161 Mode: fs.ModeDir | 0755,
167 Mode: fs.ModeDir | 0755,
177 {{if .IsHost "invalid-host"}}
183 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"),
186 "templates-invalid-path",
188 fmt.Errorf("templates-invalid-path/templates.yaml: \"/etc/motd\" does not exist in files/"),
191 "templates-invalid-template",
192 map[string]*safcm.File{
195 Mode: fs.ModeDir | 0755,
201 Mode: fs.ModeDir | 0755,
210 Data: []byte("{{\n"),
213 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"),
216 "templates-invalid-type",
217 map[string]*safcm.File{
220 Mode: fs.ModeDir | 0755,
226 Mode: fs.ModeDir | 0755,
238 fmt.Errorf("templates-invalid-type/templates.yaml: \"/etc\" is not a regular file"),
242 for _, tc := range tests {
243 t.Run(tc.group, func(t *testing.T) {
244 // Use LoadFiles() so we work on real data and don't
245 // make any mistakes generating it
246 files, err := LoadFiles(tc.group)
248 t.Fatalf("err = %#v, want nil", err)
250 err = LoadTemplates(tc.group, files,
251 host, groups, allHosts, allGroups)
253 testutil.AssertEqual(t, "res", files, tc.exp)
254 testutil.AssertErrorEqual(t, "err", err, tc.expErr)