Example #1
0
def test_edit_existing_ifaces_with_profile(nm_device_mock, nm_connection_mock):
    con_profiles = [mock.MagicMock(), mock.MagicMock()]

    nm.applier.edit_existing_ifaces(con_profiles)

    nm_connection_mock.commit_profile.assert_has_calls(
        [mock.call(con_profiles[0]), mock.call(con_profiles[1])])
Example #2
0
def test_create_setting_with_link_local_addresses(NM_mock):
    config = {
        'enabled':
        True,
        'address': [
            {
                'ip': IPV6_LINK_LOCAL_ADDRESS1,
                'prefix-length': 64
            },
            {
                'ip': IPV6_ADDRESS1,
                'prefix-length': 64
            },
        ],
    }
    ipv6_setting = nm.ipv6.create_setting(config=config, base_con_profile=None)

    assert (
        ipv6_setting.props.method == NM_mock.SETTING_IP6_CONFIG_METHOD_MANUAL)
    NM_mock.IPAddress.new.assert_has_calls([
        mock.call(nm.ipv6.socket.AF_INET6, config['address'][1]['ip'],
                  config['address'][1]['prefix-length'])
    ])
    NM_mock.SettingIP6Config.new.return_value.add_address.assert_has_calls(
        [mock.call(NM_mock.IPAddress.new.return_value)])
Example #3
0
def test_create_setting_with_static_addresses(NM_mock):
    config = {
        'enabled':
        True,
        'address': [
            {
                'ip': 'fd12:3456:789a:1::1',
                'prefix-length': 24
            },
            {
                'ip': 'fd12:3456:789a:2::1',
                'prefix-length': 24
            },
        ],
    }
    ipv6_setting = nm.ipv6.create_setting(config=config, base_con_profile=None)

    assert (
        ipv6_setting.props.method == NM_mock.SETTING_IP6_CONFIG_METHOD_MANUAL)
    NM_mock.IPAddress.new.assert_has_calls([
        mock.call(nm.ipv6.socket.AF_INET6, config['address'][0]['ip'],
                  config['address'][0]['prefix-length']),
        mock.call(nm.ipv6.socket.AF_INET6, config['address'][1]['ip'],
                  config['address'][1]['prefix-length'])
    ])
    NM_mock.SettingIP6Config.new.return_value.add_address.assert_has_calls([
        mock.call(NM_mock.IPAddress.new.return_value),
        mock.call(NM_mock.IPAddress.new.return_value)
    ])
Example #4
0
    def test_set_bond_and_its_slaves_admin_state_up(self,
                                                    nm_device_mock,
                                                    nm_bond_mock):
        ifaces_desired_state = [
            {
                'name': 'bond0',
                'type': 'bond',
                'state': 'up',
                'link-aggregation': {
                    'mode': '802.3ad',
                    'slaves': ['eth0']
                },
            },
            {
                'name': 'eth0',
                'type': 'ethernet',
                'state': 'up',
            }
        ]

        nm_device_mock.get_device_by_name = lambda devname: devname
        bond = ifaces_desired_state[0]['name']
        slaves = ifaces_desired_state[0]['link-aggregation']['slaves']
        nm_bond_mock.BOND_TYPE = nm.bond.BOND_TYPE
        nm_bond_mock.get_slaves.return_value = slaves

        nm.applier.set_ifaces_admin_state(ifaces_desired_state)

        expected_calls = [
            mock.call(bond),
            mock.call(slaves[0])
        ]
        actual_calls = nm_device_mock.activate.mock_calls
        assert sorted(expected_calls) == sorted(actual_calls)
Example #5
0
def test_create_setting_with_static_addresses(NM_mock):
    config = {
        'enabled':
        True,
        'address': [
            {
                'ip': '10.10.10.1',
                'prefix-length': 24
            },
            {
                'ip': '10.10.20.1',
                'prefix-length': 24
            },
        ],
    }
    ipv4_setting = nm.ipv4.create_setting(config=config)

    assert (
        ipv4_setting.props.method == NM_mock.SETTING_IP4_CONFIG_METHOD_MANUAL)
    NM_mock.IPAddress.new.assert_has_calls([
        mock.call(nm.ipv4.socket.AF_INET, config['address'][0]['ip'],
                  config['address'][0]['prefix-length']),
        mock.call(nm.ipv4.socket.AF_INET, config['address'][1]['ip'],
                  config['address'][1]['prefix-length'])
    ])
    NM_mock.SettingIP4Config.new.return_value.add_address.assert_has_calls([
        mock.call(NM_mock.IPAddress.new.return_value),
        mock.call(NM_mock.IPAddress.new.return_value)
    ])
