def test_rollback_for_bond(eth1_up, eth2_up): current_state = netinfo.show() desired_state = { INTERFACES: [{ 'name': 'bond99', 'type': 'bond', 'state': 'up', 'ipv4': { 'enabled': True, 'address': [{ 'ip': '192.168.122.250', 'prefix-length': 24 }] }, 'link-aggregation': { 'mode': 'balance-rr', 'slaves': ['eth1', 'eth2'], 'options': { 'miimon': '140' } }, }] } desired_state[INTERFACES][0]['invalid_key'] = 'foo' with pytest.raises(netapplier.DesiredStateIsNotCurrentError): netapplier.apply(desired_state) time.sleep(5) current_state_after_apply = netinfo.show() assert current_state == current_state_after_apply
def test_rollback_for_vlans(eth1_up): current_state = netinfo.show() desired_state = TWO_VLANS_STATE desired_state[INTERFACES][1]['invalid_key'] = 'foo' with pytest.raises(netapplier.DesiredStateIsNotCurrentError): netapplier.apply(desired_state) time.sleep(5) # Give some time for NetworkManager to rollback current_state_after_apply = netinfo.show() assert current_state == current_state_after_apply
def test_preserve_dns_config_with_empty_state(setup_ipv4_ipv6_name_server): old_state = setup_ipv4_ipv6_name_server netapplier.apply({Interface.KEY: []}) current_state = netinfo.show() assert old_state[DNS.KEY][DNS.CONFIG] == current_state[DNS.KEY][DNS.CONFIG]
def test_netinfo_show_bond_iface(nm_mock): current_config = { 'interfaces': [{ 'name': 'bond99', 'type': 'bond', 'state': 'up', 'link-aggregation': { 'mode': 'balance-rr', 'slaves': [], 'options': { 'miimon': '100', } }, 'ipv4': { 'enabled': False, }, }] } nm_mock.device.list_devices.return_value = ['one-item'] nm_mock.translator.Nm2Api.get_common_device_info.return_value = { 'name': current_config['interfaces'][0]['name'], 'type': current_config['interfaces'][0]['type'], 'state': current_config['interfaces'][0]['state'], } nm_mock.bond.is_bond_type_id.return_value = True nm_mock.translator.Nm2Api.get_bond_info.return_value = { 'link-aggregation': current_config['interfaces'][0]['link-aggregation'] } nm_mock.ipv4.get_info.return_value = ( current_config['interfaces'][0]['ipv4']) report = netinfo.show() assert current_config == report
def _apply_ifaces_state( desired_state, verify_change, commit, rollback_timeout ): current_state = state.State(netinfo.show()) desired_state.sanitize_ethernet(current_state) desired_state.sanitize_dynamic_ip() desired_state.merge_routes(current_state) desired_state.merge_dns(current_state) metadata.generate_ifaces_metadata(desired_state, current_state) validator.validate_interfaces_state(desired_state, current_state) validator.validate_routes(desired_state, current_state) new_interfaces = _list_new_interfaces(desired_state, current_state) try: with nm.checkpoint.CheckPoint( autodestroy=commit, timeout=rollback_timeout ) as checkpoint: with _setup_providers(): _add_interfaces(new_interfaces, desired_state) state2edit = _create_editable_desired_state( desired_state, current_state, new_interfaces ) _edit_interfaces(state2edit) if verify_change: _verify_change(desired_state) if not commit: return checkpoint except nm.checkpoint.NMCheckPointPermissionError: raise NmstatePermissionError('Error creating a check point') except nm.checkpoint.NMCheckPointCreationError: raise NmstateConflictError('Error creating a check point')
def _verify_change(desired_state): current_state = state.State(netinfo.show()) verifiable_desired_state = copy.deepcopy(desired_state) verifiable_desired_state.verify_interfaces(current_state) verifiable_desired_state.verify_routes(current_state) verifiable_desired_state.verify_dns(current_state) verifiable_desired_state.verify_route_rule(current_state)
def __init__(self, module, module_name): self.previous_state = netinfo.show() self.interfaces = deepcopy(self.previous_state["interfaces"]) self.module = module self.params = module.params self.result = dict(changed=False) self.module_name = module_name
def apply_partial_interface_state(self, interface_state): if isinstance(interface_state, list): interfaces = interface_state else: interfaces = [interface_state] new_partial_state = {"interfaces": interfaces} if self.params.get("debug"): self.result["previous_state"] = self.previous_state self.result["new_partial_state"] = new_partial_state self.result["debugfile"] = write_debug_state( self.module_name, new_partial_state) if self.module.check_mode: new_full_state = deepcopy(self.previous_state) new_full_state.update(new_partial_state) self.result["state"] = new_full_state # TODO: maybe compare only the state of the defined interfaces if self.previous_state != new_full_state: self.result["changed"] = True self.module.exit_json(**self.result) else: netapplier.apply(new_partial_state) current_state = netinfo.show() if current_state != self.previous_state: self.result["changed"] = True self.result["state"] = current_state self.module.exit_json(**self.result)
def show(args): state = netinfo.show() if args.only != '*': patterns = [p for p in args.only.split(',')] state['interfaces'] = _filter_interfaces(state, patterns) print_state(state, use_yaml=args.yaml)
def test_netinfo_show_generic_iface(nm_mock): current_config = { INTERFACES: [ { 'name': 'foo', 'type': 'unknown', 'state': 'up', 'ipv4': { 'enabled': False, }, 'ipv6': { 'enabled': False, }, } ] } nm_mock.device.list_devices.return_value = ['one-item'] nm_mock.translator.Nm2Api.get_common_device_info.return_value = ( current_config[INTERFACES][0]) nm_mock.bond.is_bond_type_id.return_value = False nm_mock.ipv4.get_info.return_value = ( current_config[INTERFACES][0]['ipv4']) nm_mock.ipv6.get_info.return_value = ( current_config[INTERFACES][0]['ipv6']) report = netinfo.show() assert current_config == report
def test_netinfo_show_generic_iface(nm_mock, nm_dns_mock): current_config = { DNS.KEY: {DNS.RUNNING: {}, DNS.CONFIG: {}}, ROUTES: {Route.CONFIG: [], Route.RUNNING: []}, RouteRule.KEY: {RouteRule.CONFIG: []}, INTERFACES: [ { Interface.NAME: "foo", Interface.TYPE: InterfaceType.UNKNOWN, Interface.STATE: InterfaceState.UP, Interface.IPV4: {InterfaceIPv4.ENABLED: False}, Interface.IPV6: {InterfaceIPv6.ENABLED: False}, } ], } current_iface0 = current_config[INTERFACES][0] nm_mock.device.list_devices.return_value = ["one-item"] nm_mock.translator.Nm2Api.get_common_device_info.return_value = ( current_iface0 ) nm_mock.bond.is_bond_type_id.return_value = False nm_mock.ipv4.get_info.return_value = current_iface0[Interface.IPV4] nm_mock.ipv6.get_info.return_value = current_iface0[Interface.IPV6] nm_mock.ipv4.get_route_running.return_value = [] nm_mock.ipv4.get_route_config.return_value = [] nm_mock.ipv6.get_route_running.return_value = [] nm_mock.ipv6.get_route_config.return_value = [] nm_dns_mock.get_running.return_value = current_config[DNS.KEY][DNS.RUNNING] nm_dns_mock.get_config.return_value = current_config[DNS.KEY][DNS.CONFIG] report = netinfo.show() assert current_config == report
def test_netinfo_show_generic_iface(nm_mock, nm_dns_mock): current_config = { DNS.KEY: {DNS.RUNNING: {}, DNS.CONFIG: {}}, ROUTES: {'config': [], 'running': []}, INTERFACES: [ { 'name': 'foo', 'type': 'unknown', 'state': 'up', 'ipv4': {InterfaceIPv4.ENABLED: False}, 'ipv6': {InterfaceIPv6.ENABLED: False}, } ], } current_iface0 = current_config[INTERFACES][0] nm_mock.device.list_devices.return_value = ['one-item'] nm_mock.translator.Nm2Api.get_common_device_info.return_value = ( current_iface0 ) nm_mock.bond.is_bond_type_id.return_value = False nm_mock.ipv4.get_info.return_value = current_iface0['ipv4'] nm_mock.ipv6.get_info.return_value = current_iface0['ipv6'] nm_mock.ipv4.get_route_running.return_value = [] nm_mock.ipv4.get_route_config.return_value = [] nm_mock.ipv6.get_route_running.return_value = [] nm_mock.ipv6.get_route_config.return_value = [] nm_dns_mock.get_running.return_value = current_config[DNS.KEY][DNS.RUNNING] nm_dns_mock.get_config.return_value = current_config[DNS.KEY][DNS.CONFIG] report = netinfo.show() assert current_config == report
def show_interfaces(filter=None): net_info = netinfo.show() filter_set = set(filter) if filter else set() return { ifstate[Interface.NAME]: ifstate for ifstate in net_info[Interface.KEY] if ifstate[Interface.NAME] in filter_set }
def test_dns_edit_nameserver_with_static_gateway(dns_config): desired_state = { Interface.KEY: _get_test_iface_states(), Route.KEY: {Route.CONFIG: _gen_default_gateway_route()}, DNS.KEY: {DNS.CONFIG: dns_config}, } netapplier.apply(desired_state) current_state = netinfo.show() assert dns_config == current_state[DNS.KEY][DNS.CONFIG]
def test_netinfo_show_bond_iface(nm_mock, nm_dns_mock): current_config = { DNS.KEY: { DNS.RUNNING: {}, DNS.CONFIG: {} }, ROUTES: { Route.CONFIG: [], Route.RUNNING: [] }, RouteRule.KEY: { RouteRule.CONFIG: [] }, INTERFACES: [{ Interface.NAME: "bond99", Interface.TYPE: InterfaceType.BOND, Interface.STATE: InterfaceState.UP, Bond.CONFIG_SUBTREE: { Bond.MODE: BondMode.ROUND_ROBIN, Bond.SLAVES: [], Bond.OPTIONS_SUBTREE: { "miimon": "100" }, }, Interface.IPV4: { InterfaceIPv4.ENABLED: False }, Interface.IPV6: { InterfaceIPv6.ENABLED: False }, }], } nm_mock.device.list_devices.return_value = ["one-item"] nm_mock.translator.Nm2Api.get_common_device_info.return_value = { Interface.NAME: current_config[INTERFACES][0][Interface.NAME], Interface.TYPE: current_config[INTERFACES][0][Interface.TYPE], Interface.STATE: current_config[INTERFACES][0][Interface.STATE], } nm_mock.bond.is_bond_type_id.return_value = True nm_mock.translator.Nm2Api.get_bond_info.return_value = { Bond.CONFIG_SUBTREE: current_config[INTERFACES][0][Bond.CONFIG_SUBTREE] } nm_mock.ipv4.get_info.return_value = current_config[INTERFACES][0][ Interface.IPV4] nm_mock.ipv6.get_info.return_value = current_config[INTERFACES][0][ Interface.IPV6] nm_mock.ipv4.get_route_running.return_value = [] nm_mock.ipv4.get_route_config.return_value = [] nm_mock.ipv6.get_route_running.return_value = [] nm_mock.ipv6.get_route_config.return_value = [] nm_dns_mock.get_running.return_value = current_config[DNS.KEY][DNS.RUNNING] nm_dns_mock.get_config.return_value = current_config[DNS.KEY][DNS.CONFIG] report = netinfo.show() assert current_config == report
def test_dns_edit(eth1_up): with example_state("dns_edit_eth1.yml", cleanup="dns_remove.yml") as desired_state: assertlib.assert_state(desired_state) current_state = netinfo.show() assert current_state.get(DNS.KEY, {}).get(DNS.CONFIG, {}) == { DNS.SERVER: [], DNS.SEARCH: [], }
def show_only(ifnames): """ Report the current state, filtering based on the given interface names. """ base_filter_state = { INTERFACES: [{'name': ifname} for ifname in ifnames] } current_state = State(netinfo.show()) current_state.filter(base_filter_state) return current_state.state
def test_error_show(nm_mock, nm_dns_mock): current_config = { DNS.KEY: { DNS.RUNNING: {}, DNS.CONFIG: {} }, ROUTES: { "config": [], "running": [] }, RouteRule.KEY: { RouteRule.CONFIG: [] }, INTERFACES: [{ "name": "foo", "type": "unknown", "state": "up", "ipv4": { InterfaceIPv4.ENABLED: False }, "ipv6": { InterfaceIPv6.ENABLED: False }, }], } current_iface0 = current_config[INTERFACES][0] nm_mock.device.list_devices.return_value = ["one-item"] nm_mock.translator.Nm2Api.get_common_device_info.return_value = ( current_iface0) nm_mock.bond.is_bond_type_id.return_value = False nm_mock.ipv4.get_info.return_value = current_iface0["ipv4"] nm_mock.ipv6.get_info.return_value = current_iface0["ipv6"] nm_mock.ipv4.get_route_running.return_value = [] nm_mock.ipv4.get_route_config.return_value = [] nm_mock.ipv6.get_route_running.return_value = [] nm_mock.ipv6.get_route_config.return_value = [] nm_dns_mock.get_running.return_value = current_config[DNS.KEY][DNS.RUNNING] nm_dns_mock.get_config.return_value = current_config[DNS.KEY][DNS.CONFIG] with pytest.raises(TypeError): # pylint: disable=too-many-function-args netinfo.show(None)
def test_netinfo_show_bond_iface(nm_mock, nm_dns_mock): current_config = { DNS.KEY: { DNS.RUNNING: {}, DNS.CONFIG: {} }, ROUTES: { "config": [], "running": [] }, RouteRule.KEY: { RouteRule.CONFIG: [] }, INTERFACES: [{ "name": "bond99", "type": "bond", "state": "up", "link-aggregation": { "mode": "balance-rr", "slaves": [], "options": { "miimon": "100" }, }, "ipv4": { InterfaceIPv4.ENABLED: False }, "ipv6": { InterfaceIPv6.ENABLED: False }, }], } nm_mock.device.list_devices.return_value = ["one-item"] nm_mock.translator.Nm2Api.get_common_device_info.return_value = { "name": current_config[INTERFACES][0]["name"], "type": current_config[INTERFACES][0]["type"], "state": current_config[INTERFACES][0]["state"], } nm_mock.bond.is_bond_type_id.return_value = True nm_mock.translator.Nm2Api.get_bond_info.return_value = { "link-aggregation": current_config[INTERFACES][0]["link-aggregation"] } nm_mock.ipv4.get_info.return_value = current_config[INTERFACES][0]["ipv4"] nm_mock.ipv6.get_info.return_value = current_config[INTERFACES][0]["ipv6"] nm_mock.ipv4.get_route_running.return_value = [] nm_mock.ipv4.get_route_config.return_value = [] nm_mock.ipv6.get_route_running.return_value = [] nm_mock.ipv6.get_route_config.return_value = [] nm_dns_mock.get_running.return_value = current_config[DNS.KEY][DNS.RUNNING] nm_dns_mock.get_config.return_value = current_config[DNS.KEY][DNS.CONFIG] report = netinfo.show() assert current_config == report
def test_error_show(show_with_plugins_mock, plugin_context_mock): current_config = { DNS.KEY: {DNS.RUNNING: {}, DNS.CONFIG: {}}, Route.KEY: {"config": [], "running": []}, RouteRule.KEY: {RouteRule.CONFIG: []}, Interface.KEY: [ { "name": "foo", "type": "unknown", "state": "up", "ipv4": {InterfaceIPv4.ENABLED: False}, "ipv6": {InterfaceIPv6.ENABLED: False}, } ], } show_with_plugins_mock.return_value = current_config with pytest.raises(TypeError): # pylint: disable=too-many-function-args netinfo.show(None)
def test_netinfo_show_bond_iface(nm_mock, nm_dns_mock): current_config = { DNS.KEY: { DNS.RUNNING: {}, DNS.CONFIG: {} }, ROUTES: { 'config': [], 'running': [] }, INTERFACES: [{ 'name': 'bond99', 'type': 'bond', 'state': 'up', 'link-aggregation': { 'mode': 'balance-rr', 'slaves': [], 'options': { 'miimon': '100' }, }, 'ipv4': { 'enabled': False }, 'ipv6': { 'enabled': False }, }], } nm_mock.device.list_devices.return_value = ['one-item'] nm_mock.translator.Nm2Api.get_common_device_info.return_value = { 'name': current_config[INTERFACES][0]['name'], 'type': current_config[INTERFACES][0]['type'], 'state': current_config[INTERFACES][0]['state'], } nm_mock.bond.is_bond_type_id.return_value = True nm_mock.translator.Nm2Api.get_bond_info.return_value = { 'link-aggregation': current_config[INTERFACES][0]['link-aggregation'] } nm_mock.ipv4.get_info.return_value = current_config[INTERFACES][0]['ipv4'] nm_mock.ipv6.get_info.return_value = current_config[INTERFACES][0]['ipv6'] nm_mock.ipv4.get_route_running.return_value = [] nm_mock.ipv4.get_route_config.return_value = [] nm_mock.ipv6.get_route_running.return_value = [] nm_mock.ipv6.get_route_config.return_value = [] nm_dns_mock.get_running.return_value = current_config[DNS.KEY][DNS.RUNNING] nm_dns_mock.get_config.return_value = current_config[DNS.KEY][DNS.CONFIG] report = netinfo.show() assert current_config == report
def test_dns_edit_ipv6_nameserver_before_ipv4(): dns_config = { DNS.SERVER: [IPV6_DNS_NAMESERVERS[0], IPV4_DNS_NAMESERVERS[0]], DNS.SEARCH: [], } desired_state = { Interface.KEY: _get_test_iface_states(), Route.KEY: {Route.CONFIG: _gen_default_gateway_route()}, DNS.KEY: {DNS.CONFIG: dns_config}, } netapplier.apply(desired_state) current_state = netinfo.show() assert dns_config == current_state[DNS.KEY][DNS.CONFIG]
def _apply_ifaces_state(desired_state, verify_change, commit, rollback_timeout): original_desired_state = copy.deepcopy(desired_state) current_state = state.State(netinfo.show()) desired_state.sanitize_ethernet(current_state) desired_state.sanitize_dynamic_ip() desired_state.merge_routes(current_state) desired_state.merge_dns(current_state) desired_state.merge_route_rules(current_state) desired_state.remove_unknown_interfaces() desired_state.complement_master_interfaces_removal(current_state) metadata.generate_ifaces_metadata(desired_state, current_state) validator.validate_interfaces_state(original_desired_state, current_state) validator.validate_routes(desired_state, current_state) try: with nm.checkpoint.CheckPoint(autodestroy=commit, timeout=rollback_timeout) as checkpoint: with _setup_providers(): state2edit = state.State(desired_state.state) state2edit.merge_interfaces(current_state) nm.applier.apply_changes( list(state2edit.interfaces.values()), original_desired_state, ) verified = False if verify_change: for _ in range(VERIFY_RETRY_TIMEOUT): try: _verify_change(desired_state) verified = True break except NmstateVerificationError: time.sleep(VERIFY_RETRY_INTERNAL) if not verified: _verify_change(desired_state) if not commit: return checkpoint except nm.checkpoint.NMCheckPointPermissionError: raise NmstatePermissionError("Error creating a check point") except nm.checkpoint.NMCheckPointCreationError: raise NmstateConflictError("Error creating a check point") except NmstateError: # Assume rollback occurred. # Checkpoint rollback is async, there is a need to wait for it to # finish before proceeding with other actions. time.sleep(5) raise
def assert_state(desired_state_data): """Given a state, assert it against the current state.""" current_state_data = netinfo.show() current_state = statelib.State(current_state_data) current_state.filter(desired_state_data) current_state.normalize() full_desired_state = statelib.State(current_state.state) full_desired_state.update(desired_state_data) full_desired_state.remove_absent_entries() full_desired_state.normalize() assert full_desired_state.state == current_state.state
def _apply_ifaces_state( desired_state, verify_change, commit, rollback_timeout ): current_state = state.State(netinfo.show()) desired_state.sanitize_ethernet(current_state) desired_state.sanitize_dynamic_ip() desired_state.merge_routes(current_state) desired_state.merge_dns(current_state) metadata.generate_ifaces_metadata(desired_state, current_state) validator.validate_interfaces_state(desired_state, current_state) validator.validate_routes(desired_state, current_state) new_interfaces = _list_new_interfaces(desired_state, current_state) try: with nm.checkpoint.CheckPoint( autodestroy=commit, timeout=rollback_timeout ) as checkpoint: with _setup_providers(): ifaces2add, ifaces_add_configs = _add_interfaces( new_interfaces, desired_state ) state2edit = _create_editable_desired_state( desired_state, current_state, new_interfaces ) ifaces2edit, ifaces_edit_configs = _edit_interfaces(state2edit) nm.applier.set_ifaces_admin_state( ifaces2add + ifaces2edit, con_profiles=ifaces_add_configs + ifaces_edit_configs, ) _disable_ipv6(desired_state) if verify_change: _verify_change(desired_state) if not commit: return checkpoint except nm.checkpoint.NMCheckPointPermissionError: raise NmstatePermissionError('Error creating a check point') except nm.checkpoint.NMCheckPointCreationError: raise NmstateConflictError('Error creating a check point') except NmstateError: # Assume rollback occured, revert IPv6 stack state. # Checkpoint rollback is async, there is a need to wait for it to # finish before proceeding with other actions. # TODO: https://nmstate.atlassian.net/browse/NMSTATE-103 time.sleep(5) _disable_ipv6(current_state) raise
def test_warning_show(nm_mock, nm_dns_mock): current_config = { DNS.KEY: {DNS.RUNNING: {}, DNS.CONFIG: {}}, ROUTES: {"config": [], "running": []}, RouteRule.KEY: {RouteRule.CONFIG: []}, INTERFACES: [ { "name": "foo", "type": "unknown", "state": "up", "ipv4": {InterfaceIPv4.ENABLED: False}, "ipv6": {InterfaceIPv6.ENABLED: False}, } ], } current_iface0 = current_config[INTERFACES][0] nm_mock.device.list_devices.return_value = ["one-item"] nm_mock.translator.Nm2Api.get_common_device_info.return_value = ( current_iface0 ) nm_mock.bond.is_bond_type_id.return_value = False nm_mock.ipv4.get_info.return_value = current_iface0["ipv4"] nm_mock.ipv6.get_info.return_value = current_iface0["ipv6"] nm_mock.ipv4.get_route_running.return_value = [] nm_mock.ipv4.get_route_config.return_value = [] nm_mock.ipv6.get_route_running.return_value = [] nm_mock.ipv6.get_route_config.return_value = [] nm_dns_mock.get_running.return_value = current_config[DNS.KEY][DNS.RUNNING] nm_dns_mock.get_config.return_value = current_config[DNS.KEY][DNS.CONFIG] with pytest.warns(FutureWarning) as record: netinfo.show(None) assert len(record) == 1 assert "'include_status_data'" in record[0].message.args[0]
def test_remove_dns_config(): dns_config = { DNS.SERVER: [IPV6_DNS_NAMESERVERS[0], IPV4_DNS_NAMESERVERS[0]], DNS.SEARCH: [], } desired_state = { Interface.KEY: _get_test_iface_states(), Route.KEY: {Route.CONFIG: _gen_default_gateway_route()}, DNS.KEY: {DNS.CONFIG: dns_config}, } netapplier.apply(desired_state) netapplier.apply({Interface.KEY: [], DNS.KEY: {DNS.CONFIG: {}}}) current_state = netinfo.show() dns_config = {DNS.SERVER: [], DNS.SEARCH: []} assert dns_config == current_state[DNS.KEY][DNS.CONFIG]
def test_netinfo_show_generic_iface(nm_mock, nm_dns_mock): current_config = { DNS.KEY: { DNS.RUNNING: {}, DNS.CONFIG: {} }, ROUTES: { "config": [], "running": [] }, RouteRule.KEY: { RouteRule.CONFIG: [] }, INTERFACES: [{ "name": "foo", "type": "unknown", "state": "up", "ipv4": { InterfaceIPv4.ENABLED: False }, "ipv6": { InterfaceIPv6.ENABLED: False }, }], } current_iface0 = current_config[INTERFACES][0] nm_mock.device.list_devices.return_value = ["one-item"] nm_mock.translator.Nm2Api.get_common_device_info.return_value = ( current_iface0) nm_mock.bond.is_bond_type_id.return_value = False nm_mock.ipv4.get_info.return_value = current_iface0["ipv4"] nm_mock.ipv6.get_info.return_value = current_iface0["ipv6"] nm_mock.ipv4.get_route_running.return_value = [] nm_mock.ipv4.get_route_config.return_value = [] nm_mock.ipv6.get_route_running.return_value = [] nm_mock.ipv6.get_route_config.return_value = [] nm_dns_mock.get_running.return_value = current_config[DNS.KEY][DNS.RUNNING] nm_dns_mock.get_config.return_value = current_config[DNS.KEY][DNS.CONFIG] report = netinfo.show() assert current_config == report
def filter_current_state(desired_state): """ Given the desired state, return a filtered version of the current state which includes only entities such as interfaces that have been mentioned in the desired state. In case there are no entities for filtering, all are reported. """ current_state = netinfo.show() desired_iface_names = {ifstate['name'] for ifstate in desired_state[INTERFACES]} if not desired_iface_names: return current_state filtered_iface_current_state = [ ifstate for ifstate in current_state[INTERFACES] if ifstate['name'] in desired_iface_names ] return {INTERFACES: filtered_iface_current_state}
def test_netinfo_show(show_with_plugins_mock, plugin_context_mock): current_config = { DNS.KEY: {DNS.RUNNING: {}, DNS.CONFIG: {}}, Route.KEY: {Route.CONFIG: [], Route.RUNNING: []}, RouteRule.KEY: {RouteRule.CONFIG: []}, Interface.KEY: [ { Interface.NAME: "foo", Interface.TYPE: InterfaceType.UNKNOWN, Interface.STATE: InterfaceState.UP, Interface.IPV4: {InterfaceIPv4.ENABLED: False}, Interface.IPV6: {InterfaceIPv6.ENABLED: False}, } ], } show_with_plugins_mock.return_value = current_config report = netinfo.show() assert current_config == report