Improve the unit test coverage for pkg/install
Improve the unit test coverage for pkg/install Fixes #6339 Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>pull/6351/head
parent
8427a9fdb3
commit
2d6f4e5462
|
@ -0,0 +1,149 @@
|
||||||
|
package install
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
||||||
|
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
||||||
|
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||||
|
|
||||||
|
v1crds "github.com/vmware-tanzu/velero/config/crd/v1/crds"
|
||||||
|
"github.com/vmware-tanzu/velero/pkg/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInstall(t *testing.T) {
|
||||||
|
dc := &test.FakeDynamicClient{}
|
||||||
|
dc.On("Create", mock.Anything).Return(&unstructured.Unstructured{}, nil)
|
||||||
|
|
||||||
|
factory := &test.FakeDynamicFactory{}
|
||||||
|
factory.On("ClientForGroupVersionResource", mock.Anything, mock.Anything, mock.Anything).Return(dc, nil)
|
||||||
|
|
||||||
|
c := fake.NewClientBuilder().WithObjects(
|
||||||
|
&apiextv1.CustomResourceDefinition{
|
||||||
|
ObjectMeta: v1.ObjectMeta{
|
||||||
|
Name: "backuprepositories.velero.io",
|
||||||
|
},
|
||||||
|
|
||||||
|
Status: apiextv1.CustomResourceDefinitionStatus{
|
||||||
|
Conditions: []apiextv1.CustomResourceDefinitionCondition{
|
||||||
|
{
|
||||||
|
Type: apiextv1.Established,
|
||||||
|
Status: apiextv1.ConditionTrue,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: apiextv1.NamesAccepted,
|
||||||
|
Status: apiextv1.ConditionTrue,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
).Build()
|
||||||
|
|
||||||
|
resources := &unstructured.UnstructuredList{}
|
||||||
|
require.Nil(t, appendUnstructured(resources, v1crds.CRDs[0]))
|
||||||
|
require.Nil(t, appendUnstructured(resources, Namespace("velero")))
|
||||||
|
|
||||||
|
assert.Nil(t, Install(factory, c, resources, os.Stdout))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_crdsAreReady(t *testing.T) {
|
||||||
|
c := fake.NewClientBuilder().WithObjects(
|
||||||
|
&apiextv1beta1.CustomResourceDefinition{
|
||||||
|
ObjectMeta: v1.ObjectMeta{
|
||||||
|
Name: "backuprepositories.velero.io",
|
||||||
|
},
|
||||||
|
|
||||||
|
Status: apiextv1beta1.CustomResourceDefinitionStatus{
|
||||||
|
Conditions: []apiextv1beta1.CustomResourceDefinitionCondition{
|
||||||
|
{
|
||||||
|
Type: apiextv1beta1.Established,
|
||||||
|
Status: apiextv1beta1.ConditionTrue,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: apiextv1beta1.NamesAccepted,
|
||||||
|
Status: apiextv1beta1.ConditionTrue,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
).Build()
|
||||||
|
|
||||||
|
crd := &apiextv1beta1.CustomResourceDefinition{
|
||||||
|
TypeMeta: v1.TypeMeta{
|
||||||
|
Kind: "CustomResourceDefinition",
|
||||||
|
APIVersion: "v1beta1",
|
||||||
|
},
|
||||||
|
ObjectMeta: v1.ObjectMeta{
|
||||||
|
Name: "backuprepositories.velero.io",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(crd)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
crds := []*unstructured.Unstructured{
|
||||||
|
{
|
||||||
|
Object: obj,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ready, err := crdsAreReady(c, crds)
|
||||||
|
require.Nil(t, err)
|
||||||
|
assert.True(t, ready)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeploymentIsReady(t *testing.T) {
|
||||||
|
deployment := &appsv1.Deployment{
|
||||||
|
Status: appsv1.DeploymentStatus{
|
||||||
|
Conditions: []appsv1.DeploymentCondition{
|
||||||
|
{
|
||||||
|
Type: appsv1.DeploymentAvailable,
|
||||||
|
Status: corev1.ConditionTrue,
|
||||||
|
LastTransitionTime: v1.NewTime(time.Now().Add(-15 * time.Second)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(deployment)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
dc := &test.FakeDynamicClient{}
|
||||||
|
dc.On("Get", mock.Anything, mock.Anything).Return(&unstructured.Unstructured{Object: obj}, nil)
|
||||||
|
|
||||||
|
factory := &test.FakeDynamicFactory{}
|
||||||
|
factory.On("ClientForGroupVersionResource", mock.Anything, mock.Anything, mock.Anything).Return(dc, nil)
|
||||||
|
|
||||||
|
ready, err := DeploymentIsReady(factory, "velero")
|
||||||
|
require.Nil(t, err)
|
||||||
|
assert.True(t, ready)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDaemonSetIsReady(t *testing.T) {
|
||||||
|
daemonset := &appsv1.DaemonSet{
|
||||||
|
Status: appsv1.DaemonSetStatus{
|
||||||
|
NumberAvailable: 1,
|
||||||
|
DesiredNumberScheduled: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(daemonset)
|
||||||
|
require.Nil(t, err)
|
||||||
|
|
||||||
|
dc := &test.FakeDynamicClient{}
|
||||||
|
dc.On("Get", mock.Anything, mock.Anything).Return(&unstructured.Unstructured{Object: obj}, nil)
|
||||||
|
|
||||||
|
factory := &test.FakeDynamicFactory{}
|
||||||
|
factory.On("ClientForGroupVersionResource", mock.Anything, mock.Anything, mock.Anything).Return(dc, nil)
|
||||||
|
|
||||||
|
ready, err := DaemonSetIsReady(factory, "velero")
|
||||||
|
require.Nil(t, err)
|
||||||
|
assert.True(t, ready)
|
||||||
|
}
|
|
@ -20,6 +20,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestResources(t *testing.T) {
|
func TestResources(t *testing.T) {
|
||||||
|
@ -62,3 +64,49 @@ func TestResources(t *testing.T) {
|
||||||
assert.Equal(t, "velero", sa.ObjectMeta.Namespace)
|
assert.Equal(t, "velero", sa.ObjectMeta.Namespace)
|
||||||
assert.Equal(t, "cbd", sa.ObjectMeta.Annotations["abcd"])
|
assert.Equal(t, "cbd", sa.ObjectMeta.Annotations["abcd"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAllCRDs(t *testing.T) {
|
||||||
|
list := AllCRDs()
|
||||||
|
assert.Len(t, list.Items, 13)
|
||||||
|
assert.Equal(t, Labels(), list.Items[0].GetLabels())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAllResources(t *testing.T) {
|
||||||
|
option := &VeleroOptions{
|
||||||
|
Namespace: "velero",
|
||||||
|
SecretData: []byte{'a'},
|
||||||
|
UseVolumeSnapshots: true,
|
||||||
|
UseNodeAgent: true,
|
||||||
|
}
|
||||||
|
list := AllResources(option)
|
||||||
|
|
||||||
|
objects := map[string]unstructured.Unstructured{}
|
||||||
|
for _, item := range list.Items {
|
||||||
|
objects[item.GetKind()] = item
|
||||||
|
}
|
||||||
|
|
||||||
|
ns, exist := objects["Namespace"]
|
||||||
|
require.True(t, exist)
|
||||||
|
assert.Equal(t, "velero", ns.GetName())
|
||||||
|
|
||||||
|
_, exist = objects["ClusterRoleBinding"]
|
||||||
|
assert.True(t, exist)
|
||||||
|
|
||||||
|
_, exist = objects["ServiceAccount"]
|
||||||
|
assert.True(t, exist)
|
||||||
|
|
||||||
|
_, exist = objects["Secret"]
|
||||||
|
assert.True(t, exist)
|
||||||
|
|
||||||
|
_, exist = objects["BackupStorageLocation"]
|
||||||
|
assert.True(t, exist)
|
||||||
|
|
||||||
|
_, exist = objects["VolumeSnapshotLocation"]
|
||||||
|
assert.True(t, exist)
|
||||||
|
|
||||||
|
_, exist = objects["Deployment"]
|
||||||
|
assert.True(t, exist)
|
||||||
|
|
||||||
|
_, exist = objects["DaemonSet"]
|
||||||
|
assert.True(t, exist)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue