Skip to content

Commit 9d37c9f

Browse files
authored
Move mock k8s functions to one files (#2569)
1 parent 1bad4c3 commit 9d37c9f

File tree

6 files changed

+120
-103
lines changed

6 files changed

+120
-103
lines changed

operatorapi/k8s_client_mock.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// This file is part of MinIO Console Server
2+
// Copyright (c) 2023 MinIO, Inc.
3+
//
4+
// This program is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Affero General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// This program is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Affero General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Affero General Public License
15+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package operatorapi
18+
19+
import (
20+
"context"
21+
22+
corev1 "k8s.io/api/core/v1"
23+
v1 "k8s.io/api/core/v1"
24+
storagev1 "k8s.io/api/storage/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
)
27+
28+
type k8sClientMock struct{}
29+
30+
var (
31+
k8sClientGetResourceQuotaMock func(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error)
32+
k8sClientGetNameSpaceMock func(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Namespace, error)
33+
k8sClientStorageClassesMock func(ctx context.Context, opts metav1.ListOptions) (*storagev1.StorageClassList, error)
34+
35+
k8sClientGetConfigMapMock func(ctx context.Context, namespace, configMap string, opts metav1.GetOptions) (*corev1.ConfigMap, error)
36+
k8sClientCreateConfigMapMock func(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.CreateOptions) (*corev1.ConfigMap, error)
37+
k8sClientUpdateConfigMapMock func(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.UpdateOptions) (*corev1.ConfigMap, error)
38+
k8sClientDeleteConfigMapMock func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
39+
40+
k8sClientDeletePodCollectionMock func(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error
41+
k8sClientDeleteSecretMock func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
42+
k8sClientCreateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
43+
k8sClientUpdateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error)
44+
k8sclientGetSecretMock func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error)
45+
k8sclientGetServiceMock func(ctx context.Context, namespace, serviceName string, opts metav1.GetOptions) (*corev1.Service, error)
46+
)
47+
48+
func (c k8sClientMock) getResourceQuota(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error) {
49+
return k8sClientGetResourceQuotaMock(ctx, namespace, resource, opts)
50+
}
51+
52+
func (c k8sClientMock) getNamespace(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Namespace, error) {
53+
return k8sClientGetNameSpaceMock(ctx, name, opts)
54+
}
55+
56+
func (c k8sClientMock) getStorageClasses(ctx context.Context, opts metav1.ListOptions) (*storagev1.StorageClassList, error) {
57+
return k8sClientStorageClassesMock(ctx, opts)
58+
}
59+
60+
func (c k8sClientMock) getConfigMap(ctx context.Context, namespace, configMap string, opts metav1.GetOptions) (*corev1.ConfigMap, error) {
61+
return k8sClientGetConfigMapMock(ctx, namespace, configMap, opts)
62+
}
63+
64+
func (c k8sClientMock) createConfigMap(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.CreateOptions) (*corev1.ConfigMap, error) {
65+
return k8sClientCreateConfigMapMock(ctx, namespace, cm, opts)
66+
}
67+
68+
func (c k8sClientMock) updateConfigMap(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.UpdateOptions) (*corev1.ConfigMap, error) {
69+
return k8sClientUpdateConfigMapMock(ctx, namespace, cm, opts)
70+
}
71+
72+
func (c k8sClientMock) deleteConfigMap(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
73+
return k8sClientDeleteConfigMapMock(ctx, namespace, name, opts)
74+
}
75+
76+
func (c k8sClientMock) deletePodCollection(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
77+
return k8sClientDeletePodCollectionMock(ctx, namespace, opts, listOpts)
78+
}
79+
80+
func (c k8sClientMock) deleteSecret(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
81+
return k8sClientDeleteSecretMock(ctx, namespace, name, opts)
82+
}
83+
84+
func (c k8sClientMock) createSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
85+
return k8sClientCreateSecretMock(ctx, namespace, secret, opts)
86+
}
87+
88+
func (c k8sClientMock) updateSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error) {
89+
return k8sClientUpdateSecretMock(ctx, namespace, secret, opts)
90+
}
91+
92+
func (c k8sClientMock) getSecret(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) {
93+
return k8sclientGetSecretMock(ctx, namespace, secretName, opts)
94+
}
95+
96+
func (c k8sClientMock) getService(ctx context.Context, namespace, serviceName string, opts metav1.GetOptions) (*corev1.Service, error) {
97+
return k8sclientGetServiceMock(ctx, namespace, serviceName, opts)
98+
}

