]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/sync_test.go
config: remove unnecessary os.Chdir in test
[safcm/safcm.git] / cmd / safcm / sync_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 main
17
18 import (
19         "fmt"
20         "os"
21         "reflect"
22         "testing"
23
24         "github.com/google/go-cmp/cmp"
25
26         "ruderich.org/simon/safcm/cmd/safcm/config"
27 )
28
29 func TestHostsToSync(t *testing.T) {
30         cwd, err := os.Getwd()
31         if err != nil {
32                 t.Fatal(err)
33         }
34         defer os.Chdir(cwd)
35
36         err = os.Chdir("testdata/project")
37         if err != nil {
38                 t.Fatal(err)
39         }
40         _, allHosts, allGroups, err := LoadBaseFiles()
41         if err != nil {
42                 t.Fatal(err)
43         }
44
45         tests := []struct {
46                 name   string
47                 names  []string
48                 exp    []*config.Host
49                 expErr error
50         }{
51                 {
52                         "empty names",
53                         nil,
54                         nil,
55                         nil,
56                 },
57
58                 {
59                         "no match",
60                         []string{"unknown-host/group"},
61                         nil,
62                         fmt.Errorf("hosts/groups not found: \"unknown-host/group\""),
63                 },
64
65                 {
66                         "host: single name",
67                         []string{"host2"},
68                         []*config.Host{
69                                 allHosts.Map["host2"],
70                         },
71                         nil,
72                 },
73                 {
74                         "host: multiple names",
75                         []string{"host2", "host1.example.org"},
76                         []*config.Host{
77                                 allHosts.Map["host1.example.org"],
78                                 allHosts.Map["host2"],
79                         },
80                         nil,
81                 },
82                 {
83                         "host: multiple identical names",
84                         []string{"host2", "host2"},
85                         []*config.Host{
86                                 allHosts.Map["host2"],
87                         },
88                         nil,
89                 },
90                 {
91                         "host: multiple names, including unknown",
92                         []string{"host2", "unknown-host"},
93                         nil,
94                         fmt.Errorf("hosts/groups not found: \"unknown-host\""),
95                 },
96                 {
97                         "host: multiple names, including unknowns",
98                         []string{"host2", "unknown-host", "unknown-host-2"},
99                         nil,
100                         fmt.Errorf("hosts/groups not found: \"unknown-host\" \"unknown-host-2\""),
101                 },
102
103                 {
104                         "group: single name",
105                         []string{"group"},
106                         []*config.Host{
107                                 allHosts.Map["host1.example.org"],
108                         },
109                         nil,
110                 },
111                 {
112                         "group: multiple names",
113                         []string{"group", "group2"},
114                         []*config.Host{
115                                 allHosts.Map["host1.example.org"],
116                                 allHosts.Map["host2"],
117                         },
118                         nil,
119                 },
120                 {
121                         "group: multiple identical names",
122                         []string{"group", "group2", "group"},
123                         []*config.Host{
124                                 allHosts.Map["host1.example.org"],
125                                 allHosts.Map["host2"],
126                         },
127                         nil,
128                 },
129                 {
130                         "group: multiple names, including unknown",
131                         []string{"group", "group2", "unknown-group"},
132                         nil,
133                         fmt.Errorf("hosts/groups not found: \"unknown-group\""),
134                 },
135                 {
136                         "group: \"all\"",
137                         []string{"all"},
138                         []*config.Host{
139                                 allHosts.Map["host1.example.org"],
140                                 allHosts.Map["host2"],
141                                 allHosts.Map["host3.example.net"],
142                         },
143                         nil,
144                 },
145
146                 {
147                         "\"all\" and name",
148                         []string{"all", "group2"},
149                         []*config.Host{
150                                 allHosts.Map["host1.example.org"],
151                                 allHosts.Map["host2"],
152                                 allHosts.Map["host3.example.net"],
153                         },
154                         nil,
155                 },
156                 {
157                         "\"all\" and names",
158                         []string{"all", "group2", "host2"},
159                         []*config.Host{
160                                 allHosts.Map["host1.example.org"],
161                                 allHosts.Map["host2"],
162                                 allHosts.Map["host3.example.net"],
163                         },
164                         nil,
165                 },
166         }
167
168         for _, tc := range tests {
169                 t.Run(tc.name, func(t *testing.T) {
170                         res, err := hostsToSync(tc.names, allHosts, allGroups)
171                         if !reflect.DeepEqual(tc.exp, res) {
172                                 t.Errorf("res: %s", cmp.Diff(tc.exp, res))
173                         }
174                         // Ugly but the simplest way to compare errors (including nil)
175                         if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
176                                 t.Errorf("err = %#v, want %#v",
177                                         err, tc.expErr)
178                         }
179                 })
180         }
181 }