// SPDX-License-Identifier: GPL-3.0-or-later // Copyright (C) 2021-2024 Simon Ruderich package config import ( "fmt" "os" "path/filepath" "testing" "ruderich.org/simon/safcm/testutil" ) func TestLoadHosts(t *testing.T) { cwd, err := os.Getwd() if err != nil { t.Fatal(err) } defer os.Chdir(cwd) //nolint:errcheck sliceToHosts := func(hosts []*Host) *Hosts { res := &Hosts{ List: hosts, Map: make(map[string]*Host), } for _, x := range hosts { res.Map[x.Name] = x } return res } tests := []struct { path string exp *Hosts expErr error }{ { "../testdata/project", sliceToHosts([]*Host{ { Name: "host1.example.org", }, { Name: "host2", }, { Name: "host3.example.net", }, }), nil, }, { "../testdata/host-invalid-duplicate", nil, fmt.Errorf("hosts.yaml: host \"host1.example.org\": host name already exists"), }, { "../testdata/host-invalid-all", nil, fmt.Errorf("hosts.yaml: host \"all\": conflict with pre-defined group \"all\""), }, { "../testdata/host-invalid-detected", nil, fmt.Errorf("hosts.yaml: host \"detected\": name must not start with \"detected\" (reserved for detected groups)"), }, { "../testdata/host-invalid-special", nil, fmt.Errorf("hosts.yaml: host \"special:host\": name must not contain \":\""), }, } for _, tc := range tests { t.Run(tc.path, func(t *testing.T) { err := os.Chdir(filepath.Join(cwd, tc.path)) if err != nil { t.Fatal(err) } res, err := LoadHosts() testutil.AssertEqual(t, "res", res, tc.exp) testutil.AssertErrorEqual(t, "err", err, tc.expErr) }) } }