]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/triggers_test.go
Add README.adoc
[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                                 },
95                                 "/etc/rc.local": {
96                                         Path: "/etc/rc.local",
97                                         Mode: 0755,
98                                         Uid:  -1,
99                                         Gid:  -1,
100                                         Data: []byte("#!/bin/sh\n"),
101                                         TriggerCommands: []string{
102                                                 "/etc/rc.local",
103                                         },
104                                 },
105                                 "/etc/resolv.conf": {
106                                         Path: "/etc/resolv.conf",
107                                         Mode: 0644,
108                                         Uid:  -1,
109                                         Gid:  -1,
110                                         Data: []byte("nameserver ::1\n"),
111                                         TriggerCommands: []string{
112                                                 "echo resolv.conf updated",
113                                         },
114                                 },
115                                 "/etc/test": {
116                                         Path: "/etc/test",
117                                         Mode: fs.ModeSymlink | 0777,
118                                         Uid:  -1,
119                                         Gid:  -1,
120                                         Data: []byte("doesnt-exist"),
121                                 },
122                         },
123                         nil,
124                 },
125
126                 {
127                         "triggers-invalid-path",
128                         nil,
129                         fmt.Errorf("triggers-invalid-path/triggers.yaml: \"/etc/resolv.conf\" does not exist in files/"),
130                 },
131         }
132
133         for _, tc := range tests {
134                 t.Run(tc.group, func(t *testing.T) {
135                         // Use LoadFiles() so we work on real data and don't
136                         // make any mistakes generating it
137                         files, err := LoadFiles(tc.group)
138                         if err != nil {
139                                 t.Fatalf("err = %#v, want nil", err)
140                         }
141                         err = LoadTriggers(tc.group, files)
142
143                         testutil.AssertEqual(t, "res", files, tc.exp)
144                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
145                 })
146         }
147 }