Thanks to visit codestin.com
Credit goes to github.com

Skip to content

test: Fix flaky TestServer/Logging/{Multiple,Stackdriver} #5727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 60 additions & 63 deletions cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/coder/coder/coderd/database/postgres"
"github.com/coder/coder/coderd/telemetry"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/cryptorand"
"github.com/coder/coder/pty/ptytest"
"github.com/coder/coder/testutil"
)
Expand Down Expand Up @@ -1132,12 +1131,7 @@ func TestServer(t *testing.T) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

random, err := cryptorand.String(5)
require.NoError(t, err)
fiName := fmt.Sprint(os.TempDir(), "/coder-logging-test-", random)
defer func() {
_ = os.Remove(fiName)
}()
fiName := testutil.TempFile(t, "", "coder-logging-test-*")

root, _ := clitest.New(t,
"server",
Expand Down Expand Up @@ -1165,27 +1159,23 @@ func TestServer(t *testing.T) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

fi, err := os.CreateTemp("", "coder-logging-test-*")
require.NoError(t, err)
defer func() {
_ = os.Remove(fi.Name())
}()
fi := testutil.TempFile(t, "", "coder-logging-test-*")

root, _ := clitest.New(t,
"server",
"--verbose",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
"--log-human", fi.Name(),
"--log-human", fi,
)
serverErr := make(chan error, 1)
go func() {
serverErr <- root.ExecuteContext(ctx)
}()

assert.Eventually(t, func() bool {
stat, err := os.Stat(fi.Name())
stat, err := os.Stat(fi)
return err == nil && stat.Size() > 0
}, testutil.WaitShort, testutil.IntervalFast)
cancelFunc()
Expand All @@ -1197,27 +1187,23 @@ func TestServer(t *testing.T) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

fi, err := os.CreateTemp("", "coder-logging-test-*")
require.NoError(t, err)
defer func() {
_ = os.Remove(fi.Name())
}()
fi := testutil.TempFile(t, "", "coder-logging-test-*")

root, _ := clitest.New(t,
"server",
"--verbose",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
"--log-json", fi.Name(),
"--log-json", fi,
)
serverErr := make(chan error, 1)
go func() {
serverErr <- root.ExecuteContext(ctx)
}()

assert.Eventually(t, func() bool {
stat, err := os.Stat(fi.Name())
stat, err := os.Stat(fi)
return err == nil && stat.Size() > 0
}, testutil.WaitShort, testutil.IntervalFast)
cancelFunc()
Expand All @@ -1229,86 +1215,97 @@ func TestServer(t *testing.T) {
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

fi, err := os.CreateTemp("", "coder-logging-test-*")
require.NoError(t, err)
defer func() {
_ = os.Remove(fi.Name())
}()
fi := testutil.TempFile(t, "", "coder-logging-test-*")

root, _ := clitest.New(t,
"server",
"--verbose",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
"--log-stackdriver", fi.Name(),
"--log-stackdriver", fi,
)
// Attach pty so we get debug output from the command if this test
// fails.
pty := ptytest.New(t)
root.SetOut(pty.Output())
root.SetErr(pty.Output())

serverErr := make(chan error, 1)
go func() {
serverErr <- root.ExecuteContext(ctx)
}()
defer func() {
cancelFunc()
<-serverErr
}()

assert.Eventually(t, func() bool {
stat, err := os.Stat(fi.Name())
require.Eventually(t, func() bool {
line := pty.ReadLine()
return strings.HasPrefix(line, "Started HTTP listener at ")
}, testutil.WaitLong*2, testutil.IntervalMedium, "wait for server to listen on http")

require.Eventually(t, func() bool {
stat, err := os.Stat(fi)
return err == nil && stat.Size() > 0
}, testutil.WaitLong, testutil.IntervalMedium)
cancelFunc()
<-serverErr
})

t.Run("Multiple", func(t *testing.T) {
t.Parallel()
ctx, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

fi1, err := os.CreateTemp("", "coder-logging-test-*")
require.NoError(t, err)
defer func() {
_ = os.Remove(fi1.Name())
}()

fi2, err := os.CreateTemp("", "coder-logging-test-*")
require.NoError(t, err)
defer func() {
_ = os.Remove(fi2.Name())
}()

fi3, err := os.CreateTemp("", "coder-logging-test-*")
require.NoError(t, err)
defer func() {
_ = os.Remove(fi3.Name())
}()
fi1 := testutil.TempFile(t, "", "coder-logging-test-*")
fi2 := testutil.TempFile(t, "", "coder-logging-test-*")
fi3 := testutil.TempFile(t, "", "coder-logging-test-*")

// NOTE(mafredri): This test might end up downloading Terraform
// which can take a long time and end up failing the test.
// This is why we wait extra long below for server to listen on
// HTTP.
root, _ := clitest.New(t,
"server",
"--verbose",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
"--log-human", fi1.Name(),
"--log-json", fi2.Name(),
"--log-stackdriver", fi3.Name(),
"--log-human", fi1,
"--log-json", fi2,
"--log-stackdriver", fi3,
)
// Attach pty so we get debug output from the command if this test
// fails.
pty := ptytest.New(t)
root.SetOut(pty.Output())
root.SetErr(pty.Output())

serverErr := make(chan error, 1)
go func() {
serverErr <- root.ExecuteContext(ctx)
}()
defer func() {
cancelFunc()
<-serverErr
}()

assert.Eventually(t, func() bool {
stat, err := os.Stat(fi1.Name())
require.Eventually(t, func() bool {
line := pty.ReadLine()
return strings.HasPrefix(line, "Started HTTP listener at ")
}, testutil.WaitLong*2, testutil.IntervalMedium, "wait for server to listen on http")

require.Eventually(t, func() bool {
stat, err := os.Stat(fi1)
return err == nil && stat.Size() > 0
}, testutil.WaitLong, testutil.IntervalMedium)
assert.Eventually(t, func() bool {
stat, err := os.Stat(fi2.Name())
}, testutil.WaitShort, testutil.IntervalMedium, "log human size > 0")
require.Eventually(t, func() bool {
stat, err := os.Stat(fi2)
return err == nil && stat.Size() > 0
}, testutil.WaitLong, testutil.IntervalMedium)
assert.Eventually(t, func() bool {
stat, err := os.Stat(fi3.Name())
}, testutil.WaitShort, testutil.IntervalMedium, "log json size > 0")
require.Eventually(t, func() bool {
stat, err := os.Stat(fi3)
return err == nil && stat.Size() > 0
}, testutil.WaitLong, testutil.IntervalMedium)

cancelFunc()
<-serverErr
}, testutil.WaitShort, testutil.IntervalMedium, "log stackdriver size > 0")
})
})
}
Expand Down
47 changes: 47 additions & 0 deletions testutil/temp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package testutil

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

// TempFile returns the name of a temporary file that does not exist.
func TempFile(t *testing.T, dir, pattern string) string {
t.Helper()

if dir == "" {
dir = t.TempDir()
}
f, err := os.CreateTemp(dir, pattern)
require.NoError(t, err, "create temp file")
name := f.Name()
err = f.Close()
require.NoError(t, err, "close temp file")
err = os.Remove(name)
require.NoError(t, err, "remove temp file")

return name
}

// CreateTemp is a convenience function for creating a temporary file, like
// os.CreateTemp, but it also registers a cleanup function to close and remove
// the file.
func CreateTemp(t *testing.T, dir, pattern string) *os.File {
t.Helper()

if dir == "" {
dir = t.TempDir()
}
f, err := os.CreateTemp(dir, pattern)
require.NoError(t, err, "create temp file")
t.Cleanup(func() {
_ = f.Close()
err = os.Remove(f.Name())
if err != nil {
t.Logf("CreateTemp: Cleanup: remove failed for %q: %v", f.Name(), err)
}
})
return f
}