Skip to content
Merged
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
Prev Previous commit
Next Next commit
add test case for checkResAuth function in manager.go
Signed-off-by: YuChen <[email protected]>
  • Loading branch information
YuChen committed Nov 28, 2024
commit c8a5f6bcf3da6ea2e3ff8271ec484d84f5af3e86
64 changes: 64 additions & 0 deletions controllers/operator/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ package operator

import (
"context"
"fmt"
"reflect"
"testing"

olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
operatorsv1 "github.com/operator-framework/operator-lifecycle-manager/pkg/package-server/apis/operators/v1"
"github.com/stretchr/testify/mock"
authorizationv1 "k8s.io/api/authorization/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestChannelCheck(t *testing.T) {
Expand Down Expand Up @@ -400,6 +406,64 @@ func TestGetCatalogSourceAndChannelFromPackage(t *testing.T) {

}

type MockClient struct {
mock.Mock
}

func (m *MockClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
args := m.Called(ctx, key, obj)
return args.Error(0)
}

func (m *MockClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
args := m.Called(ctx, list, opts)
return args.Error(0)
}

func TestCheckResAuth(t *testing.T) {
ctx := context.TODO()

mockClient := &MockClient{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to use the above mockReader := &MockReader{} to replace mockClient := &MockClient{}?

operator := &ODLMOperator{
Client: fake.NewClientBuilder().Build(),
Reader: mockClient, // Using the same mock for Reader
Config: &rest.Config{},
Recorder: record.NewFakeRecorder(10),
}

namespace := "test-namespace"
group := "test-group"
resource := "test-resource"
verb := "get"

// Test when SubjectAccessReview is allowed
mockClient.On("Create", ctx, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
sar := args.Get(1).(*authorizationv1.SubjectAccessReview)
sar.Status.Allowed = true
})

if !operator.CheckResAuth(ctx, namespace, group, resource, verb) {
t.Errorf("Expected CheckResAuth to return true, but got false")
}

// Test when SubjectAccessReview is not allowed
mockClient.On("Create", ctx, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
sar := args.Get(1).(*authorizationv1.SubjectAccessReview)
sar.Status.Allowed = false
})

if operator.CheckResAuth(ctx, namespace, group, resource, verb) {
t.Errorf("Expected CheckResAuth to return false, but got true")
}

// Test when Create returns an error
mockClient.On("Create", ctx, mock.Anything, mock.Anything).Return(fmt.Errorf("create error"))

if operator.CheckResAuth(ctx, namespace, group, resource, verb) {
t.Errorf("Expected CheckResAuth to return false, but got true")
}
}

func assertCatalogSourceAndChannel(t *testing.T, catalogSourceName, expectedCatalogSourceName, catalogSourceNs, expectedCatalogSourceNs, availableChannel, expectedAvailableChannel string) {
t.Helper()

Expand Down