Skip to content

Commit

Permalink
Add startmode migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jcronenberg committed Nov 26, 2024
1 parent a6e67f0 commit 5059d59
Show file tree
Hide file tree
Showing 13 changed files with 199 additions and 2 deletions.
51 changes: 50 additions & 1 deletion src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use agama_lib::network::types::Status;
use agama_server::network::model::{self, IpConfig, IpRoute, Ipv4Method, Ipv6Method, MacAddress};
use cidr::IpInet;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, skip_serializing_none};
use serde_with::{serde_as, skip_serializing_none, DeserializeFromStr, SerializeDisplay};
use std::{net::IpAddr, str::FromStr};
use strum_macros::{Display, EnumString};

#[skip_serializing_none]
#[derive(Debug, PartialEq, Default, Serialize, Deserialize)]
Expand Down Expand Up @@ -46,6 +47,7 @@ pub struct Interface {
pub infiniband_child: Option<InfinibandChild>,
pub tun: Option<Tun>,
pub tap: Option<Tap>,
pub control: Control,
}

#[skip_serializing_none]
Expand Down Expand Up @@ -159,6 +161,34 @@ pub struct Nexthop {
pub gateway: String,
}

#[derive(Debug, PartialEq, Default, Serialize, Deserialize)]
pub struct Control {
pub mode: ControlMode,
}

#[derive(
Debug, PartialEq, Default, SerializeDisplay, DeserializeFromStr, EnumString, Clone, Display,
)]
#[strum(serialize_all = "snake_case")]
pub enum ControlMode {
#[default]
Manual,
Off,
Boot,
Hotplug,
}

impl From<ControlMode> for bool {
fn from(value: ControlMode) -> Self {
match value {
ControlMode::Manual => false,
ControlMode::Off => false,
ControlMode::Boot => true,
ControlMode::Hotplug => true,
}
}
}

pub struct ConnectionResult {
pub connections: Vec<model::Connection>,
pub warnings: Vec<anyhow::Error>,
Expand All @@ -181,6 +211,7 @@ impl Interface {
ip_config: ip_config.ip_config,
status: Status::Down,
mtu: self.link.mtu.unwrap_or_default(),
autoconnect: self.control.mode.clone().into(),
..Default::default()
};
let mut connections: Vec<model::Connection> = vec![];
Expand Down Expand Up @@ -513,4 +544,22 @@ mod tests {
let con: model::Connection = ifc.to_connection().unwrap().connections[0].to_owned();
assert_eq!(con.firewall_zone, Some("topsecret".to_string()));
}

#[test]
fn test_startmode_to_connection() {
setup_default_migration_settings();
let mut ifc = Interface {
control: Control {
mode: ControlMode::Manual,
},
..Default::default()
};

let con: model::Connection = ifc.to_connection().unwrap().connections[0].to_owned();
assert_eq!(con.autoconnect, false);

ifc.control.mode = ControlMode::Boot;
let con: model::Connection = ifc.to_connection().unwrap().connections[0].to_owned();
assert_eq!(con.autoconnect, true);
}
}
21 changes: 21 additions & 0 deletions tests/startmode/system-connections/startmode0.nmconnection
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[connection]
id=startmode0
uuid=9c634f1a-166b-4c91-b1b5-df831add1623
type=dummy
autoconnect=false
interface-name=startmode0

[ethernet]

[dummy]

[match]

[ipv4]
method=disabled

[ipv6]
addr-gen-mode=default
method=auto

[proxy]
20 changes: 20 additions & 0 deletions tests/startmode/system-connections/startmode1.nmconnection
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[connection]
id=startmode1
uuid=43c52f71-5298-4147-9950-f41ed65f9093
type=dummy
interface-name=startmode1

[ethernet]

[dummy]

[match]

[ipv4]
method=disabled

[ipv6]
addr-gen-mode=default
method=auto

[proxy]
21 changes: 21 additions & 0 deletions tests/startmode/system-connections/startmode2.nmconnection
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[connection]
id=startmode2
uuid=37374441-f8c1-495f-abf5-33dd859b1691
type=dummy
autoconnect=false
interface-name=startmode2

