Skip to content

feat: capture cli logs in tests #10669

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 1 commit into from
Nov 14, 2023
Merged

feat: capture cli logs in tests #10669

merged 1 commit into from
Nov 14, 2023

Conversation

spikecurtis
Copy link
Contributor

@spikecurtis spikecurtis commented Nov 14, 2023

Adds a Logger to cli Invocation and standardizes CLI commands to use it. clitest creates a test logger by default so that CLI command logs are captured in the test logs.

CLI commands that do their own log configuration are modified to add sinks to the existing logger, rather than create a new one. This ensures we still capture logs in CLI tests.

Copy link
Contributor Author

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

@spikecurtis spikecurtis force-pushed the spike/10663-race-sink-write-close branch from 31c8047 to fcd5e39 Compare November 14, 2023 12:24
@spikecurtis spikecurtis force-pushed the spike/cli-logs-in-tests branch from bea860d to ca45c99 Compare November 14, 2023 12:32
@@ -44,17 +44,17 @@ func TestWorkspaceAgent(t *testing.T) {
"--log-dir", logDir,
)

pty := ptytest.New(t).Attach(inv)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attaching a PTY to agent invocation results in logging twice to the test log: once via our logger and a second time via stderr. I modified the test to not need the PTY at all.

Base automatically changed from spike/10663-race-sink-write-close to main November 14, 2023 12:38
"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"

"github.com/coder/coder/v2/testutil"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import order is messed up here. Usually due to editor inserting on autocomplete but in the wrong place and formatter not being strict to repair it :/.

@@ -170,6 +172,7 @@ func (c *Cmd) Invoke(args ...string) *Invocation {
Stdout: io.Discard,
Stderr: io.Discard,
Stdin: strings.NewReader(""),
Logger: slog.Make(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least for tests, this could be a named logger based on the command being run? Might help when there are multiple commands being run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, it's just called "cli" for any Invocations created by clitest.New(). You can override it in any complex test by setting inv.Logger before starting the command.

d.Lock()
defer d.Unlock()
if d.closed {
return len(p), nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does slog print errors if we return io.EOF here? If yes and since this writer has no way to inform routines using it to stop, should we rename it something slog specific since we're essentially working around the fact that slog doesn't let you close it or close/detach sinks.

Also, if we need this at all, it means we're leaving goroutines behind when exciting, right? Often it's not a problem but it could mean we're not shutting down things in the proper order. This would essentially hide that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does slog print errors if we return io.EOF here?

the Logger does not, but some of the Sinks call println if they get an error writing, which I'd like to avoid.

If yes and since this writer has no way to inform routines using it to stop, should we rename it something slog specific since we're essentially working around the fact that slog doesn't let you close it or close/detach sinks.

I did mention slog in the comment on the constructor, but I'm open to a name change if you have a suggestion.

Also, if we need this at all, it means we're leaving goroutines behind when exciting, right? Often it's not a problem but it could mean we're not shutting down things in the proper order. This would essentially hide that.

Yes, many of our components just use a Context expiring to know when to shut down, and others that have an explicit Close() don't wait for all the goroutines to exit before returning. These components are still "correct" in the sense that the goroutines do exit eventually---they're not leaks, just shut down is async with no way to wait for it.

We could mandate that everything has a Close() and it doesn't return until every goroutine is shut down, but that's pretty heavy handed and a lot of work.

If there are actual leaked goroutines, then we have goleak to detect those in tests. It's not worth trying to use the logger to police that.

@spikecurtis spikecurtis force-pushed the spike/cli-logs-in-tests branch from ca45c99 to ac451ac Compare November 14, 2023 18:47
@spikecurtis spikecurtis merged commit 4894eda into main Nov 14, 2023
@spikecurtis spikecurtis deleted the spike/cli-logs-in-tests branch November 14, 2023 18:56
Copy link
Contributor Author

Merge activity

ericpaulsen pushed a commit to lbi22/coder that referenced this pull request Nov 27, 2023
Adds a Logger to cli Invocation and standardizes CLI commands to use it.  clitest creates a test logger by default so that CLI command logs are captured in the test logs.

CLI commands that do their own log configuration are modified to add sinks to the existing logger, rather than create a new one.  This ensures we still capture logs in CLI tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants