Skip to content

Commit

Permalink
Fix ip method selection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jcronenberg committed Oct 1, 2024
1 parent a6257fd commit 9851fad
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 24 deletions.
55 changes: 40 additions & 15 deletions rust/migrate-wicked/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct Interface {
pub ipv4: Ipv4,
#[serde(rename = "ipv4-static")]
pub ipv4_static: Option<Ipv4Static>,
#[serde(rename = "ipv4-dhcp")]
pub ipv4_dhcp: Option<Ipv4Dhcp>,
pub ipv6: Ipv6,
#[serde(rename = "ipv6-static")]
pub ipv6_static: Option<Ipv6Static>,
Expand Down Expand Up @@ -61,18 +63,34 @@ pub struct Link {
pub mtu: Option<u32>,
}

#[derive(Debug, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
fn default_true() -> bool {
true
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Ipv4 {
#[serde(default = "default_true")]
pub enabled: bool,
}

#[derive(Debug, PartialEq, Default, Serialize, Deserialize)]
#[serde(default)]
impl Default for Ipv4 {
fn default() -> Self {
Self { enabled: true }
}
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Ipv6 {
#[serde(default = "default_true")]
pub enabled: bool,
}

impl Default for Ipv6 {
fn default() -> Self {
Self { enabled: true }
}
}

#[skip_serializing_none]
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Ipv4Static {
Expand All @@ -82,6 +100,11 @@ pub struct Ipv4Static {
pub routes: Option<Vec<Route>>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Ipv4Dhcp {
pub enabled: bool,
}

#[skip_serializing_none]
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Ipv6Static {
Expand Down Expand Up @@ -242,19 +265,18 @@ impl Interface {
},
warnings: vec![],
};
let method4 = if self.ipv4.enabled && self.ipv4_static.is_some() {
let method4 = if self.ipv4_static.is_some() {
Ipv4Method::Manual
} else if !self.ipv4.enabled {
Ipv4Method::Disabled
} else {
} else if self.ipv4_dhcp.is_some() {
Ipv4Method::Auto
} else {
Ipv4Method::Disabled
};
let method6 = if self.ipv6.enabled && self.ipv6_static.is_some() {
let method6 = if self.ipv6_static.is_some() {
Ipv6Method::Manual
} else if self.ipv6.enabled
&& self.ipv6_dhcp.is_some()
&& self.ipv6_dhcp.as_ref().unwrap().mode == "managed"
{
} else if self.ipv6_dhcp.is_some() && self.ipv6_dhcp.as_ref().unwrap().mode == "managed" {
Ipv6Method::Dhcp
} else if !self.ipv6.enabled {
Ipv6Method::Disabled
Expand Down Expand Up @@ -373,7 +395,9 @@ mod tests {
fn test_static_interface_to_connection() {
setup_default_migration_settings();
let static_interface = Interface {
ipv4: Ipv4 { enabled: true },
ipv4: Ipv4 {
enabled: true,
},
ipv4_static: Some(Ipv4Static {
addresses: Some(vec![Address {
local: "127.0.0.1/8".to_string(),
Expand All @@ -385,7 +409,9 @@ mod tests {
..Default::default()
}]),
}),
ipv6: Ipv6 { enabled: true },
ipv6: Ipv6 {
enabled: true,
},
ipv6_static: Some(Ipv6Static {
addresses: Some(vec![Address {
local: "::1/128".to_string(),
Expand Down Expand Up @@ -451,8 +477,7 @@ mod tests {
fn test_dhcp_interface_to_connection() {
setup_default_migration_settings();
let static_interface = Interface {
ipv4: Ipv4 { enabled: true },
ipv6: Ipv6 { enabled: true },
ipv4_dhcp: Some(Ipv4Dhcp { enabled: true }),
..Default::default()
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ method=disabled

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

[proxy]
23 changes: 23 additions & 0 deletions rust/migrate-wicked/tests/ipv4_static/wicked_xml/ipv4_static.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,26 @@
<accept-redirects>false</accept-redirects>
</ipv6>
</interface>
<interface origin="compat:suse:/etc/sysconfig/network/ifcfg-eth9">
<name>eth9</name>
<control>
<mode>boot</mode>
</control>
<firewall/>
<link/>
<ipv4>
<arp-verify>true</arp-verify>
</ipv4>
<ipv4:static>
<address>
<local>192.168.100.5/24</local>
</address>
<address>
<local>192.168.101.5/24</local>
</address>
</ipv4:static>
<ipv6>
<privacy>prefer-public</privacy>
<accept-redirects>false</accept-redirects>
</ipv6>
</interface>
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface-name=eth7
[match]

[ipv4]
method=auto
method=disabled

[ipv6]
addr-gen-mode=default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface-name=eth5
[match]

[ipv4]
method=auto
method=disabled

[ipv6]
addr-gen-mode=default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface-name=eth6
[match]

[ipv4]
method=auto
method=disabled

[ipv6]
addr-gen-mode=default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface-name=eth5
[match]

[ipv4]
method=auto
method=disabled

[ipv6]
addr-gen-mode=default
Expand Down
24 changes: 24 additions & 0 deletions rust/migrate-wicked/tests/ipv6_static/wicked_xml/ipv6_static.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,27 @@
</address>
</ipv6:static>
</interface>
<interface origin="compat:suse:/etc/sysconfig/network/ifcfg-eth6">
<name>eth6</name>
<control>
<mode>boot</mode>
</control>
<firewall/>
<link/>
<ipv4>
<enabled>true</enabled>
<arp-verify>true</arp-verify>
</ipv4>
<ipv6>
<privacy>prefer-public</privacy>
<accept-redirects>false</accept-redirects>
</ipv6>
<ipv6:static>
<address>
<local>2001:db8:0:1::5/64</local>
</address>
<address>
<local>2001:db8:0:2::5/64</local>
</address>
</ipv6:static>
</interface>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ method=disabled

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

[proxy]
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ method=disabled

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

[proxy]
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ method=disabled

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

[proxy]
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ method=disabled

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

[proxy]

0 comments on commit 9851fad

Please sign in to comment.