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"}}
97 {{if .InGroup "host1.example.org"}}
100 {{if .InGroup "host2"}}
103 {{if .InGroup "host3.example.net"}}
109 Path: "/etc/rc.local",
113 Data: []byte("#!/bin/sh\n"),
114 TriggerCommands: []string{
118 "/etc/resolv.conf": {
119 Path: "/etc/resolv.conf",
123 Data: []byte("nameserver ::1\n"),
124 TriggerCommands: []string{
125 "echo resolv.conf updated",
130 Mode: fs.ModeSymlink | 0777,
133 Data: []byte("doesnt-exist"),
140 "triggers-invalid-path",
142 fmt.Errorf("triggers-invalid-path/triggers.yaml: \"/etc/resolv.conf\" does not exist in files/"),
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)
152 t.Fatalf("err = %#v, want nil", err)
154 err = LoadTriggers(tc.group, files)
156 testutil.AssertEqual(t, "res", files, tc.exp)
157 testutil.AssertErrorEqual(t, "err", err, tc.expErr)