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