Fix default BSL setting not work

Signed-off-by: Ming Qiu <mqiu@vmware.com>
This commit is contained in:
Ming Qiu
2023-12-01 02:06:35 +00:00
parent b9b2c88c5b
commit c6cba300fb
8 changed files with 276 additions and 56 deletions
@@ -17,12 +17,15 @@ limitations under the License.
package controller
import (
"context"
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -172,3 +175,125 @@ var _ = Describe("Backup Storage Location Reconciler", func() {
}
})
})
func TestEnsureSingleDefaultBSL(t *testing.T) {
tests := []struct {
name string
locations velerov1api.BackupStorageLocationList
defaultBackupInfo storage.DefaultBackupLocationInfo
expectedDefaultSet bool
expectedError error
}{
{
name: "MultipleDefaults",
locations: func() velerov1api.BackupStorageLocationList {
var locations velerov1api.BackupStorageLocationList
locations.Items = append(locations.Items, *builder.ForBackupStorageLocation(velerov1api.DefaultNamespace, "location-1").LastValidationTime(time.Now()).Default(true).Result())
locations.Items = append(locations.Items, *builder.ForBackupStorageLocation(velerov1api.DefaultNamespace, "location-2").LastValidationTime(time.Now().Add(-1 * time.Hour)).Default(true).Result())
return locations
}(),
expectedDefaultSet: true,
expectedError: nil,
},
{
name: "NoDefault with exist default bsl in defaultBackupInfo",
locations: func() velerov1api.BackupStorageLocationList {
var locations velerov1api.BackupStorageLocationList
locations.Items = append(locations.Items, *builder.ForBackupStorageLocation(velerov1api.DefaultNamespace, "location-1").Default(false).Result())
locations.Items = append(locations.Items, *builder.ForBackupStorageLocation(velerov1api.DefaultNamespace, "location-2").Default(false).Result())
return locations
}(),
defaultBackupInfo: storage.DefaultBackupLocationInfo{
StorageLocation: "location-2",
},
expectedDefaultSet: true,
expectedError: nil,
},
{
name: "NoDefault with non-exist default bsl in defaultBackupInfo",
locations: func() velerov1api.BackupStorageLocationList {
var locations velerov1api.BackupStorageLocationList
locations.Items = append(locations.Items, *builder.ForBackupStorageLocation(velerov1api.DefaultNamespace, "location-1").Default(false).Result())
locations.Items = append(locations.Items, *builder.ForBackupStorageLocation(velerov1api.DefaultNamespace, "location-2").Default(false).Result())
return locations
}(),
defaultBackupInfo: storage.DefaultBackupLocationInfo{
StorageLocation: "location-3",
},
expectedDefaultSet: false,
expectedError: nil,
},
{
name: "SingleDefault",
locations: func() velerov1api.BackupStorageLocationList {
var locations velerov1api.BackupStorageLocationList
locations.Items = append(locations.Items, *builder.ForBackupStorageLocation(velerov1api.DefaultNamespace, "location-1").Default(true).Result())
locations.Items = append(locations.Items, *builder.ForBackupStorageLocation(velerov1api.DefaultNamespace, "location-2").Default(false).Result())
return locations
}(),
expectedDefaultSet: true,
expectedError: nil,
},
}
for _, test := range tests {
// Setup reconciler
assert.Nil(t, velerov1api.AddToScheme(scheme.Scheme))
t.Run(test.name, func(t *testing.T) {
r := &backupStorageLocationReconciler{
ctx: context.Background(),
client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(&test.locations).Build(),
defaultBackupLocationInfo: test.defaultBackupInfo,
log: velerotest.NewLogger(),
}
defaultFound, err := r.ensureSingleDefaultBSL(test.locations)
assert.Equal(t, test.expectedDefaultSet, defaultFound)
assert.Equal(t, test.expectedError, err)
})
}
}
func TestBSLReconcile(t *testing.T) {
tests := []struct {
name string
locationList velerov1api.BackupStorageLocationList
defaultFound bool
expectedError error
}{
{
name: "NoBSL",
locationList: velerov1api.BackupStorageLocationList{},
defaultFound: false,
expectedError: nil,
},
{
name: "BSLNotFound",
locationList: func() velerov1api.BackupStorageLocationList {
var locations velerov1api.BackupStorageLocationList
locations.Items = append(locations.Items, *builder.ForBackupStorageLocation(velerov1api.DefaultNamespace, "location-2").Result())
return locations
}(),
defaultFound: false,
expectedError: nil,
},
}
pluginManager := &pluginmocks.Manager{}
pluginManager.On("CleanupClients").Return(nil)
for _, test := range tests {
// Setup reconciler
assert.Nil(t, velerov1api.AddToScheme(scheme.Scheme))
t.Run(test.name, func(t *testing.T) {
r := &backupStorageLocationReconciler{
ctx: context.Background(),
client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithRuntimeObjects(&test.locationList).Build(),
newPluginManager: func(logrus.FieldLogger) clientmgmt.Manager { return pluginManager },
log: velerotest.NewLogger(),
}
result, err := r.Reconcile(context.TODO(), ctrl.Request{NamespacedName: types.NamespacedName{Namespace: velerov1api.DefaultNamespace, Name: "location-1"}})
assert.Equal(t, test.expectedError, err)
assert.Equal(t, ctrl.Result{}, result)
})
}
}