]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/triggers_test.go
config: permit "all" and host group in .InGroup of templates
[safcm/safcm.git] / cmd / safcm / config / triggers_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 TestLoadTriggers(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         tests := []struct {
41                 group  string
42                 exp    map[string]*safcm.File
43                 expErr error
44         }{
45
46                 {
47                         "empty",
48                         nil,
49                         nil,
50                 },
51
52                 {
53                         "group",
54                         map[string]*safcm.File{
55                                 "/": {
56                                         Path: "/",
57                                         Mode: fs.ModeDir | 0755,
58                                         Uid:  -1,
59                                         Gid:  -1,
60                                         TriggerCommands: []string{
61                                                 "touch /.update",
62                                         },
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
83 {{- if .IsHost "host1.example.org"}} Host ONE
84 {{- else if "host2"}} Host TWO
85 {{- end}}
86
87 {{if .InGroup "detected_linux"}}
88 This is GNU/Linux host
89 {{end}}
90 {{if .InGroup "detected_freebsd"}}
91 This is FreeBSD host
92 {{end}}
93
94 {{if .InGroup "all"}}
95 all
96 {{end}}
97 {{if .InGroup "host1.example.org"}}
98 host1.example.org
99 {{end}}
100 {{if .InGroup "host2"}}
101 host2
102 {{end}}
103 {{if .InGroup "host3.example.net"}}
104 host3.example.net
105 {{end}}
106 `),
107                                 },
108                                 "/etc/rc.local": {
109                                         Path: "/etc/rc.local",
110                                         Mode: 0755,
111                                         Uid:  -1,
112                                         Gid:  -1,
113                                         Data: []byte("#!/bin/sh\n"),
114                                         TriggerCommands: []string{
115                                                 "/etc/rc.local",
116                                         },
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                                         TriggerCommands: []string{
125                                                 "echo resolv.conf updated",
126                                         },
127                                 },
128                                 "/etc/test": {
129                                         Path: "/etc/test",
130                                         Mode: fs.ModeSymlink | 0777,
131                                         Uid:  -1,
132                                         Gid:  -1,
133                                         Data: []byte("doesnt-exist"),
134                                 },
135                         },
136                         nil,
137                 },
138
139                 {
140                         "triggers-invalid-path",
141                         nil,
142                         fmt.Errorf("triggers-invalid-path/triggers.yaml: \"/etc/resolv.conf\" does not exist in files/"),
143                 },
144         }
145
146         for _, tc := range tests {
147                 t.Run(tc.group, func(t *testing.T) {
148                         // Use LoadFiles() so we work on real data and don't
149                         // make any mistakes generating it
150                         files, err := LoadFiles(tc.group)
151                         if err != nil {
152                                 t.Fatalf("err = %#v, want nil", err)
153                         }
154                         err = LoadTriggers(tc.group, files)
155
156                         testutil.AssertEqual(t, "res", files, tc.exp)
157                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
158                 })
159         }
160 }