@@ -21,6 +21,9 @@ import (
21
21
"strings"
22
22
"sync"
23
23
"testing"
24
+ "fmt"
25
+
26
+
24
27
25
28
"github.com/containerd/containerd/log"
26
29
@@ -102,6 +105,61 @@ func TestPatchStubDrive(t *testing.T) {
102
105
}
103
106
}
104
107
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
+
105
163
func TestPatchStubDrive_concurrency (t * testing.T ) {
106
164
ctx := context .Background ()
107
165
mockClient := & fctesting.MockClient {
0 commit comments