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/>.
25 "github.com/google/go-cmp/cmp"
27 "ruderich.org/simon/safcm"
30 func TestLoadTemplates(t *testing.T) {
31 cwd, err := os.Getwd()
37 err = os.Chdir("../testdata/project")
42 allHosts, err := LoadHosts()
46 allGroups, err := LoadGroups(&Config{}, allHosts)
50 host := "host1.example.org"
51 groups, err := ResolveHostGroups(host, allGroups,
52 []string{"detected_amd64", "detected_linux"})
59 exp map[string]*safcm.File
71 map[string]*safcm.File{
74 Mode: fs.ModeDir | 0755,
80 Mode: fs.ModeDir | 0755,
96 Data: []byte(`Welcome to Host ONE
99 This is GNU/Linux host
105 Path: "/etc/rc.local",
109 Data: []byte("#!/bin/sh\n"),
111 "/etc/resolv.conf": {
112 Path: "/etc/resolv.conf",
116 Data: []byte("nameserver ::1\n"),
120 Mode: fs.ModeSymlink | 0777,
123 Data: []byte("doesnt-exist"),
130 "templates-invalid-group",
131 map[string]*safcm.File{
134 Mode: fs.ModeDir | 0755,
140 Mode: fs.ModeDir | 0755,
150 {{if .InGroup "invalid-group"}}
156 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"),
159 "templates-invalid-host",
160 map[string]*safcm.File{
163 Mode: fs.ModeDir | 0755,
169 Mode: fs.ModeDir | 0755,
179 {{if .IsHost "invalid-host"}}
185 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"),
188 "templates-invalid-path",
190 fmt.Errorf("templates-invalid-path/templates.yaml: \"/etc/motd\" does not exist in files/"),
193 "templates-invalid-template",
194 map[string]*safcm.File{
197 Mode: fs.ModeDir | 0755,
203 Mode: fs.ModeDir | 0755,
212 Data: []byte("{{\n"),
215 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"),
218 "templates-invalid-type",
219 map[string]*safcm.File{
222 Mode: fs.ModeDir | 0755,
228 Mode: fs.ModeDir | 0755,
240 fmt.Errorf("templates-invalid-type/templates.yaml: \"/etc\" is not a regular file"),
244 for _, tc := range tests {
245 // Use LoadFiles() so we work on real data and don't make any
246 // mistakes generating it
247 files, err := LoadFiles(tc.group)
249 t.Fatalf("%s: err = %#v, want nil",
252 err = LoadTemplates(tc.group, files,
253 host, groups, allHosts, allGroups)
255 if !reflect.DeepEqual(tc.exp, files) {
256 t.Errorf("%s: res: %s", tc.group,
257 cmp.Diff(tc.exp, files))
259 // Ugly but the simplest way to compare errors (including nil)
260 if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
261 t.Errorf("%s: err = %#v, want %#v",
262 tc.group, err, tc.expErr)