]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/templates_test.go
eaceb06e107eb4a6626ae91a563839aa52dcbf27
[safcm/safcm.git] / cmd / safcm / config / templates_test.go
1 // Copyright (C) 2021-2024  Simon Ruderich
2 //
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.
7 //
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.
12 //
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/>.
15
16 package config
17
18 import (
19         "fmt"
20         "io/fs"
21         "os"
22         "testing"
23
24         "ruderich.org/simon/safcm"
25         "ruderich.org/simon/safcm/testutil"
26 )
27
28 func TestLoadTemplates(t *testing.T) {
29         cwd, err := os.Getwd()
30         if err != nil {
31                 t.Fatal(err)
32         }
33         defer os.Chdir(cwd) //nolint:errcheck
34
35         err = os.Chdir("../testdata/project")
36         if err != nil {
37                 t.Fatal(err)
38         }
39
40         allHosts, err := LoadHosts()
41         if err != nil {
42                 t.Fatal(err)
43         }
44         allGroups, err := LoadGroups(&Config{}, allHosts)
45         if err != nil {
46                 t.Fatal(err)
47         }
48         host := "host1.example.org"
49         groups, err := ResolveHostGroups(host, allGroups,
50                 []string{"detected_amd64", "detected_linux"})
51         if err != nil {
52                 t.Fatal(err)
53         }
54
55         tests := []struct {
56                 group  string
57                 exp    map[string]*safcm.File
58                 expErr error
59         }{
60
61                 {
62                         "empty",
63                         nil,
64                         nil,
65                 },
66
67                 {
68                         "group",
69                         map[string]*safcm.File{
70                                 "/": {
71                                         Path: "/",
72                                         Mode: fs.ModeDir | 0755,
73                                         Uid:  -1,
74                                         Gid:  -1,
75                                 },
76                                 "/etc": {
77                                         Path: "/etc",
78                                         Mode: fs.ModeDir | 0755,
79                                         Uid:  -1,
80                                         Gid:  -1,
81                                 },
82                                 "/etc/.hidden": {
83                                         Path: "/etc/.hidden",
84                                         Mode: 0644,
85                                         Uid:  -1,
86                                         Gid:  -1,
87                                         Data: []byte("..."),
88                                 },
89                                 "/etc/motd": {
90                                         Path: "/etc/motd",
91                                         Mode: 0644,
92                                         Uid:  -1,
93                                         Gid:  -1,
94                                         Data: []byte(`Welcome to Host ONE
95
96
97 This is GNU/Linux host
98
99
100
101
102 all
103
104
105 host1.example.org
106
107
108
109 `),
110                                 },
111                                 "/etc/rc.local": {
112                                         Path: "/etc/rc.local",
113                                         Mode: 0755,
114                                         Uid:  -1,
115                                         Gid:  -1,
116                                         Data: []byte("#!/bin/sh\n"),
117                                 },
118                                 "/etc/resolv.conf": {
119                                         Path: "/etc/resolv.conf",
120                                         Mode: 0644,
121                                         Uid:  -1,
122                                         Gid:  -1,
123                                         Data: []byte("nameserver ::1\n"),
124                                 },
125                                 "/etc/test": {
126                                         Path: "/etc/test",
127                                         Mode: fs.ModeSymlink | 0777,
128                                         Uid:  -1,
129                                         Gid:  -1,
130                                         Data: []byte("doesnt-exist"),
131                                 },
132                         },
133                         nil,
134                 },
135
136                 {
137                         "templates-invalid-group",
138                         map[string]*safcm.File{
139                                 "/": {
140                                         Path: "/",
141                                         Mode: fs.ModeDir | 0755,
142                                         Uid:  -1,
143                                         Gid:  -1,
144                                 },
145                                 "/etc": {
146                                         Path: "/etc",
147                                         Mode: fs.ModeDir | 0755,
148                                         Uid:  -1,
149                                         Gid:  -1,
150                                 },
151                                 "/etc/motd": {
152                                         Path: "/etc/motd",
153                                         Mode: 0644,
154                                         Uid:  -1,
155                                         Gid:  -1,
156                                         Data: []byte(`
157 {{if .InGroup "invalid-group"}}
158 ...
159 {{end}}
160 `),
161                                 },
162                         },
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"),
164                 },
165                 {
166                         "templates-invalid-host",
167                         map[string]*safcm.File{
168                                 "/": {
169                                         Path: "/",
170                                         Mode: fs.ModeDir | 0755,
171                                         Uid:  -1,
172                                         Gid:  -1,
173                                 },
174                                 "/etc": {
175                                         Path: "/etc",
176                                         Mode: fs.ModeDir | 0755,
177                                         Uid:  -1,
178                                         Gid:  -1,
179                                 },
180                                 "/etc/motd": {
181                                         Path: "/etc/motd",
182                                         Mode: 0644,
183                                         Uid:  -1,
184                                         Gid:  -1,
185                                         Data: []byte(`
186 {{if .IsHost "invalid-host"}}
187 ...
188 {{end}}
189 `),
190                                 },
191                         },
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"),
193                 },
194                 {
195                         "templates-invalid-path",
196                         nil,
197                         fmt.Errorf("templates-invalid-path/templates.yaml: \"/etc/motd\" does not exist in files/"),
198                 },
199                 {
200                         "templates-invalid-template",
201                         map[string]*safcm.File{
202                                 "/": {
203                                         Path: "/",
204                                         Mode: fs.ModeDir | 0755,
205                                         Uid:  -1,
206                                         Gid:  -1,
207                                 },
208                                 "/etc": {
209                                         Path: "/etc",
210                                         Mode: fs.ModeDir | 0755,
211                                         Uid:  -1,
212                                         Gid:  -1,
213                                 },
214                                 "/etc/motd": {
215                                         Path: "/etc/motd",
216                                         Mode: 0644,
217                                         Uid:  -1,
218                                         Gid:  -1,
219                                         Data: []byte("{{\n"),
220                                 },
221                         },
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"),
223                 },
224                 {
225                         "templates-invalid-type",
226                         map[string]*safcm.File{
227                                 "/": {
228                                         Path: "/",
229                                         Mode: fs.ModeDir | 0755,
230                                         Uid:  -1,
231                                         Gid:  -1,
232                                 },
233                                 "/etc": {
234                                         Path: "/etc",
235                                         Mode: fs.ModeDir | 0755,
236                                         Uid:  -1,
237                                         Gid:  -1,
238                                 },
239                                 "/etc/motd": {
240                                         Path: "/etc/motd",
241                                         Mode: 0644,
242                                         Uid:  -1,
243                                         Gid:  -1,
244                                         Data: []byte{},
245                                 },
246                         },
247                         fmt.Errorf("templates-invalid-type/templates.yaml: \"/etc\" is not a regular file"),
248                 },
249         }
250
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)
256                         if err != nil {
257                                 t.Fatalf("err = %#v, want nil", err)
258                         }
259                         err = LoadTemplates(tc.group, files,
260                                 host, groups, allHosts, allGroups)
261
262                         testutil.AssertEqual(t, "res", files, tc.exp)
263                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
264                 })
265         }
266 }