Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWill Chandler <wchandler@gitlab.com>2023-11-09 06:00:26 +0300
committerWill Chandler <wchandler@gitlab.com>2023-11-10 17:47:37 +0300
commit51cfb034224b8104a9646c8ad9ec36222c116aa2 (patch)
tree11e9d650e93fa1f0b7dcac945a82b518f36fa92f
parent321504664f3580f0f7f16d8538519b32a0601420 (diff)
cgroups: Introduce mockCgroup interface
Introduce a `mockCgroup` interface to allow tests to create a v1 or v2 mock depending on a test parameter, rather than duplicating the entire test. This requires accessing the `root` path via the new `rootPath()` method, and a convenience method for calculating the mock's repository cgroup paths is also added.
-rw-r--r--internal/cgroups/mock_linux_test.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/internal/cgroups/mock_linux_test.go b/internal/cgroups/mock_linux_test.go
index bd546545c..16f7c1900 100644
--- a/internal/cgroups/mock_linux_test.go
+++ b/internal/cgroups/mock_linux_test.go
@@ -35,6 +35,35 @@ import (
"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
)
+type mockCgroup interface {
+ setupMockCgroupFiles(
+ t *testing.T,
+ manager *CGroupManager,
+ shards []uint,
+ inputContent ...mockCgroupFile,
+ )
+ newCgroupManager(cfg cgroupscfg.Config, logger log.Logger, pid int) *CGroupManager
+ pruneOldCgroups(cfg cgroupscfg.Config, logger log.Logger)
+ // rootPath returns the mock's root directory.
+ rootPath() string
+ // repoPaths returns the full disk path for each subcomponent, e.g. memory, cpu, of
+ // a repository cgroup. On v2 this is a single path.
+ repoPaths(pid int, repoID uint) []string
+ // version returns the cgroup version number, 1 or 2.
+ version() int
+}
+
+func newMock(t *testing.T, version int) mockCgroup {
+ var mock mockCgroup
+ if version == 1 {
+ mock = newMockV1(t)
+ } else {
+ mock = newMockV2(t)
+ }
+
+ return mock
+}
+
type mockCgroupV1 struct {
root string
subsystems []cgroup1.Subsystem
@@ -150,6 +179,26 @@ func (m *mockCgroupV1) pruneOldCgroups(cfg cgroupscfg.Config, logger log.Logger)
pruneOldCgroupsWithMode(cfg, logger, cgrps.Legacy)
}
+func (m *mockCgroupV1) rootPath() string {
+ return m.root
+}
+
+func (m *mockCgroupV1) repoPaths(pid int, repoID uint) []string {
+ paths := make([]string, 0, len(m.subsystems))
+
+ for _, s := range m.subsystems {
+ path := filepath.Join(m.root, string(s.Name()), "gitaly",
+ fmt.Sprintf("gitaly-%d", pid), fmt.Sprintf("repos-%d", repoID))
+ paths = append(paths, path)
+ }
+
+ return paths
+}
+
+func (m *mockCgroupV1) version() int {
+ return 1
+}
+
type mockCgroupV2 struct {
root string
}
@@ -221,3 +270,18 @@ func (m *mockCgroupV2) newCgroupManager(cfg cgroupscfg.Config, logger log.Logger
func (m *mockCgroupV2) pruneOldCgroups(cfg cgroupscfg.Config, logger log.Logger) {
pruneOldCgroupsWithMode(cfg, logger, cgrps.Unified)
}
+
+func (m *mockCgroupV2) rootPath() string {
+ return m.root
+}
+
+func (m *mockCgroupV2) repoPaths(pid int, repoID uint) []string {
+ path := filepath.Join(m.root, "gitaly",
+ fmt.Sprintf("gitaly-%d", pid), fmt.Sprintf("repos-%d", repoID))
+
+ return []string{path}
+}
+
+func (m *mockCgroupV2) version() int {
+ return 2
+}