4
4
package integration_test
5
5
6
6
import (
7
- "context"
8
- "crypto/tls"
9
- "encoding/json"
10
7
"fmt"
11
8
"net"
12
- "net/http"
13
- "net/http/httptest"
14
9
"os"
15
10
"path/filepath"
16
11
"strconv"
@@ -21,8 +16,6 @@ import (
21
16
dockertest "github.com/ory/dockertest/v3"
22
17
"github.com/stretchr/testify/require"
23
18
24
- "github.com/coder/coder/v2/codersdk"
25
- "github.com/coder/coder/v2/codersdk/agentsdk"
26
19
"github.com/coder/envbox/cli"
27
20
"github.com/coder/envbox/integration/integrationtest"
28
21
)
@@ -285,87 +278,77 @@ func TestDocker(t *testing.T) {
285
278
t .Parallel ()
286
279
287
280
var (
288
- dir = integrationtest .TmpDir (t )
289
- binds = integrationtest .DefaultBinds (t , dir )
290
- ctx , cancel = context .WithTimeout (context .Background (), time .Minute * 5 )
281
+ dir = integrationtest .TmpDir (t )
282
+ binds = integrationtest .DefaultBinds (t , dir )
291
283
)
292
- t .Cleanup (cancel )
293
284
294
285
pool , err := dockertest .NewPool ("" )
295
286
require .NoError (t , err )
296
287
288
+ // Create some listeners for the Docker and Coder
289
+ // services we'll be running with self signed certs.
297
290
bridgeIP := integrationtest .DockerBridgeIP (t )
298
- l , err := net .Listen ("tcp" , fmt .Sprintf ("%s:0" , bridgeIP ))
299
- require .NoError (t , err )
300
- defer l .Close ()
301
-
302
- host , port , err := net .SplitHostPort (l .Addr ().String ())
291
+ coderListener , err := net .Listen ("tcp" , fmt .Sprintf ("%s:0" , bridgeIP ))
303
292
require .NoError (t , err )
293
+ defer coderListener .Close ()
294
+ coderAddr := tcpAddr (t , coderListener )
304
295
305
296
registryListener , err := net .Listen ("tcp" , fmt .Sprintf ("%s:0" , bridgeIP ))
306
297
require .NoError (t , err )
307
298
err = registryListener .Close ()
308
299
require .NoError (t , err )
300
+ registryAddr := tcpAddr (t , registryListener )
309
301
310
- registryHost , registryPort , err := net . SplitHostPort ( registryListener . Addr () .String ())
311
- require . NoError (t , err )
302
+ coderCert := integrationtest . GenerateTLSCertificate ( t , "host.docker.internal" , coderAddr . IP .String ())
303
+ dockerCert := integrationtest . GenerateTLSCertificate (t , "host.docker.internal" , registryAddr . IP . String () )
312
304
313
- t .Logf ("registryHost: %s" , registryHost )
314
- coderCert := integrationtest .GenerateTLSCertificate (t , "host.docker.internal" , host )
315
- dockerCert := integrationtest .GenerateTLSCertificate (t , "host.docker.internal" , registryHost )
305
+ // Startup our fake Coder "control-plane".
306
+ recorder := integrationtest .FakeBuildLogRecorder (t , coderListener , coderCert )
316
307
317
- fakeServer , buildLogCh := fakeCoder (t )
318
- s := httptest .NewUnstartedServer (fakeServer )
319
- s .Listener = l
320
- s .TLS = & tls.Config {
321
- Certificates : []tls.Certificate {coderCert },
322
- }
323
- s .StartTLS ()
308
+ certDir := integrationtest .MkdirAll (t , dir , "certs" )
324
309
325
- certDir := filepath .Join (dir , "certs" )
326
- err = os .MkdirAll (certDir , 0777 )
327
- require .NoError (t , err )
310
+ // Write the Coder cert disk.
328
311
coderCertPath := filepath .Join (certDir , "coder_cert.pem" )
329
312
coderKeyPath := filepath .Join (certDir , "coder_key.pem" )
330
313
integrationtest .WriteCertificate (t , coderCert , coderCertPath , coderKeyPath )
331
- certMount := integrationtest .BindMount (certDir , "/tmp/certs" , false )
314
+ coderCertMount := integrationtest .BindMount (certDir , "/tmp/certs" , false )
332
315
316
+ // Write the Registry cert to disk.
333
317
regCertPath := filepath .Join (certDir , "registry_cert.crt" )
334
318
regKeyPath := filepath .Join (certDir , "registry_key.pem" )
335
319
integrationtest .WriteCertificate (t , dockerCert , regCertPath , regKeyPath )
336
320
321
+ // Start up the docker registry and push an image
322
+ // to it that we can reference.
337
323
image := integrationtest .RunLocalDockerRegistry (t , pool , integrationtest.RegistryConfig {
338
324
HostCertPath : regCertPath ,
339
325
HostKeyPath : regKeyPath ,
340
326
Image : integrationtest .UbuntuImage ,
341
- TLSPort : registryPort ,
327
+ TLSPort : strconv . Itoa ( registryAddr . Port ) ,
342
328
})
343
329
344
- innerImageHost := strings .Split (image , "/" )[0 ]
345
- t .Logf ("innerhost: %s" , innerImageHost )
346
- regCAPath := filepath .Join ("/etc/docker/certs.d" , innerImageHost , "ca.crt" )
347
- t .Log ("regcapath: " , regCAPath )
348
-
330
+ // Mount the cert into the expected location
331
+ // for the Envbox Docker daemon.
332
+ regCAPath := filepath .Join ("/etc/docker/certs.d" , image .Registry (), "ca.crt" )
349
333
registryCAMount := integrationtest .BindMount (regCertPath , regCAPath , false )
350
334
351
335
envs := []string {
352
336
integrationtest .EnvVar (cli .EnvAgentToken , "faketoken" ),
353
- integrationtest .EnvVar (cli .EnvAgentURL , fmt .Sprintf ("https://%s:%s " , "host.docker.internal" , port )),
337
+ integrationtest .EnvVar (cli .EnvAgentURL , fmt .Sprintf ("https://%s:%d " , "host.docker.internal" , coderAddr . Port )),
354
338
integrationtest .EnvVar (cli .EnvExtraCertsPath , "/tmp/certs" ),
355
339
}
356
340
357
- buildLogDone := waitForBuildLog (t , ctx , buildLogCh )
358
-
359
- t .Logf ("image: %s" , image )
360
341
// Run the envbox container.
361
342
_ = integrationtest .RunEnvbox (t , pool , & integrationtest.CreateDockerCVMConfig {
362
- Image : image ,
343
+ Image : image . String () ,
363
344
Username : "coder" ,
364
345
Envs : envs ,
365
- OuterMounts : append (binds , certMount , registryCAMount ),
346
+ OuterMounts : append (binds , coderCertMount , registryCAMount ),
366
347
})
367
348
368
- <- buildLogDone
349
+ // This indicates we've made it all the way to end
350
+ // of the logs we attempt to push.
351
+ require .True (t , recorder .ContainsLog ("Bootstrapping workspace..." ))
369
352
})
370
353
}
371
354
@@ -392,70 +375,10 @@ func requireSliceContains(t *testing.T, ss []string, els ...string) {
392
375
}
393
376
}
394
377
395
- func bindMount (src , dest string , ro bool ) string {
396
- if ro {
397
- return fmt .Sprintf ("%s:%s:%s" , src , dest , "ro" )
398
- }
399
- return fmt .Sprintf ("%s:%s" , src , dest )
400
- }
401
-
402
- func fakeCoder (t testing.TB ) (http.Handler , <- chan string ) {
378
+ func tcpAddr (t testing.TB , l net.Listener ) * net.TCPAddr {
403
379
t .Helper ()
404
380
405
- logCh := make (chan string )
406
- t .Cleanup (func () { close (logCh ) })
407
-
408
- mux := http .NewServeMux ()
409
- mux .Handle ("/api/v2/buildinfo" , http .HandlerFunc (
410
- func (w http.ResponseWriter , r * http.Request ) {
411
- w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
412
- w .WriteHeader (http .StatusOK )
413
-
414
- enc := json .NewEncoder (w )
415
- enc .SetEscapeHTML (true )
416
-
417
- // We can't really do much about these errors, it's probably due to a
418
- // dropped connection.
419
- _ = enc .Encode (& codersdk.BuildInfoResponse {
420
- Version : "v1.0.0" ,
421
- })
422
- }))
423
-
424
- mux .Handle ("/api/v2/workspaceagents/me/logs" , http .HandlerFunc (
425
- func (w http.ResponseWriter , r * http.Request ) {
426
- var logs agentsdk.PatchLogs
427
- err := json .NewDecoder (r .Body ).Decode (& logs )
428
- require .NoError (t , err )
429
- w .WriteHeader (http .StatusOK )
430
- for _ , log := range logs .Logs {
431
- logCh <- log .Output
432
- }
433
- }))
434
-
435
- mux .Handle ("/" , http .HandlerFunc (
436
- func (w http.ResponseWriter , r * http.Request ) {
437
- t .Fatalf ("unexpected route %v" , r .URL .Path )
438
- }))
439
-
440
- return mux , logCh
441
- }
442
-
443
- // todo this sucks refactor it.
444
- func waitForBuildLog (t testing.TB , ctx context.Context , buildLogCh <- chan string ) <- chan struct {} {
445
- t .Helper ()
446
- done := make (chan struct {})
447
- go func () {
448
- defer close (done )
449
- for {
450
- select {
451
- case <- ctx .Done ():
452
- t .Fatalf ("timed out waiting for final build log" )
453
- case log := <- buildLogCh :
454
- if log == "Bootstrapping workspace..." {
455
- return
456
- }
457
- }
458
- }
459
- }()
460
- return done
381
+ tcpAddr , ok := l .Addr ().(* net.TCPAddr )
382
+ require .True (t , ok )
383
+ return tcpAddr
461
384
}
0 commit comments