mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-08 14:21:18 +00:00
Improve the unit test coverage of pkg/cmd/server package (#6342)
Improve the unit test coverage of pkg/cmd/server package Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
This commit is contained in:
@@ -17,14 +17,28 @@ limitations under the License.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
kubefake "k8s.io/client-go/kubernetes/fake"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
v1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
"github.com/vmware-tanzu/velero/pkg/client/mocks"
|
||||
"github.com/vmware-tanzu/velero/pkg/controller"
|
||||
discovery_mocks "github.com/vmware-tanzu/velero/pkg/discovery/mocks"
|
||||
velerotest "github.com/vmware-tanzu/velero/pkg/test"
|
||||
"github.com/vmware-tanzu/velero/pkg/uploader"
|
||||
)
|
||||
|
||||
func TestVeleroResourcesExist(t *testing.T) {
|
||||
@@ -151,3 +165,191 @@ func TestRemoveControllers(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewCommand(t *testing.T) {
|
||||
assert.NotNil(t, NewCommand(nil))
|
||||
}
|
||||
|
||||
func Test_newServer(t *testing.T) {
|
||||
factory := &mocks.Factory{}
|
||||
logger := logrus.New()
|
||||
|
||||
// invalid uploader type
|
||||
_, err := newServer(factory, serverConfig{
|
||||
uploaderType: "invalid",
|
||||
}, logger)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// invalid clientQPS
|
||||
_, err = newServer(factory, serverConfig{
|
||||
uploaderType: uploader.KopiaType,
|
||||
clientQPS: -1,
|
||||
}, logger)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// invalid clientBurst
|
||||
factory.On("SetClientQPS", mock.Anything).Return()
|
||||
_, err = newServer(factory, serverConfig{
|
||||
uploaderType: uploader.KopiaType,
|
||||
clientQPS: 1,
|
||||
clientBurst: -1,
|
||||
}, logger)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// invalid clientBclientPageSizeurst
|
||||
factory.On("SetClientQPS", mock.Anything).Return().
|
||||
On("SetClientBurst", mock.Anything).Return()
|
||||
_, err = newServer(factory, serverConfig{
|
||||
uploaderType: uploader.KopiaType,
|
||||
clientQPS: 1,
|
||||
clientBurst: 1,
|
||||
clientPageSize: -1,
|
||||
}, logger)
|
||||
assert.NotNil(t, err)
|
||||
|
||||
// got error when creating client
|
||||
factory.On("SetClientQPS", mock.Anything).Return().
|
||||
On("SetClientBurst", mock.Anything).Return().
|
||||
On("KubeClient").Return(nil, nil).
|
||||
On("Client").Return(nil, nil).
|
||||
On("DynamicClient").Return(nil, errors.New("error"))
|
||||
_, err = newServer(factory, serverConfig{
|
||||
uploaderType: uploader.KopiaType,
|
||||
clientQPS: 1,
|
||||
clientBurst: 1,
|
||||
clientPageSize: 100,
|
||||
}, logger)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func Test_namespaceExists(t *testing.T) {
|
||||
client := kubefake.NewSimpleClientset(&corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "velero",
|
||||
},
|
||||
})
|
||||
server := &server{
|
||||
kubeClient: client,
|
||||
logger: logrus.New(),
|
||||
}
|
||||
|
||||
// namespace doesn't exist
|
||||
assert.NotNil(t, server.namespaceExists("not-exist"))
|
||||
|
||||
// namespace exists
|
||||
assert.Nil(t, server.namespaceExists("velero"))
|
||||
}
|
||||
|
||||
func Test_veleroResourcesExist(t *testing.T) {
|
||||
helper := &discovery_mocks.Helper{}
|
||||
server := &server{
|
||||
discoveryHelper: helper,
|
||||
logger: logrus.New(),
|
||||
}
|
||||
|
||||
// velero resources don't exist
|
||||
helper.On("Resources").Return(nil)
|
||||
assert.NotNil(t, server.veleroResourcesExist())
|
||||
|
||||
// velero resources exist
|
||||
helper.On("Resources").Unset()
|
||||
helper.On("Resources").Return([]*metav1.APIResourceList{
|
||||
{
|
||||
GroupVersion: velerov1api.SchemeGroupVersion.String(),
|
||||
APIResources: []metav1.APIResource{
|
||||
{Kind: "Backup"},
|
||||
{Kind: "Restore"},
|
||||
{Kind: "Schedule"},
|
||||
{Kind: "DownloadRequest"},
|
||||
{Kind: "DeleteBackupRequest"},
|
||||
{Kind: "PodVolumeBackup"},
|
||||
{Kind: "PodVolumeRestore"},
|
||||
{Kind: "BackupRepository"},
|
||||
{Kind: "BackupStorageLocation"},
|
||||
{Kind: "VolumeSnapshotLocation"},
|
||||
{Kind: "ServerStatusRequest"},
|
||||
},
|
||||
},
|
||||
})
|
||||
assert.Nil(t, server.veleroResourcesExist())
|
||||
}
|
||||
|
||||
func Test_markInProgressBackupsFailed(t *testing.T) {
|
||||
scheme := runtime.NewScheme()
|
||||
velerov1api.AddToScheme(scheme)
|
||||
|
||||
c := fake.NewClientBuilder().
|
||||
WithScheme(scheme).
|
||||
WithLists(&velerov1api.BackupList{
|
||||
Items: []velerov1api.Backup{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "velero",
|
||||
Name: "backup01",
|
||||
},
|
||||
Status: velerov1api.BackupStatus{
|
||||
Phase: velerov1api.BackupPhaseInProgress,
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "velero",
|
||||
Name: "backup02",
|
||||
},
|
||||
Status: velerov1api.BackupStatus{
|
||||
Phase: velerov1api.BackupPhaseCompleted,
|
||||
},
|
||||
},
|
||||
},
|
||||
}).
|
||||
Build()
|
||||
markInProgressBackupsFailed(context.Background(), c, "velero", logrus.New())
|
||||
|
||||
backup01 := &velerov1api.Backup{}
|
||||
require.Nil(t, c.Get(context.Background(), client.ObjectKey{Namespace: "velero", Name: "backup01"}, backup01))
|
||||
assert.Equal(t, velerov1api.BackupPhaseFailed, backup01.Status.Phase)
|
||||
|
||||
backup02 := &velerov1api.Backup{}
|
||||
require.Nil(t, c.Get(context.Background(), client.ObjectKey{Namespace: "velero", Name: "backup02"}, backup02))
|
||||
assert.Equal(t, velerov1api.BackupPhaseCompleted, backup02.Status.Phase)
|
||||
}
|
||||
|
||||
func Test_markInProgressRestoresFailed(t *testing.T) {
|
||||
scheme := runtime.NewScheme()
|
||||
velerov1api.AddToScheme(scheme)
|
||||
|
||||
c := fake.NewClientBuilder().
|
||||
WithScheme(scheme).
|
||||
WithLists(&velerov1api.RestoreList{
|
||||
Items: []velerov1api.Restore{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "velero",
|
||||
Name: "restore01",
|
||||
},
|
||||
Status: velerov1api.RestoreStatus{
|
||||
Phase: velerov1api.RestorePhaseInProgress,
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "velero",
|
||||
Name: "restore02",
|
||||
},
|
||||
Status: velerov1api.RestoreStatus{
|
||||
Phase: velerov1api.RestorePhaseCompleted,
|
||||
},
|
||||
},
|
||||
},
|
||||
}).
|
||||
Build()
|
||||
markInProgressRestoresFailed(context.Background(), c, "velero", logrus.New())
|
||||
|
||||
restore01 := &velerov1api.Restore{}
|
||||
require.Nil(t, c.Get(context.Background(), client.ObjectKey{Namespace: "velero", Name: "restore01"}, restore01))
|
||||
assert.Equal(t, velerov1api.RestorePhaseFailed, restore01.Status.Phase)
|
||||
|
||||
restore02 := &velerov1api.Restore{}
|
||||
require.Nil(t, c.Get(context.Background(), client.ObjectKey{Namespace: "velero", Name: "restore02"}, restore02))
|
||||
assert.Equal(t, velerov1api.RestorePhaseCompleted, restore02.Status.Phase)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user