2018-06-20 22:46:41 +00:00
|
|
|
/*
|
2019-03-20 19:32:48 +00:00
|
|
|
Copyright 2018 the Velero contributors.
|
2018-06-20 22:46:41 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-10-23 14:36:11 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-06-20 22:46:41 +00:00
|
|
|
|
2019-01-25 03:33:07 +00:00
|
|
|
velerov1api "github.com/heptio/velero/pkg/apis/velero/v1"
|
|
|
|
velerotest "github.com/heptio/velero/pkg/util/test"
|
2018-06-20 22:46:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPVBHandler(t *testing.T) {
|
|
|
|
controllerNode := "foo"
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-01-25 03:33:07 +00:00
|
|
|
obj *velerov1api.PodVolumeBackup
|
2018-06-20 22:46:41 +00:00
|
|
|
shouldEnqueue bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Empty phase pvb on same node should be enqueued",
|
2019-01-25 03:33:07 +00:00
|
|
|
obj: &velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-20 22:46:41 +00:00
|
|
|
Node: controllerNode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
shouldEnqueue: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "New phase pvb on same node should be enqueued",
|
2019-01-25 03:33:07 +00:00
|
|
|
obj: &velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-20 22:46:41 +00:00
|
|
|
Node: controllerNode,
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{
|
|
|
|
Phase: velerov1api.PodVolumeBackupPhaseNew,
|
2018-06-20 22:46:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
shouldEnqueue: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "InProgress phase pvb on same node should not be enqueued",
|
2019-01-25 03:33:07 +00:00
|
|
|
obj: &velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-20 22:46:41 +00:00
|
|
|
Node: controllerNode,
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{
|
|
|
|
Phase: velerov1api.PodVolumeBackupPhaseInProgress,
|
2018-06-20 22:46:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
shouldEnqueue: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Completed phase pvb on same node should not be enqueued",
|
2019-01-25 03:33:07 +00:00
|
|
|
obj: &velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-20 22:46:41 +00:00
|
|
|
Node: controllerNode,
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{
|
|
|
|
Phase: velerov1api.PodVolumeBackupPhaseCompleted,
|
2018-06-20 22:46:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
shouldEnqueue: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Failed phase pvb on same node should not be enqueued",
|
2019-01-25 03:33:07 +00:00
|
|
|
obj: &velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-20 22:46:41 +00:00
|
|
|
Node: controllerNode,
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{
|
|
|
|
Phase: velerov1api.PodVolumeBackupPhaseFailed,
|
2018-06-20 22:46:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
shouldEnqueue: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "Empty phase pvb on different node should not be enqueued",
|
2019-01-25 03:33:07 +00:00
|
|
|
obj: &velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-20 22:46:41 +00:00
|
|
|
Node: "some-other-node",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
shouldEnqueue: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "New phase pvb on different node should not be enqueued",
|
2019-01-25 03:33:07 +00:00
|
|
|
obj: &velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-20 22:46:41 +00:00
|
|
|
Node: "some-other-node",
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{
|
|
|
|
Phase: velerov1api.PodVolumeBackupPhaseNew,
|
2018-06-20 22:46:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
shouldEnqueue: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "InProgress phase pvb on different node should not be enqueued",
|
2019-01-25 03:33:07 +00:00
|
|
|
obj: &velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-20 22:46:41 +00:00
|
|
|
Node: "some-other-node",
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{
|
|
|
|
Phase: velerov1api.PodVolumeBackupPhaseInProgress,
|
2018-06-20 22:46:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
shouldEnqueue: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Completed phase pvb on different node should not be enqueued",
|
2019-01-25 03:33:07 +00:00
|
|
|
obj: &velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-20 22:46:41 +00:00
|
|
|
Node: "some-other-node",
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{
|
|
|
|
Phase: velerov1api.PodVolumeBackupPhaseCompleted,
|
2018-06-20 22:46:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
shouldEnqueue: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Failed phase pvb on different node should not be enqueued",
|
2019-01-25 03:33:07 +00:00
|
|
|
obj: &velerov1api.PodVolumeBackup{
|
|
|
|
Spec: velerov1api.PodVolumeBackupSpec{
|
2018-06-20 22:46:41 +00:00
|
|
|
Node: "some-other-node",
|
|
|
|
},
|
2019-01-25 03:33:07 +00:00
|
|
|
Status: velerov1api.PodVolumeBackupStatus{
|
|
|
|
Phase: velerov1api.PodVolumeBackupPhaseFailed,
|
2018-06-20 22:46:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
shouldEnqueue: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
c := &podVolumeBackupController{
|
2019-01-25 03:33:07 +00:00
|
|
|
genericController: newGenericController("pod-volume-backup", velerotest.NewLogger()),
|
2018-06-20 22:46:41 +00:00
|
|
|
nodeName: controllerNode,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.pvbHandler(test.obj)
|
|
|
|
|
|
|
|
if !test.shouldEnqueue {
|
|
|
|
assert.Equal(t, 0, c.queue.Len())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, 1, c.queue.Len())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|