From 20686d0d25d7d22f577fc80df31c192b6a88d318 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Wed, 28 Apr 2021 07:32:37 +0200 Subject: [PATCH] safcm: group_order: higher priority for listed groups over remaining groups The priority for all groups listed in `group_order` was properly respected: Early entries had the highest priority. However, groups which were not listed in `group_order` had a higher priority and overwrote files from all groups configured in `group_order`. The priority is now as expected (from high to low): host itself, groups in group_order (from high to low), remaining groups. --- cmd/safcm/sync_sync.go | 5 +-- cmd/safcm/sync_sync_test.go | 33 +++++++++++++++++++ .../project-group_order-single/config.yaml | 2 ++ .../group-a/files/file.txt | 1 + .../group-b/files/file.txt | 1 + .../project-group_order-single/groups.yaml | 4 +++ .../project-group_order-single/hosts.yaml | 1 + 7 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 cmd/safcm/testdata/project-group_order-single/config.yaml create mode 100644 cmd/safcm/testdata/project-group_order-single/group-a/files/file.txt create mode 100644 cmd/safcm/testdata/project-group_order-single/group-b/files/file.txt create mode 100644 cmd/safcm/testdata/project-group_order-single/groups.yaml create mode 100644 cmd/safcm/testdata/project-group_order-single/hosts.yaml diff --git a/cmd/safcm/sync_sync.go b/cmd/safcm/sync_sync.go index 1328af7..8b4cec4 100644 --- a/cmd/safcm/sync_sync.go +++ b/cmd/safcm/sync_sync.go @@ -19,6 +19,7 @@ package main import ( "fmt" + "math" "os" "path/filepath" "sort" @@ -188,10 +189,10 @@ func (s *Sync) resolveHostGroups(detectedGroups []string) ( // Early entries have higher priorities groupPriority := make(map[string]int) for i, x := range s.config.GroupOrder { - groupPriority[x] = i + 1 + groupPriority[x] = math.MinInt32 + i + 1 } // Host itself always has highest priority - groupPriority[s.host.Name] = -1 + groupPriority[s.host.Name] = math.MinInt32 // Sort groups after priority and name sort.Slice(groups, func(i, j int) bool { diff --git a/cmd/safcm/sync_sync_test.go b/cmd/safcm/sync_sync_test.go index e96fe14..a5f6581 100644 --- a/cmd/safcm/sync_sync_test.go +++ b/cmd/safcm/sync_sync_test.go @@ -428,6 +428,39 @@ func TestHostSyncReq(t *testing.T) { }, nil, }, + + { + "group_order (single group)", + "project-group_order-single", + "host1.example.org", + nil, + safcm.LogDebug3, + safcm.MsgSyncReq{ + Groups: []string{"all", "group-b", "group-a", "host1.example.org"}, + Files: map[string]*safcm.File{ + "/": { + Path: "/", + Mode: fs.ModeDir | 0755, + Uid: -1, + Gid: -1, + OrigGroup: "group-a", + }, + "/file.txt": { + Path: "/file.txt", + Mode: 0644, + Uid: -1, + Gid: -1, + Data: []byte("file.txt: from group-a\n"), + OrigGroup: "group-a", + }, + }, + }, + []string{ + "host1.example.org: 3 host groups: all group-a group-b host1.example.org", + "host1.example.org: 3 host group priorities (desc. order): host1.example.org group-a", + }, + nil, + }, } for _, tc := range tests { diff --git a/cmd/safcm/testdata/project-group_order-single/config.yaml b/cmd/safcm/testdata/project-group_order-single/config.yaml new file mode 100644 index 0000000..5a673b2 --- /dev/null +++ b/cmd/safcm/testdata/project-group_order-single/config.yaml @@ -0,0 +1,2 @@ +group_order: + - group-a diff --git a/cmd/safcm/testdata/project-group_order-single/group-a/files/file.txt b/cmd/safcm/testdata/project-group_order-single/group-a/files/file.txt new file mode 100644 index 0000000..95fb500 --- /dev/null +++ b/cmd/safcm/testdata/project-group_order-single/group-a/files/file.txt @@ -0,0 +1 @@ +file.txt: from group-a diff --git a/cmd/safcm/testdata/project-group_order-single/group-b/files/file.txt b/cmd/safcm/testdata/project-group_order-single/group-b/files/file.txt new file mode 100644 index 0000000..2aaaa0d --- /dev/null +++ b/cmd/safcm/testdata/project-group_order-single/group-b/files/file.txt @@ -0,0 +1 @@ +file.txt: from group-b diff --git a/cmd/safcm/testdata/project-group_order-single/groups.yaml b/cmd/safcm/testdata/project-group_order-single/groups.yaml new file mode 100644 index 0000000..b53a9fe --- /dev/null +++ b/cmd/safcm/testdata/project-group_order-single/groups.yaml @@ -0,0 +1,4 @@ +group-a: + - host1.example.org +group-b: + - host1.example.org diff --git a/cmd/safcm/testdata/project-group_order-single/hosts.yaml b/cmd/safcm/testdata/project-group_order-single/hosts.yaml new file mode 100644 index 0000000..57e8465 --- /dev/null +++ b/cmd/safcm/testdata/project-group_order-single/hosts.yaml @@ -0,0 +1 @@ +- name: host1.example.org -- 2.43.2