diff options
author | Will Chandler <wchandler@gitlab.com> | 2023-11-09 07:01:07 +0300 |
---|---|---|
committer | Will Chandler <wchandler@gitlab.com> | 2023-11-10 18:36:26 +0300 |
commit | baa2aeb2a7d69635a79dc7395b62acffd29339ef (patch) | |
tree | 239d2fce86c429830e3f8c844334c45d5543e501 | |
parent | da5f6f3af4f7543b7bf231db6f1d258c4cee4d8f (diff) |
cgroups: Move remaining test functions to handler
Now that all version-specific tests have been converted to unified tests
in `handler_linux_test.go`, move the remaining helper functions into
that file as well. Remove the version-specific test files as they are
now empty.
-rw-r--r-- | internal/cgroups/handler_linux_test.go | 55 | ||||
-rw-r--r-- | internal/cgroups/v1_linux_test.go | 80 | ||||
-rw-r--r-- | internal/cgroups/v2_linux_test.go | 80 |
3 files changed, 55 insertions, 160 deletions
diff --git a/internal/cgroups/handler_linux_test.go b/internal/cgroups/handler_linux_test.go index e419cbf67..9556b393b 100644 --- a/internal/cgroups/handler_linux_test.go +++ b/internal/cgroups/handler_linux_test.go @@ -22,6 +22,18 @@ import ( "golang.org/x/exp/slices" ) +func defaultCgroupsConfig() cgroups.Config { + return cgroups.Config{ + HierarchyRoot: "gitaly", + Repositories: cgroups.Repositories{ + Count: 3, + MemoryBytes: 1024000, + CPUShares: 256, + CPUQuotaUs: 200, + }, + } +} + func TestNewManager(t *testing.T) { cfg := cgroups.Config{Repositories: cgroups.Repositories{Count: 10}} @@ -821,6 +833,49 @@ func requireCgroupComponents(t *testing.T, version int, root string, cgroupPath } } +func readCgroupFile(t *testing.T, path string) []byte { + t.Helper() + + // The cgroups package defaults to permission 0 as it expects the file to be existing (the kernel creates the file) + // and its testing override the permission private variable to something sensible, hence we have to chmod ourselves + // so we can read the file. + require.NoError(t, os.Chmod(path, perm.PublicFile)) + + return testhelper.MustReadFile(t, path) +} + +func requireCgroupWithInt(t *testing.T, cgroupFile string, want int) { + t.Helper() + + if want <= 0 { + return + } + + require.Equal(t, + string(readCgroupFile(t, cgroupFile)), + strconv.Itoa(want), + ) +} + +func requireCgroupWithString(t *testing.T, cgroupFile string, want string) { + t.Helper() + + if want == "" { + return + } + require.Equal(t, + string(readCgroupFile(t, cgroupFile)), + want, + ) +} + +func calculateWantCPUWeight(wantCPUWeight int) int { + if wantCPUWeight == 0 { + return 0 + } + return 1 + ((wantCPUWeight-2)*9999)/262142 +} + func requireShards(t *testing.T, version int, mock mockCgroup, mgr *CGroupManager, pid int, expectedShards ...uint) { for shard := uint(0); shard < mgr.cfg.Repositories.Count; shard++ { shouldExist := slices.Contains(expectedShards, shard) diff --git a/internal/cgroups/v1_linux_test.go b/internal/cgroups/v1_linux_test.go deleted file mode 100644 index cd00e4fa2..000000000 --- a/internal/cgroups/v1_linux_test.go +++ /dev/null @@ -1,80 +0,0 @@ -//go:build linux - -package cgroups - -import ( - "fmt" - "os" - "path/filepath" - "strconv" - "testing" - - "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config/cgroups" - "gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm" - "gitlab.com/gitlab-org/gitaly/v16/internal/testhelper" - "golang.org/x/exp/slices" -) - -func defaultCgroupsConfig() cgroups.Config { - return cgroups.Config{ - HierarchyRoot: "gitaly", - Repositories: cgroups.Repositories{ - Count: 3, - MemoryBytes: 1024000, - CPUShares: 256, - CPUQuotaUs: 200, - }, - } -} - -func requireShardsV1(t *testing.T, mock *mockCgroupV1, mgr *CGroupManager, pid int, expectedShards ...uint) { - t.Helper() - - for shard := uint(0); shard < mgr.cfg.Repositories.Count; shard++ { - for _, s := range mock.subsystems { - cgroupPath := filepath.Join("gitaly", - fmt.Sprintf("gitaly-%d", pid), fmt.Sprintf("repos-%d", shard)) - diskPath := filepath.Join(mock.root, string(s.Name()), cgroupPath) - - if slices.Contains(expectedShards, shard) { - require.DirExists(t, diskPath) - - cgLock := mgr.status.getLock(cgroupPath) - require.True(t, cgLock.isCreated()) - } else { - require.NoDirExists(t, diskPath) - - // Confirm we pre-populated this map entry. - _, lockInserted := mgr.status.m[cgroupPath] - require.True(t, lockInserted) - } - } - } -} - -func requireCgroup(t *testing.T, cgroupFile string, want int) { - t.Helper() - - if want <= 0 { - // If files doesn't exist kernel will create it with default values - require.NoFileExistsf(t, cgroupFile, "cgroup file should not exist: %q", cgroupFile) - return - } - - require.Equal(t, - string(readCgroupFile(t, cgroupFile)), - strconv.Itoa(want), - ) -} - -func readCgroupFile(t *testing.T, path string) []byte { - t.Helper() - - // The cgroups package defaults to permission 0 as it expects the file to be existing (the kernel creates the file) - // and its testing override the permission private variable to something sensible, hence we have to chmod ourselves - // so we can read the file. - require.NoError(t, os.Chmod(path, perm.PublicFile)) - - return testhelper.MustReadFile(t, path) -} diff --git a/internal/cgroups/v2_linux_test.go b/internal/cgroups/v2_linux_test.go deleted file mode 100644 index 0bc7a9ed2..000000000 --- a/internal/cgroups/v2_linux_test.go +++ /dev/null @@ -1,80 +0,0 @@ -//go:build linux - -package cgroups - -import ( - "fmt" - "path/filepath" - "strconv" - "testing" - - "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/config/cgroups" - "golang.org/x/exp/slices" -) - -func defaultCgroupsV2Config() cgroups.Config { - return cgroups.Config{ - HierarchyRoot: "gitaly", - Repositories: cgroups.Repositories{ - Count: 3, - MemoryBytes: 1024000, - CPUShares: 256, - CPUQuotaUs: 2000, - }, - } -} - -func calculateWantCPUWeight(wantCPUWeight int) int { - if wantCPUWeight == 0 { - return 0 - } - return 1 + ((wantCPUWeight-2)*9999)/262142 -} - -func requireShardsV2(t *testing.T, mock *mockCgroupV2, mgr *CGroupManager, pid int, expectedShards ...uint) { - t.Helper() - - for shard := uint(0); shard < mgr.cfg.Repositories.Count; shard++ { - cgroupPath := filepath.Join("gitaly", fmt.Sprintf("gitaly-%d", pid), fmt.Sprintf("repos-%d", shard)) - diskPath := filepath.Join(mock.root, cgroupPath) - - if slices.Contains(expectedShards, shard) { - require.DirExists(t, diskPath) - - cgLock := mgr.status.getLock(cgroupPath) - require.True(t, cgLock.isCreated()) - } else { - require.NoDirExists(t, diskPath) - - // Confirm we pre-populated this map entry. - _, lockInserted := mgr.status.m[cgroupPath] - require.True(t, lockInserted) - } - } -} - -func requireCgroupWithString(t *testing.T, cgroupFile string, want string) { - t.Helper() - - if want == "" { - return - } - require.Equal(t, - string(readCgroupFile(t, cgroupFile)), - want, - ) -} - -func requireCgroupWithInt(t *testing.T, cgroupFile string, want int) { - t.Helper() - - if want <= 0 { - return - } - - require.Equal(t, - string(readCgroupFile(t, cgroupFile)), - strconv.Itoa(want), - ) -} |