Enable GCS to handle dynamic log file uploads.
Test: util/python/reporting/report_file_utils_test.py
Bug: 112607823
Change-Id: I006f8ac37cbd1b52d98d51a088d0a08fa315e709
diff --git a/utils/python/reporting/report_file_utils.py b/utils/python/reporting/report_file_utils.py
index 8dd2e42..39e974e 100644
--- a/utils/python/reporting/report_file_utils.py
+++ b/utils/python/reporting/report_file_utils.py
@@ -17,6 +17,7 @@
import logging
import os
import shutil
+import tempfile
from vts.utils.python.common import cmd_utils
from vts.utils.python.gcs import gcs_api_utils
@@ -167,10 +168,22 @@
src_path = NotNoneStr(src_path)
dest_path = NotNoneStr(dest_path)
+ # Copy snapshot to temp as GCS will not handle dynamic files.
+ temp_dir = tempfile.mkdtemp()
+ shutil.copy(src_path, temp_dir)
+ src_path = os.path.join(temp_dir, os.path.basename(src_path))
+ logging.debug('Snapshot of logs: %s', src_path)
+
try:
self._gcs_api_utils.UploadFile(src_path, dest_path)
except IOError as e:
logging.exception(e)
+ finally:
+ logging.debug('removing temporary directory')
+ try:
+ shutil.rmtree(temp_dir)
+ except OSError as e:
+ logging.exception(e)
def SaveReport(self, src_path, new_file_name=None, file_name_prefix=None):
'''Save report file to destination.
diff --git a/utils/python/reporting/report_file_utils_test.py b/utils/python/reporting/report_file_utils_test.py
index fe7ec1f..529afea 100644
--- a/utils/python/reporting/report_file_utils_test.py
+++ b/utils/python/reporting/report_file_utils_test.py
@@ -22,11 +22,6 @@
from vts.utils.python.reporting import report_file_utils
-def simple_PushReportFileGcs(src_path, dest_path):
- """mock function created for _PushReportFileGcs"""
- return
-
-
def simple_os_walk(source_dir, followlinks):
"""mock function created for os.walk"""
root = "/root"
@@ -36,6 +31,16 @@
return [(root, dirs, files)]
+def simple_PushReportFileGcs(src_path, dest_path):
+ """mock function created for _PushReportFileGcs"""
+ return
+
+
+def simple_tempfile_mkdtemp():
+ """mock function for tempfile.mkdtemp"""
+ return 'temp_dir'
+
+
class ReportFileUtilsTest(unittest.TestCase):
"""Unit tests for report_file_utils module"""
@@ -149,14 +154,19 @@
os.path.join(datetime.datetime.now().strftime('%Y-%m-%d'),
os.path.basename(src_path))))
- def testPushReportFileGcs(self):
+ @mock.patch('tempfile.mkdtemp', side_effect=simple_tempfile_mkdtemp)
+ def testPushReportFileGcs(self, simple_tempfile_mkdtemp):
"""Tests the _PushReportFileGcs function with mocking"""
_report_file_util = report_file_utils.ReportFileUtil()
_report_file_util._gcs_api_utils = mock.MagicMock()
_report_file_util._gcs_available = True
+ report_file_utils.shutil = mock.MagicMock()
_report_file_util._PushReportFileGcs("src_path", "dest_path")
_report_file_util._gcs_api_utils.UploadFile.assert_called_with(
- "src_path", "dest_path")
+ "temp_dir/src_path", "dest_path")
+ report_file_utils.shutil.copy.assert_called_with(
+ 'src_path', 'temp_dir')
+ report_file_utils.shutil.rmtree.assert_called_with('temp_dir')
@mock.patch(
'vts.utils.python.reporting.report_file_utils.ReportFileUtil._PushReportFileGcs',