rbd: add validation to ToCSI() for rbdVolume and rbdSnapshot

After an unfortnate timed restart of the provisioner, a volume that got
cloned did not get a `rbdVolume.VolID` set. The `.VolID` is used as the
CSI Volume Handle, and is a required attribute.

The `rbdVolume` and `rbdSnapshot` structs have a `.ToCSI()` function
that can do the validation of required attributes. This is now added,
including unit-tests.

Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
Niels de Vos
2025-02-17 16:01:42 +01:00
committed by mergify[bot]
parent b3faa04504
commit ac8cda5e37
4 changed files with 186 additions and 1 deletions

View File

@ -16,7 +16,10 @@ limitations under the License.
package rbd
import "testing"
import (
"context"
"testing"
)
func TestValidateStriping(t *testing.T) {
t.Parallel()
@ -85,3 +88,81 @@ func TestValidateStriping(t *testing.T) {
})
}
}
func TestToCSIVolume(t *testing.T) {
t.Parallel()
tests := []struct {
name string
rv *rbdVolume
wantErr bool
}{
{
name: "all attributes set",
rv: &rbdVolume{
rbdImage: rbdImage{
VolID: "0001-unique-volume-id",
Pool: "ecpool",
JournalPool: "replicapool",
RbdImageName: "csi-vol-01234-5678-90abc",
},
},
wantErr: false,
},
{
name: "missing volume-id",
rv: &rbdVolume{
rbdImage: rbdImage{
VolID: "",
Pool: "ecpool",
JournalPool: "replicapool",
RbdImageName: "csi-vol-01234-5678-90abc",
},
},
wantErr: true,
},
{
name: "missing pool",
rv: &rbdVolume{
rbdImage: rbdImage{
VolID: "0001-unique-volume-id",
Pool: "",
JournalPool: "replicapool",
RbdImageName: "csi-vol-01234-5678-90abc",
},
},
wantErr: true,
},
{
name: "missing journal-pool",
rv: &rbdVolume{
rbdImage: rbdImage{
VolID: "0001-unique-volume-id",
Pool: "ecpool",
JournalPool: "",
RbdImageName: "csi-vol-01234-5678-90abc",
},
},
wantErr: true,
},
{
name: "missing image-name",
rv: &rbdVolume{
rbdImage: rbdImage{
VolID: "0001-unique-volume-id",
Pool: "ecpool",
JournalPool: "",
RbdImageName: "",
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if _, err := tt.rv.ToCSI(context.TODO()); (err != nil) != tt.wantErr {
t.Errorf("ToCSI() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}