Skip to content

Commit 76f3d57

Browse files
committed
[tests] Test 2-stage save/restore function
1 parent a042bf5 commit 76f3d57

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

tests/apple/test_apple_virtual_machine.cpp

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,19 @@ TEST_F(AppleVirtualMachine_UnitTests, startFromStoppedStateCallsStartVm)
8686

8787
TEST_F(AppleVirtualMachine_UnitTests, startFromPausedStateCallsResumeVm)
8888
{
89+
const auto suspend_file =
90+
std::filesystem::path(instance_dir.filePath("suspend.vm_state").toStdString());
91+
std::ofstream{suspend_file}.close();
92+
8993
mp::apple::AppleVirtualMachine vm{desc, mock_monitor, stub_key_provider, instance_dir.path()};
9094

9195
EXPECT_CALL(mock_apple_vz, get_state(_))
92-
.WillOnce(Return(apple::AppleVMState::paused))
96+
.WillOnce(Return(apple::AppleVMState::stopped))
9397
.WillOnce(Return(apple::AppleVMState::running));
98+
EXPECT_CALL(mock_apple_vz, restore_vm_from_file(_, _)).WillOnce(Invoke([](auto&, auto&) {
99+
return apple::CFError{};
100+
}));
101+
94102
EXPECT_CALL(mock_apple_vz, can_resume(_)).WillOnce(Return(true));
95103
EXPECT_CALL(mock_apple_vz, resume_vm(_)).WillOnce(Invoke([](auto&) {
96104
return apple::CFError{};
@@ -137,14 +145,23 @@ TEST_F(AppleVirtualMachine_UnitTests, startVmErrorThrowsRuntimeError)
137145

138146
TEST_F(AppleVirtualMachine_UnitTests, startResumeErrorThrowsRuntimeError)
139147
{
148+
const auto suspend_file =
149+
std::filesystem::path(instance_dir.filePath("suspend.vm_state").toStdString());
150+
std::ofstream{suspend_file}.close();
151+
140152
mp::apple::AppleVirtualMachine vm{desc, mock_monitor, stub_key_provider, instance_dir.path()};
141153

142154
CFErrorRef error_ref = CFErrorCreate(kCFAllocatorDefault, CFSTR("TestDomain"), 42, nullptr);
143155
apple::CFError error{error_ref};
144156

145157
EXPECT_CALL(mock_apple_vz, get_state(_))
146-
.WillOnce(Return(apple::AppleVMState::paused))
158+
.WillOnce(Return(apple::AppleVMState::stopped))
147159
.WillOnce(Return(apple::AppleVMState::error));
160+
161+
EXPECT_CALL(mock_apple_vz, restore_vm_from_file(_, _)).WillOnce(Invoke([](auto&, auto&) {
162+
return apple::CFError{};
163+
}));
164+
148165
EXPECT_CALL(mock_apple_vz, can_resume(_)).WillOnce(Return(true));
149166
EXPECT_CALL(mock_apple_vz, resume_vm(_)).WillOnce(Return(ByMove(std::move(error))));
150167
EXPECT_CALL(mock_monitor, persist_state_for(_, _)).Times(AtLeast(1));
@@ -153,6 +170,32 @@ TEST_F(AppleVirtualMachine_UnitTests, startResumeErrorThrowsRuntimeError)
153170
EXPECT_EQ(vm.current_state(), VirtualMachine::State::unknown);
154171
}
155172

173+
TEST_F(AppleVirtualMachine_UnitTests, startRestoreErrorThrowsRuntimeError)
174+
{
175+
const auto suspend_file =
176+
std::filesystem::path(instance_dir.filePath("suspend.vm_state").toStdString());
177+
std::ofstream{suspend_file}.close();
178+
179+
mp::apple::AppleVirtualMachine vm{desc, mock_monitor, stub_key_provider, instance_dir.path()};
180+
181+
CFErrorRef error_ref = CFErrorCreate(kCFAllocatorDefault, CFSTR("TestDomain"), 42, nullptr);
182+
apple::CFError error{error_ref};
183+
184+
EXPECT_CALL(mock_apple_vz, get_state(_))
185+
.WillOnce(Return(apple::AppleVMState::stopped))
186+
.WillOnce(Return(apple::AppleVMState::error));
187+
188+
EXPECT_CALL(mock_apple_vz, restore_vm_from_file(_, _))
189+
.WillOnce(Return(ByMove(std::move(error))));
190+
191+
EXPECT_CALL(mock_apple_vz, can_resume(_)).Times(0);
192+
EXPECT_CALL(mock_apple_vz, resume_vm(_)).Times(0);
193+
EXPECT_CALL(mock_monitor, persist_state_for(_, _)).Times(AtLeast(1));
194+
195+
EXPECT_THROW(vm.start(), std::runtime_error);
196+
EXPECT_EQ(vm.current_state(), VirtualMachine::State::unknown);
197+
}
198+
156199
TEST_F(AppleVirtualMachine_UnitTests, shutdownFromRunningStateWithPowerdownCallsStopVm)
157200
{
158201
EXPECT_CALL(mock_apple_vz, get_state(_))

0 commit comments

Comments
 (0)