[ethernet]

[dummy]

[match]

[ipv4]
method=disabled

[ipv6]
addr-gen-mode=default
method=auto

[proxy]
20 changes: 20 additions & 0 deletions tests/startmode/system-connections/startmode3.nmconnection
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[connection]
id=startmode3
uuid=4cddd65e-11ca-4f75-bc01-cc2f62336681
type=dummy
interface-name=startmode3

[ethernet]

[dummy]

[match]

[ipv4]
method=disabled

[ipv6]
addr-gen-mode=default
method=auto

[proxy]
60 changes: 60 additions & 0 deletions tests/startmode/wicked_xml/startmode.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<interface origin="compat:suse:/etc/sysconfig/network/ifcfg-startmode0">
<name>startmode0</name>
<control>
<mode>manual</mode>
</control>
<firewall/>
<dummy/>
<link/>
<ipv4>
<enabled>true</enabled>
</ipv4>
<ipv6>
<enabled>true</enabled>
</ipv6>
</interface>
<interface origin="compat:suse:/etc/sysconfig/network/ifcfg-startmode1">
<name>startmode1</name>
<control>
<mode>boot</mode>
</control>
<firewall/>
<dummy/>
<link/>
<ipv4>
<enabled>true</enabled>
</ipv4>
<ipv6>
<enabled>true</enabled>
</ipv6>
</interface>
<interface origin="compat:suse:/etc/sysconfig/network/ifcfg-startmode2">
<name>startmode2</name>
<control>
<mode>off</mode>
</control>
<firewall/>
<dummy/>
<link/>
<ipv4>
<enabled>true</enabled>
</ipv4>
<ipv6>
<enabled>true</enabled>
</ipv6>
</interface>
<interface origin="compat:suse:/etc/sysconfig/network/ifcfg-startmode3">
<name>startmode3</name>
<control>
<mode>hotplug</mode>
</control>
<firewall/>
<dummy/>
<link/>
<ipv4>
<enabled>true</enabled>
</ipv4>
<ipv6>
<enabled>true</enabled>
</ipv6>
</interface>
1 change: 1 addition & 0 deletions tests/wireless/system-connections/wlan0-0.nmconnection
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
id=wlan0-0
uuid=2a262d6e-ccba-4afb-a190-ec08d0175c56
type=wifi
autoconnect=false
interface-name=wlan0

[wifi]
Expand Down
1 change: 1 addition & 0 deletions tests/wireless/system-connections/wlan0-1.nmconnection
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
id=wlan0-1
uuid=e05ab8eb-9905-4746-b81a-54e190b16354
type=wifi
autoconnect=false
interface-name=wlan0

[wifi]
Expand Down
1 change: 1 addition & 0 deletions tests/wireless/system-connections/wlan1.nmconnection
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
id=wlan1
uuid=9d48ef42-2c96-4e43-8ab6-6c66385efdb6
type=wifi
autoconnect=false
interface-name=wlan1

[wifi]
Expand Down
2 changes: 1 addition & 1 deletion tests/wireless/wicked_xml/wireless.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<interface origin="compat:suse:/etc/sysconfig/network/ifcfg-wlan2">
<name>wlan2</name>
<control>
<mode>auto</mode>
<mode>boot</mode>
</control>
<firewall/>
<wireless>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
id=wlan_eap0
uuid=62c02744-5807-445b-9636-421b1162e000
type=wifi
autoconnect=false
interface-name=wlan_eap0

[wifi]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
id=wlan_eap1
uuid=f16a2831-d858-4d0c-8799-5262884c50cc
type=wifi
autoconnect=false
interface-name=wlan_eap1

[wifi]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
id=wlan_eap2
uuid=36eca905-1ac0-42f2-873d-f2ed783f2ce6
type=wifi
autoconnect=false
interface-name=wlan_eap2

[wifi]
Expand Down

0 comments on commit 5059d59

Please sign in to comment.