1 // Copyright (C) 2021 Simon Ruderich
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.
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.
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/>.
25 "github.com/google/go-cmp/cmp"
27 "ruderich.org/simon/safcm"
30 func TestLoadTriggers(t *testing.T) {
31 cwd, err := os.Getwd()
37 err = os.Chdir("../testdata/project")
44 exp map[string]*safcm.File
56 map[string]*safcm.File{
59 Mode: fs.ModeDir | 0755,
62 TriggerCommands: []string{
68 Mode: fs.ModeDir | 0755,
84 Data: []byte(`Welcome to
85 {{- if .IsHost "host1.example.org"}} Host ONE
86 {{- else if "host2"}} Host TWO
89 {{if .InGroup "detected_linux"}}
90 This is GNU/Linux host
92 {{if .InGroup "detected_freebsd"}}
98 Path: "/etc/rc.local",
102 Data: []byte("#!/bin/sh\n"),
103 TriggerCommands: []string{
107 "/etc/resolv.conf": {
108 Path: "/etc/resolv.conf",
112 Data: []byte("nameserver ::1\n"),
113 TriggerCommands: []string{
114 "echo resolv.conf updated",
119 Mode: fs.ModeSymlink | 0777,
122 Data: []byte("doesnt-exist"),
129 "triggers-invalid-path",
131 fmt.Errorf("triggers-invalid-path/triggers.yaml: \"/etc/resolv.conf\" does not exist in files/"),
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)
140 t.Fatalf("%s: err = %#v, want nil",
143 err = LoadTriggers(tc.group, files)
145 if !reflect.DeepEqual(tc.exp, files) {
146 t.Errorf("%s: res: %s", tc.group,
147 cmp.Diff(tc.exp, files))
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)