def test_macos_ipv6(self): mock_loopback_device_name = MagicMock( return_value=self.loopback_device_name) mock_is_linux = MagicMock(return_value=False) mock_is_macos = MagicMock(return_value=True) with patch('conductr_cli.host.loopback_device_name', mock_loopback_device_name), \ patch('conductr_cli.host.is_linux', mock_is_linux), \ patch('conductr_cli.host.is_macos', mock_is_macos): result = host.addr_alias_setup_instructions( self.addrs_ipv6, self.subnet_mask_ipv6) expected_result = strip_margin( """|Whoops. Network address aliases are required so that the sandbox can operate as a cluster of machines. | |Please run the following and then try your command again: | |sudo ifconfig ix0 alias 0000:0000:0000:0000:0000:ffff:c0a8:0101 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff |sudo ifconfig ix0 alias 0000:0000:0000:0000:0000:ffff:c0a8:0102 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff |""") self.assertEqual(expected_result, result) mock_loopback_device_name.assert_called_once_with() mock_is_linux.assert_called_once_with() mock_is_macos.assert_called_once_with()
def test_unknown_ipv6(self): mock_loopback_device_name = MagicMock( return_value=self.loopback_device_name) mock_is_linux = MagicMock(return_value=False) mock_is_macos = MagicMock(return_value=False) with patch('conductr_cli.host.loopback_device_name', mock_loopback_device_name), \ patch('conductr_cli.host.is_linux', mock_is_linux), \ patch('conductr_cli.host.is_macos', mock_is_macos): result = host.addr_alias_setup_instructions( self.addrs_ipv6, self.subnet_mask_ipv6) expected_result = 'Setup aliases for ::ffff:c0a8:101, ::ffff:c0a8:102 addresses with ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff subnet mask' self.assertEqual(expected_result, result) mock_loopback_device_name.assert_called_once_with() mock_is_linux.assert_called_once_with() mock_is_macos.assert_called_once_with()
def find_bind_addrs(nr_of_addrs, addr_range): """ Finds for the presence of address which can be bound to the sandbox given an address range, i.e. - Let's say 3 address aliases is required. - The address range is 192.168.128.0/24 These addresses requires setup using ifconfig as such (MacOS example): sudo ifconfig lo0 alias 192.168.128.1 255.255.255.0 sudo ifconfig lo0 alias 192.168.128.2 255.255.255.0 sudo ifconfig lo0 alias 192.168.128.3 255.255.255.0 This command will check if 192.168.128.1, 192.168.128.2, and 192.168.128.3 can be bound. The check is done by binding a socket to each of these address using a test port. If the number of required address is not present, provide the commands so the end user is able to copy-paste and execute these commands. :param nr_of_addrs: number of address aliases required :param addr_range: the range of address which is available to core and agent to bind to. The address is specified in the CIDR format, i.e. 192.168.128.0/24 """ addrs_to_bind = [] addrs_unavailable = [] for ip_addr in addr_range.hosts(): if host.can_bind(ip_addr, BIND_TEST_PORT): addrs_to_bind.append(ip_addr) else: addrs_unavailable.append(ip_addr) if len(addrs_to_bind) >= nr_of_addrs: break if len(addrs_to_bind) < nr_of_addrs: nr_of_addr_setup = nr_of_addrs - len(addrs_to_bind) setup_instructions = host.addr_alias_setup_instructions( addrs_unavailable[0:nr_of_addr_setup], addr_range.netmask) raise BindAddressNotFound(setup_instructions) else: return addrs_to_bind