def test_make_config(self, mock_exists): d = dhcp.DHCP('notauuid', 'eth0') mock_open = mock.mock_open() with mock.patch.object(six.moves.builtins, 'open', new=mock_open): d._make_config() handle = mock_open() handle.write.assert_called_with('\n'.join([ 'domain-needed # Do not forward DNS lookups for unqualified names', 'bogus-priv # Do not forward DNS lookups for RFC1918 blocks', 'no-hosts # Do not use /etc/hosts', 'no-resolv # Do not use /etc/resolv.conf', 'filterwin2k # Filter weird windows 2000 queries', '', '# Disable DNS', 'port=0', '', 'pid-file=/a/b/c/dhcp/notauuid/pid', 'dhcp-leasefile=/a/b/c/dhcp/notauuid/leases', '', 'interface=eth0', 'listen-address=10.0.0.1', '', 'domain=shakenfist', 'local=/shakenfist/', '', 'dhcp-range=eth0,10.0.0.2,static,255.0.0.0,10.255.255.255,1h', 'dhcp-option=eth0,1,255.0.0.0', 'dhcp-option=eth0,3,10.0.0.1', 'dhcp-option=eth0,6,8.8.8.8', 'dhcp-option=eth0,15,shakenfist', 'dhcp-hostsfile=/a/b/c/dhcp/notauuid/hosts', ]))
def test_restart_dhcpd(self, mock_execute, mock_hosts, mock_config, mock_signal): d = dhcp.DHCP('notauuid', 'eth0') d.restart_dhcpd() mock_signal.assert_called_with(signal.SIGHUP) mock_execute.assert_called_with( 'ip netns exec notauuid dnsmasq --conf-file=/a/b/c/dhcp/notauuid/config', shell=True)
def test_restart_dhcpd(self, mock_execute, mock_hosts, mock_config, mock_signal, mock_exists): d = dhcp.DHCP(FakeNetwork(), 'eth0') d.restart_dhcpd() mock_signal.assert_called_with(signal.SIGHUP) mock_execute.assert_called_with( None, 'dnsmasq --conf-file=/a/b/c/dhcp/notauuid/config', namespace='notauuid')
def remove_dhcp(self): if config.NODE_IS_NETWORK_NODE: subst = self.subst_dict() with util_general.RecordedOperation('remove dhcp', self): with self.get_lock(op='Network remove DHCP'): d = dhcp.DHCP(self, subst['vx_veth_inner']) d.remove_dhcpd() else: etcd.enqueue('networknode', RemoveDHCPNetworkTask(self.uuid))
def is_dnsmasq_running(self): """Determine if dnsmasq process is running for this network""" subst = self.subst_dict() d = dhcp.DHCP(self, subst['vx_veth_inner']) pid = d.get_pid() if pid and psutil.pid_exists(pid): return True LOG.withObj(self).warning('dnsmasq is not running') return False
def test_send_signal(self, mock_kill, mock_pid_exists, mock_path_exists): d = dhcp.DHCP('notauuid', 'eth0') mock_open = mock.mock_open(read_data='424242') with mock.patch.object(six.moves.builtins, 'open', new=mock_open): self.assertEqual(True, d._send_signal(signal.SIGKILL)) mock_pid_exists.assert_called() mock_path_exists.assert_called_with('/a/b/c/dhcp/notauuid/pid') mock_kill.assert_called_with(424242, signal.SIGKILL)
def update_dhcp(self): if not self.provide_dhcp: return if config.NODE_IS_NETWORK_NODE: subst = self.subst_dict() with util_general.RecordedOperation('update dhcp', self): with self.get_lock(op='Network update DHCP'): d = dhcp.DHCP(self, subst['vx_veth_inner']) d.restart_dhcpd() else: etcd.enqueue('networknode', UpdateDHCPNetworkTask(self.uuid))
def test_make_hosts(self, mock_instances, mock_interfaces, mock_exists): d = dhcp.DHCP('notauuid', 'eth0') mock_open = mock.mock_open() with mock.patch.object(six.moves.builtins, 'open', new=mock_open): d._make_hosts() handle = mock_open() handle.write.assert_called_with('\n'.join([ '', '1a:91:64:d2:15:39,inst1,127.0.0.5', '1a:91:64:d2:15:40,inst2,127.0.0.6', ]))
def remove_dhcp(self): if util.is_network_node(): subst = self.subst_dict() with util.RecordedOperation('remove dhcp', self): with db.get_lock('network', None, self.uuid, ttl=120): d = dhcp.DHCP(self.uuid, subst['vx_veth_inner']) d.remove_dhcpd() else: db.enqueue('networknode', { 'type': 'remove_dhcp', 'network_uuid': self.uuid }) db.add_event('network', self.uuid, 'remove dhcp', 'enqueued', None, None)
def remove_dhcp(self): if util.is_network_node(): subst = self.subst_dict() with util.RecordedOperation('remove dhcp', self): with db.get_object_lock(self, ttl=120, op='Network remove DHCP'): d = dhcp.DHCP(self, subst['vx_veth_inner']) d.remove_dhcpd() else: db.enqueue('networknode', RemoveDHCPNetworkTask(self.db_entry['uuid'])) db.add_event('network', self.db_entry['uuid'], 'remove dhcp', 'enqueued', None, None)
def remove_dhcp(self): if config.parsed.get('NODE_IP') == config.parsed.get( 'NETWORK_NODE_IP'): subst = self.subst_dict() with util.RecordedOperation('remove dhcp', self) as _: with lockutils.lock('sf_net_%s' % self.uuid, external=True, lock_path='/tmp/'): d = dhcp.DHCP(self.uuid, subst['vx_veth_inner']) d.remove_dhcpd() else: requests.request('put', ('http://%s:%d/remove_dhcp' % (config.parsed.get('NETWORK_NODE_IP'), config.parsed.get('API_PORT'))), data=json.dumps({'uuid': self.uuid}))
def update_dhcp(self): if not self.db_entry['provide_dhcp']: return if util.is_network_node(): subst = self.subst_dict() with util.RecordedOperation('update dhcp', self): with db.get_object_lock(self, ttl=120, op='Network update DHCP'): d = dhcp.DHCP(self, subst['vx_veth_inner']) d.restart_dhcpd() else: db.enqueue('networknode', UpdateDHCPNetworkTask(self.db_entry['uuid'])) db.add_event('network', self.db_entry['uuid'], 'update dhcp', 'enqueued', None, None)
def update_dhcp(self): if not self.provide_dhcp: return if util.is_network_node(): self.ensure_mesh() subst = self.subst_dict() with util.RecordedOperation('update dhcp', self): with db.get_lock('network', None, self.uuid, ttl=120): d = dhcp.DHCP(self.uuid, subst['vx_veth_inner']) d.restart_dhcpd() else: db.enqueue('networknode', { 'type': 'update_dhcp', 'network_uuid': self.uuid }) db.add_event('network', self.uuid, 'update dhcp', 'enqueued', None, None)
def remove_dhcp(self): if config.parsed.get('NODE_IP') == config.parsed.get('NETWORK_NODE_IP'): subst = self.subst_dict() with util.RecordedOperation('remove dhcp', self) as _: with db.get_lock('sf/net/%s' % self.uuid, ttl=120) as _: d = dhcp.DHCP(self.uuid, subst['vx_veth_inner']) d.remove_dhcpd() else: admin_token = util.get_api_token( 'http://%s:%d' % (config.parsed.get('NETWORK_NODE_IP'), config.parsed.get('API_PORT')), namespace='system') requests.request( 'put', ('http://%s:%d/remove_dhcp' % (config.parsed.get('NETWORK_NODE_IP'), config.parsed.get('API_PORT'))), data=json.dumps({'uuid': self.uuid}), headers={'Authorization': admin_token, 'User-Agent': util.get_user_agent()})
def test_str(self): d = dhcp.DHCP('notauuid', 'eth0') s = str(d) self.assertEqual('dhcp(notauuid)', s)
def test_init(self): d = dhcp.DHCP('notauuid', 'eth0') self.assertEqual('/a/b/c/dhcp/notauuid', d.subst['config_dir'])
def test_remove_dhcpd(self, mock_remove_config, mock_signal): d = dhcp.DHCP('notauuid', 'eth0') d.remove_dhcpd() mock_remove_config.assert_called() mock_signal.assert_called_with(signal.SIGKILL)
def test_remove_dhcpd(self, mock_remove_config, mock_signal): d = dhcp.DHCP(FakeNetwork(), 'eth0') d.remove_dhcpd() mock_remove_config.assert_called() mock_signal.assert_called_with(signal.SIGKILL)
def test_remove_config(self, mock_rmtree, mock_exists): d = dhcp.DHCP('notauuid', 'eth0') d._remove_config() mock_exists.assert_called_with('/a/b/c/dhcp/notauuid') mock_rmtree.assert_called_with('/a/b/c/dhcp/notauuid')
def test_str(self): d = dhcp.DHCP(FakeNetwork(), 'eth0') s = str(d) self.assertEqual('dhcp(notauuid)', s)