]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/hosts_test.go
ecbc37e45314b7453864eb9d9d1cac22a57b4a53
[safcm/safcm.git] / cmd / safcm / config / hosts_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         "os"
21         "path/filepath"
22         "testing"
23
24         "ruderich.org/simon/safcm/testutil"
25 )
26
27 func TestLoadHosts(t *testing.T) {
28         cwd, err := os.Getwd()
29         if err != nil {
30                 t.Fatal(err)
31         }
32         defer os.Chdir(cwd)
33
34         sliceToHosts := func(hosts []*Host) *Hosts {
35                 res := &Hosts{
36                         List: hosts,
37                         Map:  make(map[string]*Host),
38                 }
39                 for _, x := range hosts {
40                         res.Map[x.Name] = x
41                 }
42                 return res
43         }
44
45         tests := []struct {
46                 path   string
47                 exp    *Hosts
48                 expErr error
49         }{
50
51                 {
52                         "../testdata/project",
53                         sliceToHosts([]*Host{
54                                 {
55                                         Name: "host1.example.org",
56                                 },
57                                 {
58                                         Name: "host2",
59                                 },
60                                 {
61                                         Name: "host3.example.net",
62                                 },
63                         }),
64                         nil,
65                 },
66
67                 {
68                         "../testdata/host-invalid-all",
69                         nil,
70                         fmt.Errorf("hosts.yaml: host \"all\": conflict with pre-defined group \"all\""),
71                 },
72                 {
73                         "../testdata/host-invalid-detected",
74                         nil,
75                         fmt.Errorf("hosts.yaml: host \"detected\": name must not start with \"detected\" (reserved for detected groups)"),
76                 },
77                 {
78                         "../testdata/host-invalid-special",
79                         nil,
80                         fmt.Errorf("hosts.yaml: host \"special:host\": name must not contain \":\""),
81                 },
82         }
83
84         for _, tc := range tests {
85                 t.Run(tc.path, func(t *testing.T) {
86                         err := os.Chdir(filepath.Join(cwd, tc.path))
87                         if err != nil {
88                                 t.Fatal(err)
89                         }
90
91                         res, err := LoadHosts()
92                         testutil.AssertEqual(t, "res", res, tc.exp)
93                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
94                 })
95         }
96 }