Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ws-daemon/pkg/quota/xfs_test.go
2499 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package quota
6
7
import (
8
"fmt"
9
"sort"
10
"strings"
11
"testing"
12
13
"github.com/google/go-cmp/cmp"
14
)
15
16
func TestGetUsedProjectIDs(t *testing.T) {
17
type Expectation struct {
18
ProjectIDs []int
19
Error string
20
}
21
tests := []struct {
22
Name string
23
Input string
24
InputErr error
25
Expectation Expectation
26
}{
27
{
28
Name: "no projects",
29
Input: "",
30
},
31
{
32
Name: "single project",
33
Input: "#0 4 0 0 00 [------]",
34
Expectation: Expectation{
35
ProjectIDs: []int{0},
36
},
37
},
38
{
39
Name: "multiple projects none used",
40
Input: "#0 0 0 0 00 [------]\n#100 0 5M 5M 00 [------]\n#200 0 10M 10M 00 [------]",
41
},
42
{
43
Name: "multiple projects in use",
44
Input: "#0 0 0 0 00 [------]\n#100 4 5M 5M 00 [------]\n#200 1 10M 10M 00 [------]",
45
Expectation: Expectation{
46
ProjectIDs: []int{100, 200},
47
},
48
},
49
{
50
Name: "exec failure",
51
InputErr: fmt.Errorf("exec failed"),
52
Expectation: Expectation{
53
Error: "exec failed",
54
},
55
},
56
}
57
58
for _, test := range tests {
59
t.Run(test.Name, func(t *testing.T) {
60
xfs := &XFS{
61
exec: func(dir, command string) (output string, err error) {
62
return test.Input, test.InputErr
63
},
64
}
65
66
var (
67
act Expectation
68
err error
69
)
70
act.ProjectIDs, err = xfs.getUsedProjectIDs()
71
if err != nil {
72
act.Error = err.Error()
73
}
74
75
if diff := cmp.Diff(test.Expectation, act); diff != "" {
76
t.Errorf("unexpected getUsedProjectIDs (-want +got):\n%s", diff)
77
}
78
})
79
}
80
}
81
82
func TestSetQuota(t *testing.T) {
83
type Expectation struct {
84
ProjectID int
85
ProjectIDs []int
86
Execs []string
87
Error string
88
}
89
tests := []struct {
90
Name string
91
Size Size
92
IsHard bool
93
ExecErr func(cmd string) error
94
ProjectIDs []int
95
Expectation Expectation
96
}{
97
{
98
Name: "happpy path",
99
Size: 100 * Kilobyte,
100
IsHard: true,
101
Expectation: Expectation{
102
ProjectID: 1000,
103
ProjectIDs: []int{1000},
104
Execs: []string{
105
"project -s -d 1 -p /foo 1000",
106
"limit -p bhard=102400 1000",
107
},
108
},
109
},
110
{
111
Name: "with soft limit",
112
Size: 100 * Kilobyte,
113
IsHard: false,
114
Expectation: Expectation{
115
ProjectID: 1000,
116
ProjectIDs: []int{1000},
117
Execs: []string{
118
"project -s -d 1 -p /foo 1000",
119
"limit -p bsoft=102400 1000",
120
},
121
},
122
},
123
{
124
Name: "with other prj",
125
Size: 100 * Kilobyte,
126
IsHard: true,
127
ProjectIDs: []int{1000},
128
Expectation: Expectation{
129
ProjectID: 1001,
130
ProjectIDs: []int{1000, 1001},
131
Execs: []string{
132
"project -s -d 1 -p /foo 1001",
133
"limit -p bhard=102400 1001",
134
},
135
},
136
},
137
{
138
Name: "prj creation failure",
139
Size: 100 * Kilobyte,
140
IsHard: true,
141
ExecErr: func(cmd string) error {
142
if strings.Contains(cmd, "project") {
143
return fmt.Errorf("failed to create project")
144
}
145
return nil
146
},
147
Expectation: Expectation{
148
ProjectID: 0,
149
Execs: []string{
150
"project -s -d 1 -p /foo 1000",
151
},
152
Error: "failed to create project",
153
},
154
},
155
}
156
157
for _, test := range tests {
158
t.Run(test.Name, func(t *testing.T) {
159
var (
160
act Expectation
161
err error
162
)
163
xfs := &XFS{
164
exec: func(dir, command string) (output string, err error) {
165
act.Execs = append(act.Execs, command)
166
if test.ExecErr != nil {
167
return "", test.ExecErr(command)
168
}
169
170
return "", nil
171
},
172
projectIDs: make(map[int]struct{}),
173
Dir: "/",
174
}
175
for _, prjid := range test.ProjectIDs {
176
xfs.projectIDs[prjid] = struct{}{}
177
}
178
179
act.ProjectID, err = xfs.SetQuota("/foo", test.Size, test.IsHard)
180
if err != nil {
181
act.Error = err.Error()
182
}
183
for p := range xfs.projectIDs {
184
act.ProjectIDs = append(act.ProjectIDs, p)
185
}
186
sort.Ints(act.ProjectIDs)
187
188
if diff := cmp.Diff(test.Expectation, act); diff != "" {
189
t.Errorf("unexpected SetQuota (-want +got):\n%s", diff)
190
}
191
})
192
}
193
}
194
195