operatorapi/marketplace_test.go

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,9 @@ import (
3535
)
3636

3737
var (
38-
testWithError = false
39-
testServerWithError = false
40-
errMock = errors.New("mock error")
41-
k8sClientGetConfigMapMock func(ctx context.Context, namespace, configMap string, opts metav1.GetOptions) (*corev1.ConfigMap, error)
42-
k8sClientCreateConfigMapMock func(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.CreateOptions) (*corev1.ConfigMap, error)
43-
k8sClientUpdateConfigMapMock func(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.UpdateOptions) (*corev1.ConfigMap, error)
44-
k8sClientDeleteConfigMapMock func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
38+
testWithError = false
39+
testServerWithError = false
40+
errMock = errors.New("mock error")
4541
)
4642

4743
type MarketplaceTestSuite struct {
@@ -52,22 +48,6 @@ type MarketplaceTestSuite struct {
5248
postServer *httptest.Server
5349
}
5450

55-
func (c k8sClientMock) getConfigMap(ctx context.Context, namespace, configMap string, opts metav1.GetOptions) (*corev1.ConfigMap, error) {
56-
return k8sClientGetConfigMapMock(ctx, namespace, configMap, opts)
57-
}
58-
59-
func (c k8sClientMock) createConfigMap(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.CreateOptions) (*corev1.ConfigMap, error) {
60-
return k8sClientCreateConfigMapMock(ctx, namespace, cm, opts)
61-
}
62-
63-
func (c k8sClientMock) updateConfigMap(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.UpdateOptions) (*corev1.ConfigMap, error) {
64-
return k8sClientUpdateConfigMapMock(ctx, namespace, cm, opts)
65-
}
66-
67-
func (c k8sClientMock) deleteConfigMap(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
68-
return k8sClientDeleteConfigMapMock(ctx, namespace, name, opts)
69-
}
70-
7151
func (suite *MarketplaceTestSuite) SetupSuite() {
7252
suite.assert = assert.New(suite.T())
7353
suite.namespace = "default"

operatorapi/operator_subnet_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (suite *OperatorSubnetTestSuite) SetupSuite() {
5858
suite.getAPIKeyServer = httptest.NewServer(http.HandlerFunc(suite.getAPIKeyHandler))
5959
suite.registerAPIKeyServer = httptest.NewServer(http.HandlerFunc(suite.registerAPIKeyHandler))
6060
suite.k8sClient = k8sClientMock{}
61-
CreateSecretMock = func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
61+
k8sClientCreateSecretMock = func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
6262
return &corev1.Secret{}, nil
6363
}
6464
k8sclientGetSecretMock = func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) {

operatorapi/resource_quota_test.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,12 @@ import (
2222
"reflect"
2323
"testing"
2424

25-
storagev1 "k8s.io/api/storage/v1"
26-
2725
"github.com/minio/console/models"
2826
v1 "k8s.io/api/core/v1"
2927
"k8s.io/apimachinery/pkg/api/resource"
3028
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3129
)
3230

33-
type k8sClientMock struct{}
34-
35-
var (
36-
k8sclientGetResourceQuotaMock func(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error)
37-
k8sclientGetNameSpaceMock func(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Namespace, error)
38-
k8sclientStorageClassesMock func(ctx context.Context, opts metav1.ListOptions) (*storagev1.StorageClassList, error)
39-
)
40-
41-
// mock functions
42-
func (c k8sClientMock) getResourceQuota(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error) {
43-
return k8sclientGetResourceQuotaMock(ctx, namespace, resource, opts)
44-
}
45-
46-
// mock functions
47-
func (c k8sClientMock) getNamespace(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Namespace, error) {
48-
return k8sclientGetNameSpaceMock(ctx, name, opts)
49-
}
50-
51-
// mock functions
52-
func (c k8sClientMock) getStorageClasses(ctx context.Context, opts metav1.ListOptions) (*storagev1.StorageClassList, error) {
53-
return k8sclientStorageClassesMock(ctx, opts)
54-
}
55-
5631
func Test_ResourceQuota(t *testing.T) {
5732
mockHardResourceQuota := v1.ResourceList{
5833
"storage": resource.MustParse("1000"),
@@ -141,7 +116,7 @@ func Test_ResourceQuota(t *testing.T) {
141116
}
142117
for _, tt := range tests {
143118
t.Run(tt.name, func(t *testing.T) {
144-
k8sclientGetResourceQuotaMock = tt.mockResourceQuota
119+
k8sClientGetResourceQuotaMock = tt.mockResourceQuota
145120
got, err := getResourceQuota(tt.args.ctx, tt.args.client, "ns", mockRQResponse.Name)
146121
if err != nil {
147122
if tt.wantErr {

operatorapi/tenants_helper_test.go

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,6 @@ import (
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
)
3232

33-
var (
34-
DeletePodCollectionMock func(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error
35-
DeleteSecretMock func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
36-
CreateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
37-
UpdateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error)
38-
)
39-
40-
func (c k8sClientMock) deletePodCollection(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
41-
return DeletePodCollectionMock(ctx, namespace, opts, listOpts)
42-
}
43-
44-
func (c k8sClientMock) deleteSecret(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
45-
return DeleteSecretMock(ctx, namespace, name, opts)
46-
}
47-
48-
func (c k8sClientMock) createSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
49-
return CreateSecretMock(ctx, namespace, secret, opts)
50-
}
51-
52-
func (c k8sClientMock) updateSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error) {
53-
return UpdateSecretMock(ctx, namespace, secret, opts)
54-
}
55-
5633
func Test_tenantUpdateCertificates(t *testing.T) {
5734
k8sClient := k8sClientMock{}
5835
opClient := opClientMock{}
@@ -198,9 +175,9 @@ func Test_tenantUpdateCertificates(t *testing.T) {
198175
}
199176
for _, tt := range tests {
200177
opClientTenantGetMock = tt.args.mockTenantGet
201-
DeleteSecretMock = tt.args.mockDeleteSecret
202-
CreateSecretMock = tt.args.mockCreateSecret
203-
DeletePodCollectionMock = tt.args.mockDeletePodCollection
178+
k8sClientDeleteSecretMock = tt.args.mockDeleteSecret
179+
k8sClientCreateSecretMock = tt.args.mockCreateSecret
180+
k8sClientDeletePodCollectionMock = tt.args.mockDeletePodCollection
204181
t.Run(tt.name, func(t *testing.T) {
205182
if err := tenantUpdateCertificates(tt.args.ctx, tt.args.opClient, tt.args.clientSet, tt.args.namespace, tt.args.params); (err != nil) != tt.wantErr {
206183
t.Errorf("tenantUpdateCertificates() error = %v, wantErr %v", err, tt.wantErr)
@@ -246,9 +223,9 @@ func Test_tenantUpdateEncryption(t *testing.T) {
246223
for _, tt := range tests {
247224
t.Run(tt.name, func(t *testing.T) {
248225
opClientTenantGetMock = tt.args.mockTenantGet
249-
DeleteSecretMock = tt.args.mockDeleteSecret
250-
CreateSecretMock = tt.args.mockCreateSecret
251-
DeletePodCollectionMock = tt.args.mockDeletePodCollection
226+
k8sClientDeleteSecretMock = tt.args.mockDeleteSecret
227+
k8sClientCreateSecretMock = tt.args.mockCreateSecret
228+
k8sClientDeletePodCollectionMock = tt.args.mockDeletePodCollection
252229
if err := tenantUpdateEncryption(tt.args.ctx, tt.args.opClient, tt.args.clientSet, tt.args.namespace, tt.args.params); (err != nil) != tt.wantErr {
253230
t.Errorf("tenantUpdateEncryption() error = %v, wantErr %v", err, tt.wantErr)
254231
}
@@ -380,8 +357,8 @@ func Test_createOrReplaceKesConfigurationSecrets(t *testing.T) {
380357
}
381358
for _, tt := range tests {
382359
t.Run(tt.name, func(t *testing.T) {
383-
DeleteSecretMock = tt.args.mockDeleteSecret
384-
CreateSecretMock = tt.args.mockCreateSecret
360+
k8sClientDeleteSecretMock = tt.args.mockDeleteSecret
361+
k8sClientCreateSecretMock = tt.args.mockCreateSecret
385362
got, got1, err := createOrReplaceKesConfigurationSecrets(tt.args.ctx, tt.args.clientSet, tt.args.ns, tt.args.encryptionCfg, tt.args.kesConfigurationSecretName, tt.args.kesClientCertSecretName, tt.args.tenantName)
386363
if (err != nil) != tt.wantErr {
387364
t.Errorf("createOrReplaceKesConfigurationSecrets() error = %v, wantErr %v", err, tt.wantErr)

operatorapi/tenants_test.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ import (
2828
"testing"
2929
"time"
3030

31-
"github.com/stretchr/testify/assert"
32-
33-
xhttp "github.com/minio/console/pkg/http"
34-
35-
"github.com/minio/console/operatorapi/operations/operator_api"
36-
3731
"github.com/go-openapi/swag"
3832
"github.com/minio/console/models"
33+
"github.com/minio/console/operatorapi/operations/operator_api"
34+
xhttp "github.com/minio/console/pkg/http"
3935
miniov2 "github.com/minio/operator/pkg/apis/minio.min.io/v2"
36+
"github.com/stretchr/testify/assert"
4037
corev1 "k8s.io/api/core/v1"
4138
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
4239
"k8s.io/apimachinery/pkg/api/resource"
@@ -55,12 +52,10 @@ var (
5552
)
5653

5754
var (
58-
opClientTenantListMock func(ctx context.Context, namespace string, opts metav1.ListOptions) (*miniov2.TenantList, error)
59-
httpClientGetMock func(url string) (resp *http.Response, err error)
60-
httpClientPostMock func(url, contentType string, body io.Reader) (resp *http.Response, err error)
61-
httpClientDoMock func(req *http.Request) (*http.Response, error)
62-
k8sclientGetSecretMock func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error)
63-
k8sclientGetServiceMock func(ctx context.Context, namespace, serviceName string, opts metav1.GetOptions) (*corev1.Service, error)
55+
opClientTenantListMock func(ctx context.Context, namespace string, opts metav1.ListOptions) (*miniov2.TenantList, error)
56+
httpClientGetMock func(url string) (resp *http.Response, err error)
57+
httpClientPostMock func(url, contentType string, body io.Reader) (resp *http.Response, err error)
58+
httpClientDoMock func(req *http.Request) (*http.Response, error)
6459
)
6560

6661
// mock function of TenantDelete()
@@ -103,14 +98,6 @@ func (h httpClientMock) Do(req *http.Request) (*http.Response, error) {
10398
return httpClientDoMock(req)
10499
}
105100

106-
func (c k8sClientMock) getSecret(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) {
107-
return k8sclientGetSecretMock(ctx, namespace, secretName, opts)
108-
}
109-
110-
func (c k8sClientMock) getService(ctx context.Context, namespace, serviceName string, opts metav1.GetOptions) (*corev1.Service, error) {
111-
return k8sclientGetServiceMock(ctx, namespace, serviceName, opts)
112-
}
113-
114101
func Test_TenantInfoTenantAdminClient(t *testing.T) {
115102
ctx, cancel := context.WithCancel(context.Background())
116103
defer cancel()
@@ -1834,8 +1821,8 @@ func Test_updateTenantConfigurationFile(t *testing.T) {
18341821
for _, tt := range tests {
18351822
k8sclientGetSecretMock = tt.args.mockGetSecret
18361823
opClientTenantGetMock = tt.args.mockTenantGet
1837-
UpdateSecretMock = tt.args.mockUpdateSecret
1838-
DeletePodCollectionMock = tt.args.mockDeletePodCollection
1824+
k8sClientUpdateSecretMock = tt.args.mockUpdateSecret
1825+
k8sClientDeletePodCollectionMock = tt.args.mockDeletePodCollection
18391826
t.Run(tt.name, func(t *testing.T) {
18401827
err := updateTenantConfigurationFile(tt.args.ctx, tt.args.operatorClient, tt.args.client, tt.args.namespace, tt.args.params)
18411828
if (err != nil) != tt.wantErr {

0 commit comments

Comments
 (0)