Example #6
0
def test_create_setting_with_link_local_addresses(NM_mock):
    config = {
        InterfaceIPv6.ENABLED:
        True,
        InterfaceIPv6.ADDRESS: [
            {
                InterfaceIPv6.ADDRESS_IP: IPV6_LINK_LOCAL_ADDRESS1,
                InterfaceIPv6.ADDRESS_PREFIX_LENGTH: 64,
            },
            {
                InterfaceIPv6.ADDRESS_IP: IPV6_ADDRESS1,
                InterfaceIPv6.ADDRESS_PREFIX_LENGTH: 64,
            },
        ],
    }
    ipv6_setting = nm.ipv6.create_setting(config=config, base_con_profile=None)

    assert (
        ipv6_setting.props.method == NM_mock.SETTING_IP6_CONFIG_METHOD_MANUAL)
    NM_mock.IPAddress.new.assert_has_calls([
        mock.call(
            nm.ipv6.socket.AF_INET6,
            config[InterfaceIPv6.ADDRESS][1][InterfaceIPv6.ADDRESS_IP],
            config[InterfaceIPv6.ADDRESS][1][
                InterfaceIPv6.ADDRESS_PREFIX_LENGTH],
        )
    ])
    NM_mock.SettingIP6Config.new.return_value.add_address.assert_has_calls(
        [mock.call(NM_mock.IPAddress.new.return_value)])
Example #7
0
def test_create_profile(NM_mock):
    settings = [11, 22]
    profile = nm.connection.create_profile(settings)

    con_profile_mock = NM_mock.SimpleConnection.new.return_value

    con_profile_mock.add_setting.assert_has_calls(
        [mock.call(settings[0]), mock.call(settings[1])])
    assert con_profile_mock == profile
Example #8
0
def test_create_new_ifaces(nm_connection_mock):
    con_profiles = ['profile1', 'profile2']

    nm.applier.create_new_ifaces(con_profiles)

    nm_connection_mock.add_profile.assert_has_calls([
        mock.call(con_profiles[0], save_to_disk=True),
        mock.call(con_profiles[1], save_to_disk=True)
    ])
Example #9
0
def test_prepare_new_ifaces_configuration(nm_bond_mock,
                                          nm_connection_mock,
                                          nm_ipv4_mock,
                                          nm_ipv6_mock,
                                          nm_ovs_mock):
    nm_ovs_mock.translate_bridge_options.return_value = {}
    nm_ovs_mock.translate_port_options.return_value = {}

    ifaces_desired_state = [
        {
            'name': 'eth0',
            'type': 'ethernet',
            'state': 'up',
            '_master': 'bond99',
            '_master_type': 'bond'
        },
        {
            'name': 'bond99',
            'type': 'bond',
            'state': 'up',
            'link-aggregation': {
                'mode': 'balance-rr',
                'slaves': ['eth0'],
                'options': {
                    'miimon': 120
                }
            }
        }
    ]

    nm.applier.prepare_new_ifaces_configuration(ifaces_desired_state)

    con_setting = nm_connection_mock.create_setting.return_value
    nm_connection_mock.set_master_setting.assert_has_calls(
        [
            mock.call(con_setting, 'bond99', 'bond'),
            mock.call(con_setting, None, None)
        ],
        any_order=True
    )
    nm_connection_mock.create_profile.assert_has_calls(
        [
            mock.call([
                nm_ipv4_mock.create_setting.return_value,
                nm_ipv6_mock.create_setting.return_value,
                con_setting,
            ]),
            mock.call([
                nm_ipv4_mock.create_setting.return_value,
                nm_ipv6_mock.create_setting.return_value,
                con_setting,
                nm_bond_mock.create_setting.return_value,
            ])
        ]
    )
Example #10
0
def test_edit_existing_ifaces_without_profile(nm_device_mock,
                                              nm_connection_mock):
    con_profiles = [mock.MagicMock(), mock.MagicMock()]
    nm_connection_mock.get_device_connection.return_value = None

    nm.applier.edit_existing_ifaces(con_profiles)

    nm_connection_mock.add_profile.assert_has_calls([
        mock.call(con_profiles[0], save_to_disk=True),
        mock.call(con_profiles[1], save_to_disk=True)
    ])
Example #11
0
def test_edit_existing_ifaces_with_profile(nm_device_mock, nm_connection_mock):
    con_profiles = [mock.MagicMock(), mock.MagicMock()]

    nm.applier.edit_existing_ifaces(con_profiles)

    nm_connection_mock.commit_profile.assert_has_calls([
        mock.call(con_profiles[0],
                  nmdev=nm_device_mock.get_device_by_name.return_value),
        mock.call(con_profiles[1],
                  nmdev=nm_device_mock.get_device_by_name.return_value)
    ])
Example #12
0
def test_create_setting(NM_mock):
    bond_setting_mock = NM_mock.SettingBond.new.return_value
    bond_setting_mock.add_option.return_value = True

    options = {'mode': 'balance-rr', 'miimon': '100'}
    nm.bond.create_setting(options)

    bond_setting_mock.add_option.assert_has_calls(
        [mock.call('mode', 'balance-rr'),
         mock.call('miimon', '100')],
        any_order=True,
    )
