Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cmd/harbor/root/project/preheat/policy/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import (
"github.com/spf13/cobra"
)

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

func UpdatePolicyCommand() *cobra.Command {
var isID bool

Expand Down Expand Up @@ -75,14 +79,18 @@ func UpdatePolicyCommand() *cobra.Command {
}

log.Debug("Fetching preheat policy...")
existingPolicy, err := api.GetPreheatPolicy(projectName, policyName)
existingPolicy, err := getPreheatPolicyFunc(projectName, policyName)
if err != nil {
if utils.ParseHarborErrorCode(err) == "404" {
return fmt.Errorf("preheat policy %s not found in project %s", policyName, projectName)
}
return fmt.Errorf("failed to get preheat policy: %v", utils.ParseHarborErrorMsg(err))
}

if existingPolicy == nil || existingPolicy.Payload == nil {
return fmt.Errorf("preheat policy %s payload is empty", policyName)
}

log.Debug("Fetching available providers...")
providers, err := api.ListProvidersUnderProject(projectName)
if err != nil {
Expand Down
65 changes: 65 additions & 0 deletions cmd/harbor/root/project/preheat/policy/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package policy

import (
"bytes"
"testing"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/preheat"
"github.com/stretchr/testify/assert"
)

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

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

cmd := UpdatePolicyCommand()
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.


err := cmd.Execute()
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
}

cmd := UpdatePolicyCommand()
var buf bytes.Buffer
cmd.SetOut(&buf)
cmd.SetErr(&buf)
cmd.SetArgs([]string{"my-project", "my-policy"})

err := cmd.Execute()
assert.Error(t, err)
assert.Contains(t, err.Error(), "payload is empty")
}