Skip to content

Commit b44ae40

Browse files
authored
test(cli): speed up stackdriver logging tests by setting GCE env (#16298)
1 parent ab92306 commit b44ae40

File tree

1 file changed

+116
-93
lines changed

1 file changed

+116
-93
lines changed

cli/server_test.go

Lines changed: 116 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,26 +1387,6 @@ func TestServer(t *testing.T) {
13871387
})
13881388
})
13891389

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-
14101390
t.Run("Logging", func(t *testing.T) {
14111391
t.Parallel()
14121392

@@ -1426,7 +1406,7 @@ func TestServer(t *testing.T) {
14261406
)
14271407
clitest.Start(t, root)
14281408

1429-
waitFile(t, fiName, testutil.WaitLong)
1409+
loggingWaitFile(t, fiName, testutil.WaitLong)
14301410
})
14311411

14321412
t.Run("Human", func(t *testing.T) {
@@ -1445,7 +1425,7 @@ func TestServer(t *testing.T) {
14451425
)
14461426
clitest.Start(t, root)
14471427

1448-
waitFile(t, fi, testutil.WaitShort)
1428+
loggingWaitFile(t, fi, testutil.WaitShort)
14491429
})
14501430

14511431
t.Run("JSON", func(t *testing.T) {
@@ -1464,77 +1444,7 @@ func TestServer(t *testing.T) {
14641444
)
14651445
clitest.Start(t, root)
14661446

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)
1447+
loggingWaitFile(t, fi, testutil.WaitShort)
15381448
})
15391449
})
15401450

@@ -1629,6 +1539,119 @@ func TestServer(t *testing.T) {
16291539
})
16301540
}
16311541

1542+
//nolint:tparallel,paralleltest // This test sets environment variables.
1543+
func TestServer_Logging_NoParallel(t *testing.T) {
1544+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1545+
_, _ = io.Copy(io.Discard, r.Body)
1546+
_ = r.Body.Close()
1547+
w.WriteHeader(http.StatusOK)
1548+
}))
1549+
t.Cleanup(func() { server.Close() })
1550+
1551+
// Speed up stackdriver test by using custom host. This is like
1552+
// saying we're running on GCE, so extra checks are skipped.
1553+
//
1554+
// Note, that the server isn't actually hit by the test, unsure why
1555+
// but kept just in case.
1556+
//
1557+
// From cloud.google.com/go/compute/metadata/metadata.go (used by coder/slog):
1558+
//
1559+
// metadataHostEnv is the environment variable specifying the
1560+
// GCE metadata hostname. If empty, the default value of
1561+
// metadataIP ("169.254.169.254") is used instead.
1562+
// This is variable name is not defined by any spec, as far as
1563+
// I know; it was made up for the Go package.
1564+
t.Setenv("GCE_METADATA_HOST", server.URL)
1565+
1566+
t.Run("Stackdriver", func(t *testing.T) {
1567+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
1568+
defer cancelFunc()
1569+
1570+
fi := testutil.TempFile(t, "", "coder-logging-test-*")
1571+
1572+
inv, _ := clitest.New(t,
1573+
"server",
1574+
"--log-filter=.*",
1575+
"--in-memory",
1576+
"--http-address", ":0",
1577+
"--access-url", "http://example.com",
1578+
"--provisioner-daemons=3",
1579+
"--provisioner-types=echo",
1580+
"--log-stackdriver", fi,
1581+
)
1582+
// Attach pty so we get debug output from the command if this test
1583+
// fails.
1584+
pty := ptytest.New(t).Attach(inv)
1585+
1586+
clitest.Start(t, inv.WithContext(ctx))
1587+
1588+
// Wait for server to listen on HTTP, this is a good
1589+
// starting point for expecting logs.
1590+
_ = pty.ExpectMatchContext(ctx, "Started HTTP listener at")
1591+
1592+
loggingWaitFile(t, fi, testutil.WaitSuperLong)
1593+
})
1594+
1595+
t.Run("Multiple", func(t *testing.T) {
1596+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
1597+
defer cancelFunc()
1598+
1599+
fi1 := testutil.TempFile(t, "", "coder-logging-test-*")
1600+
fi2 := testutil.TempFile(t, "", "coder-logging-test-*")
1601+
fi3 := testutil.TempFile(t, "", "coder-logging-test-*")
1602+
1603+
// NOTE(mafredri): This test might end up downloading Terraform
1604+
// which can take a long time and end up failing the test.
1605+
// This is why we wait extra long below for server to listen on
1606+
// HTTP.
1607+
inv, _ := clitest.New(t,
1608+
"server",
1609+
"--log-filter=.*",
1610+
"--in-memory",
1611+
"--http-address", ":0",
1612+
"--access-url", "http://example.com",
1613+
"--provisioner-daemons=3",
1614+
"--provisioner-types=echo",
1615+
"--log-human", fi1,
1616+
"--log-json", fi2,
1617+
"--log-stackdriver", fi3,
1618+
)
1619+
// Attach pty so we get debug output from the command if this test
1620+
// fails.
1621+
pty := ptytest.New(t).Attach(inv)
1622+
1623+
clitest.Start(t, inv)
1624+
1625+
// Wait for server to listen on HTTP, this is a good
1626+
// starting point for expecting logs.
1627+
_ = pty.ExpectMatchContext(ctx, "Started HTTP listener at")
1628+
1629+
loggingWaitFile(t, fi1, testutil.WaitSuperLong)
1630+
loggingWaitFile(t, fi2, testutil.WaitSuperLong)
1631+
loggingWaitFile(t, fi3, testutil.WaitSuperLong)
1632+
})
1633+
}
1634+
1635+
func loggingWaitFile(t *testing.T, fiName string, dur time.Duration) {
1636+
var lastStat os.FileInfo
1637+
require.Eventually(t, func() bool {
1638+
var err error
1639+
lastStat, err = os.Stat(fiName)
1640+
if err != nil {
1641+
if !os.IsNotExist(err) {
1642+
t.Fatalf("unexpected error: %v", err)
1643+
}
1644+
return false
1645+
}
1646+
return lastStat.Size() > 0
1647+
},
1648+
dur, //nolint:gocritic
1649+
testutil.IntervalFast,
1650+
"file at %s should exist, last stat: %+v",
1651+
fiName, lastStat,
1652+
)
1653+
}
1654+
16321655
func TestServer_Production(t *testing.T) {
16331656
t.Parallel()
16341657
if runtime.GOOS != "linux" || testing.Short() {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy