From af6c565da7712a92266384c35ce756fafd5512d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Pe=C3=B1aranda?= Date: Wed, 13 Sep 2023 08:58:43 -0300 Subject: [PATCH] [tests] Check set local.instance.bridged. --- tests/test_instance_settings_handler.cpp | 76 +++++++++++++++++++++--- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/tests/test_instance_settings_handler.cpp b/tests/test_instance_settings_handler.cpp index d5954bbe397..bdf33c7c3ba 100644 --- a/tests/test_instance_settings_handler.cpp +++ b/tests/test_instance_settings_handler.cpp @@ -82,7 +82,9 @@ struct TestInstanceSettingsHandler : public Test std::unordered_map deleted_vms; std::unordered_set preparing_vms; bool fake_persister_called = false; - inline static constexpr auto properties = std::array{"cpus", "disk", "memory"}; + inline static constexpr auto numeric_properties = std::array{"cpus", "disk", "memory"}; + inline static constexpr auto boolean_properties = std::array{"bridged"}; + inline static constexpr auto properties = std::array{"cpus", "disk", "memory", "bridged"}; }; QString make_key(const QString& instance_name, const QString& property) @@ -193,6 +195,26 @@ TEST_F(TestInstanceSettingsHandler, getReturnsMemorySizesInHumanReadableFormat) EXPECT_EQ(handler.get(make_key(target_instance_name, "memory")), "337.6KiB"); } +struct TestBridgedInstanceSettings : public TestInstanceSettingsHandler, public WithParamInterface +{ +}; + +TEST_P(TestBridgedInstanceSettings, getFetchesBridged) +{ + const auto bridged = GetParam(); + + constexpr auto target_instance_name = "lemmy"; + specs.insert({{"mikkey", {}}, {"phil", {}}, {target_instance_name, {}}}); + + // An extra interface in auto mode will result in bridging. + specs[target_instance_name].extra_interfaces = {{"id", "52:54:00:12:34:56", bridged}}; + + auto got = make_handler().get(make_key(target_instance_name, "bridged")); + EXPECT_EQ(got, bridged ? "true" : "false"); +} + +INSTANTIATE_TEST_SUITE_P(getFetchesBridged, TestBridgedInstanceSettings, Values(true, false)); + TEST_F(TestInstanceSettingsHandler, getFetchesPropertiesOfInstanceInSpecialState) { constexpr auto preparing_instance = "nouvelle", deleted_instance = "vague"; @@ -424,6 +446,19 @@ TEST_F(TestInstanceSettingsHandler, setRefusesWrongProperty) EXPECT_EQ(original_specs, specs[target_instance_name]); } +TEST_F(TestInstanceSettingsHandler, setRefusesToUnbridge) +{ + constexpr auto target_instance_name = "hendrix"; + specs.insert({{"voodoo", {}}, {"chile", {}}, {target_instance_name, {}}}); + specs[target_instance_name].extra_interfaces = {{"id", "52:54:00:78:90:12", true}}; + + mock_vm(target_instance_name); // TODO: make this an expectation. + + MP_EXPECT_THROW_THAT(make_handler().set(make_key(target_instance_name, "bridged"), "false"), + mp::InvalidSettingException, + mpt::match_what(HasSubstr("Bridged interface cannot be removed"))); +} + using VMSt = mp::VirtualMachine::State; using Property = const char*; using PropertyAndState = std::tuple; // no subliminal political msg intended :) @@ -476,7 +511,8 @@ TEST_P(TestInstanceModOnStoppedInstance, setWorksOnOtherStates) } INSTANTIATE_TEST_SUITE_P(TestInstanceSettingsHandler, TestInstanceModOnStoppedInstance, - Combine(ValuesIn(TestInstanceSettingsHandler::properties), Values(VMSt::off, VMSt::stopped))); + Combine(ValuesIn(TestInstanceSettingsHandler::numeric_properties), + Values(VMSt::off, VMSt::stopped))); struct TestInstanceModPersists : public TestInstanceSettingsHandler, public WithParamInterface { @@ -496,7 +532,7 @@ TEST_P(TestInstanceModPersists, setPersistsInstances) } INSTANTIATE_TEST_SUITE_P(TestInstanceSettingsHandler, TestInstanceModPersists, - ValuesIn(TestInstanceSettingsHandler::properties)); + ValuesIn(TestInstanceSettingsHandler::numeric_properties)); TEST_F(TestInstanceSettingsHandler, setRefusesToModifyInstancesInSpecialState) { @@ -554,12 +590,12 @@ TEST_F(TestInstanceSettingsHandler, getAndSetThrowOnBadKey) using PlainValue = const char*; using PropertyValue = std::tuple; -struct TestInstanceSettingsHandlerBadValues : public TestInstanceSettingsHandler, - public WithParamInterface +struct TestInstanceSettingsHandlerBadNumericValues : public TestInstanceSettingsHandler, + public WithParamInterface { }; -TEST_P(TestInstanceSettingsHandlerBadValues, setRefusesBadValues) +TEST_P(TestInstanceSettingsHandlerBadNumericValues, setRefusesBadNumericValues) { constexpr auto target_instance_name = "xanana"; const auto& [property, bad_val] = GetParam(); @@ -574,8 +610,32 @@ TEST_P(TestInstanceSettingsHandlerBadValues, setRefusesBadValues) EXPECT_EQ(original_specs, specs[target_instance_name]); } -INSTANTIATE_TEST_SUITE_P(TestInstanceSettingsHandler, TestInstanceSettingsHandlerBadValues, - Combine(ValuesIn(TestInstanceSettingsHandler::properties), +INSTANTIATE_TEST_SUITE_P(TestInstanceSettingsHandler, TestInstanceSettingsHandlerBadNumericValues, + Combine(ValuesIn(TestInstanceSettingsHandler::numeric_properties), Values("0", "2u", "1.5f", "2.0", "0xa", "0x8", "-4", "-1", "rubbish", " 123nonsense ", "ยค9", "\n", "\t", "^", ""))); + +struct TestInstanceSettingsHandlerBadBooleanValues : public TestInstanceSettingsHandler, + public WithParamInterface +{ +}; + +TEST_P(TestInstanceSettingsHandlerBadBooleanValues, setRefusesBadBooleanValues) +{ + constexpr auto target_instance_name = "zappa"; + const auto& [property, bad_val] = GetParam(); + + const auto original_specs = specs[target_instance_name]; + mock_vm(target_instance_name); // TODO: make this an expectation. + + MP_EXPECT_THROW_THAT(make_handler().set(make_key(target_instance_name, property), bad_val), + mp::InvalidSettingException, + mpt::match_what(AllOf(HasSubstr(bad_val), HasSubstr("try \"true\" or \"false\"")))); + + EXPECT_EQ(original_specs, specs[target_instance_name]); +} + +INSTANTIATE_TEST_SUITE_P(TestInstanceSettingsHandler, TestInstanceSettingsHandlerBadBooleanValues, + Combine(ValuesIn(TestInstanceSettingsHandler::boolean_properties), + Values("apostrophe", "(')", "1974"))); } // namespace