]> ruderich.org/simon Gitweb - safcm/safcm.git/commitdiff
safcm: group_order: higher priority for listed groups over remaining groups
authorSimon Ruderich <simon@ruderich.org>
Wed, 28 Apr 2021 05:32:37 +0000 (07:32 +0200)
committerSimon Ruderich <simon@ruderich.org>
Wed, 28 Apr 2021 05:53:19 +0000 (07:53 +0200)
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
cmd/safcm/sync_sync_test.go
cmd/safcm/testdata/project-group_order-single/config.yaml [new file with mode: 0644]
cmd/safcm/testdata/project-group_order-single/group-a/files/file.txt [new file with mode: 0644]
cmd/safcm/testdata/project-group_order-single/group-b/files/file.txt [new file with mode: 0644]
cmd/safcm/testdata/project-group_order-single/groups.yaml [new file with mode: 0644]
cmd/safcm/testdata/project-group_order-single/hosts.yaml [new file with mode: 0644]

index 1328af7ca7acf6d87073c12cc8b71d36ca489e44..8b4cec45d0ba837cc490ac332a9b5d124883c136 100644 (file)
@@ -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 {
index e96fe140ae612a6a08f2429388c2d333fced2191..a5f65811e0d82297f642f01dcf027dd175ff9da3 100644 (file)
@@ -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: <nil> 3 host groups: all group-a group-b host1.example.org",
+                               "host1.example.org: <nil> 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 (file)
index 0000000..5a673b2
--- /dev/null
@@ -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 (file)
index 0000000..95fb500
--- /dev/null
@@ -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 (file)
index 0000000..2aaaa0d
--- /dev/null
@@ -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 (file)
index 0000000..b53a9fe
--- /dev/null
@@ -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 (file)
index 0000000..57e8465
--- /dev/null
@@ -0,0 +1 @@
+- name: host1.example.org