def clear_all_reservations(): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts lease4 lease6; do psql -U {db_user} -d {db_name} -c "delete from $table_name" ; done'.format( **locals()) fabric_run_command(command)
def set_logger(): file_name = world.name.replace(".", "_") type = BIND_LOG_TYPE lvl = BIND_LOG_LVL module = BIND_MODULE logger_str = ''' config add Logging/loggers config set Logging/loggers[0]/name "{module}" config set Logging/loggers[0]/severity "{type}" config set Logging/loggers[0]/debuglevel {lvl} config add Logging/loggers[0]/output_options config set Logging/loggers[0]/output_options[0]/destination file config set Logging/loggers[0]/output_options[0]/output log_file config commit '''.format(**locals()) cfg_file = open("logger.cfg", "w") cfg_file.write(logger_str) cfg_file.close() cfg_file = 'logger.cfg' prepare_config_file(cfg_file) fabric_send_file(cfg_file + '_processed', cfg_file + '_processed') fabric_run_command('(rm -f log_file | echo "execute file ' + cfg_file + '_processed" | ' + world.f_cfg.software_install_path + 'bin/bindctl ); sleep ' + str(world.f_cfg.sleep_time_2)) remove_local_file(cfg_file + '_processed')
def _clear_db_config(db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd, destination_address=world.f_cfg.mgmt_address): tables = [ 'dhcp4_audit_revision', 'dhcp4_audit', 'dhcp4_global_parameter', 'dhcp4_global_parameter_server', 'dhcp4_option_def', 'dhcp4_option_def_server', 'dhcp4_options', 'dhcp4_options_server', 'dhcp4_pool', 'dhcp4_shared_network', 'dhcp4_shared_network_server', 'dhcp4_subnet', 'dhcp4_subnet_server', 'dhcp6_audit_revision', 'dhcp6_audit', 'dhcp6_global_parameter', 'dhcp6_global_parameter_server', 'dhcp6_option_def', 'dhcp6_option_def_server', 'dhcp6_options', 'dhcp6_options_server', 'dhcp6_pd_pool', 'dhcp6_pool', 'dhcp6_shared_network', 'dhcp6_shared_network_server', 'dhcp6_subnet', 'dhcp6_subnet_server' ] tables = ' '.join(tables) command = 'for table_name in {tables}; do mysql -u {db_user} -p{db_passwd} -e ' command += '"SET foreign_key_checks = 0; truncate $table_name" {db_name}; done' command = command.format(**locals()) fabric_run_command(command, destination_host=destination_address, hide_all=True) tables = ['dhcp6_server', 'dhcp4_server'] tables = ' '.join(tables) command = 'for table_name in {tables}; do mysql -u {db_user} -p{db_passwd} -e ' command += '"SET foreign_key_checks = 0; DELETE FROM $table_name WHERE tag != \'all\'" {db_name}; done' command = command.format(**locals()) fabric_run_command(command, destination_host=destination_address, hide_all=True)
def db_table_contain(table_name, db_type, condition, line, db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd): if db_type in ["mysql", "MySQL"]: command = 'mysql -u {db_user} -p{db_passwd} -e "select * from {table_name}"' \ ' {db_name} --silent --raw > /tmp/mysql_out'.format(**locals()) fabric_run_command(command) result = fabric_sudo_command('grep -c "{line}" /tmp/mysql_out'.format(**locals())) elif db_type in ["postgresql", "PostgreSQL"]: command = 'psql -U {db_user} -d {db_name} -c "select * from {table_name}" > /tmp/pgsql_out'.format(**locals()) fabric_run_command(command) result = fabric_sudo_command('grep -c "{line}" /tmp/pgsql_out'.format(**locals())) elif world.f_cfg.db_type == "cql": result = -1 # command = 'for table_name in dhcp_option_scope host_reservations lease4 lease6;' \ # ' do cqlsh --keyspace=keatest --user=keatest --password=keatest -e "TRUNCATE $table_name;"' \ # ' ; done'.format(**locals()) # fabric_run_command(command) else: assert False, "db type {db_type} not recognized/not supported".format(**locals()) if condition is not None: if int(result) > 0: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".' \ ' That is to much.'.format(db_type, table_name, result, line) else: if int(result) < 1: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".'.format(db_type, table_name, result, line)
def set_logger(): file_name = world.name.replace(".", "_") type = BIND_LOG_TYPE lvl = BIND_LOG_LVL module = BIND_MODULE logger_str = ''' config add Logging/loggers config set Logging/loggers[0]/name "{module}" config set Logging/loggers[0]/severity "{type}" config set Logging/loggers[0]/debuglevel {lvl} config add Logging/loggers[0]/output_options config set Logging/loggers[0]/output_options[0]/destination file config set Logging/loggers[0]/output_options[0]/output log_file config commit '''.format(**locals()) cfg_file = open("logger.cfg", "w") cfg_file.write(logger_str) cfg_file.close() cfg_file = 'logger.cfg' prepare_config_file(cfg_file) fabric_send_file(cfg_file + '_processed', cfg_file + '_processed') fabric_run_command('(rm -f log_file | echo "execute file ' + cfg_file + '_processed" | ' + SOFTWARE_INSTALL_DIR + 'bin/bindctl ); sleep ' + str(SLEEP_TIME_2)) remove_local_file(cfg_file + '_processed')
def clear_all_reservations(): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts;' \ ' do mysql -u {db_user} -p{db_passwd} -e "SET foreign_key_checks = 0;' \ ' delete from $table_name" {db_name}; done'.format(**locals()) fabric_run_command(command)
def clear_all_reservations(): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts lease4 lease6;' \ ' do mysql -u {db_user} -p{db_passwd} -e "SET foreign_key_checks = 0;' \ ' delete from $table_name" {db_name}; done'.format(**locals()) fabric_run_command(command)
def clear_all_reservations(): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd command = 'for table_name in host_reservations; do cqlsh --keyspace=keatest' \ ' --user=keatest --password=keatest -e "TRUNCATE $table_name;"' \ ' ; done'.format(**locals()) fabric_run_command(command) fabric_run_command(command)
def start_srv(a, b): """ Start ISC-DHCPv6 with generated config. """ cfg_write() stop_srv() get_common_logger().debug("Starting Dibbler with generated config:") fabric_send_file(world.cfg["cfg_file"], '/etc/dibbler/server.conf') remove_local_file(world.cfg["cfg_file"]) fabric_run_command ('('+world.f_cfg.software_install_path+'sbin/dibbler-server start & ); sleep 4;')
def start_srv(a, b): """ Start ISC-DHCPv6 with generated config. """ cfg_write() stop_srv() get_common_logger().debug("Starting Dibbler with generated config:") fabric_send_file(world.cfg["cfg_file"], '/etc/dibbler/server.conf') remove_local_file(world.cfg["cfg_file"]) fabric_run_command ('('+SOFTWARE_INSTALL_DIR+'sbin/dibbler-server start & ); sleep 4;')
def _create_pgsql_dump(): # dump for postrgesql is bit different because we do not have config backend for postgres # but we will still use kea 1.6.3 to generate this. In future we will have to create new dump with # config backend data srv_msg.remove_file_from_server('$(SOFTWARE_INSTALL_PATH)/pg_db_v4.sql') world.f_cfg.multi_threading_enabled = False misc.test_setup() srv_control.config_srv_subnet('192.168.50.0/24', '192.168.50.10-192.168.50.10') srv_control.add_hooks('libdhcp_host_cmds.so') srv_control.add_hooks('libdhcp_cb_cmds.so') srv_control.add_hooks('libdhcp_mysql_cb.so') srv_control.open_control_channel() srv_control.agent_control_channel('$(MGMT_ADDRESS)') srv_control.enable_db_backend_reservation('PostgreSQL') srv_control.define_temporary_lease_db_backend('postgresql') srv_control.build_and_send_config_files() srv_control.start_srv('DHCP', 'started') hr = {"reservation": {"subnet-id": 1, "hw-address": "01:0a:0b:0c:0d:0e:0f", "ip-address": "192.168.50.205", "next-server": "192.0.2.1", "server-hostname": "hal9000", "boot-file-name": "/dev/null", "option-data": [{"name": "domain-name-servers", "data": "10.1.1.202,10.1.1.203"}], "client-classes": ["special_snowflake", "office"]}} _send_cmd("reservation-add", hr) misc.test_procedure() srv_msg.client_sets_value('Client', 'chaddr', 'ff:01:02:03:ff:04') srv_msg.client_send_msg('DISCOVER') misc.pass_criteria() srv_msg.send_wait_for_message('MUST', 'OFFER') srv_msg.response_check_content('yiaddr', '192.168.50.10') misc.test_procedure() srv_msg.client_copy_option('server_id') srv_msg.client_does_include_with_value('requested_addr', '192.168.50.10') srv_msg.client_sets_value('Client', 'chaddr', 'ff:01:02:03:ff:04') srv_msg.client_send_msg('REQUEST') misc.pass_criteria() srv_msg.send_wait_for_message('MUST', 'ACK') # wanted to do this with fabric_sudo_command(cmd, sudo_user='******' but it failed cmd = "sudo -S -u postgres pg_dump %s >%s/pg_db_v4.sql" % (world.f_cfg.db_name, world.f_cfg.software_install_path) fabric_run_command(cmd, ignore_errors=False, destination_host=world.f_cfg.mgmt_address) srv_msg.execute_shell_cmd("sed -i 's/$(DB_USER)/!db_user!/g' %s/pg_db_v4.sql" % world.f_cfg.software_install_path)
def db_table_contain(table_name, db_type, line="", grep_cmd=None, expect=True, db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd, destination=world.f_cfg.mgmt_address): # TODO add checking count of records if db_type.lower() == "mysql": if table_name == 'lease6': select = 'select hex(duid), address, iaid, valid_lifetime' elif table_name == 'lease4': select = 'select hex(address), hex(hwaddr), valid_lifetime' else: select = 'select *' command = 'mysql -u {db_user} -p{db_passwd} -e "{select} from {table_name}"' \ ' {db_name} --silent > /tmp/db_out'.format(**locals()) elif db_type.lower() in ["postgresql", "pgsql"]: if table_name == 'lease4': select = "select to_hex(address), encode(hwaddr,'hex'), valid_lifetime" else: select = 'select *' command = 'PGPASSWORD={db_passwd} psql -h localhost -U {db_user} -d {db_name} ' \ '-c "{select} from {table_name}" > /tmp/db_out'.format(**locals()) else: assert False, "db type {db_type} not recognized/not supported".format( **locals()) fabric_run_command(command, destination_host=destination) cmd = 'grep -c "{line}" /tmp/db_out'.format(**locals()) if grep_cmd is not None: cmd = grep_cmd result = fabric_sudo_command(cmd, ignore_errors=True, destination_host=destination) if not expect: if int(result) > 0: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".' \ ' That is to much.'.format(db_type, table_name, result, line) else: if int(result) < 1: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".'.format( db_type, table_name, result, line)
def save_leases(): db_name = DB_NAME db_user = DB_USER db_passwd = DB_PASSWD if DB_TYPE == "mysql": # that is tmp solution - just clearing not saving. command = '''mysql -u {db_user} -p{db_passwd} -Nse 'show tables' {db_name} | while read table; do mysql -u {db_user} -p{db_passwd} -e "truncate table $table" {db_name}; done'''.format(**locals()) fabric_run_command(command) elif DB_TYPE == "postgresql": pointer_start = '{' pointer_end = '}' command = """psql -U {db_user} -d {db_name} -c "\\\\dtvs" -t | awk '{pointer_start}print $3{pointer_end}' | while read table; do if [ ! -z "$table" -a "$table" != " " ]; then psql -U {db_user} -d {db_name} -c "truncate $table"; fi done""".format(**locals()) fabric_run_command(command) else: fabric_download_file(world.cfg['leases'], world.cfg["dir_name"] + '/kea_leases.csv')
def clear_all(): fabric_remove_file_command(SOFTWARE_INSTALL_DIR + 'var/kea/kea.log') if world.ddns_enable: fabric_remove_file_command(SOFTWARE_INSTALL_DIR + 'var/kea/kea.log_ddns') db_name = DB_NAME db_user = DB_USER db_passwd = DB_PASSWD if DB_TYPE in ["memfile", ""]: fabric_remove_file_command(world.cfg['leases']) elif DB_TYPE == "mysql": command = '''mysql -u {db_user} -p{db_passwd} -Nse 'show tables' {db_name} | while read table; do mysql -u {db_user} -p{db_passwd} -e "truncate table $table" {db_name}; done'''.format(**locals()) fabric_run_command(command) elif DB_TYPE == "postgresql": pointer_start = '{' pointer_end = '}' command = """psql -U {db_user} -d {db_name} -c "\\\\dtvs" -t | awk '{pointer_start}print $3{pointer_end}' | while read table; do if [ ! -z "$table" -a "$table" != " " ]; then psql -U {db_user} -d {db_name} -c "truncate $table"; fi done""".format(**locals()) fabric_run_command(command)
def clear_leases(): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd if world.f_cfg.db_type == "mysql": # that is tmp solution - just clearing not saving. #command = '''mysql -u {db_user} -p{db_passwd} -Nse 'show tables' {db_name} | while read table; do mysql -u {db_user} -p{db_passwd} -e "truncate table $table" {db_name}; done'''.format(**locals()) command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts lease4 lease6; do mysql -u {db_user} -p{db_passwd} -e "delete from $table_name" {db_name}; done'.format( **locals()) fabric_run_command(command) elif world.f_cfg.db_type == "postgresql": pointer_start = '{' pointer_end = '}' #command = """psql -U {db_user} -d {db_name} -c "\\\\dtvs" -t | awk '{pointer_start}print $3{pointer_end}' | while read table; do if [ ! -z "$table" -a "$table" != " " ]; then psql -U {db_user} -d {db_name} -c "truncate $table"; fi done""".format(**locals()) command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts lease4 lease6; do psql -U {db_user} -d {db_name} -c "delete from $table_name" ; done'.format( **locals()) fabric_run_command(command) else: fabric_remove_file_command(world.cfg['leases'])
def clear_all(tmp_db_type=None): fabric_remove_file_command(world.f_cfg.software_install_path + 'var/kea/kea.log*') db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd if world.f_cfg.db_type in ["memfile", ""]: fabric_remove_file_command(world.cfg['leases']) elif world.f_cfg.db_type == "mysql": #command = '''mysql -u {db_user} -p{db_passwd} -Nse 'show tables' {db_name} | while read table; do mysql -u {db_user} -p{db_passwd} -e "truncate table $table" {db_name}; done'''.format(**locals()) command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts lease4 lease6; do mysql -u {db_user} -p{db_passwd} -e "delete from $table_name" {db_name}; done'.format( **locals()) fabric_run_command(command) elif world.f_cfg.db_type == "postgresql": pointer_start = '{' pointer_end = '}' command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts lease4 lease6; do psql -U {db_user} -d {db_name} -c "delete from $table_name" ; done'.format( **locals()) fabric_run_command(command)
def clear_leases(db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd, destination_address=world.f_cfg.mgmt_address): if world.f_cfg.db_type == "mysql": # that is tmp solution - just clearing not saving. command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts lease4 lease6 logs; ' \ 'do mysql -u {db_user} -p{db_passwd} -e' \ ' "SET foreign_key_checks = 0; delete from $table_name" {db_name}; done'.format(**locals()) fabric_run_command(command, destination_host=destination_address) elif world.f_cfg.db_type == "postgresql": command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts lease4 lease6 logs;' \ ' do psql -U {db_user} -d {db_name} -c "delete from $table_name" ; done'.format(**locals()) fabric_run_command(command, destination_host=destination_address) elif world.f_cfg.db_type == "cql": # TODO: hardcoded passwords for now in cassandra, extend it in some time :) command = 'for table_name in dhcp_option_scope host_reservations lease4 lease6 logs;' \ ' do cqlsh --keyspace=keatest --user=keatest --password=keatest -e "TRUNCATE $table_name;"' \ ' ; done'.format(**locals()) fabric_run_command(command, destination_host=destination_address) elif world.f_cfg.db_type in ["memfile", ""]: fabric_remove_file_command(world.f_cfg.get_leases_path(), destination_host=destination_address) else: raise Exception('Unsupported db type %s' % world.f_cfg.db_type)
def db_table_contain(table_name, db_type, condition, line, db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd): if db_type in ["mysql", "MySQL"]: command = 'mysql -u {db_user} -p{db_passwd} -e "select * from {table_name}"' \ ' {db_name} --silent --raw > /tmp/mysql_out'.format(**locals()) fabric_run_command(command) result = fabric_sudo_command( 'grep -c "{line}" /tmp/mysql_out'.format(**locals())) elif db_type in ["postgresql", "PostgreSQL"]: command = 'psql -U {db_user} -d {db_name} -c "select * from {table_name}" > /tmp/pgsql_out'.format( **locals()) fabric_run_command(command) result = fabric_sudo_command( 'grep -c "{line}" /tmp/pgsql_out'.format(**locals())) elif world.f_cfg.db_type == "cql": result = -1 # command = 'for table_name in dhcp_option_scope host_reservations lease4 lease6;' \ # ' do cqlsh --keyspace=keatest --user=keatest --password=keatest -e "TRUNCATE $table_name;"' \ # ' ; done'.format(**locals()) # fabric_run_command(command) else: assert False, "db type {db_type} not recognized/not supported".format( **locals()) if condition is not None: if int(result) > 0: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".' \ ' That is to much.'.format(db_type, table_name, result, line) else: if int(result) < 1: assert False, 'In database {0} table name "{1}" has {2} of: "{3}".'.format( db_type, table_name, result, line)
def run_bindctl(succeed, opt): """ Run bindctl with prepered config file """ world.cfg[ 'leases'] = world.f_cfg.software_install_path + 'var/bind10/kea-leases4.csv' if opt == "clean": get_common_logger().debug('cleaning kea configuration') cfg_file = 'kea4-stop.cfg' prepare_cfg_kea4_for_kea4_stop(cfg_file) prepare_config_file(cfg_file) fabric_send_file(cfg_file + '_processed', cfg_file + '_processed') remove_local_file(cfg_file + '_processed') if opt == "start": if world.f_cfg.save_logs: set_logger() get_common_logger().debug('starting fresh kea') cfg_file = 'kea4-start.cfg' prepare_cfg_kea4_for_kea4_start(cfg_file) prepare_config_file(cfg_file) fabric_send_file(cfg_file + '_processed', cfg_file + '_processed') remove_local_file(cfg_file + '_processed') if opt == "configuration": get_common_logger().debug('kea configuration') cfg_file = world.cfg["cfg_file"] prepare_config_file(cfg_file) add_last = open(cfg_file + "_processed", 'a') # add 'config commit' we don't put it before add_last.write("config commit") add_last.close() fabric_send_file(cfg_file + '_processed', cfg_file + '_processed') copy_configuration_file(cfg_file + '_processed') remove_local_file(cfg_file + '_processed') world.cfg["conf"] = "" if opt == "restart": restart_srv() result = fabric_run_command('(echo "execute file ' + cfg_file + '_processed" | ' + world.f_cfg.software_install_path + 'bin/bindctl ); sleep 1') search_for_errors(succeed, opt, result, ["ImportError:", '"config revert".', "Error"]) parsing_bind_stdout(result.stdout, opt, ['Broken pipe'])
def check_kea_status(): v6 = 0 v4 = 0 result = fabric_run_command(SOFTWARE_INSTALL_DIR + "sbin/keactrl status") # not very sophisticated but easiest fastest way ;) if "DHCPv4 server: inactive" in result: v4 = 0 elif "DHCPv4 server: active" in result: v4 = 1 if "DHCPv6 server: inactive" in result: v6 = 0 elif "DHCPv6 server: active" in result: v6 = 1 return v6, v4
def _clear_db_config(db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd, destination_address=world.f_cfg.mgmt_address): tables = ['dhcp4_audit_revision', 'dhcp4_audit', 'dhcp4_global_parameter', 'dhcp4_global_parameter_server', 'dhcp4_option_def', 'dhcp4_option_def_server', 'dhcp4_options', 'dhcp4_options_server', 'dhcp4_pool', 'dhcp4_shared_network', 'dhcp4_shared_network_server', 'dhcp4_subnet', 'dhcp4_subnet_server', 'dhcp6_audit_revision', 'dhcp6_audit', 'dhcp6_global_parameter', 'dhcp6_global_parameter_server', 'dhcp6_option_def', 'dhcp6_option_def_server', 'dhcp6_options', 'dhcp6_options_server', 'dhcp6_pd_pool', 'dhcp6_pool', 'dhcp6_shared_network', 'dhcp6_shared_network_server', 'dhcp6_subnet', 'dhcp6_subnet_server'] tables = ' '.join(tables) command = 'for table_name in {tables}; do mysql -u {db_user} -p{db_passwd} -e ' command += '"SET foreign_key_checks = 0; truncate $table_name" {db_name}; done' command = command.format(**locals()) fabric_run_command(command, destination_host=destination_address, hide_all=True)
def test_start(): """ Server starting before testing """ # clear tests results if os.path.exists('tests_results'): rmtree('tests_results') os.makedirs('tests_results') if not os.path.exists( 'tests_results_archive') and world.f_cfg.auto_archive: os.makedirs('tests_results_archive') world.result = [] # Initialize the common logger. logging_facility.logger_initialize(world.f_cfg.loglevel) # check remote system if it is redhat or debian based result = fabric_run_command('ls -al /etc/redhat-release 2>/dev/null', ignore_errors=True) if result.succeeded: world.server_system = 'redhat' else: world.server_system = 'debian' # stop any SUT running kea_under_test = False if not world.f_cfg.no_server_management: for sut_name in world.f_cfg.software_under_test: sut_module = importlib.import_module( "softwaresupport.%s.functions" % sut_name) # True passed to stop_srv is to hide output in console. sut_module.stop_srv() if 'kea' in sut_name: kea_under_test = True if kea_under_test: # for now let's assume that both systems are the same kea.db_setup() if world.f_cfg.mgmt_address_2: kea.db_setup(dest=world.f_cfg.mgmt_address_2)
def start_srv(start, process): """ Start ISC-DHCPv6 with generated config. """ if not "conf_option" in world.cfg: world.cfg["conf_option"] = "" world.cfg['log_file'] = build_log_path() fabric_run_command('cat /dev/null >' + world.cfg['log_file']) world.cfg["dhcp_log_file"] = world.cfg['log_file'] log = "local7" if ISC_DHCP_LOG_FACILITY != "": log = ISC_DHCP_LOG_FACILITY world.cfg['log_facility'] = '''\nlog-facility {log};\n'''.format(**locals()) add_defaults() cfg_write() get_common_logger().debug("Start ISC-DHCPv6 with generated config:") convert_cfg_file(world.cfg["cfg_file"]) fabric_send_file(world.cfg["cfg_file"] + '_processed', world.cfg["cfg_file"] + '_processed') copy_configuration_file(world.cfg["cfg_file"] + '_processed') remove_local_file(world.cfg["cfg_file"]) #set_ethernet_interface() stop_srv() world.cfg['leases'] = build_leases_path() fabric_run_command('echo y |rm ' + world.cfg['leases']) fabric_run_command('touch ' + world.cfg['leases']) result = fabric_sudo_command('(' + SOFTWARE_INSTALL_DIR + 'sbin/dhcpd -6 -cf server.cfg_processed' + ' -lf ' + world.cfg['leases'] + '&); sleep ' + str(SLEEP_TIME_1) + ';') check_process_result(start, result, process) # clear configs in case we would like make couple configs in one test world.cfg["conf_time"] = "" world.cfg["log_facility"] = "" world.cfg["custom_lines"] = "" world.cfg["conf_option"] = "" world.cfg["conf_vendor"] = ""
def remove_from_db_table(table_name, db_type, db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd): if db_type in ["mysql", "MySQL"]: # that is tmp solution - just clearing not saving. command = 'mysql -u {db_user} -p{db_passwd} -e "delete from {table_name}" {db_name}'.format(**locals()) fabric_run_command(command) elif db_type in ["postgresql", "PostgreSQL"]: command = 'psql -U {db_user} -d {db_name} -c "delete from {table_name}"'.format(**locals()) fabric_run_command(command) elif db_type == "cql": # TODO: hardcoded passwords for now in cassandra, extend it in some time :) command = 'for table_name in dhcp_option_scope host_reservations lease4 lease6 logs;' \ ' do cqlsh --keyspace=keatest --user=keatest --password=keatest -e "TRUNCATE $table_name;"' \ ' ; done'.format(**locals()) fabric_run_command(command) else: assert False, "db type {db_type} not recognized/not supported".format(**locals())
def clear_leases(db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd, destination_address=world.f_cfg.mgmt_address): if world.f_cfg.db_type == "mysql": # that is tmp solution - just clearing not saving. command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts lease4 lease6 logs; ' \ 'do mysql -u {db_user} -p{db_passwd} -e' \ ' "SET foreign_key_checks = 0; delete from $table_name" {db_name}; done'.format(**locals()) fabric_run_command(command, destination_host=destination_address) elif world.f_cfg.db_type == "postgresql": command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts lease4 lease6 logs;' \ ' do psql -U {db_user} -d {db_name} -c "delete from $table_name" ; done'.format(**locals()) fabric_run_command(command, destination_host=destination_address) elif world.f_cfg.db_type == "cql": # TODO: hardcoded passwords for now in cassandra, extend it in some time :) command = 'for table_name in dhcp_option_scope host_reservations lease4 lease6 logs;' \ ' do cqlsh --keyspace=keatest --user=keatest --password=keatest -e "TRUNCATE $table_name;"' \ ' ; done'.format(**locals()) fabric_run_command(command, destination_host=destination_address) else: fabric_remove_file_command(world.cfg['leases'], destination_host=destination_address)
def remove_from_db_table(table_name, db_type, db_name=world.f_cfg.db_name, db_user=world.f_cfg.db_user, db_passwd=world.f_cfg.db_passwd): if db_type in ["mysql", "MySQL"]: # that is tmp solution - just clearing not saving. command = 'mysql -u {db_user} -p{db_passwd} -e "delete from {table_name}" {db_name}'.format( **locals()) fabric_run_command(command) elif db_type in ["postgresql", "PostgreSQL"]: command = 'PGPASSWORD={db_passwd} psql -h localhost -U {db_user} -d {db_name} -c "delete from {table_name}"'.format( **locals()) fabric_run_command(command) elif db_type == "cql": # TODO: hardcoded passwords for now in cassandra, extend it in some time :) command = 'for table_name in dhcp_option_scope host_reservations lease4 lease6 logs;' \ ' do cqlsh --keyspace=keatest --user=keatest --password=keatest -e "TRUNCATE $table_name;"' \ ' ; done'.format(**locals()) fabric_run_command(command) else: assert False, "db type {db_type} not recognized/not supported".format( **locals())
def run_bindctl (succeed, opt): """ Run bindctl with prepered config file """ world.cfg['leases'] = world.f_cfg.software_install_path + 'var/bind10/kea-leases6.csv' if opt == "clean": get_common_logger().debug('cleaning kea configuration') # build configuration file with for: # - stopping Kea # - cleaning configuration # - default logging prepare_cfg_kea6_for_kea6_stop() cfg_file = 'kea6stop.cfg' prepare_config_file(cfg_file) # send file fabric_send_file(cfg_file + '_processed', cfg_file + '_processed') remove_local_file(cfg_file + '_processed') elif opt == "start": # build configuration file with for: # - clean start Kea get_common_logger().debug('starting fresh kea') prepare_cfg_kea6_for_kea6_start() cfg_file = 'kea6start.cfg' prepare_config_file(cfg_file) # send file fabric_send_file(cfg_file + '_processed', cfg_file + '_processed') remove_local_file(cfg_file + '_processed') elif opt == "configuration": # start logging on different file: if world.f_cfg.save_logs: set_logger() # build configuration file with for: # - configure all needed to test features get_common_logger().debug('kea configuration') cfg_file = world.cfg["cfg_file"] prepare_config_file(cfg_file) add_last = open(cfg_file + "_processed", 'a') # add 'config commit' we don't put it before add_last.write("config commit") add_last.close() # send file fabric_send_file(cfg_file + '_processed', cfg_file + '_processed') copy_configuration_file(cfg_file + '_processed') remove_local_file(cfg_file + '_processed') elif opt == "restart": # restart server without changing it's configuration restart_srv() result = fabric_run_command('(echo "execute file ' + cfg_file + '_processed" | ' + world.f_cfg.software_install_path + 'bin/bindctl ); sleep ' + str(world.f_cfg.sleep_time_2)) # now let's test output, looking for errors, # some times clean can fail, so we wanna test only start and conf # for now we fail test on any presence of stderr, probably this will # need some more specific search. search_for_errors(succeed, opt, result, ["ImportError:", '"config revert".', "Error"]) # Error 32: Broken pipe # this error needs different aproach then others. Bind10 needs to be restarted. parsing_bind_stdout(result.stdout, opt, ['Broken pipe'])
def restart_srv(): # can't be less then 7, server needs time to restart. fabric_run_command('(echo "Dhcp6 shutdown" | ' + SOFTWARE_INSTALL_DIR + 'bin/bindctl ); sleep 10')
def restart_srv(): fabric_run_command("("+SOFTWARE_INSTALL_DIR+"sbin/dibbler-server restart); sleep 1;")
def run_bindctl (succeed, opt): """ Run bindctl with prepered config file """ world.cfg['leases'] = SOFTWARE_INSTALL_DIR + 'var/bind10/kea-leases6.csv' if opt == "clean": get_common_logger().debug('cleaning kea configuration') # build configuration file with for: # - stopping Kea # - cleaning configuration # - default logging prepare_cfg_kea6_for_kea6_stop() cfg_file = 'kea6stop.cfg' prepare_config_file(cfg_file) # send file fabric_send_file(cfg_file + '_processed', cfg_file + '_processed') remove_local_file(cfg_file + '_processed') elif opt == "start": # build configuration file with for: # - clean start Kea get_common_logger().debug('starting fresh kea') prepare_cfg_kea6_for_kea6_start() cfg_file = 'kea6start.cfg' prepare_config_file(cfg_file) # send file fabric_send_file(cfg_file + '_processed', cfg_file + '_processed') remove_local_file(cfg_file + '_processed') elif opt == "configuration": # start logging on different file: if SAVE_LOGS: set_logger() # build configuration file with for: # - configure all needed to test features get_common_logger().debug('kea configuration') cfg_file = world.cfg["cfg_file"] prepare_config_file(cfg_file) add_last = open(cfg_file + "_processed", 'a') # add 'config commit' we don't put it before add_last.write("config commit") add_last.close() # send file fabric_send_file(cfg_file + '_processed', cfg_file + '_processed') copy_configuration_file(cfg_file + '_processed') remove_local_file(cfg_file + '_processed') elif opt == "restart": # restart server without changing it's configuration restart_srv() result = fabric_run_command('(echo "execute file ' + cfg_file + '_processed" | ' + SOFTWARE_INSTALL_DIR + 'bin/bindctl ); sleep ' + str(SLEEP_TIME_2)) # now let's test output, looking for errors, # some times clean can fail, so we wanna test only start and conf # for now we fail test on any presence of stderr, probably this will # need some more specific search. search_for_errors(succeed, opt, result, ["ImportError:", '"config revert".', "Error"]) # Error 32: Broken pipe # this error needs different aproach then others. Bind10 needs to be restarted. parsing_bind_stdout(result.stdout, opt, ['Broken pipe'])
def stop_srv(value = False): #pass fabric_run_command("("+world.f_cfg.software_install_path+"sbin/dibbler-server stop); sleep 1;", value)
def restart_srv(): fabric_run_command("("+world.f_cfg.software_install_path+"sbin/dibbler-server restart); sleep 1;")
def test_v6_upgrade_pgsql_db(): # new db parameters world.f_cfg.multi_threading_enabled = False tmp_db_name = "kea_tmp_db" tmp_user_name = "kea_tmp_user" # create new db without schema srv_control.build_database(db_name=tmp_db_name, db_user=tmp_user_name, init_db=False) # send db dump file srv_msg.remove_file_from_server('/tmp/pg_db_v6.sql') srv_msg.send_file_to_server('tests/dhcpv6/db_upgrade/pg_db_v6.sql', '/tmp/pg_db_v6.sql') # switch username to the one setup is using srv_msg.execute_shell_cmd("sed -i 's/!db_user!/%s/g' /tmp/pg_db_v6.sql" % tmp_user_name) # recreate db content in new db cmd = "sudo -S -u postgres psql -d %s -f/tmp/pg_db_v6.sql" % tmp_db_name fabric_run_command(cmd, ignore_errors=False, destination_host=world.f_cfg.mgmt_address) # start kea, which should fail due to mismatch in db version misc.test_setup() srv_control.config_srv_subnet('2001:db8:1::/64', '2001:db8:1::10-2001:db8:1::10') srv_control.config_srv_prefix('2001:db8:2::', 0, 90, 96) srv_control.add_hooks('libdhcp_host_cmds.so') srv_control.add_hooks('libdhcp_lease_cmds.so') srv_control.open_control_channel() srv_control.agent_control_channel('$(MGMT_ADDRESS)') hosts = { "hosts-databases": [{ "user": tmp_user_name, "password": "******", "name": tmp_db_name, "type": "postgresql" }] } leases = { "lease-database": { "user": tmp_user_name, "password": "******", "name": tmp_db_name, "type": "postgresql" } } world.dhcp_cfg.update(hosts) world.dhcp_cfg.update(leases) srv_control.build_and_send_config_files() srv_control.start_srv_during_process('DHCP', 'started') kea_admin = world.f_cfg.sbin_join('kea-admin') srv_msg.execute_shell_cmd( "sudo %s db-upgrade pgsql -u %s -p $(DB_PASSWD) -n %s" % (kea_admin, tmp_user_name, tmp_db_name)) # start kea srv_control.start_srv('DHCP', 'started') # check reservation hr_get = { "subnet-id": 1, "identifier-type": "duid", "identifier": "01:02:03:04:05:06:07:08:09:0A" } resp = _send_cmd("reservation-get", hr_get)["arguments"] assert resp["duid"] == "01:02:03:04:05:06:07:08:09:0a" assert resp["hostname"] == "foo.example.com" assert resp["ip-addresses"] == ["2001:db8:1::100"] assert resp["option-data"] == [{ "always-send": False, "code": 17, "csv-format": True, "data": "4491", "name": "vendor-opts", "space": "dhcp6" }] assert resp["prefixes"] == ["2001:db8:2:abcd::/64"] # check lease lease_get = {"duid": "00:03:00:01:f6:f5:f4:f3:f2:01"} resp = _send_cmd("lease6-get-by-duid", lease_get)["arguments"] assert len(resp["leases"]) == 2 for lease in resp["leases"]: if lease["type"] == "IA_NA": assert lease["duid"] == "00:03:00:01:f6:f5:f4:f3:f2:01" assert lease["hw-address"] == "f6:f5:f4:f3:f2:01" assert lease["iaid"] == 55701 assert lease["ip-address"] == "2001:db8:1::10" assert lease["preferred-lft"] == 3000 assert lease["state"] == 0 assert lease["subnet-id"] == 1 assert lease["valid-lft"] == 4000 if lease["type"] == "IA_PD": assert lease["duid"] == "00:03:00:01:f6:f5:f4:f3:f2:01" assert lease["hw-address"] == "f6:f5:f4:f3:f2:01" assert lease["iaid"] == 76159 assert lease["ip-address"] == "2001:db8:2::" assert lease["preferred-lft"] == 3000 assert lease["prefix-len"] == 96 assert lease["state"] == 0 assert lease["subnet-id"] == 1 assert lease["valid-lft"] == 4000
def stop_srv(value = False): #pass fabric_run_command("("+SOFTWARE_INSTALL_DIR+"sbin/dibbler-server stop); sleep 1;", value)
def db_setup(): if world.f_cfg.disable_db_setup: return db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd # MYSQL # cmd = "mysql -u root -N -B -e \"DROP DATABASE IF EXISTS '{db_name}';\"".format(**locals()) # result = fabric_sudo_command(cmd) cmd = "mysql -u root -N -B -e \"SHOW DATABASES LIKE '{db_name}';\"".format( **locals()) result = fabric_sudo_command(cmd) if result == db_name: # db exsists, so try migration cmd = "kea-admin db-upgrade mysql -u {db_user} -p {db_passwd} -n {db_name}".format( **locals()) fabric_run_command(cmd) else: # no db, create from scratch cmd = "mysql -u root -e 'CREATE DATABASE {db_name};'".format( **locals()) fabric_sudo_command(cmd) cmd = "mysql -u root -e \"CREATE USER IF NOT EXISTS '{db_user}'@'localhost' IDENTIFIED BY '{db_passwd}';\"".format( **locals()) fabric_sudo_command(cmd) cmd = "mysql -u root -e 'GRANT ALL ON {db_name}.* TO {db_user}@localhost;'".format( **locals()) fabric_sudo_command(cmd) cmd = "kea-admin db-init mysql -u {db_user} -p {db_passwd} -n {db_name}".format( **locals()) fabric_run_command(cmd) # POSTGRESQL # cmd = "psql -U postgres -t -c \"DROP DATABASE IF EXISTS {db_name}\"".format(**locals()) # result = fabric_sudo_command(cmd, sudo_user='******') cmd = "psql -U postgres -t -c \"SELECT datname FROM pg_database WHERE datname = '{db_name}'\"".format( **locals()) result = fabric_sudo_command(cmd, sudo_user='******') if result.strip() == db_name: # db exsists, so try migration cmd = "kea-admin db-upgrade pgsql -u {db_user} -p {db_passwd} -n {db_name}".format( **locals()) fabric_run_command(cmd) else: # no db, create from scratch cmd = "psql -U postgres -c \"CREATE DATABASE {db_name};\"".format( **locals()) fabric_sudo_command(cmd, sudo_user='******') cmd = "psql -U postgres -c \"DROP USER IF EXISTS {db_user};\"".format( **locals()) fabric_sudo_command(cmd, sudo_user='******') cmd = "psql -U postgres -c \"CREATE USER {db_user} WITH PASSWORD '{db_passwd}';\"".format( **locals()) fabric_sudo_command(cmd, sudo_user='******') cmd = "psql -U postgres -c \"GRANT ALL PRIVILEGES ON DATABASE {db_name} TO {db_user};\"".format( **locals()) fabric_sudo_command(cmd, sudo_user='******') cmd = "kea-admin db-init pgsql -u {db_user} -p {db_passwd} -n {db_name}".format( **locals()) fabric_run_command(cmd)
def restart_srv(): # can't be less then 7, server needs time to restart. fabric_run_command('(echo "Dhcp4 shutdown" | ' + world.f_cfg.software_install_path + 'bin/bindctl ); sleep 10')
def test_v4_upgrade_pgsql_db(): # new db parameters tmp_db_name = "kea_tmp_db" tmp_user_name = "kea_tmp_user" # create new db without schema srv_control.build_database(db_name=tmp_db_name, db_user=tmp_user_name, init_db=False) # send db dump file srv_msg.remove_file_from_server('/tmp/pg_db_v4.sql') srv_msg.send_file_to_server('tests/dhcpv4/db_upgrade/pg_db_v4.sql', '/tmp/pg_db_v4.sql') # switch username to the one setup is using srv_msg.execute_shell_cmd("sed -i 's/!db_user!/%s/g' /tmp/pg_db_v4.sql" % tmp_user_name) # recreate db content in new db cmd = "sudo -S -u postgres psql -d %s -f/tmp/pg_db_v4.sql" % tmp_db_name fabric_run_command(cmd, ignore_errors=False, destination_host=world.f_cfg.mgmt_address) # start kea, which should fail due to mismatch in db version misc.test_setup() srv_control.config_srv_subnet('192.168.50.0/24', '192.168.50.10-192.168.50.10') srv_control.add_hooks('libdhcp_host_cmds.so') srv_control.add_hooks('libdhcp_lease_cmds.so') srv_control.open_control_channel() srv_control.agent_control_channel('$(MGMT_ADDRESS)') hosts = {"hosts-databases": [{"user": tmp_user_name, "password": "******", "name": tmp_db_name, "type": "postgresql"}]} leases = {"lease-database": {"user": tmp_user_name, "password": "******", "name": tmp_db_name, "type": "postgresql"}} world.dhcp_cfg.update(hosts) world.dhcp_cfg.update(leases) srv_control.build_and_send_config_files() srv_control.start_srv_during_process('DHCP', 'started') # upgrade database kea_admin = world.f_cfg.sbin_join('kea-admin') srv_msg.execute_shell_cmd("sudo %s db-upgrade pgsql -u %s -p $(DB_PASSWD) -n %s" % (kea_admin, tmp_user_name, tmp_db_name)) # start kea srv_control.start_srv('DHCP', 'started') cmd = dict(command="config-get", arguments={}) srv_msg.send_ctrl_cmd(cmd, exp_result=0) # check reservation hr_get = {"subnet-id": 1, "identifier-type": "hw-address", "identifier": "01:0a:0b:0c:0d:0e:0f"} response = _send_cmd("reservation-get", hr_get)["arguments"] assert response["boot-file-name"] == "/dev/null" assert response["client-classes"] == ["special_snowflake", "office"] assert response["hw-address"] == "01:0a:0b:0c:0d:0e:0f" assert response["ip-address"] == "192.168.50.205" assert response["option-data"] == [{"always-send": False, "code": 6, "csv-format": True, "data": "10.1.1.202,10.1.1.203", "name": "domain-name-servers", "space": "dhcp4"}] assert response["server-hostname"] == "hal9000" # check lease lease_get = {"hw-address": "ff:01:02:03:ff:04"} resp = _send_cmd("lease4-get-by-hw-address", lease_get)["arguments"] assert resp["leases"][0]["hw-address"] == "ff:01:02:03:ff:04" assert resp["leases"][0]["ip-address"] == "192.168.50.10" assert resp["leases"][0]["state"] == 0 assert resp["leases"][0]["subnet-id"] == 1 assert resp["leases"][0]["valid-lft"] == 4000
def clear_all_reservations(): db_name = world.f_cfg.db_name db_user = world.f_cfg.db_user db_passwd = world.f_cfg.db_passwd command = 'for table_name in dhcp4_options dhcp6_options ipv6_reservations hosts lease4 lease6; do psql -U {db_user} -d {db_name} -c "delete from $table_name" ; done'.format(**locals()) fabric_run_command(command)
def clear_leases(): fabric_download_file(world.cfg['leases'], world.cfg["test_result_dir"] + '/dhcpd6.leases') fabric_remove_file_command(world.cfg["leases"]) fabric_run_command('echo y |rm ' + world.cfg['leases'])
def clear_leases(): fabric_download_file(world.cfg['leases'], world.cfg["dir_name"] + '/dhcpd6.leases') fabric_remove_file_command(world.cfg["leases"]) fabric_run_command('echo y |rm ' + world.cfg['leases'])
def _create_pgsql_dump(): # dump for postrgesql is bit different because we do not have config backend for postgres # but we will still use kea 1.6.3 to generate this. In future we will have to create new dump with # config backend data srv_msg.remove_file_from_server('$(SOFTWARE_INSTALL_PATH)/pg_db_v6.sql') world.f_cfg.multi_threading_enabled = False misc.test_setup() srv_control.config_srv_subnet('2001:db8:1::/64', '2001:db8:1::10-2001:db8:1::10') srv_control.config_srv_prefix('2001:db8:2::', 0, 90, 96) srv_control.add_hooks('libdhcp_host_cmds.so') srv_control.add_hooks('libdhcp_cb_cmds.so') srv_control.add_hooks('libdhcp_mysql_cb.so') srv_control.open_control_channel() srv_control.agent_control_channel('$(MGMT_ADDRESS)') srv_control.enable_db_backend_reservation('PostgreSQL') srv_control.define_temporary_lease_db_backend('postgresql') srv_control.build_and_send_config_files() srv_control.start_srv('DHCP', 'started') hr = { "reservation": { "subnet-id": 1, "duid": "01:02:03:04:05:06:07:08:09:0A", "ip-addresses": ["2001:db8:1::100"], "prefixes": ["2001:db8:2:abcd::/64"], "hostname": "foo.example.com", "option-data": [{ "name": "vendor-opts", "data": "4491" }] } } _send_cmd("reservation-add", hr) misc.test_procedure() srv_msg.client_sets_value('Client', 'DUID', '00:03:00:01:f6:f5:f4:f3:f2:01') srv_msg.client_sets_value('Client', 'ia_id', 55701) srv_msg.client_sets_value('Client', 'ia_pd', 76159) srv_msg.client_does_include('Client', 'IA-PD') srv_msg.client_does_include('Client', 'client-id') srv_msg.client_does_include('Client', 'IA-NA') srv_msg.client_send_msg('SOLICIT') misc.pass_criteria() srv_msg.send_wait_for_message('MUST', 'ADVERTISE') misc.test_procedure() srv_msg.client_copy_option('server-id') srv_msg.client_copy_option('IA_NA') srv_msg.client_copy_option('IA_PD') srv_msg.client_sets_value('Client', 'DUID', '00:03:00:01:f6:f5:f4:f3:f2:01') srv_msg.client_does_include('Client', 'client-id') srv_msg.client_send_msg('REQUEST') misc.pass_criteria() srv_msg.send_wait_for_message('MUST', 'REPLY') srv_msg.response_check_include_option(3) srv_msg.response_check_option_content(3, 'sub-option', 5) srv_msg.response_check_suboption_content(5, 3, 'addr', '2001:db8:1::10') # wanted to do this with fabric_sudo_command(cmd, sudo_user='******' but it failed cmd = "sudo -S -u postgres pg_dump %s >%s/pg_db_v6.sql" % ( world.f_cfg.db_name, world.f_cfg.software_install_path) fabric_run_command(cmd, ignore_errors=False, destination_host=world.f_cfg.mgmt_address) srv_msg.execute_shell_cmd( "sed -i 's/$(DB_USER)/!db_user!/g' %s/pg_db_v6.sql" % world.f_cfg.software_install_path)