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