Skip to content

Commit 11c51c8

Browse files
committed
fixed test
On-behalf-of: @SAP [email protected] Signed-off-by: Artem Shcherbatiuk <[email protected]>
1 parent ea54208 commit 11c51c8

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

.testcoverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ exclude:
66
- common/config/config.go
77
- mocks
88
- common/apis/*
9+
- export_test*.go
910
# remove it later:
1011
- listener/reconciler/clusteraccess/subroutines.go
11-
- listener/reconciler/singlecluster

listener/reconciler/clusteraccess/reconciler_test.go

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ import (
1414

1515
"github.com/openmfp/golang-commons/logger"
1616
gatewayv1alpha1 "github.com/openmfp/kubernetes-graphql-gateway/common/apis/v1alpha1"
17+
"github.com/openmfp/kubernetes-graphql-gateway/common/config"
1718
"github.com/openmfp/kubernetes-graphql-gateway/common/mocks"
19+
"github.com/openmfp/kubernetes-graphql-gateway/listener/reconciler"
1820
"github.com/openmfp/kubernetes-graphql-gateway/listener/reconciler/clusteraccess"
21+
"k8s.io/apimachinery/pkg/runtime"
22+
"k8s.io/client-go/rest"
23+
ctrl "sigs.k8s.io/controller-runtime"
1924
)
2025

2126
func TestCheckClusterAccessCRDStatus(t *testing.T) {
@@ -92,22 +97,90 @@ func TestCreateMultiClusterReconciler(t *testing.T) {
9297

9398
tests := []struct {
9499
name string
100+
setupMocks func() *mocks.MockClient
95101
wantErr bool
96102
errContains string
97103
}{
98104
{
99-
name: "success",
105+
name: "success",
106+
setupMocks: func() *mocks.MockClient {
107+
mockClient := mocks.NewMockClient(t)
108+
// Mock successful CRD check
109+
mockClient.EXPECT().List(mock.Anything, mock.AnythingOfType("*v1alpha1.ClusterAccessList")).
110+
RunAndReturn(func(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
111+
clusterAccessList := list.(*gatewayv1alpha1.ClusterAccessList)
112+
clusterAccessList.Items = []gatewayv1alpha1.ClusterAccess{
113+
{
114+
ObjectMeta: metav1.ObjectMeta{Name: "test-cluster"},
115+
Spec: gatewayv1alpha1.ClusterAccessSpec{
116+
Host: "https://test.example.com",
117+
},
118+
},
119+
}
120+
return nil
121+
}).Once()
122+
return mockClient
123+
},
100124
wantErr: false,
101125
},
126+
{
127+
name: "crd_not_registered",
128+
setupMocks: func() *mocks.MockClient {
129+
mockClient := mocks.NewMockClient(t)
130+
// Mock CRD not found
131+
mockClient.EXPECT().List(mock.Anything, mock.AnythingOfType("*v1alpha1.ClusterAccessList")).
132+
Return(&meta.NoResourceMatchError{
133+
PartialResource: schema.GroupVersionResource{
134+
Group: "gateway.openmfp.org",
135+
Version: "v1alpha1",
136+
Resource: "clusteraccesses",
137+
},
138+
}).Once()
139+
return mockClient
140+
},
141+
wantErr: true,
142+
errContains: "ClusterAccess CRD not registered",
143+
},
102144
}
103145

104146
for _, tt := range tests {
105147
t.Run(tt.name, func(t *testing.T) {
106-
// TODO: Fix this test - CreateMultiClusterReconciler function doesn't exist
107-
// reconciler, err := clusteraccess.CreateMultiClusterReconciler(opts, mockLogger)
108-
// require.NoError(t, err)
109-
// require.NotNil(t, reconciler)
110-
_ = mockLogger // Use mockLogger to avoid unused variable warning
148+
mockClient := tt.setupMocks()
149+
150+
// Test the actual NewClusterAccessReconciler function
151+
ctx := context.Background()
152+
appCfg := config.Config{
153+
OpenApiDefinitionsPath: "/tmp/test",
154+
}
155+
opts := reconciler.ReconcilerOpts{
156+
Config: &rest.Config{Host: "https://test-api-server.com"},
157+
Scheme: runtime.NewScheme(),
158+
Client: mockClient,
159+
ManagerOpts: ctrl.Options{
160+
Scheme: runtime.NewScheme(),
161+
},
162+
OpenAPIDefinitionsPath: "/tmp/test",
163+
}
164+
165+
reconciler, err := clusteraccess.NewClusterAccessReconciler(
166+
ctx,
167+
appCfg,
168+
opts,
169+
nil, // ioHandler - will be created internally
170+
nil, // schemaResolver - will be created internally
171+
mockLogger,
172+
)
173+
174+
if tt.wantErr {
175+
assert.Error(t, err)
176+
if tt.errContains != "" {
177+
assert.Contains(t, err.Error(), tt.errContains)
178+
}
179+
assert.Nil(t, reconciler)
180+
} else {
181+
assert.NoError(t, err)
182+
assert.NotNil(t, reconciler)
183+
}
111184
})
112185
}
113186
}

0 commit comments

Comments
 (0)