-
Notifications
You must be signed in to change notification settings - Fork 54
Add unit tests for Drasi CLI #310
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
base: main
Are you sure you want to change the base?
Conversation
7025ce5 to
b726085
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Refactors CLI commands to enable dependency injection for improved testability and adds comprehensive unit tests for apply, delete, describe, and list commands while introducing a DrasiClient interface abstraction. Key changes include introducing option structs for command constructors, adding mocks for platform and API layers, and expanding test coverage with realistic scenarios. Public SDK interface was modified (CreateDrasiClient now returns an interface), and supporting mocks plus minor output handling adjustments were added.
- Introduced DrasiClient interface and updated PlatformClient to return it (abstraction / testability).
- Added dependency-injected constructors for core commands (apply, delete, describe, list) plus extensive unit tests with mocks.
- Added mocks and helper utilities (testutil package) and adjusted output handling to use command writers.
Reviewed Changes
Copilot reviewed 16 out of 17 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| cli/testutil/task_output_mock.go | Adds mock for TaskOutput to support unit tests. |
| cli/testutil/platform_client_mock.go | Adds mock PlatformClient including injectable DrasiClient. |
| cli/testutil/api_client_mock.go | Adds mock Drasi (management API) client. |
| cli/sdk/platform_client.go | Changes PlatformClient interface to return DrasiClient (interface abstraction). |
| cli/sdk/api_client.go | Introduces DrasiClient interface and assertion of implementation. |
| cli/go.mod | Adds test and Docker-related dependencies as direct requirements. |
| cli/cmd/test_helper_test.go | Shared test command execution helper. |
| cli/cmd/list_test.go | New test coverage for list command across scenarios. |
| cli/cmd/list.go | Adds dependency injection support; returns errors instead of printing; uses cmd output stream. |
| cli/cmd/describe_test.go | New tests for describe command including error paths. |
| cli/cmd/describe.go | Adds dependency injection and consistent error handling/output writing. |
| cli/cmd/delete_test.go | New tests for delete command including file + arg paths and failures. |
| cli/cmd/delete.go | Adds dependency injection (platform/output) and improves error propagation. |
| cli/cmd/apply_test.go | New tests for apply command including multi-manifest scenarios and failures. |
| cli/cmd/apply.go | Adds dependency injection (platform/output) and consistent error handling. |
| cli/.gitignore | Ignores test and coverage artifacts. |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
6f139d9 to
cd86675
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this workflow trigger the unit tests for the CLI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Added a separate job test-cli in the workflow.
Thanks!
cd86675 to
d48b04e
Compare
- Refactor CLI commands to support dependency injection via functional options pattern - Add DrasiClient interface to enable mocking of Management API - Create testutil package with mock implementations for DrasiClient, PlatformClient, and TaskOutput - Add comprehensive unit tests covering success/failure paths for all resource management commands - Fix error handling bugs: commands now properly return errors instead of printing - Fix output routing: use cmd.OutOrStdout() for testability - Individual command coverage: list 78.3%, describe 78.6%, apply 84.4%, delete 84.4% Signed-off-by: Aman Singh <aman.singh.original@gmail.com>
d48b04e to
5660760
Compare
| func NewDeleteCommand(opts ...*deleteCmdOptions) *cobra.Command { | ||
| // Use the real implementation by default | ||
| opt := &deleteCmdOptions{ | ||
| platformClientFactory: func(namespace string) (sdk.PlatformClient, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This platformClientFactory function seems to be repeated a lot.
Is there a cleaner way to organize it?
| ) | ||
|
|
||
|
|
||
| func TestListCommand(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a heads up, the list command for Source and Reaction will now have a column called INGRESS URL
Description
This pull request refactors the Drasi CLI's command structure to improve testability and introduces a comprehensive suite of unit tests for the core resource management commands (
apply,delete,describe,list).The primary goal is to build a safety net that mocks the Management API, enabling future refactoring of the API client with high confidence.
Key Changes
1. Refactoring for Testability (Dependency Injection)
The core change was to refactor the command constructors (e.g.,
NewListCommand) to support dependency injection, making them fully unit-testable.Testable*Commandfunctions, the originalNew*Commandfunctions were modified to accept an optional options struct (e.g.,*listCmdOptions).PlatformClient).RunEfunction, providing accurate code coverage metrics.2. Comprehensive Unit Test Suite
A new test suite has been added to cover the primary CLI commands:
New Test Files:
apply_test.go,delete_test.go,describe_test.go,list_test.go.Mocking Infrastructure: A new
cli/testutilpackage was created to house mock implementations for:DrasiClient: Mocks the Management API interface.PlatformClient: Mocks the platform layer.TaskOutput: Mocks the UI/console output.Test Coverage: The tests cover a wide range of scenarios, including:
-fflag).3. Code Quality Improvements
DrasiClientInterface: An interface was extracted from the concreteApiClientstruct, promoting a better, more modular design.executeCommandtest helper was consolidated into a singlecmd/test_helper_test.gofile to avoid code duplication across the test suite.Type of change
This pull request is a minor refactor, code cleanup, test improvement, or other maintenance task and doesn't change the functionality of Drasi.