Skip to content

feat: Add comprehensive unit tests and refactor for testability in UI and endpoint logic#95

Open
GitNinja36 wants to merge 1 commit intokubeslice:masterfrom
GitNinja36:feature/add-unit-tests
Open

feat: Add comprehensive unit tests and refactor for testability in UI and endpoint logic#95
GitNinja36 wants to merge 1 commit intokubeslice:masterfrom
GitNinja36:feature/add-unit-tests

Conversation

@GitNinja36
Copy link

@GitNinja36 GitNinja36 commented Aug 5, 2025

Description

This PR adds comprehensive unit tests for the GetUIEndpoint function in pkg/ui.go and the endpoint logic in pkg/internal/enterprise.go, which handle UI endpoint retrieval and service endpoint construction for the KubeSlice CLI. Both functions have been refactored to use dependency injection for improved testability.

I've refactored the functions to accept dependencies, which allowed me to mock command execution and node IP retrieval. This effort boosts test coverage to 100% for pkg/ui.go and 94.4% for GetUIEndpoint in pkg/internal/enterprise.go.

This test suite covers 5 scenarios including NodePort and LoadBalancer service types, invalid JSON handling, and command failure cases. All tests pass and existing functionality remains unchanged.

Testing Approach

UI Wrapper Function (pkg/ui.go)

  • Tests the wrapper function to ensure correct wiring and argument passing
  • Uses dependency injection to replace the internal function with a mock
  • Verifies that the mock is called with correct parameters and that the wrapper doesn't panic

Endpoint Logic (pkg/internal/enterprise.go - GetUIEndpoint)

  • Tests the core business logic including command execution, JSON parsing, and endpoint construction
  • Uses dependency injection to mock command execution and node IP retrieval
  • Covers NodePort scenario: mocks NodePort service and verifies correct endpoint construction
  • Covers LoadBalancer scenario: mocks LoadBalancer service and verifies correct endpoint construction
  • Covers Invalid JSON scenario: mocks invalid output and verifies graceful error handling
  • Covers Command failure scenario: mocks command errors and verifies proper error handling

Coverage Results

File Before After
pkg/ui.go 0% 100%
pkg/internal/enterprise.go (GetUIEndpoint) 0% 94.4%

How Has This Been Tested?

  • Test case A: UI wrapper function with dependency injection and global state setup
  • Test case B: NodePort service endpoint generation and validation
  • Test case C: LoadBalancer service endpoint generation and validation
  • Test case D: Invalid JSON error handling and graceful degradation
  • Test case E: Command failure error handling and empty endpoint return
  • Verified all existing functionality still works with go build and go test

pkg/internal/enterprise.go :- @kubeslice-cli % go tool cover -func=coverage.out | grep ui.go

Screenshot 2025-08-05 at 7 11 28 PM

pkg/ui.go file :- kubeslice-cli % go tool cover -func=enterprise_coverage.out | grep enterprise.go

Screenshot 2025-08-05 at 7 17 14 PM

Does this PR introduce a breaking change?

No breaking changes. Functions maintain the same interface and behavior while being made more testable through dependency injection.


PTAL @priyupadhyay @narmidm @Rahul-D78 @gourishkb


Signed-off-by: GitNinja36

- Add unit tests for pkg/ui.go with 100% coverage
- Add unit tests for pkg/internal/enterprise.go GetUIEndpoint with 94.4% coverage
- Refactor code to use dependency injection for better testability
- Test multiple scenarios like : NodePort, LoadBalancer, error handling
- Made all tests self-contained and independent

These changes boost test reliability and improve overall code quality.
@pnavali
Copy link

pnavali commented Aug 20, 2025

@gourishkb @bharath-avesha please review

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.

2 participants