def test_static_ip_with_routes_switch_back_to_dynamic( dhcpcli_up_with_static_ip_and_route, ): desired_state = dhcpcli_up_with_static_ip_and_route desired_state.pop(RT.KEY) dhcp_cli_desired_state = desired_state[Interface.KEY][0] dhcp_cli_desired_state[Interface.STATE] = InterfaceState.UP dhcp_cli_desired_state[Interface.IPV4] = _create_ipv4_state(enabled=True, dhcp=True) dhcp_cli_desired_state[Interface.IPV6] = _create_ipv6_state(enabled=True, dhcp=True, autoconf=True) libnmstate.apply(desired_state) assertlib.assert_state(desired_state) assert _poll(_has_ipv4_dhcp_nameserver) assert _poll(_has_ipv4_dhcp_gateway) assert _poll(_has_ipv4_classless_route) assert _poll(_has_dhcpv4_addr) assert _poll(_has_dhcpv6_addr) assert _poll(_has_ipv6_auto_gateway) assert _poll(_has_ipv6_auto_extra_route) assert _poll(_has_ipv6_auto_nameserver) current_config_routes = [ route for route in libnmstate.show()[RT.KEY][RT.CONFIG] if route[RT.NEXT_HOP_INTERFACE] == DHCP_CLI_NIC ] assert not current_config_routes
def eth1_with_dhcp6_no_dhcp_server(): # Cannot depend on eth1_up fixture as the reproducer requires the # veth profile been created with DHCPv6 enabled. iface_state = { Interface.NAME: ETH1, Interface.TYPE: InterfaceType.ETHERNET, Interface.STATE: InterfaceState.UP, } iface_state[Interface.IPV4] = _create_ipv4_state(enabled=False) iface_state[Interface.IPV6] = _create_ipv6_state(enabled=True, dhcp=True, autoconf=False) libnmstate.apply({Interface.KEY: [iface_state]}) try: yield iface_state finally: libnmstate.apply( { Interface.KEY: [{ Interface.NAME: ETH1, Interface.STATE: InterfaceState.ABSENT, }] }, verify_change=False, )
def test_ipv6_autoconf_only(dhcpcli_up): desired_state = dhcpcli_up dhcp_cli_desired_state = desired_state[Interface.KEY][0] dhcp_cli_desired_state[Interface.IPV6] = _create_ipv6_state(enabled=True, autoconf=True) libnmstate.apply(desired_state)
def dhcpcli_up_with_static_ip_and_route(dhcpcli_up_with_static_ip): desired_state = dhcpcli_up_with_static_ip desired_state[RT.KEY] = { RT.CONFIG: [ { RT.DESTINATION: IPV4_DEFAULT_GATEWAY, RT.NEXT_HOP_ADDRESS: DHCP_SRV_IP4, RT.NEXT_HOP_INTERFACE: DHCP_CLI_NIC, }, { RT.DESTINATION: IPV4_NETWORK1, RT.NEXT_HOP_ADDRESS: DHCP_SRV_IP4, RT.NEXT_HOP_INTERFACE: DHCP_CLI_NIC, }, { RT.DESTINATION: IPV6_DEFAULT_GATEWAY, RT.NEXT_HOP_ADDRESS: IPV6_ADDRESS3, RT.NEXT_HOP_INTERFACE: DHCP_CLI_NIC, }, { RT.DESTINATION: IPV6_NETWORK1, RT.NEXT_HOP_ADDRESS: IPV6_ADDRESS3, RT.NEXT_HOP_INTERFACE: DHCP_CLI_NIC, }, ] } libnmstate.apply(desired_state) yield desired_state
def test_ipv6_dhcp_off_and_option_on(dhcpcli_up): """ AUTO_ROUTES, AUTO_DNS and AUTO_GATEWAY should be silently ignored when DHCP is disabled. """ desired_state = dhcpcli_up dhcp_cli_desired_state = desired_state[Interface.KEY][0] dhcp_cli_desired_state[Interface.STATE] = InterfaceState.UP ipv6_state = _create_ipv6_state( enabled=True, dhcp=False, autoconf=False, auto_dns=False, auto_gateway=False, auto_routes=False, ) ipv6_state.pop(InterfaceIPv6.ENABLED) dhcp_cli_desired_state[Interface.IPV6] = ipv6_state libnmstate.apply(desired_state) current_state = statelib.show_only((DHCP_CLI_NIC, )) dhcp_cli_current_state = current_state[Interface.KEY][0] ipv6_current_state = dhcp_cli_current_state[Interface.IPV6] assert not ipv6_current_state[InterfaceIPv6.DHCP] assert InterfaceIPv6.AUTO_ROUTES not in ipv6_current_state assert InterfaceIPv6.AUTO_DNS not in ipv6_current_state assert InterfaceIPv6.AUTO_GATEWAY not in ipv6_current_state assert not _poll(_has_ipv6_auto_gateway) assert not _has_ipv6_auto_extra_route() assert not _has_ipv6_auto_nameserver()
def test_ipv6_dhcp_switch_on_to_off(dhcpcli_up): desired_state = dhcpcli_up dhcp_cli_desired_state = desired_state[Interface.KEY][0] dhcp_cli_desired_state[Interface.STATE] = InterfaceState.UP dhcp_cli_desired_state[Interface.IPV6] = _create_ipv6_state(enabled=True, dhcp=True, autoconf=True) libnmstate.apply(desired_state) assertlib.assert_state(desired_state) assert _poll(_has_ipv6_auto_gateway) assert _poll(_has_ipv6_auto_extra_route) assert _poll(_has_ipv6_auto_nameserver) # disable dhcp and make sure dns, route, gone. desired_state = statelib.show_only((DHCP_CLI_NIC, )) dhcp_cli_desired_state = desired_state[Interface.KEY][0] dhcp_cli_desired_state[Interface.STATE] = InterfaceState.UP dhcp_cli_desired_state[Interface.IPV6] = _create_ipv6_state(enabled=True) libnmstate.apply(desired_state) assertlib.assert_state(desired_state) assert not _poll(_has_ipv6_auto_gateway) assert not _has_ipv6_auto_extra_route() assert not _has_ipv6_auto_nameserver()
def test_ipv6_dhcp_switch_on_to_off(dhcpcli_up): desired_state = dhcpcli_up dhcp_cli_desired_state = desired_state[INTERFACES][0] dhcp_cli_desired_state[Interface.STATE] = InterfaceState.UP dhcp_cli_desired_state[Interface.IPV6] = create_ipv6_state( enabled=True, dhcp=True, autoconf=True ) libnmstate.apply(desired_state) assertlib.assert_state(desired_state) time.sleep(5) # libnm does not wait on ipv6-ra or DHCPv6. assert _has_ipv6_auto_gateway() assert _has_ipv6_auto_extra_route() assert _has_ipv6_auto_nameserver() # disable dhcp and make sure dns, route, gone. desired_state = statelib.show_only((DHCP_CLI_NIC,)) dhcp_cli_desired_state = desired_state[INTERFACES][0] dhcp_cli_desired_state[Interface.STATE] = InterfaceState.UP dhcp_cli_desired_state[Interface.IPV6] = create_ipv6_state(enabled=True) libnmstate.apply(desired_state) assertlib.assert_state(desired_state) assert not _has_ipv6_auto_gateway() assert not _has_ipv6_auto_extra_route() assert not _has_ipv6_auto_nameserver()
def edit(args): state = _filter_state(libnmstate.show(), args.only) if not state[Interface.KEY]: sys.stderr.write("ERROR: No such interface\n") return os.EX_USAGE pretty_state = PrettyState(state) if args.yaml: suffix = ".yaml" txtstate = pretty_state.yaml else: suffix = ".json" txtstate = pretty_state.json new_state = _get_edited_state(txtstate, suffix, args.yaml) if not new_state: return os.EX_DATAERR print("Applying the following state: ") print_state(new_state, use_yaml=args.yaml) libnmstate.apply( new_state, verify_change=args.verify, save_to_disk=args.save_to_disk )
def test_ipv4_dhcp_switch_on_to_off(dhcpcli_up): desired_state = dhcpcli_up dhcp_cli_desired_state = desired_state[INTERFACES][0] dhcp_cli_desired_state[Interface.STATE] = InterfaceState.UP dhcp_cli_desired_state[Interface.IPV4] = create_ipv4_state( enabled=True, dhcp=True ) libnmstate.apply(desired_state) assertlib.assert_state(desired_state) time.sleep(5) # wait to get resolv.conf updated assert _has_ipv4_dhcp_nameserver() assert _has_ipv4_dhcp_gateway() assert _has_ipv4_classless_route() # disable dhcp and make sure dns, route, gone. desired_state = statelib.show_only((DHCP_CLI_NIC,)) dhcp_cli_desired_state = desired_state[INTERFACES][0] dhcp_cli_desired_state[Interface.STATE] = InterfaceState.UP dhcp_cli_desired_state[Interface.IPV4] = create_ipv4_state(enabled=True) libnmstate.apply(desired_state) assertlib.assert_state(desired_state) assert not _has_ipv4_dhcp_nameserver() assert not _has_ipv4_dhcp_gateway() assert not _has_ipv4_classless_route()
def test_ipv4_dhcp_off_and_option_on(dhcpcli_up): """ AUTO_ROUTES, AUTO_DNS and AUTO_GATEWAY should be silently ignored when DHCP is disabled. """ desired_state = dhcpcli_up dhcp_cli_desired_state = desired_state[INTERFACES][0] dhcp_cli_desired_state[Interface.STATE] = InterfaceState.UP ipv4_state = create_ipv4_state( enabled=True, dhcp=False, auto_dns=False, auto_gateway=False, auto_routes=False, ) ipv4_state.pop(InterfaceIPv4.ENABLED) dhcp_cli_desired_state[Interface.IPV4] = ipv4_state libnmstate.apply(desired_state) dhcp_cli_current_state = statelib.show_only((DHCP_CLI_NIC,))[INTERFACES][0] ipv4_current_state = dhcp_cli_current_state[Interface.IPV4] assert not ipv4_current_state[InterfaceIPv4.DHCP] assert InterfaceIPv4.AUTO_ROUTES not in ipv4_current_state assert InterfaceIPv4.AUTO_DNS not in ipv4_current_state assert InterfaceIPv4.AUTO_GATEWAY not in ipv4_current_state assert not _has_ipv4_dhcp_nameserver() assert not _has_ipv4_dhcp_gateway() assert not _has_ipv4_classless_route()
def test_dhcp_with_addresses(dhcpcli_up): ipv4_state = create_ipv4_state(enabled=True, dhcp=True) ipv4_state[InterfaceIPv4.ADDRESS] = [ create_ipv4_address_state(IPV4_ADDRESS1, 24), create_ipv4_address_state(IPV4_ADDRESS2, 24), ] ipv6_state = create_ipv6_state(enabled=True, dhcp=True, autoconf=True) ipv6_state[InterfaceIPv6.ADDRESS] = [ create_ipv6_address_state(IPV6_ADDRESS1, 64), create_ipv6_address_state(IPV6_ADDRESS2, 64), ] desired_state = { INTERFACES: [ { Interface.NAME: DHCP_CLI_NIC, Interface.STATE: InterfaceState.UP, Interface.IPV4: ipv4_state, Interface.IPV6: ipv6_state, } ] } libnmstate.apply(desired_state) assertlib.assert_state(desired_state)
def test_dynamic_ip_with_static_dns(dhcpcli_up_with_dns_cleanup, clean_state): iface_state = { Interface.NAME: DHCP_CLI_NIC, Interface.STATE: InterfaceState.UP, Interface.IPV4: _create_ipv4_state(enabled=True, dhcp=True, auto_dns=False), Interface.IPV6: _create_ipv6_state(enabled=True, dhcp=True, autoconf=True, auto_dns=False), } dns_config = { DNS.CONFIG: { DNS.SERVER: [IPV6_DNS_NAMESERVER, IPV4_DNS_NAMESERVER], DNS.SEARCH: EXAMPLE_SEARCHES, } } desired_state = {Interface.KEY: [iface_state], DNS.KEY: dns_config} libnmstate.apply(desired_state) assertlib.assert_state_match(desired_state) assert _poll(_has_ipv4_dhcp_gateway) assert _poll(_has_ipv6_auto_gateway) assert _poll(_has_dhcpv4_addr) assert _poll(_has_dhcpv6_addr) assert not _has_ipv4_dhcp_nameserver() assert not _has_ipv6_auto_nameserver() new_state = libnmstate.show() assert dns_config[DNS.CONFIG] == new_state[DNS.KEY][DNS.CONFIG] assert dns_config[DNS.CONFIG] == new_state[DNS.KEY][DNS.RUNNING]
def iface_with_dynamic_ip_up(ifname): desired_state = { Interface.KEY: [{ Interface.NAME: ifname, Interface.STATE: InterfaceState.UP, Interface.IPV4: _create_ipv4_state(enabled=True, dhcp=True), Interface.IPV6: _create_ipv6_state(enabled=True, dhcp=True, autoconf=True), }] } try: libnmstate.apply(desired_state) assert _poll(_has_ipv4_dhcp_gateway) assert _poll(_has_dhcpv4_addr) assert _poll(_has_ipv6_auto_gateway) assert _poll(_has_dhcpv6_addr) yield statelib.show_only((ifname, )) finally: libnmstate.apply({ Interface.KEY: [{ Interface.NAME: ifname, Interface.STATE: InterfaceState.ABSENT, }] })
def iface_with_dynamic_ip_up(ifname, delay_state_time=0): desired_state = { Interface.KEY: [ { Interface.NAME: ifname, Interface.STATE: InterfaceState.UP, Interface.IPV4: create_ipv4_state(enabled=True, dhcp=True), Interface.IPV6: create_ipv6_state( enabled=True, dhcp=True, autoconf=True ), } ] } try: libnmstate.apply(desired_state) if delay_state_time: time.sleep(delay_state_time) yield statelib.show_only((ifname,)) finally: libnmstate.apply( { Interface.KEY: [ { Interface.NAME: ifname, Interface.STATE: InterfaceState.DOWN, } ] } )
def test_ipv6_dhcp_only(dhcpcli_up): desired_state = dhcpcli_up dhcp_cli_desired_state = desired_state[INTERFACES][0] dhcp_cli_desired_state[Interface.STATE] = InterfaceState.UP dhcp_cli_desired_state[Interface.IPV6] = create_ipv6_state( enabled=True, dhcp=True, autoconf=False ) libnmstate.apply(desired_state) assertlib.assert_state(desired_state) time.sleep(5) # libnm does not wait on ipv6-ra or DHCPv6. current_state = statelib.show_only((DHCP_CLI_NIC,)) dhcp_cli_current_state = current_state[INTERFACES][0] has_dhcp_ip_addr = False for addr in dhcp_cli_current_state[Interface.IPV6][InterfaceIPv6.ADDRESS]: if ( addr[InterfaceIPv6.ADDRESS_PREFIX_LENGTH] == 128 and DHCP_SRV_IP6_PREFIX in addr[InterfaceIPv6.ADDRESS_IP] ): has_dhcp_ip_addr = True break assert has_dhcp_ip_addr assert not _has_ipv6_auto_gateway() # DHCPv6 does not provide routes assert not _has_ipv6_auto_extra_route() # DHCPv6 does not provide routes assert _has_ipv6_auto_nameserver()
def test_slave_ipaddr_learned_via_dhcp_added_as_static_to_linux_bridge( dhcpcli_up): desired_state = { INTERFACES: [{ 'name': 'dhcpcli', 'type': 'ethernet', 'state': 'up', 'ipv4': { InterfaceIPv4.ENABLED: True, InterfaceIPv4.DHCP: True, }, }] } libnmstate.apply(desired_state) current_state = statelib.show_only(('dhcpcli', )) client_current_state = current_state[INTERFACES][0] dhcpcli_ip = client_current_state['ipv4'][InterfaceIPv4.ADDRESS] bridge_desired_state = { INTERFACES: [ { 'name': 'linux-br0', 'type': 'linux-bridge', 'state': 'up', 'ipv4': { InterfaceIPv4.ENABLED: True, InterfaceIPv4.DHCP: False, InterfaceIPv4.ADDRESS: dhcpcli_ip, }, 'bridge': { 'options': {}, 'port': [{ 'name': 'dhcpcli', 'stp-hairpin-mode': False, 'stp-path-cost': 100, 'stp-priority': 32, }], }, }, { 'name': 'dhcpcli', 'type': 'ethernet', 'state': 'up', 'ipv4': { InterfaceIPv4.ENABLED: False, InterfaceIPv4.DHCP: False, }, 'ipv6': { InterfaceIPv6.ENABLED: False, InterfaceIPv6.DHCP: False, }, }, ] } libnmstate.apply(bridge_desired_state) assertlib.assert_state(bridge_desired_state)
def test_slave_ipaddr_learned_via_dhcp_added_as_static_to_linux_bridge( dhcp_env, setup_remove_dhcpcli): desired_state = { INTERFACES: [{ 'name': 'dhcpcli', 'type': 'ethernet', 'state': 'up', 'ipv4': { 'enabled': True, 'dhcp': True }, }] } libnmstate.apply(desired_state) current_state = statelib.show_only(('dhcpcli', )) client_current_state = current_state[INTERFACES][0] dhcpcli_ip = client_current_state['ipv4']['address'] bridge_desired_state = { INTERFACES: [ { 'name': 'linux-br0', 'type': 'linux-bridge', 'state': 'up', 'ipv4': { 'enabled': True, 'dhcp': False, 'address': dhcpcli_ip, }, 'bridge': { 'options': {}, 'port': [{ 'name': 'dhcpcli', 'stp-hairpin-mode': False, 'stp-path-cost': 100, 'stp-priority': 32, }], }, }, { 'name': 'dhcpcli', 'type': 'ethernet', 'state': 'up', 'ipv4': { 'enabled': False, 'dhcp': False }, 'ipv6': { 'enabled': False, 'dhcp': False }, }, ] } libnmstate.apply(bridge_desired_state) assertlib.assert_state(bridge_desired_state)
def test_increase_more_than_jambo_iface_mtu(): desired_state = statelib.show_only(('eth1', )) eth1_desired_state = desired_state[INTERFACES][0] eth1_desired_state['mtu'] = 10000 libnmstate.apply(desired_state) assertlib.assert_state(desired_state)
def test_upper_limit_jambo_iface_mtu(): desired_state = statelib.show_only(('eth1', )) eth1_desired_state = desired_state[INTERFACES][0] eth1_desired_state['mtu'] = 9000 libnmstate.apply(desired_state) assertlib.assert_state(desired_state)
def test_decrease_to_ipv6_min_ethernet_frame_size_iface_mtu(): desired_state = statelib.show_only(('eth1', )) eth1_desired_state = desired_state[INTERFACES][0] eth1_desired_state['mtu'] = 1280 libnmstate.apply(desired_state) assertlib.assert_state(desired_state)
def test_memory_only_profile_absent_interface(): with dummy_interface(DUMMY0_IFNAME) as dstate: dstate[Interface.KEY][0][Interface.STATE] = InterfaceState.ABSENT libnmstate.apply(dstate, save_to_disk=False) assertlib.assert_absent(DUMMY0_IFNAME) assert _profile_exists(DUMMY_PROFILE_DIRECTORY + "dummy0.nmconnection") assertlib.assert_absent(DUMMY0_IFNAME)
def test_add_invalid_slave_ip_config(eth1_up): desired_state = eth1_up desired_state[Interface.KEY][0][Interface.IPV4][InterfaceIP.ENABLED] = True desired_state[Interface.KEY][0][Interface.IPV4][InterfaceIP.DHCP] = True with pytest.raises(NmstateValueError): with team_interface(TEAM0, slaves=("eth1", )) as state: desired_state[Interface.KEY].append(state[Interface.KEY][0]) libnmstate.apply(desired_state)
def test_increase_more_than_jambo_iface_mtu(): desired_state = statelib.show_only(("eth1",)) eth1_desired_state = desired_state[Interface.KEY][0] eth1_desired_state[Interface.MTU] = 10000 libnmstate.apply(desired_state) assertlib.assert_state(desired_state)
def test_upper_limit_jambo_iface_mtu(): desired_state = statelib.show_only(("eth1",)) eth1_desired_state = desired_state[Interface.KEY][0] eth1_desired_state[Interface.MTU] = 9000 libnmstate.apply(desired_state) assertlib.assert_state(desired_state)
def test_change_mtu_with_stable_link_up(eth1_up): desired_state = statelib.show_only(("eth1",)) eth1_desired_state = desired_state[Interface.KEY][0] eth1_desired_state[Interface.MTU] = 1900 libnmstate.apply(desired_state) assertlib.assert_state(desired_state)
def test_decrease_to_ipv6_min_ethernet_frame_size_iface_mtu(eth1_with_ipv6): desired_state = statelib.show_only(("eth1",)) eth1_desired_state = desired_state[Interface.KEY][0] eth1_desired_state[Interface.MTU] = 1280 libnmstate.apply(desired_state) assertlib.assert_state(desired_state)
def test_delete_existing_interface_inactive_profiles(eth1_up): with create_inactive_profile(eth1_up[Interface.KEY][0][Interface.NAME]): eth1_up[Interface.KEY][0][Interface.MTU] = 2000 libnmstate.apply(eth1_up) profile_exists = _profile_exists( ETH_PROFILE_DIRECTORY + "ifcfg-testProfile" ) assert not profile_exists
def test_ipv6_autoconf_only(dhcpcli_up): desired_state = dhcpcli_up dhcp_cli_desired_state = desired_state[INTERFACES][0] dhcp_cli_desired_state['ipv6'][InterfaceIPv6.ENABLED] = True dhcp_cli_desired_state['ipv6'][InterfaceIPv6.DHCP] = False dhcp_cli_desired_state['ipv6'][InterfaceIPv6.AUTOCONF] = True libnmstate.apply(desired_state)
def test_env_setup(): _logging_setup() old_state = libnmstate.show() _empty_net_state() _ethx_init() yield libnmstate.apply(old_state, verify_change=False) _diff_initial_state(old_state)
def vxlan_interfaces(*vxlans, create=True): setup_state = vxlans_up(vxlans) if create: libnmstate.apply(setup_state) try: yield setup_state finally: libnmstate.apply(vxlans_absent(vxlans), verify_change=False)