]> ruderich.org/simon Gitweb - safcm/safcm.git/blob - cmd/safcm/config/hosts_test.go
Use SPDX license identifiers
[safcm/safcm.git] / cmd / safcm / config / hosts_test.go
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright (C) 2021-2024  Simon Ruderich
3
4 package config
5
6 import (
7         "fmt"
8         "os"
9         "path/filepath"
10         "testing"
11
12         "ruderich.org/simon/safcm/testutil"
13 )
14
15 func TestLoadHosts(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         sliceToHosts := func(hosts []*Host) *Hosts {
23                 res := &Hosts{
24                         List: hosts,
25                         Map:  make(map[string]*Host),
26                 }
27                 for _, x := range hosts {
28                         res.Map[x.Name] = x
29                 }
30                 return res
31         }
32
33         tests := []struct {
34                 path   string
35                 exp    *Hosts
36                 expErr error
37         }{
38
39                 {
40                         "../testdata/project",
41                         sliceToHosts([]*Host{
42                                 {
43                                         Name: "host1.example.org",
44                                 },
45                                 {
46                                         Name: "host2",
47                                 },
48                                 {
49                                         Name: "host3.example.net",
50                                 },
51                         }),
52                         nil,
53                 },
54
55                 {
56                         "../testdata/host-invalid-duplicate",
57                         nil,
58                         fmt.Errorf("hosts.yaml: host \"host1.example.org\": host name already exists"),
59                 },
60                 {
61                         "../testdata/host-invalid-all",
62                         nil,
63                         fmt.Errorf("hosts.yaml: host \"all\": conflict with pre-defined group \"all\""),
64                 },
65                 {
66                         "../testdata/host-invalid-detected",
67                         nil,
68                         fmt.Errorf("hosts.yaml: host \"detected\": name must not start with \"detected\" (reserved for detected groups)"),
69                 },
70                 {
71                         "../testdata/host-invalid-special",
72                         nil,
73                         fmt.Errorf("hosts.yaml: host \"special:host\": name must not contain \":\""),
74                 },
75         }
76
77         for _, tc := range tests {
78                 t.Run(tc.path, func(t *testing.T) {
79                         err := os.Chdir(filepath.Join(cwd, tc.path))
80                         if err != nil {
81                                 t.Fatal(err)
82                         }
83
84                         res, err := LoadHosts()
85                         testutil.AssertEqual(t, "res", res, tc.exp)
86                         testutil.AssertErrorEqual(t, "err", err, tc.expErr)
87                 })
88         }
89 }