]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/sync_test.go
Update copyright years
[safcm/safcm.git] / cmd / safcm / sync_test.go
1 // Copyright (C) 2021-2024  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) //nolint:errcheck
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         const errMsg = `
44
45 Groups depending on "detected" groups cannot be used to select hosts as these
46 are only available after the hosts were contacted.
47 `
48
49         tests := []struct {
50                 name   string
51                 names  []string
52                 exp    []*config.Host
53                 expErr error
54         }{
55                 {
56                         "empty names",
57                         nil,
58                         nil,
59                         nil,
60                 },
61
62                 {
63                         "no match",
64                         []string{"unknown-host/group"},
65                         nil,
66                         fmt.Errorf("hosts/groups not found: \"unknown-host/group\""),
67                 },
68
69                 {
70                         "host: single name",
71                         []string{"host2"},
72                         []*config.Host{
73                                 allHosts.Map["host2"],
74                         },
75                         nil,
76                 },
77                 {
78                         "host: multiple names",
79                         []string{"host2", "host1.example.org"},
80                         []*config.Host{
81                                 allHosts.Map["host1.example.org"],
82                                 allHosts.Map["host2"],
83                         },
84                         nil,
85                 },
86                 {
87                         "host: multiple identical names",
88                         []string{"host2", "host2"},
89                         []*config.Host{
90                                 allHosts.Map["host2"],
91                         },
92                         nil,
93                 },
94                 {
95                         "host: multiple names, including unknown",
96                         []string{"host2", "unknown-host"},
97                         nil,
98                         fmt.Errorf("hosts/groups not found: \"unknown-host\""),
99                 },
100                 {
101                         "host: multiple names, including unknowns",
102                         []string{"host2", "unknown-host", "unknown-host-2"},
103                         nil,
104                         fmt.Errorf("hosts/groups not found: \"unknown-host\" \"unknown-host-2\""),
105                 },
106
107                 {
108                         "group: single name",
109                         []string{"group3"},
110                         []*config.Host{
111                                 allHosts.Map["host1.example.org"],
112                         },
113                         nil,
114                 },
115                 {
116                         "group: multiple names",
117                         []string{"group3", "group2"},
118                         []*config.Host{
119                                 allHosts.Map["host1.example.org"],
120                                 allHosts.Map["host2"],
121                         },
122                         nil,
123                 },
124                 {
125                         "group: multiple identical names",
126                         []string{"group3", "group2", "group3"},
127                         []*config.Host{
128                                 allHosts.Map["host1.example.org"],
129                                 allHosts.Map["host2"],
130                         },
131                         nil,
132                 },
133                 {
134                         "group: multiple names, including unknown",
135                         []string{"group3", "group2", "unknown-group"},
136                         nil,
137                         fmt.Errorf("hosts/groups not found: \"unknown-group\""),
138                 },
139                 {
140                         "group: \"all\"",
141                         []string{"all"},
142                         []*config.Host{
143                                 allHosts.Map["host1.example.org"],
144                                 allHosts.Map["host2"],
145                                 allHosts.Map["host3.example.net"],
146                         },
147                         nil,
148                 },
149
150                 {
151                         "group: single name (detected)",
152                         []string{"group"},
153                         nil,
154                         fmt.Errorf(`group "group" depends on "detected" groups` + errMsg),
155                 },
156                 {
157                         "group: multiple names (detected)",
158                         []string{"group", "group2"},
159                         nil,
160                         fmt.Errorf(`group "group" depends on "detected" groups` + errMsg),
161                 },
162
163                 {
164                         "\"all\" and name",
165                         []string{"all", "group2"},
166                         []*config.Host{
167                                 allHosts.Map["host1.example.org"],
168                                 allHosts.Map["host2"],
169                                 allHosts.Map["host3.example.net"],
170                         },
171                         nil,
172                 },
173                 {
174                         "\"all\" and names",
175                         []string{"all", "group2", "host2"},
176                         []*config.Host{
177                                 allHosts.Map["host1.example.org"],
178                                 allHosts.Map["host2"],
179                                 allHosts.Map["host3.example.net"],
180                         },
181                         nil,
182                 },
183         }
184
185         for _, tc := range tests {
186                 t.Run(tc.name, func(t *testing.T) {
187                         res, err := hostsToSync(tc.names, allHosts, allGroups)
188                         testutil.AssertEqual(t, "res", res, tc.exp)
189                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
190                 })
191         }
192 }