Example #13
0
def test_prepare_edited_ifaces_configuration(nm_device_mock,
                                             nm_connection_mock,
                                             nm_ipv4_mock,
                                             nm_ipv6_mock,
                                             nm_ovs_mock):
    nm_ovs_mock.translate_bridge_options.return_value = {}
    nm_ovs_mock.translate_port_options.return_value = {}

    ifaces_desired_state = [
        {
            'name': 'eth0',
            'type': 'ethernet',
            'state': 'up',
        }
    ]
    cons = nm.applier.prepare_edited_ifaces_configuration(ifaces_desired_state)

    assert len(cons) == 1

    nm_connection_mock.update_profile.assert_has_calls(
        [
            mock.call(nm_connection_mock.get_device_connection.return_value,
                      nm_connection_mock.create_profile.return_value),
        ]
    )
Example #14
0
def test_create_new_ifaces(con_profile_mock):
    con_profiles = [con_profile_mock(), con_profile_mock()]

    nm.applier.create_new_ifaces(con_profiles)

    for con_profile in con_profiles:
        con_profile.add.assert_has_calls([mock.call(save_to_disk=True)])
Example #15
0
def test_edit_existing_ifaces_with_profile(con_profile_mock, nm_device_mock):
    con_profiles = [con_profile_mock(), con_profile_mock()]

    nm.applier.edit_existing_ifaces(con_profiles)

    for con_profile in con_profiles:
        con_profile.commit.assert_has_calls(
            [mock.call(nmdev=nm_device_mock.get_device_by_name.return_value)])
Example #16
0
def test_edit_existing_ifaces_without_profile(con_profile_mock,
                                              nm_device_mock):
    con_profiles = [mock.MagicMock(), mock.MagicMock()]
    con_profile_mock.return_value.profile = None

    nm.applier.edit_existing_ifaces(con_profiles)

    for con_profile in con_profiles:
        con_profile.add.assert_has_calls([mock.call(save_to_disk=True)])
Example #17
0
def test_create_setting_with_static_addresses(NM_mock):
    config = {
        InterfaceIPv4.ENABLED: True,
        InterfaceIPv4.ADDRESS: [
            {
                InterfaceIPv4.ADDRESS_IP: '10.10.10.1',
                InterfaceIPv4.ADDRESS_PREFIX_LENGTH: 24,
            },
            {
                InterfaceIPv4.ADDRESS_IP: '10.10.20.1',
                InterfaceIPv4.ADDRESS_PREFIX_LENGTH: 24,
            },
        ],
    }
    ipv4_setting = nm.ipv4.create_setting(config=config, base_con_profile=None)

    assert (
        ipv4_setting.props.method == NM_mock.SETTING_IP4_CONFIG_METHOD_MANUAL
    )
    NM_mock.IPAddress.new.assert_has_calls(
        [
            mock.call(
                nm.ipv4.socket.AF_INET,
                config[InterfaceIPv4.ADDRESS][0][InterfaceIPv4.ADDRESS_IP],
                config[InterfaceIPv4.ADDRESS][0][
                    InterfaceIPv4.ADDRESS_PREFIX_LENGTH
                ],
            ),
            mock.call(
                nm.ipv4.socket.AF_INET,
                config[InterfaceIPv4.ADDRESS][1][InterfaceIPv4.ADDRESS_IP],
                config[InterfaceIPv4.ADDRESS][1][
                    InterfaceIPv4.ADDRESS_PREFIX_LENGTH
                ],
            ),
        ]
    )
    NM_mock.SettingIP4Config.new.return_value.add_address.assert_has_calls(
        [
            mock.call(NM_mock.IPAddress.new.return_value),
            mock.call(NM_mock.IPAddress.new.return_value),
        ]
    )
Example #18
0
    def test_set_bond_and_its_slaves_admin_state_up(self, nm_device_mock,
                                                    nm_bond_mock):
        ifaces_desired_state = [
            {
                'name': 'bond0',
                'type': 'bond',
                'state': 'up',
                'link-aggregation': {
                    'mode': '802.3ad',
                    'slaves': ['eth0']
                },
            },
            {
                'name': 'eth0',
                'type': 'ethernet',
                'state': 'up'
            },
        ]

        nm_device_mock.get_device_by_name = lambda devname: devname
        bond = ifaces_desired_state[0]['name']
        slaves = ifaces_desired_state[0]['link-aggregation']['slaves']
        nm_bond_mock.BOND_TYPE = nm.bond.BOND_TYPE
        nm_bond_mock.get_slaves.return_value = slaves

        bond_con_profile = mock.MagicMock()
        bond_con_profile.devname = ifaces_desired_state[0]['name']
        slave_con_profile = mock.MagicMock()
        slave_con_profile.devname = ifaces_desired_state[1]['name']

        nm.applier.set_ifaces_admin_state(
            ifaces_desired_state, [bond_con_profile, slave_con_profile])

        expected_calls = [
            mock.call(bond, bond_con_profile.profile),
            mock.call(slaves[0], slave_con_profile.profile),
        ]
        actual_calls = nm_device_mock.modify.mock_calls
        assert sorted(expected_calls) == sorted(actual_calls)