dns: Fix error when desired no DNS changes

When a interface holding DNS information, applying simple state like
below will cause failure on DNS interface not found.

```yml
interfaces:
  - name: eth1
```

This is caused by `current_dns_ifaces_are_still_valid()` does not have
IP stack info merged from current which lead to thinking IP stack
disabled.

The fix is skip the test if desired interface has unmentioned IP
stack(set to None). This is a workaround, not final fix. The ideal fix
should given `desire_state` and `current_state`, get merged state for
validation, state for verifying, state for applying. But that require
massive code changes.

Unit test case included.

Signed-off-by: Gris Ge <fge@redhat.com>
This commit is contained in:
Gris Ge 2022-12-13 19:39:23 +08:00 committed by Fernando Fernández Mancera
parent 9f92170cf8
commit c7d22d9f6d
2 changed files with 98 additions and 3 deletions

View File

@ -288,7 +288,9 @@ fn current_dns_ifaces_are_still_valid(
if let Some(des_iface) =
desired.interfaces.kernel_ifaces.get(iface_name)
{
if !is_iface_valid_for_dns(false, des_iface) {
if des_iface.base_iface().ipv4.is_some()
&& !is_iface_valid_for_dns(false, des_iface)
{
return false;
}
}
@ -299,7 +301,9 @@ fn current_dns_ifaces_are_still_valid(
if let Some(des_iface) =
desired.interfaces.kernel_ifaces.get(iface_name)
{
if !is_iface_valid_for_dns(true, des_iface) {
if des_iface.base_iface().ipv6.is_some()
&& !is_iface_valid_for_dns(true, des_iface)
{
return false;
}
}

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
use crate::{ErrorKind, NetworkState};
use crate::{DnsClientState, ErrorKind, NetworkState};
#[test]
fn test_dns_ignore_dns_purge_on_absent_iface() {
@ -120,3 +120,94 @@ fn test_two_dns_ipv6_link_local_iface() {
assert_eq!(e.kind(), ErrorKind::NotImplementedError);
}
}
#[test]
fn test_dns_iface_has_no_ip_stack_info() {
let desired: NetworkState = serde_yaml::from_str(
r#"---
interfaces:
- name: eth1
"#,
)
.unwrap();
let mut current: NetworkState = serde_yaml::from_str(
r#"---
dns-resolver:
config:
search:
- example.com
- example.org
server:
- 2001:db8:f::1
- 2001:db8:f::2
- 192.0.2.250
interfaces:
- name: eth1
type: ethernet
state: up
ipv4:
address:
- ip: 192.0.2.251
prefix-length: 24
dhcp: false
enabled: true
ipv6:
address:
- ip: 2001:db8:1::1
prefix-length: 64
dhcp: false
enabled: true
autoconf: false
routes:
config:
- destination: 0.0.0.0/0
next-hop-address: 192.0.2.1
next-hop-interface: eth1
- destination: ::/0
next-hop-address: 2001:db8:1::3
next-hop-interface: eth1
"#,
)
.unwrap();
if let Some(ip) = current
.interfaces
.kernel_ifaces
.get_mut("eth1")
.unwrap()
.base_iface_mut()
.ipv4
.as_mut()
{
ip.dns = Some({
DnsClientState {
server: Some(vec!["192.0.2.250".to_string()]),
priority: Some(100),
..Default::default()
}
})
};
if let Some(ip) = current
.interfaces
.kernel_ifaces
.get_mut("eth1")
.unwrap()
.base_iface_mut()
.ipv6
.as_mut()
{
ip.dns = Some({
DnsClientState {
server: Some(vec![
"2001:db8:f::1".to_string(),
"2001:db8:f::2".to_string(),
]),
search: Some(vec![
"example.com".to_string(),
"example.org".to_string(),
]),
priority: Some(10),
}
})
};
desired.gen_state_for_apply(&current).unwrap();
}