7
7
"fmt"
8
8
"os"
9
9
"path/filepath"
10
+ "strconv"
10
11
"strings"
11
12
"testing"
12
13
"time"
@@ -80,8 +81,10 @@ func TestDocker(t *testing.T) {
80
81
require .NoError (t , err )
81
82
82
83
var (
83
- tmpdir = integrationtest .TmpDir (t )
84
- binds = integrationtest .DefaultBinds (t , tmpdir )
84
+ tmpdir = integrationtest .TmpDir (t )
85
+ binds = integrationtest .DefaultBinds (t , tmpdir )
86
+ expectedMemoryLimit = "1073741824"
87
+ expectedCPULimit = 1
85
88
)
86
89
87
90
homeDir := filepath .Join (tmpdir , "home" )
@@ -116,6 +119,8 @@ func TestDocker(t *testing.T) {
116
119
"TEST_VAR=hello=world" ,
117
120
// Add a mount mapping to the inner container.
118
121
fmt .Sprintf ("%s=%s:%s,%s:%s:ro" , cli .EnvMounts , "/home/coder" , "/home/coder" , "/var/secrets" , "/var/secrets" ),
122
+ fmt .Sprintf ("%s=%s" , cli .EnvMemory , expectedMemoryLimit ),
123
+ fmt .Sprintf ("%s=%d" , cli .EnvCPUs , expectedCPULimit ),
119
124
}
120
125
)
121
126
@@ -141,6 +146,7 @@ func TestDocker(t *testing.T) {
141
146
AddFUSE : true ,
142
147
AddTUN : true ,
143
148
BootstrapScript : bootstrapScript ,
149
+ CPUs : expectedCPULimit ,
144
150
})
145
151
146
152
// Validate that the envs are set correctly.
@@ -227,6 +233,35 @@ func TestDocker(t *testing.T) {
227
233
})
228
234
require .NoError (t , err )
229
235
require .Equal (t , "1000" , strings .TrimSpace (string (out )))
236
+
237
+ // Validate that the bootstrap script ran.
238
+ out , err = integrationtest .ExecInnerContainer (t , pool , integrationtest.ExecConfig {
239
+ ContainerID : resource .Container .ID ,
240
+ Cmd : []string {"cat" , "/sys/fs/cgroup/memory/memory.limit_in_bytes" },
241
+ })
242
+ require .NoError (t , err )
243
+ require .Equal (t , expectedMemoryLimit , strings .TrimSpace (string (out )))
244
+
245
+ // Validate that the bootstrap script ran.
246
+ periodStr , err := integrationtest .ExecInnerContainer (t , pool , integrationtest.ExecConfig {
247
+ ContainerID : resource .Container .ID ,
248
+ Cmd : []string {"cat" , "/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us" },
249
+ })
250
+ require .NoError (t , err )
251
+ period , err := strconv .ParseInt (strings .TrimSpace (string (periodStr )), 10 , 64 )
252
+ require .NoError (t , err )
253
+
254
+ // Validate that the bootstrap script ran.
255
+ quotaStr , err := integrationtest .ExecInnerContainer (t , pool , integrationtest.ExecConfig {
256
+ ContainerID : resource .Container .ID ,
257
+ Cmd : []string {"cat" , "/sys/fs/cgroup/cpu/cpu.cfs_quota_us" },
258
+ })
259
+ require .NoError (t , err )
260
+ quota , err := strconv .ParseInt (strings .TrimSpace (string (quotaStr )), 10 , 64 )
261
+ require .NoError (t , err )
262
+
263
+ actualLimit := float64 (quota ) / float64 (period )
264
+ require .Equal (t , expectedCPULimit , int (actualLimit ))
230
265
})
231
266
}
232
267
0 commit comments