@@ -869,6 +869,100 @@ func TestWaitForSocket(t *testing.T) {
869
869
}
870
870
}
871
871
872
+ func TestMicroVMExecutionWithMmdsV2 (t * testing.T ) {
873
+ fctesting .RequiresKVM (t )
874
+
875
+ var nCpus int64 = 2
876
+ cpuTemplate := models .CPUTemplate (models .CPUTemplateT2 )
877
+ var memSz int64 = 256
878
+
879
+ dir , err := ioutil .TempDir ("" , t .Name ())
880
+ require .NoError (t , err )
881
+ defer os .RemoveAll (dir )
882
+
883
+ socketPath := filepath .Join (dir , "TestMicroVMExecution.sock" )
884
+ logFifo := filepath .Join (dir , "firecracker.log" )
885
+ metricsFifo := filepath .Join (dir , "firecracker-metrics" )
886
+ capturedLog := filepath .Join (dir , "writer.fifo" )
887
+ fw , err := os .OpenFile (capturedLog , os .O_CREATE | os .O_RDWR , 0600 )
888
+ require .NoError (t , err , "failed to open fifo writer file" )
889
+ defer fw .Close ()
890
+
891
+ vmlinuxPath := getVmlinuxPath (t )
892
+
893
+ networkIfaces := []NetworkInterface {{
894
+ StaticConfiguration : & StaticNetworkConfiguration {
895
+ MacAddress : "01-23-45-67-89-AB-CD-EF" ,
896
+ HostDevName : "tap0" ,
897
+ },
898
+ }}
899
+
900
+ cfg := Config {
901
+ SocketPath : socketPath ,
902
+ LogFifo : logFifo ,
903
+ MetricsFifo : metricsFifo ,
904
+ LogLevel : "Debug" ,
905
+ MachineCfg : models.MachineConfiguration {
906
+ VcpuCount : Int64 (nCpus ),
907
+ CPUTemplate : cpuTemplate ,
908
+ MemSizeMib : Int64 (memSz ),
909
+ Smt : Bool (false ),
910
+ },
911
+ DisableValidation : true ,
912
+ NetworkInterfaces : networkIfaces ,
913
+ FifoLogWriter : fw ,
914
+ MmdsVersion : MMDSv2 ,
915
+ }
916
+
917
+ ctx := context .Background ()
918
+ cmd := VMCommandBuilder {}.
919
+ WithSocketPath (socketPath ).
920
+ WithBin (getFirecrackerBinaryPath ()).
921
+ Build (ctx )
922
+
923
+ m , err := NewMachine (ctx , cfg , WithProcessRunner (cmd ), WithLogger (fctesting .NewLogEntry (t )))
924
+ if err != nil {
925
+ t .Fatalf ("failed to create new machine: %v" , err )
926
+ }
927
+
928
+ m .Handlers .Validation = m .Handlers .Validation .Clear ()
929
+
930
+ vmmCtx , vmmCancel := context .WithTimeout (ctx , 30 * time .Second )
931
+ defer vmmCancel ()
932
+ exitchannel := make (chan error )
933
+ go func () {
934
+ err := m .startVMM (vmmCtx )
935
+ if err != nil {
936
+ exitchannel <- err
937
+ close (exitchannel )
938
+ return
939
+ }
940
+ defer m .StopVMM ()
941
+
942
+ exitchannel <- m .Wait (vmmCtx )
943
+ close (exitchannel )
944
+ }()
945
+
946
+ deadlineCtx , deadlineCancel := context .WithTimeout (vmmCtx , 250 * time .Millisecond )
947
+ defer deadlineCancel ()
948
+ if err := waitForAliveVMM (deadlineCtx , m .client ); err != nil {
949
+ t .Fatal (err )
950
+ }
951
+
952
+ t .Run ("TestCreateMachine" , func (t * testing.T ) { testCreateMachine (ctx , t , m ) })
953
+ t .Run ("TestCreateBootSource" , func (t * testing.T ) { testCreateBootSource (ctx , t , m , vmlinuxPath ) })
954
+ t .Run ("TestCreateNetworkInterface" , func (t * testing.T ) { testCreateNetworkInterfaceByID (ctx , t , m ) })
955
+ t .Run ("TestAttachRootDrive" , func (t * testing.T ) { testAttachRootDrive (ctx , t , m ) })
956
+ t .Run ("SetMetadata" , func (t * testing.T ) { testSetMetadata (ctx , t , m ) })
957
+ t .Run ("UpdateMetadata" , func (t * testing.T ) { testUpdateMetadata (ctx , t , m ) })
958
+ t .Run ("GetMetadata" , func (t * testing.T ) { testGetMetadata (ctx , t , m ) }) // Should be after testSetMetadata and testUpdateMetadata
959
+
960
+ // unconditionally stop the VM here. TestShutdown may have triggered a shutdown, but if it
961
+ // didn't for some reason, we still need to terminate it:
962
+ err = m .StopVMM ()
963
+ assert .NoError (t , err , "failed to stop VM" )
964
+ }
965
+
872
966
func testSetMetadata (ctx context.Context , t * testing.T , m * Machine ) {
873
967
metadata := map [string ]string {"key" : "value" }
874
968
err := m .SetMetadata (ctx , metadata )
@@ -1952,20 +2046,22 @@ func TestLoadSnapshot(t *testing.T) {
1952
2046
},
1953
2047
}
1954
2048
2049
+ cfg .Snapshot .ResumeVM = false
2050
+
1955
2051
m , err := NewMachine (ctx , cfg , func (m * Machine ) {
1956
2052
// Rewriting m.cmd partially wouldn't work since Cmd has
1957
2053
// some unexported members
1958
2054
args := m .cmd .Args [1 :]
1959
2055
m .cmd = exec .Command (getFirecrackerBinaryPath (), args ... )
1960
- }, WithLogger (logrus .NewEntry (machineLogger )), WithSnapshot (memPath , snapPath ))
2056
+ }, WithLogger (logrus .NewEntry (machineLogger )), WithSnapshot (memPath , snapPath , func (config * SnapshotConfig ) {
2057
+ config .ResumeVM = true
2058
+ }))
1961
2059
require .NoError (t , err )
2060
+ require .Equal (t , m .Cfg .Snapshot .ResumeVM , true )
1962
2061
1963
2062
err = m .Start (ctx )
1964
2063
require .NoError (t , err )
1965
2064
1966
- err = m .ResumeVM (ctx )
1967
- require .NoError (t , err )
1968
-
1969
2065
err = m .StopVMM ()
1970
2066
require .NoError (t , err )
1971
2067
},
0 commit comments