state: Complement ipv4/ipv6 keys in case they are missing

In order to simplify the state processing, complement the interface
state with the `ipv4`/`ipv6` keys in case they are missing.

Signed-off-by: Edward Haas <edwardh@redhat.com>
This commit is contained in:
Edward Haas 2019-05-05 18:52:14 +03:00
parent 30fdd96b2f
commit 9143fd916c
3 changed files with 24 additions and 9 deletions

View File

@ -54,6 +54,7 @@ class State(object):
def __init__(self, state):
self._state = copy.deepcopy(state)
self._ifaces_state = State._index_interfaces_state_by_name(self._state)
self._complement_interface_empty_ip_subtrees()
def __eq__(self, other):
return self.state == other.state
@ -80,6 +81,13 @@ class State(object):
""" Indexed interfaces state """
return self._ifaces_state
def _complement_interface_empty_ip_subtrees(self):
""" Complement the interfaces states with empty IPv4/IPv6 subtrees. """
for iface_state in six.viewvalues(self.interfaces):
for family in (Interface.IPV4, Interface.IPV6):
if family not in iface_state:
iface_state[family] = {}
def sanitize_ethernet(self, other_state):
"""
Given the other_state, update the ethernet interfaces state base on

View File

@ -109,7 +109,9 @@ def test_add_new_bond(netinfo_nm_mock, netapplier_nm_mock):
'options': {
'miimon': 200,
}
}
},
'ipv4': {},
'ipv6': {}
}
]
}

View File

@ -33,20 +33,16 @@ class TestAssertIfaceState(object):
def test_desired_is_partial_to_current(self):
desired_state = self._base_state
current_state = self._base_state
current_state.interfaces.update({
'eth0': {'name': 'eth0', 'state': 'up', 'type': 'unknown'},
'eth1': {'name': 'eth1', 'state': 'up', 'type': 'unknown'}
})
extra_state = self._extra_state
current_state.interfaces.update(extra_state.interfaces)
desired_state.verify_interfaces(current_state)
def test_current_is_partial_to_desired(self):
desired_state = self._base_state
current_state = self._base_state
desired_state.interfaces.update({
'eth0': {'name': 'eth0', 'state': 'up', 'type': 'unknown'},
'eth1': {'name': 'eth1', 'state': 'up', 'type': 'unknown'}
})
extra_state = self._extra_state
desired_state.interfaces.update(extra_state.interfaces)
with pytest.raises(NmstateVerificationError):
desired_state.verify_interfaces(current_state)
@ -133,3 +129,12 @@ class TestAssertIfaceState(object):
}
]
})
@property
def _extra_state(self):
return state.State({
Interface.KEY: [
{'name': 'eth0', 'state': 'up', 'type': 'unknown'},
{'name': 'eth1', 'state': 'up', 'type': 'unknown'}
]
})