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