X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=cmd%2Fsafcm%2Fsync_test.go;h=6d37366cb0b62c061330c968c615d1b0149f2bf2;hb=4206e0dbca82f5df7d8e534f78cb737979880916;hp=753f942fe06706d123967df5d95df9a43ee7c2e7;hpb=992eaae7fec45c2d58fff89d1bc0ae920a899296;p=safcm%2Fsafcm.git diff --git a/cmd/safcm/sync_test.go b/cmd/safcm/sync_test.go index 753f942..6d37366 100644 --- a/cmd/safcm/sync_test.go +++ b/cmd/safcm/sync_test.go @@ -1,4 +1,4 @@ -// Copyright (C) 2021 Simon Ruderich +// Copyright (C) 2021-2022 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 @@ -18,12 +18,10 @@ 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 +29,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 +40,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 +106,7 @@ func TestHostsToSync(t *testing.T) { { "group: single name", - []string{"group"}, + []string{"group3"}, []*config.Host{ allHosts.Map["host1.example.org"], }, @@ -110,7 +114,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 +123,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 +132,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 +147,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"}, @@ -167,16 +184,9 @@ func TestHostsToSync(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - res, err := hostsToSync(tc.names, allHosts, allGroups) - if !reflect.DeepEqual(tc.exp, res) { - t.Errorf("res: %s", - 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("err = %#v, want %#v", - err, tc.expErr) - } + res, err := hostsToSync(tc.names, allHosts, allGroups) + testutil.AssertEqual(t, "res", res, tc.exp) + testutil.AssertErrorEqual(t, "err", err, tc.expErr) }) } }