def test_create_archive_include_all_with_config_option(self): yaml_config = self._get_yaml_config() debug_collector = DebugInfoCollector(include_logs=True, include_configs=True, include_content=True, include_system_info=True, include_shell_commands=True, config_file=yaml_config) archive_path = debug_collector.create_archive() extract_path = tempfile.mkdtemp() self._verify_archive(archive_path=archive_path, extract_path=extract_path, required_directories=['logs', 'configs', 'content', 'commands']) # Verify commands output have been copied commands_path = os.path.join(extract_path, 'commands') command_files = os.listdir(commands_path) self.assertTrue(len(command_files), 2) # Verify command output file names self.assertTrue('echofoo.txt' in command_files) self.assertTrue('echobar12.txt' in command_files) # Verify file contents with open(os.path.join(commands_path, 'echofoo.txt')) as f: expected_content = '[BEGIN STDOUT]\nfoo\n[END STDOUT]\n[BEGIN STDERR]\n[END STDERR]' self.assertEqual(expected_content, f.read()) with open(os.path.join(commands_path, 'echobar12.txt')) as f: expected_content = '[BEGIN STDOUT]\n[END STDOUT]\n[BEGIN STDERR]\nbar\n[END STDERR]' self.assertEqual(expected_content, f.read())
def test_create_archive_exclusion(self): # Verify only system info file is included debug_collector = DebugInfoCollector(include_logs=False, include_configs=False, include_content=False, include_system_info=True) archive_path = debug_collector.create_archive() # Verify archive has been created self.assertTrue(os.path.isfile(archive_path)) self.to_delete_files.append(archive_path) extract_path = tempfile.mkdtemp() self.to_delete_directories.append(extract_path) self._extract_archive(archive_path=archive_path, extract_path=extract_path) # Verify system info file is there and other directories are empty directories = ['logs', 'configs', 'content'] for directory_name in directories: full_path = os.path.join(extract_path, directory_name) files = os.listdir(full_path) self.assertEqual(len(files), 0) full_path = os.path.join(extract_path, 'system_info.yaml') self.assertTrue(os.path.isfile(full_path))
def test_create_archive_deletes_temp_dir(self): debug_collector = DebugInfoCollector(include_logs=True, include_configs=True, include_content=True, include_system_info=True) archive_path = debug_collector.create_archive() self.to_delete_files.append(archive_path) self.assertTrue(debug_collector._temp_dir_path) self.assertTrue(not os.path.exists(debug_collector._temp_dir_path))
def test_create_archive_include_all(self): debug_collector = DebugInfoCollector(include_logs=True, include_configs=True, include_content=True, include_system_info=True) archive_path = debug_collector.create_archive() extract_path = tempfile.mkdtemp() self._verify_archive(archive_path=archive_path, extract_path=extract_path, required_directories=['logs', 'configs', 'content'])
def test_config_option_overrides_defaults(self): config = { 'log_file_paths': ['log/path/1', 'log/path/1'], 'st2_config_file_path': 'st2/config/path', 's3_bucket_url': 'my_s3_url', 'gpg_key_fingerprint': 'my_gpg_fingerprint', 'gpg_key': 'my_gpg_key', 'shell_commands': ['command 1', 'command 2'], 'company_name': 'MyCompany' } debug_collector = DebugInfoCollector(include_logs=True, include_configs=True, include_content=True, include_system_info=True, config_file=config) self.assertEqual(debug_collector.log_file_paths, ['log/path/1', 'log/path/1']) self.assertEqual(debug_collector.st2_config_file_path, 'st2/config/path') self.assertEqual(debug_collector.st2_config_file_name, 'path') self.assertEqual(debug_collector.s3_bucket_url, 'my_s3_url') self.assertEqual(debug_collector.gpg_key, 'my_gpg_key') self.assertEqual(debug_collector.gpg_key_fingerprint, 'my_gpg_fingerprint') self.assertEqual(debug_collector.shell_commands, ['command 1', 'command 2']) self.assertEqual(debug_collector.company_name, 'MyCompany')
def test_encrypt_archive(self): debug_collector = DebugInfoCollector(include_logs=True, include_configs=True, include_content=True, include_system_info=True) plaintext_archive_path = debug_collector.create_archive() plaintext_archive_size = os.stat(plaintext_archive_path).st_size encrypted_archive_path = debug_collector.encrypt_archive( archive_file_path=plaintext_archive_path) encrypt_archive_size = os.stat(encrypted_archive_path).st_size self.assertTrue(os.path.isfile(encrypted_archive_path)) self.assertTrue(encrypt_archive_size > plaintext_archive_size) self.assertRaises(Exception, archive_path=encrypted_archive_path, extract_path='/tmp')
def test_create_archive_include_all_with_config_option(self): yaml_config = self._get_yaml_config() debug_collector = DebugInfoCollector(include_logs=True, include_configs=True, include_content=True, include_system_info=True, include_shell_commands=True, config_file=yaml_config) archive_path = debug_collector.create_archive() extract_path = tempfile.mkdtemp() self._verify_archive( archive_path=archive_path, extract_path=extract_path, required_directories=['logs', 'configs', 'content', 'commands']) # Verify commands output have been copied commands_path = os.path.join(extract_path, 'commands') command_files = os.listdir(commands_path) self.assertEqual(len(command_files), 3) # Verify command output file names self.assertIn('echofoo.txt', command_files) self.assertIn('echobar12.txt', command_files) self.assertIn('pwd.txt', command_files) # Verify "cwd" is set correctly for the shells where commands run file_path = os.path.join(commands_path, 'pwd.txt') with open(file_path, 'r') as fp: content = fp.read() # cwd for the process where commands run should be set to temporary directory where # commands output is stored expected_cwd_path = os.path.join(debug_collector._temp_dir_path, 'commands') self.assertIn(expected_cwd_path, content) # Verify file contents with open(os.path.join(commands_path, 'echofoo.txt')) as f: expected_content = '[BEGIN STDOUT]\nfoo\n[END STDOUT]\n[BEGIN STDERR]\n[END STDERR]' self.assertEqual(expected_content, f.read()) with open(os.path.join(commands_path, 'echobar12.txt')) as f: expected_content = '[BEGIN STDOUT]\n[END STDOUT]\n[BEGIN STDERR]\nbar\n[END STDERR]' self.assertEqual(expected_content, f.read())
def test_encrypt_archive_with_custom_gpg_key(self): yaml_config = self._get_yaml_config() debug_collector = DebugInfoCollector(include_logs=True, include_configs=True, include_content=True, include_system_info=True, include_shell_commands=True, config_file=yaml_config) plaintext_archive_path = debug_collector.create_archive() plaintext_archive_size = os.stat(plaintext_archive_path).st_size encrypted_archive_path = debug_collector.encrypt_archive( archive_file_path=plaintext_archive_path) encrypt_archive_size = os.stat(encrypted_archive_path).st_size self.assertTrue(os.path.isfile(encrypted_archive_path)) self.assertTrue(encrypt_archive_size > plaintext_archive_size) self.assertRaises(Exception, archive_path=encrypted_archive_path, extract_path='/tmp')
def test_create_archive_include_all_with_config_option(self): yaml_config = self._get_yaml_config() debug_collector = DebugInfoCollector(include_logs=True, include_configs=True, include_content=True, include_system_info=True, include_shell_commands=True, config_file=yaml_config) archive_path = debug_collector.create_archive() extract_path = tempfile.mkdtemp() self._verify_archive(archive_path=archive_path, extract_path=extract_path, required_directories=['logs', 'configs', 'content', 'commands']) # Verify commands output have been copied commands_path = os.path.join(extract_path, 'commands') command_files = os.listdir(commands_path) self.assertEqual(len(command_files), 3) # Verify command output file names self.assertTrue('echofoo.txt' in command_files) self.assertTrue('echobar12.txt' in command_files) self.assertTrue('pwd.txt' in command_files) # Verify "cwd" is set correctly for the shells where commands run file_path = os.path.join(commands_path, 'pwd.txt') with open(file_path, 'r') as fp: content = fp.read() # cwd for the process where commands run should be set to temporary directory where # commands output is stored expected_cwd_path = os.path.join(debug_collector._temp_dir_path, 'commands') self.assertTrue(expected_cwd_path in content) # Verify file contents with open(os.path.join(commands_path, 'echofoo.txt')) as f: expected_content = '[BEGIN STDOUT]\nfoo\n[END STDOUT]\n[BEGIN STDERR]\n[END STDERR]' self.assertEqual(expected_content, f.read()) with open(os.path.join(commands_path, 'echobar12.txt')) as f: expected_content = '[BEGIN STDOUT]\n[END STDOUT]\n[BEGIN STDERR]\nbar\n[END STDERR]' self.assertEqual(expected_content, f.read())