@@ -25,15 +25,19 @@ package main
2525import (
2626 "context"
2727 "flag"
28+ "fmt"
29+ "net"
2830 "os"
31+ "os/exec"
2932 "strconv"
3033 "sync"
3134 "testing"
35+ "time"
3236
3337 ctrdlog "github.com/containerd/containerd/log"
3438 log "github.com/sirupsen/logrus"
3539 "github.com/stretchr/testify/require"
36- ctriface "github.com/vhive-serverless/vhive/ctriface"
40+ "github.com/vhive-serverless/vhive/ctriface"
3741)
3842
3943const (
@@ -77,29 +81,16 @@ func TestMain(m *testing.M) {
7781 log .Infof ("Drop cache: %t" , ! * isWithCache )
7882 log .Infof ("Bench dir: %s" , * benchDir )
7983
80- orch = ctriface .NewOrchestrator (
81- "devmapper" ,
82- "" ,
83- ctriface .WithTestModeOn (true ),
84- ctriface .WithSnapshots (* isSnapshotsEnabledTest ),
85- ctriface .WithUPF (* isUPFEnabledTest ),
86- ctriface .WithMetricsMode (* isMetricsModeTest ),
87- ctriface .WithLazyMode (* isLazyModeTest ),
88- )
89-
9084 ret := m .Run ()
91-
92- err := orch .StopActiveVMs ()
93- if err != nil {
94- log .Printf ("Failed to stop VMs, err: %v\n " , err )
95- }
96-
97- orch .Cleanup ()
98-
9985 os .Exit (ret )
10086}
10187
10288func TestSendToFunctionSerial (t * testing.T ) {
89+ if err := initFirecrackerContainerd (); err != nil {
90+ t .Fatalf ("Failed to initialize firecracker containerd: %v" , err )
91+ }
92+ defer cleanup ()
93+
10394 fID := "1"
10495 var (
10596 servedTh uint64
@@ -122,6 +113,11 @@ func TestSendToFunctionSerial(t *testing.T) {
122113}
123114
124115func TestSendToFunctionParallel (t * testing.T ) {
116+ if err := initFirecrackerContainerd (); err != nil {
117+ t .Fatalf ("Failed to initialize firecracker containerd: %v" , err )
118+ }
119+ defer cleanup ()
120+
125121 fID := "2"
126122 var (
127123 servedTh uint64
@@ -148,6 +144,11 @@ func TestSendToFunctionParallel(t *testing.T) {
148144}
149145
150146func TestStartSendStopTwice (t * testing.T ) {
147+ if err := initFirecrackerContainerd (); err != nil {
148+ t .Fatalf ("Failed to initialize firecracker containerd: %v" , err )
149+ }
150+ defer cleanup ()
151+
151152 fID := "3"
152153 var (
153154 servedTh uint64 = 1
@@ -173,6 +174,11 @@ func TestStartSendStopTwice(t *testing.T) {
173174}
174175
175176func TestStatsNotNumericFunction (t * testing.T ) {
177+ if err := initFirecrackerContainerd (); err != nil {
178+ t .Fatalf ("Failed to initialize firecracker containerd: %v" , err )
179+ }
180+ defer cleanup ()
181+
176182 fID := "not-cld"
177183 var (
178184 servedTh uint64 = 1
@@ -194,6 +200,11 @@ func TestStatsNotNumericFunction(t *testing.T) {
194200}
195201
196202func TestStatsNotColdFunction (t * testing.T ) {
203+ if err := initFirecrackerContainerd (); err != nil {
204+ t .Fatalf ("Failed to initialize firecracker containerd: %v" , err )
205+ }
206+ defer cleanup ()
207+
197208 fID := "4"
198209 var (
199210 servedTh uint64 = 1
@@ -215,6 +226,11 @@ func TestStatsNotColdFunction(t *testing.T) {
215226}
216227
217228func TestSaveMemorySerial (t * testing.T ) {
229+ if err := initFirecrackerContainerd (); err != nil {
230+ t .Fatalf ("Failed to initialize firecracker containerd: %v" , err )
231+ }
232+ defer cleanup ()
233+
218234 fID := "5"
219235 var (
220236 servedTh uint64 = 40
@@ -236,6 +252,11 @@ func TestSaveMemorySerial(t *testing.T) {
236252}
237253
238254func TestSaveMemoryParallel (t * testing.T ) {
255+ if err := initFirecrackerContainerd (); err != nil {
256+ t .Fatalf ("Failed to initialize firecracker containerd: %v" , err )
257+ }
258+ defer cleanup ()
259+
239260 fID := "6"
240261 var (
241262 servedTh uint64 = 40
@@ -266,6 +287,11 @@ func TestSaveMemoryParallel(t *testing.T) {
266287}
267288
268289func TestDirectStartStopVM (t * testing.T ) {
290+ if err := initFirecrackerContainerd (); err != nil {
291+ t .Fatalf ("Failed to initialize firecracker containerd: %v" , err )
292+ }
293+ defer cleanup ()
294+
269295 fID := "7"
270296 var (
271297 servedTh uint64
@@ -285,6 +311,10 @@ func TestDirectStartStopVM(t *testing.T) {
285311}
286312
287313func TestAllFunctions (t * testing.T ) {
314+ if err := initFirecrackerContainerd (); err != nil {
315+ t .Fatalf ("Failed to initialize firecracker containerd: %v" , err )
316+ }
317+ defer cleanup ()
288318
289319 if testing .Short () {
290320 t .Skip ("skipping TestAllFunctions in non-nightly runs." )
@@ -340,3 +370,80 @@ func TestAllFunctions(t *testing.T) {
340370 }
341371 vmGroup .Wait ()
342372}
373+
374+ func initFirecrackerContainerd () error {
375+ firecrackerBin := os .Getenv ("FIRECRACKER_BIN" )
376+ firecrackerConfigPath := os .Getenv ("FIRECRACKER_CONFIG_PATH" )
377+ logOut := os .Getenv ("LOG_OUT" )
378+ logErr := os .Getenv ("LOG_ERR" )
379+
380+ logOutFile , err := os .OpenFile (logOut , os .O_WRONLY | os .O_CREATE | os .O_APPEND , 0644 )
381+ if err != nil {
382+ return fmt .Errorf ("failed to open log file %s: %w" , logOut , err )
383+ }
384+ defer logOutFile .Close ()
385+
386+ logErrFile , err := os .OpenFile (logErr , os .O_WRONLY | os .O_CREATE | os .O_APPEND , 0644 )
387+ if err != nil {
388+ return fmt .Errorf ("failed to open error log file %s: %w" , logErr , err )
389+ }
390+ defer logErrFile .Close ()
391+
392+ cmd := exec .Command (firecrackerBin , "--config" , firecrackerConfigPath )
393+ cmd .Stdout = logOutFile
394+ cmd .Stderr = logErrFile
395+
396+ if err := cmd .Start (); err != nil {
397+ return fmt .Errorf ("failed to start firecracker containerd: %w" , err )
398+ }
399+
400+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
401+ defer cancel ()
402+ if err := waitForContainerd (ctx ); err != nil {
403+ return fmt .Errorf ("failed to connect to containerd: %w" , err )
404+ }
405+
406+ orch = ctriface .NewOrchestrator (
407+ "devmapper" ,
408+ "" ,
409+ ctriface .WithTestModeOn (true ),
410+ ctriface .WithSnapshots (* isSnapshotsEnabledTest ),
411+ ctriface .WithUPF (* isUPFEnabledTest ),
412+ ctriface .WithMetricsMode (* isMetricsModeTest ),
413+ ctriface .WithLazyMode (* isLazyModeTest ),
414+ )
415+
416+ return nil
417+ }
418+
419+ func waitForContainerd (ctx context.Context ) error {
420+ containerdAddress := "/run/firecracker-containerd/containerd.sock"
421+
422+ ticker := time .NewTicker (100 * time .Millisecond )
423+ defer ticker .Stop ()
424+
425+ for {
426+ select {
427+ case <- ctx .Done ():
428+ return ctx .Err ()
429+ case <- ticker .C :
430+ if conn , err := net .DialTimeout ("unix" , containerdAddress , 200 * time .Millisecond ); err == nil {
431+ conn .Close ()
432+ return nil
433+ }
434+ }
435+ }
436+ }
437+
438+ func cleanup () {
439+ if orch != nil {
440+ if err := orch .StopActiveVMs (); err != nil {
441+ log .Printf ("Failed to stop VMs, err: %v\n " , err )
442+ }
443+ orch .Cleanup ()
444+ }
445+ err := exec .Command ("bash" , "scripts/clean_fcctr.sh" ).Run ()
446+ if err != nil {
447+ log .Printf ("Failed to clean up firecracker containerd, err: %v\n " , err )
448+ }
449+ }
0 commit comments