Add unit test to describe pvc (#2020)

This commit is contained in:
Javier Adriel
2022-05-20 18:42:33 -05:00
committed by GitHub
parent 326d709bf9
commit 7cb04ce62b
4 changed files with 114 additions and 2 deletions

View File

@@ -38,6 +38,7 @@ type K8sClientI interface {
deleteSecret(ctx context.Context, namespace string, name string, opts metav1.DeleteOptions) error
createSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.CreateOptions) (*v1.Secret, error)
updateSecret(ctx context.Context, namespace string, secret *v1.Secret, opts metav1.UpdateOptions) (*v1.Secret, error)
getPVC(ctx context.Context, namespace string, pvcName string, opts metav1.GetOptions) (*v1.PersistentVolumeClaim, error)
}
// Interface implementation
@@ -82,3 +83,7 @@ func (c *k8sClient) getNamespace(ctx context.Context, name string, opts metav1.G
func (c *k8sClient) getStorageClasses(ctx context.Context, opts metav1.ListOptions) (*storagev1.StorageClassList, error) {
return c.client.StorageV1().StorageClasses().List(ctx, opts)
}
func (c *k8sClient) getPVC(ctx context.Context, namespace string, pvcName string, opts metav1.GetOptions) (*v1.PersistentVolumeClaim, error) {
return c.client.CoreV1().PersistentVolumeClaims(namespace).Get(ctx, pvcName, opts)
}

View File

@@ -16,7 +16,28 @@
package operatorapi
import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type (
opClientMock struct{}
httpClientMock struct{}
)
func createMockPVC(pvcMockName, pvcMockNamespace string) *v1.PersistentVolumeClaim {
var mockVolumeMode v1.PersistentVolumeMode = "mockVolumeMode"
mockStorage := "mockStorage"
return &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: pvcMockName,
Namespace: pvcMockNamespace,
},
Spec: v1.PersistentVolumeClaimSpec{
StorageClassName: &mockStorage,
VolumeMode: &mockVolumeMode,
},
}
}

View File

@@ -270,13 +270,18 @@ func getTenantCSResponse(session *models.Principal, params operator_api.ListTena
}
func getPVCDescribeResponse(session *models.Principal, params operator_api.GetPVCDescribeParams) (*models.DescribePVCWrapper, *models.Error) {
clientSet, err := cluster.K8sClient(session.STSSessionToken)
ctx, cancel := context.WithCancel(params.HTTPRequest.Context())
defer cancel()
clientset, err := cluster.K8sClient(session.STSSessionToken)
if err != nil {
return nil, errors.ErrorWithContext(ctx, err)
}
pvc, err := clientset.CoreV1().PersistentVolumeClaims(params.Namespace).Get(ctx, params.PVCName, metav1.GetOptions{})
k8sClient := k8sClient{client: clientSet}
return getPVCDescribe(ctx, params.Namespace, params.PVCName, &k8sClient)
}
func getPVCDescribe(ctx context.Context, namespace string, pvcName string, clientSet K8sClientI) (*models.DescribePVCWrapper, *models.Error) {
pvc, err := clientSet.getPVC(ctx, namespace, pvcName, metav1.GetOptions{})
if err != nil {
return nil, errors.ErrorWithContext(ctx, err)
}

View File

@@ -0,0 +1,81 @@
// This file is part of MinIO Console Server
// Copyright (c) 2021 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"
"errors"
"testing"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
getPVCWithError = true
pvcMockName = "mockName"
pvcMockNamespace = "mockNamespace"
)
func (c k8sClientMock) getPVC(ctx context.Context, namespace string, pvcName string, opts metav1.GetOptions) (*v1.PersistentVolumeClaim, error) {
if getPVCWithError {
return nil, errors.New("Mock error during getPVC")
}
return createMockPVC(pvcMockName, pvcMockNamespace), nil
}
var testCasesGetPVCDescribe = []struct {
name string
errorExpected bool
}{
{
name: "Successful getPVCDescribe",
errorExpected: false,
},
{
name: "Error getPVCDescribe",
errorExpected: true,
},
}
func TestGetPVCDescribe(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
kClient := k8sClientMock{}
for _, tt := range testCasesGetPVCDescribe {
t.Run(tt.name, func(t *testing.T) {
getPVCWithError = tt.errorExpected
pvc, err := getPVCDescribe(ctx, pvcMockNamespace, pvcMockName, kClient)
if err != nil {
if tt.errorExpected {
return
}
t.Errorf("getPVCDescribe() error = %v, errorExpected %v", err, tt.errorExpected)
}
if pvc == nil {
t.Errorf("getPVCDescribe() expected type: *v1.PersistentVolumeClaim, got: nil")
return
}
if pvc.Name != pvcMockName {
t.Errorf("Expected pvc name %s got %s", pvc.Name, pvcMockName)
}
if pvc.Namespace != pvcMockNamespace {
t.Errorf("Expected pvc namespace %s got %s", pvc.Namespace, pvcMockNamespace)
}
})
}
}