]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/files_test.go
config: permit "all" and host group in .InGroup of templates
[safcm/safcm.git] / cmd / safcm / config / files_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         "syscall"
23         "testing"
24
25         "ruderich.org/simon/safcm"
26         "ruderich.org/simon/safcm/testutil"
27 )
28
29 func chmod(name string, perm int) {
30         err := os.Chmod(name, FullPermToFileMode(perm))
31         if err != nil {
32                 panic(err)
33         }
34 }
35
36 func TestLoadFiles(t *testing.T) {
37         cwd, err := os.Getwd()
38         if err != nil {
39                 t.Fatal(err)
40         }
41         defer os.Chdir(cwd)
42
43         err = os.Chdir("../testdata/project")
44         if err != nil {
45                 t.Fatal(err)
46         }
47
48         chmod("files-invalid-perm-dir/files", 0500)
49         defer chmod("files-invalid-perm-dir/files", 0700)
50         chmod("files-invalid-perm-dir/files/etc/", 0755)
51         chmod("files-invalid-perm-dir/files/etc/resolv.conf", 0644)
52         chmod("files-invalid-perm-dir-setgid/files", 0755)
53         chmod("files-invalid-perm-dir-setgid/files/etc/", 02755)
54         chmod("files-invalid-perm-dir-setgid/files/etc/resolv.conf", 0644)
55         chmod("files-invalid-perm-file/files", 0755)
56         chmod("files-invalid-perm-file/files/etc/", 0755)
57         chmod("files-invalid-perm-file/files/etc/resolv.conf", 0600)
58         chmod("files-invalid-perm-file-executable/files", 0755)
59         chmod("files-invalid-perm-file-executable/files/etc", 0755)
60         chmod("files-invalid-perm-file-executable/files/etc/rc.local", 0750)
61         chmod("files-invalid-perm-file-sticky/files", 0755)
62         chmod("files-invalid-perm-file-sticky/files/etc", 0755)
63         chmod("files-invalid-perm-file-sticky/files/etc/resolv.conf", 01644)
64
65         err = syscall.Mkfifo("files-invalid-type/files/invalid", 0644)
66         if err != nil {
67                 t.Fatal(err)
68         }
69         defer os.Remove("files-invalid-type/files/invalid")
70
71         const errMsg = `
72 The actual permissions and user/group of files and directories are not used
73 (except for +x on files). 0644/0755 and current remote user/group is used per
74 default. Apply different file permissions via permissions.yaml. To prevent
75 confusion files must be manually chmodded 0644/0755 and directories 0755 or
76 via "safcm fixperms".
77 `
78
79         tests := []struct {
80                 group  string
81                 exp    map[string]*safcm.File
82                 expErr error
83         }{
84
85                 {
86                         "empty",
87                         nil,
88                         nil,
89                 },
90
91                 {
92                         "group",
93                         map[string]*safcm.File{
94                                 "/": {
95                                         Path: "/",
96                                         Mode: fs.ModeDir | 0755,
97                                         Uid:  -1,
98                                         Gid:  -1,
99                                 },
100                                 "/etc": {
101                                         Path: "/etc",
102                                         Mode: fs.ModeDir | 0755,
103                                         Uid:  -1,
104                                         Gid:  -1,
105                                 },
106                                 "/etc/.hidden": {
107                                         Path: "/etc/.hidden",
108                                         Mode: 0644,
109                                         Uid:  -1,
110                                         Gid:  -1,
111                                         Data: []byte("..."),
112                                 },
113                                 "/etc/motd": {
114                                         Path: "/etc/motd",
115                                         Mode: 0644,
116                                         Uid:  -1,
117                                         Gid:  -1,
118                                         Data: []byte(`Welcome to
119 {{- if .IsHost "host1.example.org"}} Host ONE
120 {{- else if "host2"}} Host TWO
121 {{- end}}
122
123 {{if .InGroup "detected_linux"}}
124 This is GNU/Linux host
125 {{end}}
126 {{if .InGroup "detected_freebsd"}}
127 This is FreeBSD host
128 {{end}}
129
130 {{if .InGroup "all"}}
131 all
132 {{end}}
133 {{if .InGroup "host1.example.org"}}
134 host1.example.org
135 {{end}}
136 {{if .InGroup "host2"}}
137 host2
138 {{end}}
139 {{if .InGroup "host3.example.net"}}
140 host3.example.net
141 {{end}}
142 `),
143                                 },
144                                 "/etc/rc.local": {
145                                         Path: "/etc/rc.local",
146                                         Mode: 0755,
147                                         Uid:  -1,
148                                         Gid:  -1,
149                                         Data: []byte("#!/bin/sh\n"),
150                                 },
151                                 "/etc/resolv.conf": {
152                                         Path: "/etc/resolv.conf",
153                                         Mode: 0644,
154                                         Uid:  -1,
155                                         Gid:  -1,
156                                         Data: []byte("nameserver ::1\n"),
157                                 },
158                                 "/etc/test": {
159                                         Path: "/etc/test",
160                                         Mode: fs.ModeSymlink | 0777,
161                                         Uid:  -1,
162                                         Gid:  -1,
163                                         Data: []byte("doesnt-exist"),
164                                 },
165                         },
166                         nil,
167                 },
168
169                 {
170                         "files-invalid-type",
171                         nil,
172                         fmt.Errorf("files-invalid-type: \"files-invalid-type/files/invalid\": file type not supported"),
173                 },
174                 {
175                         "files-invalid-perm-dir",
176                         nil,
177                         fmt.Errorf("files-invalid-perm-dir: \"files-invalid-perm-dir/files\": invalid permissions 0500" + errMsg),
178                 },
179                 {
180                         "files-invalid-perm-dir-setgid",
181                         nil,
182                         fmt.Errorf("files-invalid-perm-dir-setgid: \"files-invalid-perm-dir-setgid/files/etc\": invalid permissions 02755" + errMsg),
183                 },
184                 {
185                         "files-invalid-perm-file",
186                         nil,
187                         fmt.Errorf("files-invalid-perm-file: \"files-invalid-perm-file/files/etc/resolv.conf\": invalid permissions 0600" + errMsg),
188                 },
189                 {
190                         "files-invalid-perm-file-executable",
191                         nil,
192                         fmt.Errorf("files-invalid-perm-file-executable: \"files-invalid-perm-file-executable/files/etc/rc.local\": invalid permissions 0750" + errMsg),
193                 },
194                 {
195                         "files-invalid-perm-file-sticky",
196                         nil,
197                         fmt.Errorf("files-invalid-perm-file-sticky: \"files-invalid-perm-file-sticky/files/etc/resolv.conf\": invalid permissions 01644" + errMsg),
198                 },
199         }
200
201         for _, tc := range tests {
202                 t.Run(tc.group, func(t *testing.T) {
203                         res, err := LoadFiles(tc.group)
204                         testutil.AssertEqual(t, "res", res, tc.exp)
205                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
206                 })
207         }
208 }