Skip to content

Commit

Permalink
[yaml utils] refactored the create_default_interface_node code into a…
Browse files Browse the repository at this point in the history
… function.
  • Loading branch information
georgeliao committed Jul 29, 2024
1 parent 18af773 commit 1017c1e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
21 changes: 14 additions & 7 deletions src/utils/yaml_node_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ YAML::Node create_extra_interface_node(const std::string& extra_interface_name,

return extra_interface_data;
};

YAML::Node create_default_interface_node(const std::string& default_interface_mac_address)
{
YAML::Node default_interface_data{};

default_interface_data["match"]["macaddress"] = default_interface_mac_address;
default_interface_data["dhcp4"] = true;

return default_interface_data;
};

} // namespace

std::string mp::utils::emit_yaml(const YAML::Node& node)
Expand Down Expand Up @@ -106,15 +117,13 @@ YAML::Node mp::utils::make_cloud_init_network_config(const std::string& default_
YAML::Node network_data = file_content.empty() ? YAML::Node{} : YAML::Load(file_content);

network_data["version"] = "2";
std::string name = "default";
network_data["ethernets"][name]["match"]["macaddress"] = default_mac_addr;
network_data["ethernets"][name]["dhcp4"] = true;
network_data["ethernets"]["default"] = create_default_interface_node(default_mac_addr);

for (size_t i = 0; i < extra_interfaces.size(); ++i)
{
if (extra_interfaces[i].auto_mode)
{
name = "extra" + std::to_string(i);
const std::string name = "extra" + std::to_string(i);
network_data["ethernets"][name] = create_extra_interface_node(name, extra_interfaces[i].mac_address);
}
}
Expand All @@ -136,9 +145,7 @@ YAML::Node mp::utils::add_extra_interface_to_network_config(const std::string& d
YAML::Node network_data{};
network_data["version"] = "2";

const std::string default_network_name = "default";
network_data["ethernets"][default_network_name]["match"]["macaddress"] = default_mac_addr;
network_data["ethernets"][default_network_name]["dhcp4"] = true;
network_data["ethernets"]["default"] = create_default_interface_node(default_mac_addr);

const std::string extra_interface_name = "extra0";
network_data["ethernets"][extra_interface_name] =
Expand Down
25 changes: 16 additions & 9 deletions tests/test_daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,15 +1137,22 @@ std::vector<std::string> make_args(const std::vector<std::string>& args)
return all_args;
}

INSTANTIATE_TEST_SUITE_P(
Daemon, LaunchWithNoExtraNetworkCloudInit,
Values(make_args({}), make_args({"xenial"}), make_args({"xenial", "--network", "name=eth0,mode=manual"}),
make_args({"groovy"}), make_args({"groovy", "--network", "name=eth0,mode=manual"}),
make_args({"--network", "name=eth0,mode=manual"}), make_args({"devel"}),
make_args({"hirsute", "--network", "name=eth0,mode=manual"}), make_args({"daily:21.04"}),
make_args({"daily:21.04", "--network", "name=eth0,mode=manual"}),
make_args({"appliance:openhab", "--network", "name=eth0,mode=manual"}), make_args({"appliance:nextcloud"}),
make_args({"snapcraft:core18", "--network", "name=eth0,mode=manual"}), make_args({"snapcraft:core20"})));
INSTANTIATE_TEST_SUITE_P(Daemon,
LaunchWithNoExtraNetworkCloudInit,
Values(make_args({}),
make_args({"xenial"}),
make_args({"xenial", "--network", "name=eth0,mode=manual"}),
make_args({"groovy"}),
make_args({"groovy", "--network", "name=eth0,mode=manual"}),
make_args({"--network", "name=eth0,mode=manual"}),
make_args({"devel"}),
make_args({"hirsute", "--network", "name=eth0,mode=manual"}),
make_args({"daily:21.04"}),
make_args({"daily:21.04", "--network", "name=eth0,mode=manual"}),
make_args({"appliance:openhab", "--network", "name=eth0,mode=manual"}),
make_args({"appliance:nextcloud"}),
make_args({"snapcraft:core18", "--network", "name=eth0,mode=manual"}),
make_args({"snapcraft:core20"})));

TEST_P(LaunchWithBridges, creates_network_cloud_init_iso)
{
Expand Down

0 comments on commit 1017c1e

Please sign in to comment.