// SPDX-License-Identifier: GPL-3.0-or-later // Copyright (C) 2021-2024 Simon Ruderich package main import ( "fmt" "os" "testing" "ruderich.org/simon/safcm/cmd/safcm/config" "ruderich.org/simon/safcm/testutil" ) func TestHostsToSync(t *testing.T) { cwd, err := os.Getwd() if err != nil { t.Fatal(err) } defer os.Chdir(cwd) //nolint:errcheck err = os.Chdir("testdata/project") if err != nil { t.Fatal(err) } _, allHosts, allGroups, err := LoadBaseFiles() if err != nil { t.Fatal(err) } const errMsg = ` Groups depending on "detected" groups cannot be used to select hosts as these are only available after the hosts were contacted. ` tests := []struct { name string names []string exp []*config.Host expErr error }{ { "empty names", nil, nil, nil, }, { "no match", []string{"unknown-host/group"}, nil, fmt.Errorf("hosts/groups not found: \"unknown-host/group\""), }, { "host: single name", []string{"host2"}, []*config.Host{ allHosts.Map["host2"], }, nil, }, { "host: multiple names", []string{"host2", "host1.example.org"}, []*config.Host{ allHosts.Map["host1.example.org"], allHosts.Map["host2"], }, nil, }, { "host: multiple identical names", []string{"host2", "host2"}, []*config.Host{ allHosts.Map["host2"], }, nil, }, { "host: multiple names, including unknown", []string{"host2", "unknown-host"}, nil, fmt.Errorf("hosts/groups not found: \"unknown-host\""), }, { "host: multiple names, including unknowns", []string{"host2", "unknown-host", "unknown-host-2"}, nil, fmt.Errorf("hosts/groups not found: \"unknown-host\" \"unknown-host-2\""), }, { "group: single name", []string{"group3"}, []*config.Host{ allHosts.Map["host1.example.org"], }, nil, }, { "group: multiple names", []string{"group3", "group2"}, []*config.Host{ allHosts.Map["host1.example.org"], allHosts.Map["host2"], }, nil, }, { "group: multiple identical names", []string{"group3", "group2", "group3"}, []*config.Host{ allHosts.Map["host1.example.org"], allHosts.Map["host2"], }, nil, }, { "group: multiple names, including unknown", []string{"group3", "group2", "unknown-group"}, nil, fmt.Errorf("hosts/groups not found: \"unknown-group\""), }, { "group: \"all\"", []string{"all"}, []*config.Host{ allHosts.Map["host1.example.org"], allHosts.Map["host2"], allHosts.Map["host3.example.net"], }, nil, }, { "group: single name (detected)", []string{"group"}, nil, fmt.Errorf(`group "group" depends on "detected" groups` + errMsg), }, { "group: multiple names (detected)", []string{"group", "group2"}, nil, fmt.Errorf(`group "group" depends on "detected" groups` + errMsg), }, { "\"all\" and name", []string{"all", "group2"}, []*config.Host{ allHosts.Map["host1.example.org"], allHosts.Map["host2"], allHosts.Map["host3.example.net"], }, nil, }, { "\"all\" and names", []string{"all", "group2", "host2"}, []*config.Host{ allHosts.Map["host1.example.org"], allHosts.Map["host2"], allHosts.Map["host3.example.net"], }, nil, }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { res, err := hostsToSync(tc.names, allHosts, allGroups) testutil.AssertEqual(t, "res", res, tc.exp) testutil.AssertErrorEqual(t, "err", err, tc.expErr) }) } }