Skip to content

fix(preheat): handle nil payload during policy update#1022

Open
shellyco-code wants to merge 3 commits into
goharbor:mainfrom
shellyco-code:fix/preheat-policy-nil-payload
Open

fix(preheat): handle nil payload during policy update#1022
shellyco-code wants to merge 3 commits into
goharbor:mainfrom
shellyco-code:fix/preheat-policy-nil-payload

Conversation

@shellyco-code

Copy link
Copy Markdown
Contributor

Description

This pull request fixes a runtime nil pointer dereference panic in the harbor project preheat policy update command when the fetched preheat policy or its payload from the API is nil/empty.

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Documentation update
  • Chore / maintenance

Changes

  • Modified cmd/harbor/root/project/preheat/policy/update.go to validate if existingPolicy and existingPolicy.Payload are nil, returning a clean error instead of panicking.
  • Refactored api.GetPreheatPolicy call to use a package-level mockable variable getPreheatPolicyFunc to enable clean unit testing.
  • Created cmd/harbor/root/project/preheat/policy/update_test.go to add unit tests mocking nil policy and nil payload API return values.

Testing

  • Formatted modified code using gofmt -s -w.
  • Compiled and verified the CLI binary builds successfully locally (go build).
  • Ran preheat policy package tests (go test ./cmd/harbor/root/project/preheat/policy/...), which passed successfully.
  • Ran the full repository test suite (go test ./...), which passed successfully without regressions.

Signed-off-by: shellyco-code <shellychahar57@gmail.com>

@NucleoFusion NucleoFusion 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.

Testing for cmds is limited to testing for failures. By that I mean that we induce errors by providing malformed input/flags to check whether the command appropriately fails.
By that I mean that providing invalid flags, too large values etc.

The PR resolves the issue, but tests are not up to the general CLI standard, all else looks great!

Comment on lines +29 to +32
var (
getPreheatPolicyFunc = api.GetPreheatPolicy
)

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.

we generally discourage this method of testing since it makes it kinda complicated to understand.
Our method for testing is to just test the command failures, which is wrong flags, malformed input etc

The full command testing is TBD in the e2e tests which are in-dev by a support feature that @bupd i think is making.

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.

We can just remove this now, and the relevant things in tests, cause we dont use it

Comment on lines +36 to +39
var buf bytes.Buffer
cmd.SetOut(&buf)
cmd.SetErr(&buf)
cmd.SetArgs([]string{"my-project", "my-policy"})

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.

There is a test helper function, in /pkg/testutil/testutil.go
With function signature,
func TestCmd(t *testing.T, cmdFunc func() *cobra.Command, flags ...string) error {

This takes the cmd & the flags, and just outputs the error (if any)
so you can just use this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback! I've updated the tests to use the existing pkg/testutil.TestCmd helper and aligned them with the Harbor CLI testing conventions. I also verified that the test suite passes after the changes. Please let me know if there's anything else I should adjust.

Comment on lines +29 to +32
var (
getPreheatPolicyFunc = api.GetPreheatPolicy
)

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.

We can just remove this now, and the relevant things in tests, cause we dont use it

Comment on lines +35 to +52
err := testutil.TestCmd(t, UpdatePolicyCommand, "my-project", "my-policy")
assert.Error(t, err)
assert.Contains(t, err.Error(), "payload is empty")
}

func TestUpdatePolicyCommand_NilPayload(t *testing.T) {
originalGetPreheatPolicy := getPreheatPolicyFunc
t.Cleanup(func() {
getPreheatPolicyFunc = originalGetPreheatPolicy
})

getPreheatPolicyFunc = func(projectName, policyName string) (*preheat.GetPolicyOK, error) {
return &preheat.GetPolicyOK{Payload: nil}, nil
}

err := testutil.TestCmd(t, UpdatePolicyCommand, "my-project", "my-policy")
assert.Error(t, err)
assert.Contains(t, err.Error(), "payload is empty")

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.

These tests will fail now
cause we are not using the pr.

Have a look at cmd/harbor/root/project/list_test.go
We test only the cases for flags and stuff like that, we dont test for cases that propagate back errors from a more deeper level package

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the detailed feedback! That makes sense. I'll remove the getPreheatPolicyFunc indirection and update the tests to follow the Harbor CLI testing conventions, using list_test.go as a reference. I'll push an updated revision shortly. Thanks!

@NucleoFusion

Copy link
Copy Markdown
Contributor

Signed-off-by: shellyco-code <shellychahar57@gmail.com>
@shellyco-code shellyco-code force-pushed the fix/preheat-policy-nil-payload branch from 874bc48 to 7610a7e Compare June 25, 2026 08:16
Signed-off-by: shellyco-code <shellychahar57@gmail.com>

@NucleoFusion NucleoFusion 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.

lgtm! thanks for the contribution!

@NucleoFusion NucleoFusion 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.

lgtm!
Thanks for the contribution!

@qcserestipy qcserestipy added the status/has-approved-pr Issue has a linked pull request that has been approved and is waiting to be merged. label Jun 30, 2026
@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 9.45%. Comparing base (60ad0bd) to head (fed49bc).
⚠️ Report is 188 commits behind head on main.

Files with missing lines Patch % Lines
cmd/harbor/root/project/preheat/policy/update.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             main   #1022      +/-   ##
=========================================
- Coverage   10.99%   9.45%   -1.55%     
=========================================
  Files         173     321     +148     
  Lines        8671   16084    +7413     
=========================================
+ Hits          953    1520     +567     
- Misses       7612   14430    +6818     
- Partials      106     134      +28     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status/has-approved-pr Issue has a linked pull request that has been approved and is waiting to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: preheat policy update command panics on empty or nil server payload

3 participants