]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/triggers_test.go
a27ec6a50902be9d55cfe94147e148540bdd39f5
[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         "reflect"
23         "testing"
24
25         "github.com/google/go-cmp/cmp"
26
27         "ruderich.org/simon/safcm"
28 )
29
30 func TestLoadTriggers(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         tests := []struct {
43                 group  string
44                 exp    map[string]*safcm.File
45                 expErr error
46         }{
47
48                 {
49                         "empty",
50                         nil,
51                         nil,
52                 },
53
54                 {
55                         "group",
56                         map[string]*safcm.File{
57                                 "/": {
58                                         Path: "/",
59                                         Mode: fs.ModeDir | 0755,
60                                         Uid:  -1,
61                                         Gid:  -1,
62                                         TriggerCommands: []string{
63                                                 "touch /.update",
64                                         },
65                                 },
66                                 "/etc": {
67                                         Path: "/etc",
68                                         Mode: fs.ModeDir | 0755,
69                                         Uid:  -1,
70                                         Gid:  -1,
71                                 },
72                                 "/etc/.hidden": {
73                                         Path: "/etc/.hidden",
74                                         Mode: 0644,
75                                         Uid:  -1,
76                                         Gid:  -1,
77                                         Data: []byte("..."),
78                                 },
79                                 "/etc/motd": {
80                                         Path: "/etc/motd",
81                                         Mode: 0644,
82                                         Uid:  -1,
83                                         Gid:  -1,
84                                         Data: []byte(`Welcome to
85 {{- if .IsHost "host1.example.org"}} Host ONE
86 {{- else if "host2"}} Host TWO
87 {{- end}}
88
89 {{if .InGroup "detected_linux"}}
90 This is GNU/Linux host
91 {{end}}
92 {{if .InGroup "detected_freebsd"}}
93 This is FreeBSD host
94 {{end}}
95 `),
96                                 },
97                                 "/etc/rc.local": {
98                                         Path: "/etc/rc.local",
99                                         Mode: 0755,
100                                         Uid:  -1,
101                                         Gid:  -1,
102                                         Data: []byte("#!/bin/sh\n"),
103                                         TriggerCommands: []string{
104                                                 "/etc/rc.local",
105                                         },
106                                 },
107                                 "/etc/resolv.conf": {
108                                         Path: "/etc/resolv.conf",
109                                         Mode: 0644,
110                                         Uid:  -1,
111                                         Gid:  -1,
112                                         Data: []byte("nameserver ::1\n"),
113                                         TriggerCommands: []string{
114                                                 "echo resolv.conf updated",
115                                         },
116                                 },
117                                 "/etc/test": {
118                                         Path: "/etc/test",
119                                         Mode: fs.ModeSymlink | 0777,
120                                         Uid:  -1,
121                                         Gid:  -1,
122                                         Data: []byte("doesnt-exist"),
123                                 },
124                         },
125                         nil,
126                 },
127
128                 {
129                         "triggers-invalid-path",
130                         nil,
131                         fmt.Errorf("triggers-invalid-path/triggers.yaml: \"/etc/resolv.conf\" does not exist in files/"),
132                 },
133         }
134
135         for _, tc := range tests {
136                 // Use LoadFiles() so we work on real data and don't make any
137                 // mistakes generating it
138                 files, err := LoadFiles(tc.group)
139                 if err != nil {
140                         t.Fatalf("%s: err = %#v, want nil",
141                                 tc.group, err)
142                 }
143                 err = LoadTriggers(tc.group, files)
144
145                 if !reflect.DeepEqual(tc.exp, files) {
146                         t.Errorf("%s: res: %s", tc.group,
147                                 cmp.Diff(tc.exp, files))
148                 }
149                 // Ugly but the simplest way to compare errors (including nil)
150                 if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
151                         t.Errorf("%s: err = %#v, want %#v",
152                                 tc.group, err, tc.expErr)
153                 }
154         }
155 }