Skip to content

fix(agent/agentcontainers): fix TestDevcontainerDiscovery/AutoStart flake #19179

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 2 commits into from
Aug 5, 2025

Conversation

DanielleMaywood
Copy link
Contributor

… flake

Fixes coder/internal#864

I've ripped out the mock infrastructure for this test, and have instead
replaced it with our `fakeContainerCLI` and `fakeDevcontainerCLI`
utilities.
@DanielleMaywood DanielleMaywood marked this pull request as ready for review August 5, 2025 12:28
@DanielleMaywood
Copy link
Contributor Author

@coderabbitai review

@DanielleMaywood DanielleMaywood requested a review from Copilot August 5, 2025 12:28
Copy link

coderabbitai bot commented Aug 5, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a flaky test TestDevcontainerDiscovery/AutoStart by replacing gomock-based mocking with a deterministic fake implementation. The test was likely flaking due to race conditions or timing issues with mock expectations.

Key changes:

  • Replace gomock mocking with a deterministic fake devcontainer CLI implementation
  • Add explicit tracking of Up method calls to verify expected behavior
  • Simplify test structure by using data-driven configuration maps instead of complex mock setups
Comments suppressed due to low confidence (1)

agent/agentcontainers/api_test.go:773

  • The assertion should use assert.Equal instead of require.Equal since this is inside a loop and a failure here should not prevent checking other configurations. Using require.Equal will stop the test immediately on first failure.
					require.ErrorContains(t, got, tc.expectedErr, "want error")

Copy link

coderabbitai bot commented Aug 5, 2025

📝 Walkthrough

Walkthrough

The TestDevcontainerDiscovery test in agent/agentcontainers/api_test.go was refactored and enhanced to robustly verify the auto-start behavior of devcontainers. The test infrastructure now uses a configurable fake CLI implementation, enabling dynamic configuration and tracking of which devcontainers are started based on their AutoStart setting. The test covers multiple auto-start scenarios and asserts correct behavior.

Changes

Cohort / File(s) Change Summary
Devcontainer Discovery Test Refactor
agent/agentcontainers/api_test.go
Refactored TestDevcontainerDiscovery to use a custom fakeDevcontainerCLI with dynamic config and Up tracking. Added/updated subtests for various auto-start scenarios. Modified method signatures and struct fields to support new test logic. Removed gomock usage in favor of internal call tracking.

Sequence Diagram(s)

sequenceDiagram
    participant Test
    participant FakeDevcontainerCLI
    participant API

    Test->>FakeDevcontainerCLI: Set up configMap and up function
    Test->>API: Start API with FakeDevcontainerCLI
    API->>FakeDevcontainerCLI: ReadConfig(configPath)
    FakeDevcontainerCLI-->>API: Return DevcontainerConfig (with AutoStart)
    API->>FakeDevcontainerCLI: Up(workspaceFolder, configPath) [if AutoStart]
    FakeDevcontainerCLI-->>API: Record Up call
    Test->>API: Wait for discovery and Up calls
    Test->>Test: Assert correct Up invocations and devcontainer count
    Test->>API: Close API
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Assessment against linked issues

Objective Addressed Explanation
Ensure TestDevcontainerDiscovery/AutoStart/MultipleEnabled reliably verifies multiple enabled devcontainers auto-start (#864)
Remove reliance on gomock for Up calls in devcontainer discovery tests (#864)
Track and assert correct Up invocations for devcontainers with AutoStart enabled (#864)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes were found.

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8b66a5a and 72514f8.

📒 Files selected for processing (1)
  • agent/agentcontainers/api_test.go (5 hunks)
🔇 Additional comments (4)
agent/agentcontainers/api_test.go (4)

74-85: Good refactoring to improve test flexibility.

The addition of the up function field and configMap enables dynamic behavior in tests, making it easier to verify which devcontainers are started based on their configuration. This is a cleaner approach than using mocks for this use case.


87-102: Proper implementation of configurable Up behavior.

The method correctly delegates to the custom up function when provided, while maintaining backward compatibility with the channel-based approach for existing tests.


118-135: Excellent implementation of path-specific configuration.

The ReadConfig method now properly supports returning different configurations based on the config path, which is crucial for testing multiple devcontainers with varying AutoStart settings.


3583-3846: Excellent refactoring of the AutoStart test to address flakiness.

The test has been significantly improved:

  1. Replaced gomock expectations with a deterministic fake implementation that tracks Up calls
  2. Added comprehensive test scenarios covering different AutoStart configurations
  3. Uses proper synchronization with mutex to track concurrent operations
  4. Verifies both the discovery of devcontainers and that Up is called only for those with AutoStart: true

This approach should resolve the flaky test issue by providing better control over the test execution flow and eliminating timing dependencies.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch danielle/fix-devcontainer-flake

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏻

@DanielleMaywood DanielleMaywood merged commit 760dc8b into main Aug 5, 2025
33 checks passed
@DanielleMaywood DanielleMaywood deleted the danielle/fix-devcontainer-flake branch August 5, 2025 12:58
@github-actions github-actions bot locked and limited conversation to collaborators Aug 5, 2025
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

Some (belated) comments below, but nothing blocking.

autoStart := config.Configuration.Customizations.Coder.AutoStart
wasUpCalled := upCalledFor[configPath]

require.Equal(t, autoStart, wasUpCalled)
Copy link
Member

@johnstcn johnstcn Aug 5, 2025

Choose a reason for hiding this comment

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

Suggest assert here instead.

// And: `up` was called on the correct containers
for configPath, config := range tt.configMap {
autoStart := config.Configuration.Customizations.Coder.AutoStart
wasUpCalled := upCalledFor[configPath]
Copy link
Member

Choose a reason for hiding this comment

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

Will this handle the case where we call up for something not in config.Configuration.Customizations.Coder.AutoStart?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flake] TestDevcontainerDiscovery/AutoStart/MultipleEnabled
3 participants