Skip to content

Commit 47505a9

Browse files
author
Aaron Goulet
authored
Merge pull request #401 from swagatbora90/add-cache-type-support
Add a DriveOpt func for configuring CacheType
2 parents 7fb232e + 96eca0b commit 47505a9

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

drives.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,10 @@ func WithRateLimiter(limiter models.RateLimiter) DriveOpt {
103103
d.RateLimiter = &limiter
104104
}
105105
}
106+
107+
// WithCacheType sets the cache strategy for the block device
108+
func WithCacheType(cacheType string) DriveOpt {
109+
return func(d *models.Drive) {
110+
d.CacheType = String(cacheType)
111+
}
112+
}

drives_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestDrivesBuilder(t *testing.T) {
3232

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

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

5353
if e, a := expectedDrives, drives; !reflect.DeepEqual(e, a) {
54-
t.Errorf("expected drives %v, but received %v", e, a)
54+
t.Errorf("expected drives %+v, but received %+v", e, a)
55+
}
56+
}
57+
58+
func TestDrivesBuilderWithCacheType(t *testing.T) {
59+
expectedPath := "/path/to/rootfs"
60+
expectedCacheType := models.DriveCacheTypeWriteback
61+
expectedDrives := []models.Drive{
62+
{
63+
DriveID: String("root_drive"),
64+
PathOnHost: &expectedPath,
65+
IsRootDevice: Bool(true),
66+
IsReadOnly: Bool(false),
67+
CacheType: String(expectedCacheType),
68+
},
69+
}
70+
71+
b := NewDrivesBuilder(expectedPath)
72+
drives := b.WithRootDrive(expectedPath, WithDriveID("root_drive"), WithCacheType("Writeback")).Build()
73+
74+
if e, a := expectedDrives, drives; !reflect.DeepEqual(e, a) {
75+
t.Errorf("expected drives %+v, but received %+v", e, a)
5576
}
5677
}
5778

@@ -77,6 +98,13 @@ func TestDrivesBuilderAddDrive(t *testing.T) {
7798
drive.Partuuid = "uuid"
7899
},
79100
},
101+
{
102+
Path: "/5",
103+
ReadOnly: true,
104+
Opt: func(drive *models.Drive) {
105+
drive.CacheType = String(models.DriveCacheTypeWriteback)
106+
},
107+
},
80108
}
81109
expectedDrives := []models.Drive{
82110
{
@@ -98,6 +126,13 @@ func TestDrivesBuilderAddDrive(t *testing.T) {
98126
IsReadOnly: Bool(false),
99127
Partuuid: "uuid",
100128
},
129+
{
130+
DriveID: String("3"),
131+
PathOnHost: String("/5"),
132+
IsRootDevice: Bool(false),
133+
IsReadOnly: Bool(true),
134+
CacheType: String(models.DriveCacheTypeWriteback),
135+
},
101136
{
102137
DriveID: String(rootDriveName),
103138
PathOnHost: &rootPath,
@@ -117,6 +152,6 @@ func TestDrivesBuilderAddDrive(t *testing.T) {
117152

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

0 commit comments

Comments
 (0)