]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/templates_test.go
tests: add and use testutil package to reduce duplication
[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         "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)
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                                 "/etc/rc.local": {
103                                         Path: "/etc/rc.local",
104                                         Mode: 0755,
105                                         Uid:  -1,
106                                         Gid:  -1,
107                                         Data: []byte("#!/bin/sh\n"),
108                                 },
109                                 "/etc/resolv.conf": {
110                                         Path: "/etc/resolv.conf",
111                                         Mode: 0644,
112                                         Uid:  -1,
113                                         Gid:  -1,
114                                         Data: []byte("nameserver ::1\n"),
115                                 },
116                                 "/etc/test": {
117                                         Path: "/etc/test",
118                                         Mode: fs.ModeSymlink | 0777,
119                                         Uid:  -1,
120                                         Gid:  -1,
121                                         Data: []byte("doesnt-exist"),
122                                 },
123                         },
124                         nil,
125                 },
126
127                 {
128                         "templates-invalid-group",
129                         map[string]*safcm.File{
130                                 "/": {
131                                         Path: "/",
132                                         Mode: fs.ModeDir | 0755,
133                                         Uid:  -1,
134                                         Gid:  -1,
135                                 },
136                                 "/etc": {
137                                         Path: "/etc",
138                                         Mode: fs.ModeDir | 0755,
139                                         Uid:  -1,
140                                         Gid:  -1,
141                                 },
142                                 "/etc/motd": {
143                                         Path: "/etc/motd",
144                                         Mode: 0644,
145                                         Uid:  -1,
146                                         Gid:  -1,
147                                         Data: []byte(`
148 {{if .InGroup "invalid-group"}}
149 ...
150 {{end}}
151 `),
152                                 },
153                         },
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"),
155                 },
156                 {
157                         "templates-invalid-host",
158                         map[string]*safcm.File{
159                                 "/": {
160                                         Path: "/",
161                                         Mode: fs.ModeDir | 0755,
162                                         Uid:  -1,
163                                         Gid:  -1,
164                                 },
165                                 "/etc": {
166                                         Path: "/etc",
167                                         Mode: fs.ModeDir | 0755,
168                                         Uid:  -1,
169                                         Gid:  -1,
170                                 },
171                                 "/etc/motd": {
172                                         Path: "/etc/motd",
173                                         Mode: 0644,
174                                         Uid:  -1,
175                                         Gid:  -1,
176                                         Data: []byte(`
177 {{if .IsHost "invalid-host"}}
178 ...
179 {{end}}
180 `),
181                                 },
182                         },
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"),
184                 },
185                 {
186                         "templates-invalid-path",
187                         nil,
188                         fmt.Errorf("templates-invalid-path/templates.yaml: \"/etc/motd\" does not exist in files/"),
189                 },
190                 {
191                         "templates-invalid-template",
192                         map[string]*safcm.File{
193                                 "/": {
194                                         Path: "/",
195                                         Mode: fs.ModeDir | 0755,
196                                         Uid:  -1,
197                                         Gid:  -1,
198                                 },
199                                 "/etc": {
200                                         Path: "/etc",
201                                         Mode: fs.ModeDir | 0755,
202                                         Uid:  -1,
203                                         Gid:  -1,
204                                 },
205                                 "/etc/motd": {
206                                         Path: "/etc/motd",
207                                         Mode: 0644,
208                                         Uid:  -1,
209                                         Gid:  -1,
210                                         Data: []byte("{{\n"),
211                                 },
212                         },
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"),
214                 },
215                 {
216                         "templates-invalid-type",
217                         map[string]*safcm.File{
218                                 "/": {
219                                         Path: "/",
220                                         Mode: fs.ModeDir | 0755,
221                                         Uid:  -1,
222                                         Gid:  -1,
223                                 },
224                                 "/etc": {
225                                         Path: "/etc",
226                                         Mode: fs.ModeDir | 0755,
227                                         Uid:  -1,
228                                         Gid:  -1,
229                                 },
230                                 "/etc/motd": {
231                                         Path: "/etc/motd",
232                                         Mode: 0644,
233                                         Uid:  -1,
234                                         Gid:  -1,
235                                         Data: []byte{},
236                                 },
237                         },
238                         fmt.Errorf("templates-invalid-type/templates.yaml: \"/etc\" is not a regular file"),
239                 },
240         }
241
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)
247                         if err != nil {
248                                 t.Fatalf("err = %#v, want nil", err)
249                         }
250                         err = LoadTemplates(tc.group, files,
251                                 host, groups, allHosts, allGroups)
252
253                         testutil.AssertEqual(t, "res", files, tc.exp)
254                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
255                 })
256         }
257 }