Skip to content

Commit 0b5c159

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

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import (
2121
"strings"
2222
"sync"
2323
"testing"
24+
"fmt"
25+
26+
2427

2528
"github.com/containerd/containerd/log"
2629

@@ -102,6 +105,61 @@ func TestPatchStubDrive(t *testing.T) {
102105
}
103106
}
104107

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

0 commit comments

Comments
 (0)