]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/sync_test.go
Use SPDX license identifiers
[safcm/safcm.git] / cmd / safcm / sync_test.go
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright (C) 2021-2024  Simon Ruderich
3
4 package main
5
6 import (
7         "fmt"
8         "os"
9         "testing"
10
11         "ruderich.org/simon/safcm/cmd/safcm/config"
12         "ruderich.org/simon/safcm/testutil"
13 )
14
15 func TestHostsToSync(t *testing.T) {
16         cwd, err := os.Getwd()
17         if err != nil {
18                 t.Fatal(err)
19         }
20         defer os.Chdir(cwd) //nolint:errcheck
21
22         err = os.Chdir("testdata/project")
23         if err != nil {
24                 t.Fatal(err)
25         }
26         _, allHosts, allGroups, err := LoadBaseFiles()
27         if err != nil {
28                 t.Fatal(err)
29         }
30
31         const errMsg = `
32
33 Groups depending on "detected" groups cannot be used to select hosts as these
34 are only available after the hosts were contacted.
35 `
36
37         tests := []struct {
38                 name   string
39                 names  []string
40                 exp    []*config.Host
41                 expErr error
42         }{
43                 {
44                         "empty names",
45                         nil,
46                         nil,
47                         nil,
48                 },
49
50                 {
51                         "no match",
52                         []string{"unknown-host/group"},
53                         nil,
54                         fmt.Errorf("hosts/groups not found: \"unknown-host/group\""),
55                 },
56
57                 {
58                         "host: single name",
59                         []string{"host2"},
60                         []*config.Host{
61                                 allHosts.Map["host2"],
62                         },
63                         nil,
64                 },
65                 {
66                         "host: multiple names",
67                         []string{"host2", "host1.example.org"},
68                         []*config.Host{
69                                 allHosts.Map["host1.example.org"],
70                                 allHosts.Map["host2"],
71                         },
72                         nil,
73                 },
74                 {
75                         "host: multiple identical names",
76                         []string{"host2", "host2"},
77                         []*config.Host{
78                                 allHosts.Map["host2"],
79                         },
80                         nil,
81                 },
82                 {
83                         "host: multiple names, including unknown",
84                         []string{"host2", "unknown-host"},
85                         nil,
86                         fmt.Errorf("hosts/groups not found: \"unknown-host\""),
87                 },
88                 {
89                         "host: multiple names, including unknowns",
90                         []string{"host2", "unknown-host", "unknown-host-2"},
91                         nil,
92                         fmt.Errorf("hosts/groups not found: \"unknown-host\" \"unknown-host-2\""),
93                 },
94
95                 {
96                         "group: single name",
97                         []string{"group3"},
98                         []*config.Host{
99                                 allHosts.Map["host1.example.org"],
100                         },
101                         nil,
102                 },
103                 {
104                         "group: multiple names",
105                         []string{"group3", "group2"},
106                         []*config.Host{
107                                 allHosts.Map["host1.example.org"],
108                                 allHosts.Map["host2"],
109                         },
110                         nil,
111                 },
112                 {
113                         "group: multiple identical names",
114                         []string{"group3", "group2", "group3"},
115                         []*config.Host{
116                                 allHosts.Map["host1.example.org"],
117                                 allHosts.Map["host2"],
118                         },
119                         nil,
120                 },
121                 {
122                         "group: multiple names, including unknown",
123                         []string{"group3", "group2", "unknown-group"},
124                         nil,
125                         fmt.Errorf("hosts/groups not found: \"unknown-group\""),
126                 },
127                 {
128                         "group: \"all\"",
129                         []string{"all"},
130                         []*config.Host{
131                                 allHosts.Map["host1.example.org"],
132                                 allHosts.Map["host2"],
133                                 allHosts.Map["host3.example.net"],
134                         },
135                         nil,
136                 },
137
138                 {
139                         "group: single name (detected)",
140                         []string{"group"},
141                         nil,
142                         fmt.Errorf(`group "group" depends on "detected" groups` + errMsg),
143                 },
144                 {
145                         "group: multiple names (detected)",
146                         []string{"group", "group2"},
147                         nil,
148                         fmt.Errorf(`group "group" depends on "detected" groups` + errMsg),
149                 },
150
151                 {
152                         "\"all\" and name",
153                         []string{"all", "group2"},
154                         []*config.Host{
155                                 allHosts.Map["host1.example.org"],
156                                 allHosts.Map["host2"],
157                                 allHosts.Map["host3.example.net"],
158                         },
159                         nil,
160                 },
161                 {
162                         "\"all\" and names",
163                         []string{"all", "group2", "host2"},
164                         []*config.Host{
165                                 allHosts.Map["host1.example.org"],
166                                 allHosts.Map["host2"],
167                                 allHosts.Map["host3.example.net"],
168                         },
169                         nil,
170                 },
171         }
172
173         for _, tc := range tests {
174                 t.Run(tc.name, func(t *testing.T) {
175                         res, err := hostsToSync(tc.names, allHosts, allGroups)
176                         testutil.AssertEqual(t, "res", res, tc.exp)
177                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
178                 })
179         }
180 }