def test_get_analysis_file(self): result = self._submit() self.assertIsNotNone(result) self.assertTrue('result' in result) result = result['result'] self.assertIsNotNone(result['uuid']) uuid = result['uuid'] result = ace_api.get_analysis(uuid) self.assertIsNotNone(result) self.assertTrue('result' in result) result = result['result'] # first test getting a file by uuid file_uuid = None for o_uuid in result['observables']: o = result['observable_store'][o_uuid] if o['type'] == 'file' and o['value'] == 'sample.dat': file_uuid = o_uuid break self.assertIsNotNone(file_uuid) output_path = os.path.join(saq.SAQ_HOME, saq.TEMP_DIR, 'get_file_test.dat') self.assertTrue( ace_api.get_analysis_file(uuid, file_uuid, output_file=output_path)) with open(output_path, 'rb') as fp: self.assertEquals(fp.read(), b'Hello, world!') # same thing but with passing a file pointer with open(output_path, 'wb') as fp: self.assertTrue( ace_api.get_analysis_file(uuid, file_uuid, output_fp=fp)) # now test by using the file name self.assertTrue( ace_api.get_analysis_file(uuid, 'sample.dat', output_file=output_path)) with open(output_path, 'rb') as fp: self.assertEquals(fp.read(), b'Hello, world!')
def test_analysis_file_handling(self): analysis = ace_api.Analysis(description='Test Analysis') # add a normal file normal_file_path = self.create_test_file('normal.txt') analysis.add_file(normal_file_path) # add a normal file, passing in the file pointer fp_file_path = self.create_test_file('fp.txt') fp = open(fp_file_path, 'rb') analysis.add_file(fp_file_path, fp) # add a normal file but tell it to go into a subdirectory subdir_file_path = self.create_test_file('subdir.txt') analysis.add_file(subdir_file_path, relative_storage_path='subdir/subdir.txt') # add a file passing the contents as a string analysis.add_file('str.txt', 'This is a string.') # add a file passing the contents as a bytes analysis.add_file('bytes.txt', b'This is a bytes.') result = analysis.submit() # make sure it got our files io_buffer = io.BytesIO() ace_api.get_analysis_file(result.uuid, 'normal.txt', output_fp=io_buffer) with open(normal_file_path, 'rb') as fp: self.assertEquals(fp.read(), io_buffer.getvalue()) io_buffer = io.BytesIO() ace_api.get_analysis_file(result.uuid, 'fp.txt', output_fp=io_buffer) with open(fp_file_path, 'rb') as fp: self.assertEquals(fp.read(), io_buffer.getvalue()) #io_buffer = io.BytesIO() #ace_api.get_analysis_file(result.uuid, 'subdir/subdir.txt', output_fp=io_buffer) #with open(subdir_file_path, 'rb') as fp: #self.assertEquals(fp.read(), io_buffer.getvalue()) io_buffer = io.BytesIO() ace_api.get_analysis_file(result.uuid, 'str.txt', output_fp=io_buffer) self.assertEquals(b'This is a string.', io_buffer.getvalue()) io_buffer = io.BytesIO() ace_api.get_analysis_file(result.uuid, 'bytes.txt', output_fp=io_buffer) self.assertEquals(b'This is a bytes.', io_buffer.getvalue())