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/>.
24 "ruderich.org/simon/safcm"
25 "ruderich.org/simon/safcm/testutil"
28 func TestLoadTriggers(t *testing.T) {
29 cwd, err := os.Getwd()
35 err = os.Chdir("../testdata/project")
42 exp map[string]*safcm.File
54 map[string]*safcm.File{
57 Mode: fs.ModeDir | 0755,
60 TriggerCommands: []string{
66 Mode: fs.ModeDir | 0755,
82 Data: []byte(`Welcome to
83 {{- if .IsHost "host1.example.org"}} Host ONE
84 {{- else if "host2"}} Host TWO
87 {{if .InGroup "detected_linux"}}
88 This is GNU/Linux host
90 {{if .InGroup "detected_freebsd"}}
96 Path: "/etc/rc.local",
100 Data: []byte("#!/bin/sh\n"),
101 TriggerCommands: []string{
105 "/etc/resolv.conf": {
106 Path: "/etc/resolv.conf",
110 Data: []byte("nameserver ::1\n"),
111 TriggerCommands: []string{
112 "echo resolv.conf updated",
117 Mode: fs.ModeSymlink | 0777,
120 Data: []byte("doesnt-exist"),
127 "triggers-invalid-path",
129 fmt.Errorf("triggers-invalid-path/triggers.yaml: \"/etc/resolv.conf\" does not exist in files/"),
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)
139 t.Fatalf("err = %#v, want nil", err)
141 err = LoadTriggers(tc.group, files)
143 testutil.AssertEqual(t, "res", files, tc.exp)
144 testutil.AssertErrorEqual(t, "err", err, tc.expErr)