Coverage for testrail_api_reporter/engines/case_backup.py: 21%

1# -*- coding: utf-8 -*-

2""" TestRails backup module """

3

4 import os

5 from datetime import datetime

6

7 from ..utils.logger_config import setup_logger, DEFAULT_LOGGING_LEVEL

8 from ..utils.reporter_utils import delete_file, zip_file

9

10

11 class TCBackup:

12 """TestRails backup class"""

13

14 def __init__(

15 self,

16 test_rails_url,

17 test_rails_username,

18 test_rails_password,

19 test_rails_suite,

20 cleanup_needed=True,

21 backup_filename="backup.xml",

22 cookie_name="cookie.txt",

23 logger=None,

24 log_level=DEFAULT_LOGGING_LEVEL,

25 ):

26 """

27 General init

28

29 :param test_rails_url: TestRails server url, string, i.e. https://wwakabobik.testrails.io

30 :param test_rails_username: TestRails username, string, i.e. my@email.com, should have rights to make exports

31 :param test_rails_password: TestRails password, string

32 :param test_rails_suite: TestRails suite which needs to be downloaded, i.e. 42

33 :param backup_filename: output backup file name, string, optional, by default it is backup.xml

34 :param cookie_name: filename where TestRail cookie will be stored, string, by default is cookie.txt

35 :param cleanup_needed: delete or not cookie file after backup, bool, True or False, by default is True

36 :param logger: logger object, optional

37 :param log_level: logging level, optional, by default is 'logging.DEBUG'

38 """

39 if not logger:

40 self.___logger = setup_logger(name="TCBackup", log_file="TCBackup.log", level=log_level)

41 else:

42 self.___logger = logger

43 self.___logger.debug("Initializing TestRails Backup")

44 # TestRails

45 self.__url = test_rails_url

46 self.__username = test_rails_username

47 self.__password = test_rails_password

48 self.__suite = test_rails_suite

49 # Service

50 self.__cleanup_needed = cleanup_needed

51 self.__backup_filename = backup_filename

52 self.__cookie_name = cookie_name

53

54 # TestRails part

55 def __get_tr_cookie(self):

56 """

57 Login into TestRails and save session cookie

58

59 :return: None

60 """

61 self.___logger.debug("Get cookie %s from %s for %s", self.__cookie_name, self.__url, self.__username)

62 os.popen(

63 f"curl -c {self.__cookie_name} "

64 f'-H "Content-Type: application/x-www-form-urlencoded" '

65 f'-d "name={self.__username}&password={self.__password}" -X POST '

66 f'"{self.__url}/index.php?/auth/login"'

67 ).read()

68

69 def __download_tr_xml(self, filename=None, suite=None):

70 """

71 Download from TestRails XML file with testcases of testsuite

72

73 :param filename: output backup file name, string, optional, by default it is backup.xml

74 :param suite: TestRails suite which needs to be downloaded, i.e., 42

75 :return: backup filename

76 """

77 if not filename:

78 filename = self.__backup_filename

79 if not suite:

80 suite = self.__suite

81 self.___logger.debug("Download XML %s from from %s", filename, self.__url)

82 os.popen(

83 f"curl -b {self.__cookie_name} " f'"{self.__url}/index.php?/suites/export/{suite}" ' f"--output {filename}"

84 ).read()

85 return filename

86

87 def get_backup(self, filename=None, suite=None):

88 """

89 Download from TestRails backup file and deletes cookie if needed

90

91 :param filename: output backup file name, string, optional, by default it is backup.xml

92 :param suite: TestRails suite which needs to be downloaded, i.e., 42

93 :return: backup filename

94 """

95 if not filename:

96 filename = self.__backup_filename

97 if not suite:

98 suite = self.__suite

99 self.__get_tr_cookie()

100 backup_file = self.__download_tr_xml(filename=filename, suite=suite)

101 if self.__cleanup_needed:

102 delete_file(

103 filename=self.__cookie_name,

104 debug=self.___logger.debug == DEFAULT_LOGGING_LEVEL,

105 logger=self.___logger,

106 )

107 return backup_file

108

109 def get_archive_backup(self, filename=None, suite=None, suffix=f'_{datetime.today().strftime("%A")}'):

110 """

111 Download from TestRails backup file, add it to ZIP and deletes original backup file if needed

112

113 :param filename: output backup file name, string, optional, by default it is backup.xml

114 :param suite: TestRails suite which needs to be downloaded, i.e., 42

115 :param suffix: suffix for backup archive, by default it "_DayOfWeek"

116 :return: backup filename

117 """

118 if not filename:

119 filename = self.__backup_filename

120 if not suite:

121 suite = self.__suite

122 self.get_backup(filename=filename, suite=suite)

123 backup_file = zip_file(

124 filename=filename,

125 suffix=suffix,

126 debug=self.___logger.debug == DEFAULT_LOGGING_LEVEL,

127 logger=self.___logger,

128 )

129 if self.__cleanup_needed:

130 delete_file(filename=filename, debug=self.___logger.debug == DEFAULT_LOGGING_LEVEL)

131 return backup_file