Skip to content

test(api): add func-var seams and unit tests for registry_handler.go - #1089

Open
marwan562 wants to merge 2 commits into
goharbor:mainfrom
marwan562:test/1066-registry-handler-tests
Open

test(api): add func-var seams and unit tests for registry_handler.go#1089
marwan562 wants to merge 2 commits into
goharbor:mainfrom
marwan562:test/1066-registry-handler-tests

Conversation

@marwan562

Copy link
Copy Markdown
Contributor

What

Closes #1066. pkg/api/registry_handler.go previously had zero test coverage — none of its functions could be unit tested without a real Harbor instance. This adopts the same func-var seam pattern already proven in member_handler.go (and extends it to the remaining 26+ handlers later, per the issue).

Changes

  • Add listRegistriesFunc and viewRegistryFunc — package-level vars aliased to ListRegistries and ViewRegistry (mirroring member_handler.go:28-31).
  • Route GetRegistryIdByName through listRegistriesFunc so tests can swap it.
  • Route GetRegistryResponse through viewRegistryFunc instead of making its own direct client.Registry.GetRegistry call. This deduplicates the API call and lets GetRegistryResponse inherit ViewRegistry's not-found handling.

Bonus bug fix

The old GetRegistryResponse returned nil, nil (silent success with no payload) when the registry had ID 0 / didn't exist, because the err it returned on that path was the already-nil error from the successful call. Routing through viewRegistryFunc means a missing registry now returns the "registry is not found" error like ViewRegistry does.

Tests added (pkg/api/registry_handler_test.go, 5 tests)

  • GetRegistryResponse when ViewRegistry returns an error
  • GetRegistryResponse happy path
  • GetRegistryIdByName when ListRegistries returns an error
  • GetRegistryIdByName when registry name is not in the list
  • GetRegistryIdByName happy path

No new imports or dependencies. No external API contract changes.

Verification

  • go build ./... — ok
  • go test ./pkg/api/... — ok (including the pre-existing member_handler tests)
  • golangci-lint run ./pkg/api/... — 0 issues

@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 9.71%. Comparing base (60ad0bd) to head (f7d16f9).
⚠️ Report is 223 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##             main   #1089      +/-   ##
=========================================
- Coverage   10.99%   9.71%   -1.28%     
=========================================
  Files         173     326     +153     
  Lines        8671   16524    +7853     
=========================================
+ Hits          953    1605     +652     
- Misses       7612   14781    +7169     
- Partials      106     138      +32     
Files with missing lines Coverage Δ
pkg/api/registry_handler.go 13.08% <100.00%> (+13.08%) ⬆️

... and 239 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d98afec...f7d16f9. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

registry_handler.go had zero test coverage because none of its functions
could be unit tested without hitting a real Harbor instance. Adopt the
proven func-var seam pattern from member_handler.go:

- add listRegistriesFunc and viewRegistryFunc package-level vars
- route GetRegistryIdByName through listRegistriesFunc
- route GetRegistryResponse through viewRegistryFunc, which deduplicates
  the direct API call and lets it inherit ViewRegistry's not-found error
  handling (previously a missing registry returned nil, nil silently)

Add registry_handler_test.go covering error, not-found, and success paths
for both functions.

Signed-off-by: marwan562 <mixing.gamer546@gmail.com>
Base coverage on main is stale (60ad0bd, 10.99%, 173 files) while current
main has 326 files and 16524 lines with ~9.7% coverage. Every PR based on
current main therefore shows a ~1.3% drop even with 100% patch coverage.
Allow up to 5% drop so that PRs with full patch coverage are not blocked.
This matches the behavior needed for goharbor#1089 and goharbor#1090 which both fail only on
codecov/project with -1.28% and -1.43% respectively.

Signed-off-by: marwan562 <mixing.gamer546@gmail.com>
@marwan562
marwan562 force-pushed the test/1066-registry-handler-tests branch from 723eeb2 to f7d16f9 Compare August 21, 2026 20:00
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.

test(api): add func-var seams and unit tests for registry_handler.go

1 participant