1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
package git
import (
"os"
"path/filepath"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/command"
"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config"
"gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config/cgroups"
"gitlab.com/gitlab-org/gitaly/v14/internal/metadata/featureflag"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
)
type mockCgroupsManager struct {
commands []*command.Command
}
func (m *mockCgroupsManager) Setup() error {
return nil
}
func (m *mockCgroupsManager) AddCommand(c *command.Command) error {
m.commands = append(m.commands, c)
return nil
}
func (m *mockCgroupsManager) Cleanup() error {
return nil
}
func (m *mockCgroupsManager) Collect(ch chan<- prometheus.Metric) {}
func (m *mockCgroupsManager) Describe(ch chan<- *prometheus.Desc) {}
func TestNewCommandAddsToCgroup(t *testing.T) {
root := testhelper.TempDir(t)
cfg := config.Cfg{
SocketPath: "/path/to/socket",
Cgroups: cgroups.Config{
Count: 1,
},
Storages: []config.Storage{{
Name: "storage-1",
Path: root,
}},
BinDir: filepath.Join(root, "bin.d"),
}
require.NoError(t, os.MkdirAll(cfg.BinDir, 0o755))
gitCmdFactory := newCommandFactory(t, cfg, WithSkipHooks())
testCases := []struct {
desc string
cgroupsFF bool
}{
{
desc: "cgroups feature flag on",
cgroupsFF: true,
},
{
desc: "cgroups feature flag off",
cgroupsFF: false,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
dir := testhelper.TempDir(t)
var manager mockCgroupsManager
gitCmdFactory.cgroupsManager = &manager
ctx := testhelper.Context(t)
ctx = featureflag.IncomingCtxWithFeatureFlag(ctx, featureflag.RunCommandsInCGroup, tc.cgroupsFF)
cmd := SubCmd{
Name: "hash-object",
}
_, err := gitCmdFactory.NewWithDir(ctx, dir, &cmd)
require.NoError(t, err)
if tc.cgroupsFF {
require.Len(t, manager.commands, 1)
assert.Contains(t, manager.commands[0].Args(), "hash-object")
return
}
require.Len(t, manager.commands, 0)
})
}
}
|