Skip to content

feat: add unit tests for ferrogw-cli command behavior - #12

Closed
VedantMadane wants to merge 3 commits into
ferro-labs:mainfrom
VedantMadane:feat/add-cli-tests
Closed

feat: add unit tests for ferrogw-cli command behavior#12
VedantMadane wants to merge 3 commits into
ferro-labs:mainfrom
VedantMadane:feat/add-cli-tests

Conversation

@VedantMadane

Copy link
Copy Markdown

Fixes #11. This PR refactors the CLI entry point to be testable and adds comprehensive unit tests for command dispatch.

This PR adds dedicated unit tests for the ferrogw-cli tool by:
1. Refactoring cmd/ferrogw-cli/main.go to extract command dispatch into a testable execute function.
2. Adding cmd/ferrogw-cli/main_test.go to validate behavior for help, version, plugins, and unknown commands.

Copilot AI left a comment

Copy link
Copy Markdown

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 makes the ferrogw-cli entry point testable by extracting command dispatch into a function that returns an exit code, and adds unit tests to prevent regressions in user-facing CLI behavior.

Changes:

  • Refactor cmd/ferrogw-cli/main.go to route through execute(args, stdout, stderr) int and return exit codes instead of calling os.Exit throughout.
  • Add cmd/ferrogw-cli/main_test.go table-driven tests for core command dispatch paths (usage/help, version, plugins, unknown command).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
cmd/ferrogw-cli/main.go Refactors CLI dispatch to a testable execute(...) int function with injected writers and explicit exit codes.
cmd/ferrogw-cli/main_test.go Adds unit tests validating exit codes and stdout/stderr output patterns for several commands.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

expectedStdout: "Usage:",
expectedStderr: "",
},
{

Copilot AI Mar 2, 2026

Copy link

Choose a reason for hiding this comment

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

Tests cover help and -h, but the issue/acceptance criteria also calls out --help. Add a test case for ferrogw-cli --help to ensure that flag remains supported and prints usage with exit code 0.

Suggested change
{
{
name: "--help flag",
args: []string{"ferrogw-cli", "--help"},
expectedExit: 0,
expectedStdout: "Usage:",
expectedStderr: "",
},
{

Copilot uses AI. Check for mistakes.
Comment on lines +48 to +53
name: "plugins command",
args: []string{"ferrogw-cli", "plugins"},
expectedExit: 0,
expectedStdout: "Registered plugins:",
expectedStderr: "",
},

Copilot AI Mar 2, 2026

Copy link

Choose a reason for hiding this comment

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

The plugins test currently only asserts the header text. To better validate “lists registered plugins”, also assert that at least one known built-in plugin name (e.g. response-cache, request-logger, etc.) appears in stdout.

Copilot uses AI. Check for mistakes.

@MitulShah1 MitulShah1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks just need to fix minor changes

@MitulShah1

Copy link
Copy Markdown
Contributor

Hey Thanks for contribution but i think this issue is not anymore valid. i've added few new issues please have a look.

@MitulShah1 MitulShah1 closed this Mar 7, 2026
MitulShah1 pushed a commit that referenced this pull request Jun 15, 2026
…cording correctness (#199)

* fix(gateway): wire streaming circuit breaker at stream completion (#136)

Initialize circuit breakers via ensureCircuitBreakersLocked from New,
ReloadConfig, getStrategy, and RouteStream so streaming-only traffic
gets breaker protection without calling getStrategy.

Stop recording breaker success when CompleteStream opens; defer
success/failure to streamwrap.Meter via CircuitBreakerOutcome, skipping
client disconnects and rate limits.

Co-authored-by: Rachit Gandhi <Rachit-Gandhi@users.noreply.github.com>

* fix(gateway): do not trip stream circuit breaker on client deadline

PR #7 defers streaming circuit-breaker outcomes to stream completion via
streamwrap.Meter. Client context deadlines were counted as provider failures,
opening the breaker after enough timed-out streams even when the provider was
healthy.

Skip context.DeadlineExceeded alongside context.Canceled in
recordStreamCircuitBreakerOutcome, matching fallback retry semantics.

Co-authored-by: Rachit Gandhi <Rachit-Gandhi@users.noreply.github.com>

* fix(streamwrap): preserve provider errors when client cancels mid-stream

When a client disconnects during stream forwarding, do not overwrite an
already-captured provider error with ctx.Err(). While draining the upstream
channel after cancel, also adopt error chunks so circuit breaker outcomes
still record genuine provider failures.

Co-authored-by: Rachit Gandhi <Rachit-Gandhi@users.noreply.github.com>

* fix: skip non-streaming targets before circuit-breaker wrap in RouteStream

When a target has circuit_breaker configured, cbProvider implements
CompleteStream even if the underlying provider does not. Check the
underlying provider's StreamProvider support before wrapping so
fallback/conditional/load-balance routing can advance to the next
streaming-capable target.

Co-authored-by: Rachit Gandhi <Rachit-Gandhi@users.noreply.github.com>

* fix: address stream circuit-breaker review feedback

- Do not record startup stream failures for client cancellation/deadlines
- Skip open circuit-breaker targets during streaming fallback selection
- Preserve ErrCircuitOpen when the only compatible target is open

Co-authored-by: Rachit Gandhi <Rachit-Gandhi@users.noreply.github.com>

* fix(gateway): do not trip non-stream circuit breaker on client deadline

Apply shouldRecordCircuitBreakerFailure to cbProvider.Complete so
context.Canceled and context.DeadlineExceeded are excluded alongside
rate limits, matching the streaming path fixed in PR #12.

Add TestGateway_Route_ClientDeadlineDoesNotTripCircuit.

Co-authored-by: Rachit Gandhi <Rachit-Gandhi@users.noreply.github.com>

* fix: count provider-side timeouts as circuit breaker failures

Only suppress context.Canceled and context.DeadlineExceeded when the
request context is already done (ctx.Err() != nil). Provider SDK or
HTTP client timeouts that surface as DeadlineExceeded while the caller
context is still active now trip the circuit breaker as intended.

Adds unit and integration tests distinguishing client cancellation from
provider/upstream timeouts for both Route and RouteStream paths.

Co-authored-by: Rachit Gandhi <Rachit-Gandhi@users.noreply.github.com>

---------
Co-authored-by: Rachit Gandhi <Rachit-Gandhi@users.noreply.github.com>
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.

Add unit tests for ferrogw-cli command behavior

3 participants