]> ruderich.org/simon Gitweb - safcm/safcm.git/blobdiff - cmd/safcm/sync_test.go
Use SPDX license identifiers
[safcm/safcm.git] / cmd / safcm / sync_test.go
index dba1d7e054bd7e4b8e99238b7d0692c6aa2ef8b4..d4b0e7ce9c8c0653d6b887c2fd40ce9a246902ca 100644 (file)
@@ -1,29 +1,15 @@
-// Copyright (C) 2021  Simon Ruderich
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program.  If not, see <http://www.gnu.org/licenses/>.
+// SPDX-License-Identifier: GPL-3.0-or-later
+// Copyright (C) 2021-2024  Simon Ruderich
 
 package main
 
 import (
        "fmt"
        "os"
-       "reflect"
        "testing"
 
-       "github.com/google/go-cmp/cmp"
-
        "ruderich.org/simon/safcm/cmd/safcm/config"
+       "ruderich.org/simon/safcm/testutil"
 )
 
 func TestHostsToSync(t *testing.T) {
@@ -31,7 +17,7 @@ func TestHostsToSync(t *testing.T) {
        if err != nil {
                t.Fatal(err)
        }
-       defer os.Chdir(cwd)
+       defer os.Chdir(cwd) //nolint:errcheck
 
        err = os.Chdir("testdata/project")
        if err != nil {
@@ -42,6 +28,12 @@ func TestHostsToSync(t *testing.T) {
                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
@@ -102,7 +94,7 @@ func TestHostsToSync(t *testing.T) {
 
                {
                        "group: single name",
-                       []string{"group"},
+                       []string{"group3"},
                        []*config.Host{
                                allHosts.Map["host1.example.org"],
                        },
@@ -110,7 +102,7 @@ func TestHostsToSync(t *testing.T) {
                },
                {
                        "group: multiple names",
-                       []string{"group", "group2"},
+                       []string{"group3", "group2"},
                        []*config.Host{
                                allHosts.Map["host1.example.org"],
                                allHosts.Map["host2"],
@@ -119,7 +111,7 @@ func TestHostsToSync(t *testing.T) {
                },
                {
                        "group: multiple identical names",
-                       []string{"group", "group2", "group"},
+                       []string{"group3", "group2", "group3"},
                        []*config.Host{
                                allHosts.Map["host1.example.org"],
                                allHosts.Map["host2"],
@@ -128,7 +120,7 @@ func TestHostsToSync(t *testing.T) {
                },
                {
                        "group: multiple names, including unknown",
-                       []string{"group", "group2", "unknown-group"},
+                       []string{"group3", "group2", "unknown-group"},
                        nil,
                        fmt.Errorf("hosts/groups not found: \"unknown-group\""),
                },
@@ -143,6 +135,19 @@ func TestHostsToSync(t *testing.T) {
                        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"},
@@ -166,15 +171,10 @@ func TestHostsToSync(t *testing.T) {
        }
 
        for _, tc := range tests {
-               res, err := hostsToSync(tc.names, allHosts, allGroups)
-               if !reflect.DeepEqual(tc.exp, res) {
-                       t.Errorf("%s: res: %s", tc.name,
-                               cmp.Diff(tc.exp, res))
-               }
-               // Ugly but the simplest way to compare errors (including nil)
-               if fmt.Sprintf("%s", err) != fmt.Sprintf("%s", tc.expErr) {
-                       t.Errorf("%s: err = %#v, want %#v",
-                               tc.name, err, tc.expErr)
-               }
+               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)
+               })
        }
 }