2
2
// Licensed under the GNU Affero General Public License (AGPL).
3
3
// See License-AGPL.txt in the project root for license information.
4
4
5
- package wsmanager
5
+ package wsdaemon
6
6
7
7
import (
8
8
"context"
9
+ "encoding/json"
9
10
"strings"
10
11
"testing"
11
12
"time"
@@ -22,21 +23,34 @@ import (
22
23
corev1 "k8s.io/api/core/v1"
23
24
)
24
25
25
- const (
26
- expectedMinCpu = 200_000
27
- expectedBurstCpu = 600_000
28
- )
26
+ type DaemonConfig struct {
27
+ CpuLimitConfig struct {
28
+ Enabled bool `json:"enabled"`
29
+ Limit int64 `json:"limit,string"`
30
+ BurstLimit int64 `json:"burstLimit,string"`
31
+ } `json:"cpuLimit"`
32
+ }
29
33
30
34
func TestCpuBurst (t * testing.T ) {
31
35
f := features .New ("cpulimiting" ).WithLabel ("component" , "ws-manager" ).Assess ("check cpu limiting" , func (_ context.Context , t * testing.T , cfg * envconf.Config ) context.Context {
32
- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Minute )
36
+ t .Parallel ()
37
+
38
+ ctx , cancel := context .WithTimeout (context .Background (), 8 * time .Minute )
33
39
defer cancel ()
34
40
35
41
api := integration .NewComponentAPI (ctx , cfg .Namespace (), kubeconfig , cfg .Client ())
36
42
t .Cleanup (func () {
37
43
api .Done (t )
38
44
})
39
45
46
+ daemonConfig := getDaemonConfig (ctx , t , cfg )
47
+ if ! daemonConfig .CpuLimitConfig .Enabled {
48
+ return ctx
49
+ }
50
+
51
+ daemonConfig .CpuLimitConfig .Limit = daemonConfig .CpuLimitConfig .Limit * 100_000
52
+ daemonConfig .CpuLimitConfig .BurstLimit = daemonConfig .CpuLimitConfig .BurstLimit * 100_000
53
+
40
54
swr := func (req * wsmanapi.StartWorkspaceRequest ) error {
41
55
req .Spec .Initializer = & csapi.WorkspaceInitializer {
42
56
Spec : & csapi.WorkspaceInitializer_Git {
@@ -81,7 +95,7 @@ func TestCpuBurst(t *testing.T) {
81
95
ContainerId : containerId ,
82
96
}, & resp )
83
97
84
- if resp .CpuQuota == expectedMinCpu {
98
+ if resp .CpuQuota == daemonConfig . CpuLimitConfig . Limit {
85
99
break
86
100
}
87
101
time .Sleep (5 * time .Second )
@@ -91,8 +105,8 @@ func TestCpuBurst(t *testing.T) {
91
105
t .Fatalf ("cannot get workspace resources: %q" , err )
92
106
}
93
107
94
- if resp .CpuQuota != expectedMinCpu {
95
- t .Fatalf ("expected cpu quota of %v, but was %v" , expectedMinCpu , resp .CpuQuota )
108
+ if resp .CpuQuota != daemonConfig . CpuLimitConfig . Limit {
109
+ t .Fatalf ("expected cpu quota of %v, but was %v" , daemonConfig . CpuLimitConfig . Limit , resp .CpuQuota )
96
110
}
97
111
98
112
workspaceClient , workspaceCloser , err := integration .Instrument (integration .ComponentWorkspace , "workspace" , cfg .Namespace (), kubeconfig , cfg .Client (),
@@ -122,7 +136,7 @@ func TestCpuBurst(t *testing.T) {
122
136
ContainerId : containerId ,
123
137
}, & resp )
124
138
125
- if resp .CpuQuota == expectedBurstCpu {
139
+ if resp .CpuQuota == daemonConfig . CpuLimitConfig . BurstLimit {
126
140
break
127
141
}
128
142
time .Sleep (5 * time .Second )
@@ -132,15 +146,40 @@ func TestCpuBurst(t *testing.T) {
132
146
t .Fatalf ("cannot get workspace resources: %q" , err )
133
147
}
134
148
135
- if resp .CpuQuota != expectedBurstCpu {
136
- t .Fatalf ("expected cpu quota of %v, but was %v" , expectedBurstCpu , resp .CpuQuota )
149
+ if resp .CpuQuota != daemonConfig . CpuLimitConfig . BurstLimit {
150
+ t .Fatalf ("expected cpu quota of %v, but was %v" , daemonConfig . CpuLimitConfig . BurstLimit , resp .CpuQuota )
137
151
}
138
152
return ctx
139
153
}).Feature ()
140
154
141
155
testEnv .Test (t , f )
142
156
}
143
157
158
+ func getDaemonConfig (ctx context.Context , t * testing.T , cfg * envconf.Config ) DaemonConfig {
159
+ var daemonConfigMap corev1.ConfigMap
160
+
161
+ if err := cfg .Client ().Resources ().Get (ctx , "ws-daemon" , cfg .Namespace (), & daemonConfigMap ); err != nil {
162
+ t .Fatal (err )
163
+ }
164
+
165
+ data , ok := daemonConfigMap .Data ["config.json" ]
166
+ if ! ok {
167
+ t .Fatal ("server config map does not contain config.json" )
168
+ }
169
+
170
+ config := make (map [string ]json.RawMessage )
171
+ if err := json .Unmarshal ([]byte (data ), & config ); err != nil {
172
+ t .Fatal (err )
173
+ }
174
+
175
+ var daemonConfig DaemonConfig
176
+ if err := json .Unmarshal (config ["daemon" ], & daemonConfig ); err != nil {
177
+ t .Fatal (err )
178
+ }
179
+
180
+ return daemonConfig
181
+ }
182
+
144
183
func getWorkspaceContainerId (pod * corev1.Pod ) string {
145
184
for _ , c := range pod .Status .ContainerStatuses {
146
185
if c .Name != "workspace" {
0 commit comments