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