@@ -14,6 +14,7 @@ import (
1414"encoding/pem"
1515"fmt"
1616"io"
17+ "io/ioutil"
1718"math/big"
1819"net"
1920"net/http"
@@ -1387,26 +1388,6 @@ func TestServer(t *testing.T) {
13871388})
13881389})
13891390
1390- waitFile := func (t * testing.T ,fiName string ,dur time.Duration ) {
1391- var lastStat os.FileInfo
1392- require .Eventually (t ,func ()bool {
1393- var err error
1394- lastStat ,err = os .Stat (fiName )
1395- if err != nil {
1396- if ! os .IsNotExist (err ) {
1397- t .Fatalf ("unexpected error: %v" ,err )
1398- }
1399- return false
1400- }
1401- return lastStat .Size ()> 0
1402- },
1403- dur ,//nolint:gocritic
1404- testutil .IntervalFast ,
1405- "file at %s should exist, last stat: %+v" ,
1406- fiName ,lastStat ,
1407- )
1408- }
1409-
14101391t .Run ("Logging" ,func (t * testing.T ) {
14111392t .Parallel ()
14121393
@@ -1426,7 +1407,7 @@ func TestServer(t *testing.T) {
14261407)
14271408clitest .Start (t ,root )
14281409
1429- waitFile (t ,fiName ,testutil .WaitLong )
1410+ loggingWaitFile (t ,fiName ,testutil .WaitLong )
14301411})
14311412
14321413t .Run ("Human" ,func (t * testing.T ) {
@@ -1445,7 +1426,7 @@ func TestServer(t *testing.T) {
14451426)
14461427clitest .Start (t ,root )
14471428
1448- waitFile (t ,fi ,testutil .WaitShort )
1429+ loggingWaitFile (t ,fi ,testutil .WaitShort )
14491430})
14501431
14511432t .Run ("JSON" ,func (t * testing.T ) {
@@ -1464,77 +1445,7 @@ func TestServer(t *testing.T) {
14641445)
14651446clitest .Start (t ,root )
14661447
1467- waitFile (t ,fi ,testutil .WaitShort )
1468- })
1469-
1470- t .Run ("Stackdriver" ,func (t * testing.T ) {
1471- t .Parallel ()
1472- ctx ,cancelFunc := context .WithTimeout (context .Background (),testutil .WaitSuperLong )
1473- defer cancelFunc ()
1474-
1475- fi := testutil .TempFile (t ,"" ,"coder-logging-test-*" )
1476-
1477- inv ,_ := clitest .New (t ,
1478- "server" ,
1479- "--log-filter=.*" ,
1480- "--in-memory" ,
1481- "--http-address" ,":0" ,
1482- "--access-url" ,"http://example.com" ,
1483- "--provisioner-daemons=3" ,
1484- "--provisioner-types=echo" ,
1485- "--log-stackdriver" ,fi ,
1486- )
1487- // Attach pty so we get debug output from the command if this test
1488- // fails.
1489- pty := ptytest .New (t ).Attach (inv )
1490-
1491- clitest .Start (t ,inv .WithContext (ctx ))
1492-
1493- // Wait for server to listen on HTTP, this is a good
1494- // starting point for expecting logs.
1495- _ = pty .ExpectMatchContext (ctx ,"Started HTTP listener at" )
1496-
1497- waitFile (t ,fi ,testutil .WaitSuperLong )
1498- })
1499-
1500- t .Run ("Multiple" ,func (t * testing.T ) {
1501- t .Parallel ()
1502- ctx ,cancelFunc := context .WithTimeout (context .Background (),testutil .WaitSuperLong )
1503- defer cancelFunc ()
1504-
1505- fi1 := testutil .TempFile (t ,"" ,"coder-logging-test-*" )
1506- fi2 := testutil .TempFile (t ,"" ,"coder-logging-test-*" )
1507- fi3 := testutil .TempFile (t ,"" ,"coder-logging-test-*" )
1508-
1509- // NOTE(mafredri): This test might end up downloading Terraform
1510- // which can take a long time and end up failing the test.
1511- // This is why we wait extra long below for server to listen on
1512- // HTTP.
1513- inv ,_ := clitest .New (t ,
1514- "server" ,
1515- "--log-filter=.*" ,
1516- "--in-memory" ,
1517- "--http-address" ,":0" ,
1518- "--access-url" ,"http://example.com" ,
1519- "--provisioner-daemons=3" ,
1520- "--provisioner-types=echo" ,
1521- "--log-human" ,fi1 ,
1522- "--log-json" ,fi2 ,
1523- "--log-stackdriver" ,fi3 ,
1524- )
1525- // Attach pty so we get debug output from the command if this test
1526- // fails.
1527- pty := ptytest .New (t ).Attach (inv )
1528-
1529- clitest .Start (t ,inv )
1530-
1531- // Wait for server to listen on HTTP, this is a good
1532- // starting point for expecting logs.
1533- _ = pty .ExpectMatchContext (ctx ,"Started HTTP listener at" )
1534-
1535- waitFile (t ,fi1 ,testutil .WaitSuperLong )
1536- waitFile (t ,fi2 ,testutil .WaitSuperLong )
1537- waitFile (t ,fi3 ,testutil .WaitSuperLong )
1448+ loggingWaitFile (t ,fi ,testutil .WaitShort )
15381449})
15391450})
15401451
@@ -1629,6 +1540,123 @@ func TestServer(t *testing.T) {
16291540})
16301541}
16311542
1543+ //nolint:tparallel,paralleltest // This test sets environment variables.
1544+ func TestServer_Logging_NoParallel (t * testing.T ) {
1545+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter ,r * http.Request ) {
1546+ _ ,_ = io .Copy (ioutil .Discard ,r .Body )
1547+ _ = r .Body .Close ()
1548+ w .WriteHeader (http .StatusOK )
1549+ }))
1550+ t .Cleanup (func () {server .Close () })
1551+
1552+ // Speed up stackdriver test by using custom host. This is like
1553+ // saying we're running on GCE, so extra checks are skipped.
1554+ //
1555+ // Note, that the server isn't actually hit by the test, unsure why
1556+ // but kept just in case.
1557+ //
1558+ // From cloud.google.com/go/compute/metadata/metadata.go (used by coder/slog):
1559+ //
1560+ // metadataHostEnv is the environment variable specifying the
1561+ // GCE metadata hostname. If empty, the default value of
1562+ // metadataIP ("169.254.169.254") is used instead.
1563+ // This is variable name is not defined by any spec, as far as
1564+ // I know; it was made up for the Go package.
1565+ t .Setenv ("GCE_METADATA_HOST" ,server .URL )
1566+
1567+ t .Run ("Stackdriver" ,func (t * testing.T ) {
1568+ ctx ,cancelFunc := context .WithTimeout (context .Background (),testutil .WaitSuperLong )
1569+ defer cancelFunc ()
1570+
1571+ fi := testutil .TempFile (t ,"" ,"coder-logging-test-*" )
1572+
1573+ inv ,_ := clitest .New (t ,
1574+ "server" ,
1575+ "--log-filter=.*" ,
1576+ "--in-memory" ,
1577+ "--http-address" ,":0" ,
1578+ "--access-url" ,"http://example.com" ,
1579+ "--provisioner-daemons=3" ,
1580+ "--provisioner-types=echo" ,
1581+ "--log-stackdriver" ,fi ,
1582+ )
1583+ // Attach pty so we get debug output from the command if this test
1584+ // fails.
1585+ pty := ptytest .New (t ).Attach (inv )
1586+
1587+ clitest .Start (t ,inv .WithContext (ctx ))
1588+
1589+ // Wait for server to listen on HTTP, this is a good
1590+ // starting point for expecting logs.
1591+ _ = pty .ExpectMatchContext (ctx ,"Started HTTP listener at" )
1592+
1593+ loggingWaitFile (t ,fi ,testutil .WaitSuperLong )
1594+ b ,_ := ioutil .ReadFile (fi )
1595+ t .Logf ("file contents: %s" ,b )
1596+ t .Log ("FOUND FILE!!" )
1597+ // time.Sleep(25 * time.Second)
1598+ })
1599+
1600+ t .Run ("Multiple" ,func (t * testing.T ) {
1601+ ctx ,cancelFunc := context .WithTimeout (context .Background (),testutil .WaitSuperLong )
1602+ defer cancelFunc ()
1603+
1604+ fi1 := testutil .TempFile (t ,"" ,"coder-logging-test-*" )
1605+ fi2 := testutil .TempFile (t ,"" ,"coder-logging-test-*" )
1606+ fi3 := testutil .TempFile (t ,"" ,"coder-logging-test-*" )
1607+
1608+ // NOTE(mafredri): This test might end up downloading Terraform
1609+ // which can take a long time and end up failing the test.
1610+ // This is why we wait extra long below for server to listen on
1611+ // HTTP.
1612+ inv ,_ := clitest .New (t ,
1613+ "server" ,
1614+ "--log-filter=.*" ,
1615+ "--in-memory" ,
1616+ "--http-address" ,":0" ,
1617+ "--access-url" ,"http://example.com" ,
1618+ "--provisioner-daemons=3" ,
1619+ "--provisioner-types=echo" ,
1620+ "--log-human" ,fi1 ,
1621+ "--log-json" ,fi2 ,
1622+ "--log-stackdriver" ,fi3 ,
1623+ )
1624+ // Attach pty so we get debug output from the command if this test
1625+ // fails.
1626+ pty := ptytest .New (t ).Attach (inv )
1627+
1628+ clitest .Start (t ,inv )
1629+
1630+ // Wait for server to listen on HTTP, this is a good
1631+ // starting point for expecting logs.
1632+ _ = pty .ExpectMatchContext (ctx ,"Started HTTP listener at" )
1633+
1634+ loggingWaitFile (t ,fi1 ,testutil .WaitSuperLong )
1635+ loggingWaitFile (t ,fi2 ,testutil .WaitSuperLong )
1636+ loggingWaitFile (t ,fi3 ,testutil .WaitSuperLong )
1637+ })
1638+ }
1639+
1640+ func loggingWaitFile (t * testing.T ,fiName string ,dur time.Duration ) {
1641+ var lastStat os.FileInfo
1642+ require .Eventually (t ,func ()bool {
1643+ var err error
1644+ lastStat ,err = os .Stat (fiName )
1645+ if err != nil {
1646+ if ! os .IsNotExist (err ) {
1647+ t .Fatalf ("unexpected error: %v" ,err )
1648+ }
1649+ return false
1650+ }
1651+ return lastStat .Size ()> 0
1652+ },
1653+ dur ,//nolint:gocritic
1654+ testutil .IntervalFast ,
1655+ "file at %s should exist, last stat: %+v" ,
1656+ fiName ,lastStat ,
1657+ )
1658+ }
1659+
16321660func TestServer_Production (t * testing.T ) {
16331661t .Parallel ()
16341662if runtime .GOOS != "linux" || testing .Short () {