Skip to content

Commit

Permalink
Add a test checking whether the PVC exists on BackupPvc.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgielen committed Dec 21, 2023
1 parent ae68ecc commit d2463a9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cmd/backsnap/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ func BackupPvc(ctx context.Context, clientset kubernetes.Interface, kclient clie
}
}

// Check whether the target PVC exists
targetPvc := &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: pvcName,
},
}
if err := kclient.Get(ctx, client.ObjectKeyFromObject(targetPvc), targetPvc); err != nil {
return err
}

var snapshotClass *string
if settings.SnapshotClass != "" {
snapshotClass = &settings.SnapshotClass
Expand Down Expand Up @@ -188,6 +199,10 @@ func BackupPvc(ctx context.Context, clientset kubernetes.Interface, kclient clie

time.Sleep(time.Second)
}

if ctx.Err() != nil {
return ctx.Err()
}
}

slog.InfoContext(ctx, "volumesnapshot is ready!",
Expand Down
34 changes: 34 additions & 0 deletions cmd/backsnap/backup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main_test

import (
"context"
"testing"

volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
backsnap "github.com/skybitsnl/backsnap/cmd/backsnap"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
dynamicfake "k8s.io/client-go/dynamic/fake"
oldfake "k8s.io/client-go/kubernetes/fake"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestBackupPVC_DoesntExist(t *testing.T) {
ctx := context.Background()

scheme := runtime.NewScheme()
corev1.AddToScheme(scheme)
batchv1.AddToScheme(scheme)
volumesnapshotv1.AddToScheme(scheme)

clientset := oldfake.NewSimpleClientset()
kclient := fake.NewClientBuilder().WithScheme(scheme).Build()
dynamicClient := dynamicfake.NewSimpleDynamicClient(scheme)

err := backsnap.BackupPvc(ctx, clientset, kclient, dynamicClient, "app1", "my-storage", backsnap.BackupSettings{})
if !errors.IsNotFound(err) {
t.Fatalf("expected Not Found error, but got: %+v", err)
}
}

0 comments on commit d2463a9

Please sign in to comment.