Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions cmd/harbor/root/project/preheat/policy/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func UpdatePolicyCommand() *cobra.Command {
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
56 changes: 56 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,56 @@
// 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 (
"testing"

"github.com/goharbor/harbor-cli/pkg/testutil"
)

// TestUpdatePolicyCommand_Errors tests the custom CLI-level validations in the
// update command. API responses are not tested here.
func TestUpdatePolicyCommand_Errors(t *testing.T) {
tests := []struct {
name string
flags []string
expectError bool
}{
{
name: "--id flag without arguments",
flags: []string{"--id"},
expectError: true,
},
{
name: "too many arguments",
flags: []string{"project", "policy", "extra"},
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := testutil.TestCmd(t, UpdatePolicyCommand, tt.flags...)

if tt.expectError && err == nil {
t.Fatalf("expected error but got nil")
}

if !tt.expectError && err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}
}