Skip to content

Commit

Permalink
Move mock k8s functions to one files (#2569)
Browse files Browse the repository at this point in the history
  • Loading branch information
reivaj05 authored Jan 11, 2023
1 parent 1bad4c3 commit 9d37c9f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 103 deletions.
98 changes: 98 additions & 0 deletions operatorapi/k8s_client_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// This file is part of MinIO Console Server
// Copyright (c) 2023 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package operatorapi

import (
"context"

corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type k8sClientMock struct{}

var (
k8sClientGetResourceQuotaMock func(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error)
k8sClientGetNameSpaceMock func(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Namespace, error)
k8sClientStorageClassesMock func(ctx context.Context, opts metav1.ListOptions) (*storagev1.StorageClassList, error)

k8sClientGetConfigMapMock func(ctx context.Context, namespace, configMap string, opts metav1.GetOptions) (*corev1.ConfigMap, error)
k8sClientCreateConfigMapMock func(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.CreateOptions) (*corev1.ConfigMap, error)
k8sClientUpdateConfigMapMock func(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.UpdateOptions) (*corev1.ConfigMap, error)
k8sClientDeleteConfigMapMock func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error

k8sClientDeletePodCollectionMock func(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error
k8sClientDeleteSecretMock func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
k8sClientCreateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
k8sClientUpdateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error)
k8sclientGetSecretMock func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error)
k8sclientGetServiceMock func(ctx context.Context, namespace, serviceName string, opts metav1.GetOptions) (*corev1.Service, error)
)

func (c k8sClientMock) getResourceQuota(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error) {
return k8sClientGetResourceQuotaMock(ctx, namespace, resource, opts)
}

func (c k8sClientMock) getNamespace(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Namespace, error) {
return k8sClientGetNameSpaceMock(ctx, name, opts)
}

func (c k8sClientMock) getStorageClasses(ctx context.Context, opts metav1.ListOptions) (*storagev1.StorageClassList, error) {
return k8sClientStorageClassesMock(ctx, opts)
}

func (c k8sClientMock) getConfigMap(ctx context.Context, namespace, configMap string, opts metav1.GetOptions) (*corev1.ConfigMap, error) {
return k8sClientGetConfigMapMock(ctx, namespace, configMap, opts)
}

func (c k8sClientMock) createConfigMap(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.CreateOptions) (*corev1.ConfigMap, error) {
return k8sClientCreateConfigMapMock(ctx, namespace, cm, opts)
}

func (c k8sClientMock) updateConfigMap(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.UpdateOptions) (*corev1.ConfigMap, error) {
return k8sClientUpdateConfigMapMock(ctx, namespace, cm, opts)
}

func (c k8sClientMock) deleteConfigMap(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
return k8sClientDeleteConfigMapMock(ctx, namespace, name, opts)
}

func (c k8sClientMock) deletePodCollection(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
return k8sClientDeletePodCollectionMock(ctx, namespace, opts, listOpts)
}

func (c k8sClientMock) deleteSecret(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
return k8sClientDeleteSecretMock(ctx, namespace, name, opts)
}

func (c k8sClientMock) createSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
return k8sClientCreateSecretMock(ctx, namespace, secret, opts)
}

func (c k8sClientMock) updateSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error) {
return k8sClientUpdateSecretMock(ctx, namespace, secret, opts)
}

func (c k8sClientMock) getSecret(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) {
return k8sclientGetSecretMock(ctx, namespace, secretName, opts)
}

func (c k8sClientMock) getService(ctx context.Context, namespace, serviceName string, opts metav1.GetOptions) (*corev1.Service, error) {
return k8sclientGetServiceMock(ctx, namespace, serviceName, opts)
}
26 changes: 3 additions & 23 deletions operatorapi/marketplace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ import (
)

var (
testWithError = false
testServerWithError = false
errMock = errors.New("mock error")
k8sClientGetConfigMapMock func(ctx context.Context, namespace, configMap string, opts metav1.GetOptions) (*corev1.ConfigMap, error)
k8sClientCreateConfigMapMock func(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.CreateOptions) (*corev1.ConfigMap, error)
k8sClientUpdateConfigMapMock func(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.UpdateOptions) (*corev1.ConfigMap, error)
k8sClientDeleteConfigMapMock func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
testWithError = false
testServerWithError = false
errMock = errors.New("mock error")
)

