PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/xivo_acceptance/steps/files_steps.py

https://gitlab.com/xivo.solutions/xivo-acceptance
Python | 151 lines | 95 code | 41 blank | 15 comment | 11 complexity | 5b1023722e9cea718735a47342ed5194 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2013-2016 Avencall
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>
  16. import os
  17. import re
  18. from hamcrest import assert_that
  19. from hamcrest import equal_to
  20. from hamcrest import is_not
  21. from hamcrest import none
  22. from aloe import step, world
  23. from xivo_acceptance.helpers import file_helper
  24. from xivo_acceptance.lettuce import sysutils
  25. ASTERISK_VM_PATH = '/var/spool/asterisk/voicemail'
  26. BACKUP_DIR = '/var/backups/xivo'
  27. MOH_PATH = '/usr/share/asterisk/moh/default'
  28. DAHDI_PATH = '/dev/dahdi'
  29. ASTERISK_SOUND_PATH = '/usr/share/asterisk/sounds/en'
  30. @step(u'Given a backup file with name "([^"]*)"')
  31. def given_a_backup_file_with_name(step, filename):
  32. file_helper.create_backup_file(filename)
  33. @step(u'Given a musiconhold file with name "([^"]*)"')
  34. def given_a_musiconhold_file_with_name(step, filename):
  35. file_helper.create_musiconhold_file(filename)
  36. @step(u'Given a recording file with name "([^"]*)"')
  37. def given_a_recording_file_with_name(step, filename):
  38. file_helper.create_recordings_file(filename)
  39. @step(u'Given a recording meetme file with name "([^"]*)"')
  40. def given_a_recording_meetme_file_with_name(step, filename):
  41. file_helper.create_recordings_meetme_file(filename)
  42. @step(u'Given the file "([^"]*)" is empty')
  43. def given_the_file_is_empty(step, path):
  44. file_helper.create_empty_file(path)
  45. @step(u'Then directory of the Asterisk voicemail is empty')
  46. def then_directory_of_the_asterisk_voicemail_is_empty(step):
  47. assert sysutils.dir_is_empty(ASTERISK_VM_PATH)
  48. @step(u'Then Asterisk sound files correctly installed')
  49. def then_asterisk_sound_files_correctly_installed(step):
  50. assert not sysutils.dir_is_empty(ASTERISK_SOUND_PATH)
  51. @step(u'Then Asterisk owns /dev/dadhi')
  52. def then_asterisk_owns_dev_dadhi(step):
  53. text = sysutils.get_list_file(DAHDI_PATH)
  54. lines = text.split("\n")
  55. for line in lines:
  56. if line:
  57. file = '%s/%s' % (DAHDI_PATH, line.strip())
  58. assert sysutils.file_owned_by_user(file, 'asterisk')
  59. @step(u'Then MOH files owned by asterisk:www-data')
  60. def then_moh_files_owned_by_asterisk_www_data(step):
  61. command = ['apt-get', 'install', '-y', 'asterisk-moh-opsound-wav']
  62. sysutils.send_command(command)
  63. text = sysutils.get_list_file(MOH_PATH)
  64. lines = text.split("\n")
  65. for line in lines:
  66. if line:
  67. file = '%s/%s' % (MOH_PATH, line.strip())
  68. assert sysutils.file_owned_by_user(file, 'asterisk')
  69. assert sysutils.file_owned_by_group(file, 'www-data')
  70. @step(u'Then backup files successfully rotated')
  71. def then_backup_files_successfully_rotated(step):
  72. backuped_files = [os.path.join(BACKUP_DIR, 'data.tgz'),
  73. os.path.join(BACKUP_DIR, 'db.tgz')]
  74. rotated_files = [os.path.join(BACKUP_DIR, 'data.tgz.{num}'),
  75. os.path.join(BACKUP_DIR, 'db.tgz.{num}')]
  76. for file_ in backuped_files:
  77. sysutils.send_command(['rm', '-f', '{file_}*'.format(file_=file_)])
  78. sysutils.send_command(['touch', file_])
  79. sysutils.send_command(['/usr/sbin/logrotate', '-f', '/etc/logrotate.d/xivo-backup'])
  80. expected_files = backuped_files + [file_.format(num='1') for file_ in rotated_files]
  81. for expected_file in expected_files:
  82. assert sysutils.path_exists(expected_file)
  83. sysutils.send_command(['/usr/sbin/logrotate', '-f', '/etc/logrotate.d/xivo-backup'])
  84. expected_files = expected_files + [file_.format(num='2') for file_ in rotated_files]
  85. for expected_file in expected_files:
  86. assert sysutils.path_exists(expected_file)
  87. def _get_asterisk_pid():
  88. return sysutils.get_content_file('/var/run/asterisk/asterisk.pid')
  89. @step(u'Then max open file descriptors are equals to 8192')
  90. def then_max_open_file_descriptors_are_equals_to_8192(step):
  91. pid = _get_asterisk_pid().strip()
  92. cmd = ['grep', '\'Max open files\'', '/proc/%s/limits' % pid]
  93. string_limit = sysutils.output_command(cmd)
  94. limit = re.sub('\s+', ' ', string_limit).split()[3]
  95. assert_that(int(limit), equal_to(8192))
  96. @step(u'Then the mirror list contains a line matching "([^"]*)"')
  97. def then_the_mirror_list_contains_a_line_matching_group1(step, regex):
  98. match = _match_on_mirror_list(regex)
  99. assert_that(match, is_not(none()))
  100. @step(u'Then the mirror list does not contain a line matching "([^"]*)"')
  101. def then_the_mirror_list_does_not_contain_a_line_matching_group1(step, regex):
  102. match = _match_on_mirror_list(regex)
  103. assert_that(match, none())
  104. def _match_on_mirror_list(regex):
  105. output = sysutils.output_command(['apt-cache', 'policy'])
  106. return re.search(regex, output)
  107. @step(u'When I remove the directory "([^"]*)"')
  108. def when_i_remove_the_directory_group1(step, path):
  109. command = ['rm', '-rf', path]
  110. world.ssh_client_xivo.check_call(command)