Skip to content

Add a DriveOpt func for configuring CacheType #401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions drives.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,10 @@ func WithRateLimiter(limiter models.RateLimiter) DriveOpt {
d.RateLimiter = &limiter
}
}

// WithCacheType sets the cache strategy for the block device
func WithCacheType(cacheType string) DriveOpt {
return func(d *models.Drive) {
d.CacheType = String(cacheType)
}
}
41 changes: 38 additions & 3 deletions drives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestDrivesBuilder(t *testing.T) {

drives := NewDrivesBuilder(expectedPath).Build()
if e, a := expectedDrives, drives; !reflect.DeepEqual(e, a) {
t.Errorf("expected drives %v, but received %v", e, a)
t.Errorf("expected drives %+v, but received %+v", e, a)
}
}

Expand All @@ -51,7 +51,28 @@ func TestDrivesBuilderWithRootDrive(t *testing.T) {
drives := b.WithRootDrive(expectedPath, WithDriveID("foo")).Build()

if e, a := expectedDrives, drives; !reflect.DeepEqual(e, a) {
t.Errorf("expected drives %v, but received %v", e, a)
t.Errorf("expected drives %+v, but received %+v", e, a)
}
}

func TestDrivesBuilderWithCacheType(t *testing.T) {
expectedPath := "/path/to/rootfs"
expectedCacheType := models.DriveCacheTypeWriteback
expectedDrives := []models.Drive{
{
DriveID: String("root_drive"),
PathOnHost: &expectedPath,
IsRootDevice: Bool(true),
IsReadOnly: Bool(false),
CacheType: String(expectedCacheType),
},
}

b := NewDrivesBuilder(expectedPath)
drives := b.WithRootDrive(expectedPath, WithDriveID("root_drive"), WithCacheType("Writeback")).Build()

if e, a := expectedDrives, drives; !reflect.DeepEqual(e, a) {
t.Errorf("expected drives %+v, but received %+v", e, a)
}
}

Expand All @@ -77,6 +98,13 @@ func TestDrivesBuilderAddDrive(t *testing.T) {
drive.Partuuid = "uuid"
},
},
{
Path: "/5",
ReadOnly: true,
Opt: func(drive *models.Drive) {
drive.CacheType = String(models.DriveCacheTypeWriteback)
},
},
}
expectedDrives := []models.Drive{
{
Expand All @@ -98,6 +126,13 @@ func TestDrivesBuilderAddDrive(t *testing.T) {
IsReadOnly: Bool(false),
Partuuid: "uuid",
},
{
DriveID: String("3"),
PathOnHost: String("/5"),
IsRootDevice: Bool(false),
IsReadOnly: Bool(true),
CacheType: String(models.DriveCacheTypeWriteback),
},
{
DriveID: String(rootDriveName),
PathOnHost: &rootPath,
Expand All @@ -117,6 +152,6 @@ func TestDrivesBuilderAddDrive(t *testing.T) {

drives := b.Build()
if e, a := expectedDrives, drives; !reflect.DeepEqual(e, a) {
t.Errorf("expected drives %v, but received %v", e, a)
t.Errorf("expected drives %+v\n, but received %+v", e, a)
}
}