diff options
author | Will Chandler <wchandler@gitlab.com> | 2023-10-27 00:09:03 +0300 |
---|---|---|
committer | Will Chandler <wchandler@gitlab.com> | 2023-10-27 16:45:45 +0300 |
commit | 9e8ba7b13c0226c52c20482d243d5b2cb4f1e2b1 (patch) | |
tree | bedc7c08a9435861b1d72245edd6229bb0b490c3 /internal/cgroups | |
parent | 6db5edf173ae9d34772b8e521108139a2d0dbef7 (diff) |
cgroups: Add helper to calculate cgroup ID
We repeatedly calculate the cgroup ID for command in our tests.
Deduplicate these instances into a helper method.
Diffstat (limited to 'internal/cgroups')
-rw-r--r-- | internal/cgroups/testhelper_test.go | 8 | ||||
-rw-r--r-- | internal/cgroups/v1_linux_test.go | 13 | ||||
-rw-r--r-- | internal/cgroups/v2_linux_test.go | 13 |
3 files changed, 18 insertions, 16 deletions
diff --git a/internal/cgroups/testhelper_test.go b/internal/cgroups/testhelper_test.go index 5f594195a..55d0ae338 100644 --- a/internal/cgroups/testhelper_test.go +++ b/internal/cgroups/testhelper_test.go @@ -3,6 +3,8 @@ package cgroups import ( + "hash/crc32" + "strings" "testing" "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" @@ -11,3 +13,9 @@ import ( func TestMain(m *testing.M) { testhelper.Run(m) } + +// calcGroupID calculates the repository cgroup ID for the key provided. +func calcGroupID(key []string, ct uint) uint { + checksum := crc32.ChecksumIEEE([]byte(strings.Join(key, "/"))) + return uint(checksum) % ct +} diff --git a/internal/cgroups/v1_linux_test.go b/internal/cgroups/v1_linux_test.go index ba8a55b29..e28df87df 100644 --- a/internal/cgroups/v1_linux_test.go +++ b/internal/cgroups/v1_linux_test.go @@ -4,7 +4,6 @@ package cgroups import ( "fmt" - "hash/crc32" "io/fs" "os" "os/exec" @@ -235,12 +234,11 @@ func TestAddCommand(t *testing.T) { v1Manager2 := mock.newCgroupManager(config, testhelper.SharedLogger(t), pid) t.Run("without overridden key", func(t *testing.T) { + groupID := calcGroupID(cmd2.Args, config.Repositories.Count) + _, err := v1Manager2.AddCommand(cmd2) require.NoError(t, err) - checksum := crc32.ChecksumIEEE([]byte(strings.Join(cmd2.Args, "/"))) - groupID := uint(checksum) % config.Repositories.Count - for _, s := range mock.subsystems { path := filepath.Join(mock.root, string(s.Name()), "gitaly", fmt.Sprintf("gitaly-%d", pid), fmt.Sprintf("repos-%d", groupID), "cgroup.procs") @@ -254,15 +252,14 @@ func TestAddCommand(t *testing.T) { }) t.Run("with overridden key", func(t *testing.T) { + overridenGroupID := calcGroupID([]string{"foobar"}, config.Repositories.Count) + _, err := v1Manager2.AddCommand(cmd2, WithCgroupKey("foobar")) require.NoError(t, err) - checksum := crc32.ChecksumIEEE([]byte("foobar")) - groupID := uint(checksum) % config.Repositories.Count - for _, s := range mock.subsystems { path := filepath.Join(mock.root, string(s.Name()), "gitaly", - fmt.Sprintf("gitaly-%d", pid), fmt.Sprintf("repos-%d", groupID), "cgroup.procs") + fmt.Sprintf("gitaly-%d", pid), fmt.Sprintf("repos-%d", overridenGroupID), "cgroup.procs") content := readCgroupFile(t, path) cmdPid, err := strconv.Atoi(string(content)) diff --git a/internal/cgroups/v2_linux_test.go b/internal/cgroups/v2_linux_test.go index 96c5a3fdb..347ce2498 100644 --- a/internal/cgroups/v2_linux_test.go +++ b/internal/cgroups/v2_linux_test.go @@ -4,7 +4,6 @@ package cgroups import ( "fmt" - "hash/crc32" "io/fs" "os" "os/exec" @@ -223,12 +222,11 @@ func TestAddCommandV2(t *testing.T) { v2Manager2 := mock.newCgroupManager(config, testhelper.SharedLogger(t), pid) t.Run("without overridden key", func(t *testing.T) { + groupID := calcGroupID(cmd2.Args, config.Repositories.Count) + _, err := v2Manager2.AddCommand(cmd2) require.NoError(t, err) - checksum := crc32.ChecksumIEEE([]byte(strings.Join(cmd2.Args, "/"))) - groupID := uint(checksum) % config.Repositories.Count - path := filepath.Join(mock.root, "gitaly", fmt.Sprintf("gitaly-%d", pid), fmt.Sprintf("repos-%d", groupID), "cgroup.procs") content := readCgroupFile(t, path) @@ -240,14 +238,13 @@ func TestAddCommandV2(t *testing.T) { }) t.Run("with overridden key", func(t *testing.T) { + overriddenGroupID := calcGroupID([]string{"foobar"}, config.Repositories.Count) + _, err := v2Manager2.AddCommand(cmd2, WithCgroupKey("foobar")) require.NoError(t, err) - checksum := crc32.ChecksumIEEE([]byte("foobar")) - groupID := uint(checksum) % config.Repositories.Count - path := filepath.Join(mock.root, "gitaly", - fmt.Sprintf("gitaly-%d", pid), fmt.Sprintf("repos-%d", groupID), "cgroup.procs") + fmt.Sprintf("gitaly-%d", pid), fmt.Sprintf("repos-%d", overriddenGroupID), "cgroup.procs") content := readCgroupFile(t, path) cmdPid, err := strconv.Atoi(string(content)) |