Skip to content

test(): Add comprehensive unit tests for internal/secrets.go file#98

Open
GitNinja36 wants to merge 3 commits intokubeslice:masterfrom
GitNinja36:test/secret-go-coverage
Open

test(): Add comprehensive unit tests for internal/secrets.go file#98
GitNinja36 wants to merge 3 commits intokubeslice:masterfrom
GitNinja36:test/secret-go-coverage

Conversation

@GitNinja36
Copy link

@GitNinja36 GitNinja36 commented Aug 7, 2025

Description

This PR adds a complete and production-grade unit test suite for the GetSecrets and GetSecretName functions in pkg/internal/secrets.goof the KubeSlice CLI. These functions are responsible for fetching Kubernetes secrets and rely on shell command pipelines that involve tools like kubectl, grep, and awk.


Testing Approach

Files Covered

  • pkg/internal/secrets.go

Functions Tested

  • GetSecretName(workerName, namespace, controllerCluster)
  • GetSecrets(workerName, namespace, controllerCluster, outputFormat)

Techniques Used

  • Fake Binary Injection: Simulated kubectl behavior using temporary fake binaries with controlled outputs.
  • Command Pipeline Validation: End-to-end test coverage for kubectl | grep | awk execution to ensure proper parsing and behavior.
  • Dependency Isolation: Avoided reliance on global environment or real cluster interaction.
  • Sleep Logic Verification: Ensured that the 200ms delay inside GetSecrets executes without being skipped or altered.

Coverage Results

File Before After
pkg/internal/secrets.go 0% 100%

Sample Coverage Output

go test ./pkg/internal -coverprofile=cover.out
go tool cover -func=cover.out | grep secrets.go
github.com/kubeslice/kubeslice-cli/pkg/internal/secrets.go:13:  GetSecrets     100.0%
github.com/kubeslice/kubeslice-cli/pkg/internal/secrets.go:20:  GetSecretName  100.0%

How Has This Been Tested?

Test Cases

  • Test A: Valid secret found from command output pipeline
  • Test B: No matching secret found, returns empty string
  • Test C: Simulated kubectl failure returns empty result
  • Test D: GetSecrets function runs without panic
  • Test E: GetSecrets enforces required 200ms delay

Safe Execution

  • Temporary binaries and symlinks are cleaned up after every test
  • Zero impact to actual system state or real kubectl
  • Tests are fully idempotent and CI-safe
CLI Output Screenshot 2025-08-07 at 1 04 38 PM

Does this PR introduce a breaking change?

NA


Fixes

Fixes: #46


Reviewers

PTAL
@priyupadhyay @narmidm @Rahul-D78 @gourishkb


Signed-off-by: Rohit Kumar

- 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.
@GitNinja36 GitNinja36 changed the title test(internal/secrets.go): Add comprehensive unit tests for GetSecrets and GetSecretName using fake kubectl with 100% coverage test(): Add comprehensive unit tests for internal/secrets.go file Aug 7, 2025
- Adds a dedicated test suite in pkg/internal/secret_test.go to cover the GetSecretName function.
- Ensures robust, cross-platform test coverage using a fake kubectl binary and environment variable overrides.
- Covers successful secret retrieval, command pipeline validation, and edge cases such as missing or malformed output.
- No changes to production code; all improvements are limited to the test suite.

Signed-off-by: Rohit27 <137439791+GitNinja36@users.noreply.github.com>
Signed-off-by: Rohit27 <137439791+GitNinja36@users.noreply.github.com>
@GitNinja36 GitNinja36 force-pushed the test/secret-go-coverage branch from 0eee295 to 9fa6f3c Compare August 7, 2025 12:38
@Arpit529Srivastava
Copy link

your previous commit doesn't have DOC. Please fix that

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.

Feature: Add Testing (UT/IT)

2 participants