Skip to content

Commit 597a18d

Browse files
committed
Don't patch non-stub drives
Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent 0b05161 commit 597a18d

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

runtime/drive_handler.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,20 @@ func (h *stubDriveHandler) createStubDrive(driveID, path string) error {
125125
return nil
126126
}
127127

128+
func filterOnlyStubDrives(drives []models.Drive) []models.Drive {
129+
stubDrives := make([]models.Drive, 0)
130+
for _, d := range drives {
131+
if !*d.IsRootDevice {
132+
stubDrives = append(stubDrives, d)
133+
}
134+
}
135+
return stubDrives
136+
}
137+
128138
// SetDrives will set the given drives and the offset to which the stub drives
129139
// start.
130140
func (h *stubDriveHandler) SetDrives(offset int64, d []models.Drive) {
131-
h.drives = d
141+
h.drives = filterOnlyStubDrives(d)
132142
h.stubDriveIndex = offset
133143
}
134144

runtime/drive_handler_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package main
1515

1616
import (
1717
"context"
18+
"fmt"
1819
"io/ioutil"
1920
"os"
2021
"path/filepath"
@@ -102,6 +103,61 @@ func TestPatchStubDrive(t *testing.T) {
102103
}
103104
}
104105

106+
func TestPatchStubDrive_single(t *testing.T) {
107+
ctx := context.Background()
108+
index := 0
109+
expectedReplacements := []string{
110+
"/correct/path0",
111+
"/correct/path1",
112+
}
113+
114+
mockClient := &fctesting.MockClient{
115+
PatchGuestDriveByIDFn: func(params *ops.PatchGuestDriveByIDParams) (*ops.PatchGuestDriveByIDNoContent, error) {
116+
assert.Equal(t, expectedReplacements[index], firecracker.StringValue(params.Body.PathOnHost))
117+
index++
118+
return nil, nil
119+
},
120+
}
121+
122+
logger := log.G(ctx)
123+
124+
fcClient := firecracker.NewClient("/path/to/socket", nil, false, firecracker.WithOpsClient(mockClient))
125+
client, err := firecracker.NewMachine(ctx, firecracker.Config{}, firecracker.WithClient(fcClient))
126+
assert.NoError(t, err, "failed to create new machine")
127+
128+
tempDir := os.TempDir()
129+
path, err := ioutil.TempDir(tempDir, t.Name())
130+
assert.NoError(t, err, "failed to create test directory")
131+
defer os.RemoveAll(path)
132+
133+
handler := newStubDriveHandler(path, logger)
134+
135+
driveBuilder := firecracker.NewDrivesBuilder("fc/root")
136+
137+
stubDriveIndex := int64(len(driveBuilder.Build()) - 1)
138+
containerCount := 1
139+
140+
paths, err := handler.StubDrivePaths(containerCount)
141+
assert.NoError(t, err)
142+
143+
for i, path := range paths {
144+
driveID := fmt.Sprintf("stub%d", i)
145+
driveBuilder = driveBuilder.AddDrive(path, false, func(drive *models.Drive) {
146+
drive.DriveID = firecracker.String(driveID)
147+
})
148+
}
149+
150+
handler.SetDrives(stubDriveIndex, driveBuilder.Build())
151+
152+
drive1, err := handler.PatchStubDrive(ctx, client, "/correct/path0")
153+
assert.NoError(t, err)
154+
assert.Equal(t, "stub0", firecracker.StringValue(drive1))
155+
156+
drive2, err := handler.PatchStubDrive(ctx, client, "/correct/path1")
157+
assert.NotEqual(t, "root_drive", firecracker.StringValue(drive2))
158+
assert.Error(t, err)
159+
}
160+
105161
func TestPatchStubDrive_concurrency(t *testing.T) {
106162
ctx := context.Background()
107163
mockClient := &fctesting.MockClient{

0 commit comments

Comments
 (0)