Skip to content

Commit 1bc3d2a

Browse files
committed
[hyperv-hcs] Fix tests after changes
1 parent cc7569a commit 1bc3d2a

File tree

11 files changed

+127
-118
lines changed

11 files changed

+127
-118
lines changed

src/platform/backends/hyperv_api/hcn/hyperv_hcn_wrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ OperationResult perform_hcn_operation(const FnType& fn)
166166
// it occurred. Therefore, we can streamline all API calls through perform_hcn_operation.
167167
const auto result = ResultCode{fn(out_ptr(result_msgbuf))};
168168

169-
mpl::trace(log_category, "perform_hcn_operation(...) > result: {}", static_cast<bool>(result));
169+
mpl::trace(log_category, "perform_hcn_operation(...) > result: {}", result.success());
170170

171171
// Avoid null to be forward-compatible with C++23
172172
return {result, {result_msgbuf ? result_msgbuf.get() : L""}};

src/platform/backends/hyperv_api/hcs/hyperv_hcs_wrapper.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ OperationResult perform_hcs_operation(const FnType& fn, const HcsSystemHandle& s
146146
// Perform the operation.
147147
const auto result = ResultCode{fn(operation.get())};
148148

149-
if (!result)
149+
if (!result.success())
150150
{
151151
mpl::error(log_category,
152152
"perform_hcs_operation(...) > Operation failed! Result code {}",
153153
result);
154154
return OperationResult{result, L"HCS operation failed!"};
155155
}
156156

157-
mpl::debug(log_category, "perform_hcs_operation(...) > result: {}", static_cast<bool>(result));
157+
mpl::debug(log_category, "perform_hcs_operation(...) > result: {}", result.success());
158158

159159
return wait_for_operation_result(std::move(operation));
160160
}
@@ -182,7 +182,7 @@ OperationResult HCSWrapper::open_compute_system(const std::string& name,
182182
UniqueHcsSystem system{};
183183
const ResultCode result =
184184
API().HcsOpenComputeSystem(name_w.c_str(), requested_access_level, out_ptr(system));
185-
if (!result)
185+
if (!result.success())
186186
{
187187
mpl::debug(log_category,
188188
"open_compute_system(...) > failed to open ({}), result code: ({})",
@@ -238,7 +238,7 @@ OperationResult HCSWrapper::create_compute_system(const CreateComputeSystemParam
238238
nullptr,
239239
out_ptr(system))};
240240

241-
if (!result)
241+
if (!result.success())
242242
{
243243
return OperationResult{result, L"HcsCreateComputeSystem failed."};
244244
}
@@ -567,7 +567,7 @@ OperationResult HCSWrapper::create_empty_guest_state_file(
567567
{
568568
const std::wstring path_w = vmgs_file_path.generic_wstring();
569569
const auto result = ResultCode{API().HcsCreateEmptyGuestStateFile(path_w.c_str())};
570-
if (result)
570+
if (result.success())
571571
{
572572
return grant_vm_access(compute_system_name, vmgs_file_path);
573573
}
@@ -583,7 +583,7 @@ OperationResult HCSWrapper::create_empty_runtime_state_file(
583583
{
584584
const std::wstring path_w = vmrs_file_path.generic_wstring();
585585
const auto result = ResultCode{API().HcsCreateEmptyRuntimeStateFile(path_w.c_str())};
586-
if (result)
586+
if (result.success())
587587
{
588588
return grant_vm_access(compute_system_name, vmrs_file_path);
589589
}

src/platform/backends/hyperv_api/hcs_virtual_machine.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ void HCSVirtualMachine::start()
493493
handle_state_update();
494494
// Resume and start are the same thing in Multipass terms
495495
// Try to determine whether we need to resume or start here.
496-
const auto& [status, status_msg] = [&] {
496+
const auto result = [&] {
497497
// Fetch the latest state value.
498498
const auto hcs_state = fetch_state_from_api();
499499
switch (hcs_state)
@@ -516,11 +516,11 @@ void HCSVirtualMachine::start()
516516
}
517517
}();
518518

519-
if (!status)
519+
if (!result)
520520
{
521521
state = prev_state;
522522
handle_state_update();
523-
throw StartComputeSystemException{"Could not start the VM: {}", status};
523+
throw StartComputeSystemException{"Could not start the VM: {}", result};
524524
}
525525
else if (has_saved_state_file())
526526
{
@@ -534,7 +534,7 @@ void HCSVirtualMachine::start()
534534
}
535535
}
536536

537-
mpl::debug(get_name(), "start() -> result `{}`", get_name(), status);
537+
mpl::debug(get_name(), "start() -> result `{}`", get_name(), result);
538538
}
539539
void HCSVirtualMachine::shutdown(ShutdownPolicy shutdown_policy)
540540
{
@@ -594,7 +594,7 @@ void HCSVirtualMachine::shutdown(ShutdownPolicy shutdown_policy)
594594
void HCSVirtualMachine::suspend()
595595
{
596596
mpl::debug(get_name(), "suspend() -> Suspending VM `{}`, current state {}", get_name(), state);
597-
if (const auto& [status, status_msg] = HCS().pause_compute_system(hcs_system); status)
597+
if (const auto pause_result = HCS().pause_compute_system(hcs_system))
598598
{
599599
// Pause succeeded. We can suspend to disk now
600600
if (const auto& r = HCS().save_compute_system(hcs_system, get_saved_state_file_path()); r)
@@ -614,7 +614,7 @@ void HCSVirtualMachine::suspend()
614614
}
615615
else
616616
{
617-
throw SaveComputeSystemException{"Could not pause VM for suspend: {}", status};
617+
throw SaveComputeSystemException{"Could not pause VM for suspend: {}", pause_result};
618618
}
619619
set_state(fetch_state_from_api());
620620
handle_state_update();

src/platform/backends/hyperv_api/hcs_virtual_machine_factory.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ void HCSVirtualMachineFactory::remove_resources_for_impl(const std::string& name
104104
}
105105
// Everything for the VM is neatly packed into the VM folder, so it's enough to ensure that
106106
// the VM is stopped. The base class will take care of the nuking the VM folder.
107-
const auto& [status, status_msg] = HCS().terminate_compute_system(handle);
108-
if (status)
107+
const auto terminate_result = HCS().terminate_compute_system(handle);
108+
if (terminate_result)
109109
{
110110
mpl::warn(log_category,
111111
"remove_resources_for_impl() -> Host compute system {} was still alive.",
@@ -189,14 +189,14 @@ void HCSVirtualMachineFactory::prepare_instance_image(const VMImage& instance_im
189189
const VirtualMachineDescription& desc)
190190
{
191191
// Resize the instance image to the desired size
192-
const auto& [status, status_msg] =
192+
const auto resize_result =
193193
VirtDisk().resize_virtual_disk(instance_image.image_path.toStdString(),
194194
desc.disk_space.in_bytes());
195-
if (!status)
195+
if (!resize_result)
196196
{
197197
throw ImageResizeException{"Failed to resize VHDX file `{}`, virtdisk API error code `{}`",
198198
instance_image.image_path.toStdString(),
199-
status};
199+
resize_result};
200200
}
201201
}
202202

@@ -214,14 +214,17 @@ std::string HCSVirtualMachineFactory::create_bridge_with(const NetworkInterfaceI
214214
return network_params;
215215
}();
216216

217-
const auto& [status, status_msg] = HCN().create_network(params);
217+
const auto create_network_result = HCN().create_network(params);
218218

219-
if (status || static_cast<HRESULT>(status) == HCN_E_NETWORK_ALREADY_EXISTS)
219+
if (create_network_result ||
220+
static_cast<HRESULT>(create_network_result.code) == HCN_E_NETWORK_ALREADY_EXISTS)
220221
{
221222
return params.name;
222223
}
223224

224-
throw CreateBridgeException{"Could not create vSwitch `{}`, status: {}", params.name, status};
225+
throw CreateBridgeException{"Could not create vSwitch `{}`, status: {}",
226+
params.name,
227+
create_network_result};
225228
}
226229

227230
VirtualMachine::UPtr HCSVirtualMachineFactory::clone_vm_impl(const std::string& source_vm_name,
@@ -255,9 +258,9 @@ VirtualMachine::UPtr HCSVirtualMachineFactory::clone_vm_impl(const std::string&
255258
.path = desc.image.image_path.toStdString(),
256259
.predecessor = virtdisk::SourcePathParameters{src_vm_vhdx}};
257260

258-
const auto& [status, msg] = VirtDisk().create_virtual_disk(clone_vhdx_params);
261+
const auto create_vd_result = VirtDisk().create_virtual_disk(clone_vhdx_params);
259262

260-
if (!status)
263+
if (!create_vd_result)
261264
{
262265
throw std::runtime_error{"VHDX clone failed."};
263266
}
@@ -330,7 +333,7 @@ std::vector<NetworkInterfaceInfo> HCSVirtualMachineFactory::networks() const
330333
update_adapter_authorizations(adapters, switches);
331334

332335
if (adapters.size() > switches.size())
333-
std::swap(adapters, switches); // we want to move the smallest one
336+
std::swap(adapters, switches); // we want to move the smallest one
334337

335338
switches.reserve(adapters.size() + switches.size()); // avoid growing more times than needed
336339
std::move(adapters.begin(), adapters.end(), std::back_inserter(switches));

src/platform/backends/hyperv_api/hyperv_api_operation_result.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,19 @@ struct ResultCode
3838
ResultCode(HRESULT r) noexcept : result(r)
3939
{
4040
}
41+
4142
ResultCode& operator=(HRESULT r) noexcept
4243
{
4344
result = r;
4445
return *this;
4546
}
4647

47-
[[nodiscard]] operator HRESULT() const noexcept
48+
bool success() const noexcept
49+
{
50+
return static_cast<HRESULT>(*this) == S_OK;
51+
}
52+
53+
[[nodiscard]] explicit operator HRESULT() const noexcept
4854
{
4955
return result;
5056
}
@@ -54,7 +60,7 @@ struct ResultCode
5460
return static_cast<unsigned_hresult_t>(result);
5561
}
5662

57-
[[nodiscard]] operator std::error_code() const noexcept
63+
[[nodiscard]] explicit operator std::error_code() const noexcept
5864
{
5965
return std::error_code{result, std::system_category()};
6066
}
@@ -86,12 +92,12 @@ struct OperationResult
8692

8793
[[nodiscard]] explicit operator bool() const noexcept
8894
{
89-
return code == S_OK;
95+
return static_cast<HRESULT>(code) == S_OK;
9096
}
9197

9298
[[nodiscard]] operator std::error_code() const noexcept
9399
{
94-
return code;
100+
return static_cast<std::error_code>(code);
95101
}
96102
};
97103
} // namespace multipass::hyperv

src/platform/backends/hyperv_api/virtdisk/virtdisk_wrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ UniqueHandle open_virtual_disk(
126126
// [out] PHANDLE Handle
127127
out_ptr(handle));
128128

129-
if (!result)
129+
if (!result.success())
130130
{
131131
mpl::error(log_category,
132132
"open_virtual_disk(...) > OpenVirtualDisk failed with: {}",

tests/unit/hyperv_api/test_bb_cit_hyperv.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,32 +92,32 @@ TEST_F(HyperV_ComponentIntegrationTests, spawn_empty_test_vm)
9292
// Create the test network
9393
{
9494
const auto& [status, status_msg] = HCN().create_network(network_parameters);
95-
ASSERT_TRUE(status);
95+
ASSERT_TRUE(status.success());
9696
}
9797

9898
// Create the test endpoint
9999
{
100100
const auto& [status, status_msg] = HCN().create_endpoint(endpoint_parameters);
101-
ASSERT_TRUE(status);
101+
ASSERT_TRUE(status.success());
102102
}
103103

104104
// Create the test VHDX (empty)
105105
{
106106
const auto& [status, status_msg] = VirtDisk().create_virtual_disk(create_disk_parameters);
107-
ASSERT_TRUE(status);
107+
ASSERT_TRUE(status.success());
108108
}
109109

110110
// Create test VM
111111
{
112112
const auto& [status, status_msg] =
113113
HCS().create_compute_system(create_vm_parameters, handle);
114-
ASSERT_TRUE(status);
114+
ASSERT_TRUE(status.success());
115115
}
116116

117117
// Start test VM
118118
{
119119
const auto& [status, status_msg] = HCS().start_compute_system(handle);
120-
ASSERT_TRUE(status);
120+
ASSERT_TRUE(status.success());
121121
}
122122

123123
(void)HCS().terminate_compute_system(handle);
@@ -197,37 +197,37 @@ TEST_F(HyperV_ComponentIntegrationTests, spawn_empty_test_vm_attach_nic_after_bo
197197
// Create the test network
198198
{
199199
const auto& [status, status_msg] = HCN().create_network(network_parameters);
200-
ASSERT_TRUE(status);
200+
ASSERT_TRUE(status.success());
201201
ASSERT_TRUE(status_msg.empty());
202202
}
203203

204204
// Create the test endpoint
205205
{
206206
const auto& [status, status_msg] = HCN().create_endpoint(endpoint_parameters);
207-
ASSERT_TRUE(status);
207+
ASSERT_TRUE(status.success());
208208
ASSERT_TRUE(status_msg.empty());
209209
}
210210

211211
// Create the test VHDX (empty)
212212
{
213213
const auto& [status, status_msg] = VirtDisk().create_virtual_disk(create_disk_parameters);
214-
ASSERT_TRUE(status);
214+
ASSERT_TRUE(status.success());
215215
ASSERT_TRUE(status_msg.empty());
216216
}
217217

218218
// Create test VM
219219
{
220220
const auto& [status, status_msg] =
221221
HCS().create_compute_system(create_vm_parameters, handle);
222-
ASSERT_TRUE(status);
222+
ASSERT_TRUE(status.success());
223223
ASSERT_TRUE(status_msg.empty());
224224
}
225225

226226
std::string vm_guid{};
227227
// Start test VM
228228
{
229229
const auto& [status, status_msg] = HCS().start_compute_system(handle);
230-
ASSERT_TRUE(status);
230+
ASSERT_TRUE(status.success());
231231
ASSERT_TRUE(status_msg.empty());
232232
ASSERT_TRUE(HCS().get_compute_system_guid(handle, vm_guid));
233233
ASSERT_FALSE(vm_guid.empty());
@@ -241,7 +241,7 @@ TEST_F(HyperV_ComponentIntegrationTests, spawn_empty_test_vm_attach_nic_after_bo
241241
network_adapter};
242242
const auto& [status, status_msg] =
243243
HCS().modify_compute_system(handle, add_network_adapter_req);
244-
ASSERT_TRUE(status);
244+
ASSERT_TRUE(status.success());
245245
ASSERT_TRUE(status_msg.empty());
246246
}
247247

@@ -255,13 +255,13 @@ TEST_F(HyperV_ComponentIntegrationTests, spawn_empty_test_vm_attach_nic_after_bo
255255
.endpoint_guid = "aee79cf9-54d1-4653-81fb-8110db97029b",
256256
});
257257

258-
ASSERT_TRUE(status);
258+
ASSERT_TRUE(status.success());
259259
ASSERT_TRUE(status_msg.empty());
260260
}
261261

262262
std::vector<std::string> eps{};
263263
const auto& [status, status_msg] = HCN().enumerate_attached_endpoints(vm_guid, eps);
264-
ASSERT_TRUE(status);
264+
ASSERT_TRUE(status.success());
265265
ASSERT_TRUE(status_msg.empty());
266266
ASSERT_EQ(eps.size(), 1);
267267
ASSERT_EQ(eps[0], network_adapter.endpoint_guid);

0 commit comments

Comments
 (0)