type MarketplaceTestSuite struct {
Expand All @@ -52,22 +48,6 @@ type MarketplaceTestSuite struct {
postServer *httptest.Server
}

func (c k8sClientMock) getConfigMap(ctx context.Context, namespace, configMap string, opts metav1.GetOptions) (*corev1.ConfigMap, error) {
return k8sClientGetConfigMapMock(ctx, namespace, configMap, opts)
}

func (c k8sClientMock) createConfigMap(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.CreateOptions) (*corev1.ConfigMap, error) {
return k8sClientCreateConfigMapMock(ctx, namespace, cm, opts)
}

func (c k8sClientMock) updateConfigMap(ctx context.Context, namespace string, cm *corev1.ConfigMap, opts metav1.UpdateOptions) (*corev1.ConfigMap, error) {
return k8sClientUpdateConfigMapMock(ctx, namespace, cm, opts)
}

func (c k8sClientMock) deleteConfigMap(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
return k8sClientDeleteConfigMapMock(ctx, namespace, name, opts)
}

func (suite *MarketplaceTestSuite) SetupSuite() {
suite.assert = assert.New(suite.T())
suite.namespace = "default"
Expand Down
2 changes: 1 addition & 1 deletion operatorapi/operator_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (suite *OperatorSubnetTestSuite) SetupSuite() {
suite.getAPIKeyServer = httptest.NewServer(http.HandlerFunc(suite.getAPIKeyHandler))
suite.registerAPIKeyServer = httptest.NewServer(http.HandlerFunc(suite.registerAPIKeyHandler))
suite.k8sClient = k8sClientMock{}
CreateSecretMock = func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
k8sClientCreateSecretMock = func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
return &corev1.Secret{}, nil
}
k8sclientGetSecretMock = func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) {
Expand Down
27 changes: 1 addition & 26 deletions operatorapi/resource_quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,12 @@ import (
"reflect"
"testing"

storagev1 "k8s.io/api/storage/v1"

"github.com/minio/console/models"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type k8sClientMock struct{}

var (
k8sclientGetResourceQuotaMock func(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error)
k8sclientGetNameSpaceMock func(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Namespace, error)
k8sclientStorageClassesMock func(ctx context.Context, opts metav1.ListOptions) (*storagev1.StorageClassList, error)
)

// mock functions
func (c k8sClientMock) getResourceQuota(ctx context.Context, namespace, resource string, opts metav1.GetOptions) (*v1.ResourceQuota, error) {
return k8sclientGetResourceQuotaMock(ctx, namespace, resource, opts)
}

// mock functions
func (c k8sClientMock) getNamespace(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Namespace, error) {
return k8sclientGetNameSpaceMock(ctx, name, opts)
}

// mock functions
func (c k8sClientMock) getStorageClasses(ctx context.Context, opts metav1.ListOptions) (*storagev1.StorageClassList, error) {
return k8sclientStorageClassesMock(ctx, opts)
}

func Test_ResourceQuota(t *testing.T) {
mockHardResourceQuota := v1.ResourceList{
"storage": resource.MustParse("1000"),
Expand Down Expand Up @@ -141,7 +116,7 @@ func Test_ResourceQuota(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k8sclientGetResourceQuotaMock = tt.mockResourceQuota
k8sClientGetResourceQuotaMock = tt.mockResourceQuota
got, err := getResourceQuota(tt.args.ctx, tt.args.client, "ns", mockRQResponse.Name)
if err != nil {
if tt.wantErr {
Expand Down
39 changes: 8 additions & 31 deletions operatorapi/tenants_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
DeletePodCollectionMock func(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error
DeleteSecretMock func(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
CreateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
UpdateSecretMock func(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error)
)

func (c k8sClientMock) deletePodCollection(ctx context.Context, namespace string, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
return DeletePodCollectionMock(ctx, namespace, opts, listOpts)
}

func (c k8sClientMock) deleteSecret(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error {
return DeleteSecretMock(ctx, namespace, name, opts)
}

func (c k8sClientMock) createSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error) {
return CreateSecretMock(ctx, namespace, secret, opts)
}

func (c k8sClientMock) updateSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error) {
return UpdateSecretMock(ctx, namespace, secret, opts)
}

func Test_tenantUpdateCertificates(t *testing.T) {
k8sClient := k8sClientMock{}
opClient := opClientMock{}
Expand Down Expand Up @@ -198,9 +175,9 @@ func Test_tenantUpdateCertificates(t *testing.T) {
}
for _, tt := range tests {
opClientTenantGetMock = tt.args.mockTenantGet
DeleteSecretMock = tt.args.mockDeleteSecret
CreateSecretMock = tt.args.mockCreateSecret
DeletePodCollectionMock = tt.args.mockDeletePodCollection
k8sClientDeleteSecretMock = tt.args.mockDeleteSecret
k8sClientCreateSecretMock = tt.args.mockCreateSecret
k8sClientDeletePodCollectionMock = tt.args.mockDeletePodCollection
t.Run(tt.name, func(t *testing.T) {
if err := tenantUpdateCertificates(tt.args.ctx, tt.args.opClient, tt.args.clientSet, tt.args.namespace, tt.args.params); (err != nil) != tt.wantErr {
t.Errorf("tenantUpdateCertificates() error = %v, wantErr %v", err, tt.wantErr)
Expand Down Expand Up @@ -246,9 +223,9 @@ func Test_tenantUpdateEncryption(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opClientTenantGetMock = tt.args.mockTenantGet
DeleteSecretMock = tt.args.mockDeleteSecret
CreateSecretMock = tt.args.mockCreateSecret
DeletePodCollectionMock = tt.args.mockDeletePodCollection
k8sClientDeleteSecretMock = tt.args.mockDeleteSecret
k8sClientCreateSecretMock = tt.args.mockCreateSecret
k8sClientDeletePodCollectionMock = tt.args.mockDeletePodCollection
if err := tenantUpdateEncryption(tt.args.ctx, tt.args.opClient, tt.args.clientSet, tt.args.namespace, tt.args.params); (err != nil) != tt.wantErr {
t.Errorf("tenantUpdateEncryption() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down Expand Up @@ -380,8 +357,8 @@ func Test_createOrReplaceKesConfigurationSecrets(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
DeleteSecretMock = tt.args.mockDeleteSecret
CreateSecretMock = tt.args.mockCreateSecret
k8sClientDeleteSecretMock = tt.args.mockDeleteSecret
k8sClientCreateSecretMock = tt.args.mockCreateSecret
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)
if (err != nil) != tt.wantErr {
t.Errorf("createOrReplaceKesConfigurationSecrets() error = %v, wantErr %v", err, tt.wantErr)
Expand Down
31 changes: 9 additions & 22 deletions operatorapi/tenants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"

xhttp "github.com/minio/console/pkg/http"

"github.com/minio/console/operatorapi/operations/operator_api"

"github.com/go-openapi/swag"
"github.com/minio/console/models"
"github.com/minio/console/operatorapi/operations/operator_api"
xhttp "github.com/minio/console/pkg/http"
miniov2 "github.com/minio/operator/pkg/apis/minio.min.io/v2"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -55,12 +52,10 @@ var (
)

var (
opClientTenantListMock func(ctx context.Context, namespace string, opts metav1.ListOptions) (*miniov2.TenantList, error)
httpClientGetMock func(url string) (resp *http.Response, err error)
httpClientPostMock func(url, contentType string, body io.Reader) (resp *http.Response, err error)
httpClientDoMock func(req *http.Request) (*http.Response, error)
k8sclientGetSecretMock func(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error)
k8sclientGetServiceMock func(ctx context.Context, namespace, serviceName string, opts metav1.GetOptions) (*corev1.Service, error)
opClientTenantListMock func(ctx context.Context, namespace string, opts metav1.ListOptions) (*miniov2.TenantList, error)
httpClientGetMock func(url string) (resp *http.Response, err error)
httpClientPostMock func(url, contentType string, body io.Reader) (resp *http.Response, err error)
httpClientDoMock func(req *http.Request) (*http.Response, error)
)

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

func (c k8sClientMock) getSecret(ctx context.Context, namespace, secretName string, opts metav1.GetOptions) (*corev1.Secret, error) {
return k8sclientGetSecretMock(ctx, namespace, secretName, opts)
}

func (c k8sClientMock) getService(ctx context.Context, namespace, serviceName string, opts metav1.GetOptions) (*corev1.Service, error) {
return k8sclientGetServiceMock(ctx, namespace, serviceName, opts)
}

func Test_TenantInfoTenantAdminClient(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -1834,8 +1821,8 @@ func Test_updateTenantConfigurationFile(t *testing.T) {
for _, tt := range tests {
k8sclientGetSecretMock = tt.args.mockGetSecret
opClientTenantGetMock = tt.args.mockTenantGet
UpdateSecretMock = tt.args.mockUpdateSecret
DeletePodCollectionMock = tt.args.mockDeletePodCollection
k8sClientUpdateSecretMock = tt.args.mockUpdateSecret
k8sClientDeletePodCollectionMock = tt.args.mockDeletePodCollection
t.Run(tt.name, func(t *testing.T) {
err := updateTenantConfigurationFile(tt.args.ctx, tt.args.operatorClient, tt.args.client, tt.args.namespace, tt.args.params)
if (err != nil) != tt.wantErr {
Expand Down

0 comments on commit 9d37c9f

Please sign in to comment.