1#!/usr/bin/env python 2# 3# Copyright (C) 2018 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17import datetime 18import mock 19import os 20import unittest 21 22from vts.utils.python.reporting import report_file_utils 23 24 25def simple_os_walk(source_dir, followlinks): 26 """mock function created for os.walk""" 27 root = "/root" 28 dirs = ("dir1", "dir2") 29 files = ("dir1/file1_1.txt", "dir1/file1_2.txt", "dir1/file1_3.txt", 30 "dir2/file2_1") 31 return [(root, dirs, files)] 32 33 34def simple_PushReportFileGcs(src_path, dest_path): 35 """mock function created for _PushReportFileGcs""" 36 return 37 38 39def simple_tempfile_mkdtemp(): 40 """mock function for tempfile.mkdtemp""" 41 return 'temp_dir' 42 43 44class ReportFileUtilsTest(unittest.TestCase): 45 """Unit tests for report_file_utils module""" 46 47 def setUp(self): 48 """setup tasks""" 49 self.category = "category_default" 50 self.name = "name_default" 51 52 def testInitializationDefault(self): 53 """Tests the default setting initialization of a UploadGcs object.""" 54 _report_file_util = report_file_utils.ReportFileUtil() 55 self.assertEqual(_report_file_util._flatten_source_dir, False) 56 self.assertEqual(_report_file_util._use_destination_date_dir, False) 57 self.assertEqual(_report_file_util._source_dir, None) 58 self.assertEqual(_report_file_util._destination_dir, None) 59 self.assertEqual(_report_file_util._url_prefix, None) 60 61 def testInitializationCustom(self): 62 """Tests the custom setting initialization of a UploadGcs object.""" 63 _report_file_util = report_file_utils.ReportFileUtil( 64 flatten_source_dir=True, 65 use_destination_date_dir=True, 66 destination_dir="vts-log", 67 url_prefix="https://storage.cloud.google.com/vts-log/") 68 69 self.assertEqual(_report_file_util._flatten_source_dir, True) 70 self.assertEqual(_report_file_util._use_destination_date_dir, True) 71 self.assertEqual(_report_file_util._source_dir, None) 72 self.assertEqual(_report_file_util._destination_dir, "vts-log") 73 self.assertEqual(_report_file_util._url_prefix, 74 "https://storage.cloud.google.com/vts-log/") 75 76 def testConvertReportPath(self): 77 """Tests _ConvertReportPath.""" 78 _report_file_util = report_file_utils.ReportFileUtil( 79 flatten_source_dir=False, 80 use_destination_date_dir=False, 81 destination_dir="vts-log", 82 url_prefix="https://storage.cloud.google.com/vts-log/") 83 _report_file_util._use_gcs = True 84 85 src_path = "dir1/dir1_1/dir_1_1_1/file1" 86 root_dir = "dir1/dir1_1" 87 file_name_prefix = "" 88 dest_path, url = _report_file_util._ConvertReportPath( 89 src_path=src_path, 90 root_dir=root_dir, 91 file_name_prefix=file_name_prefix) 92 93 self.assertEqual( 94 dest_path, 95 os.path.join( 96 os.path.relpath(os.path.dirname(src_path), root_dir), 97 os.path.basename(src_path))) 98 self.assertEqual( 99 url, 100 os.path.join( 101 _report_file_util._url_prefix, 102 os.path.join( 103 os.path.relpath(os.path.dirname(src_path), root_dir), 104 os.path.basename(src_path)))) 105 106 def testConvertReportPathFlatten(self): 107 """Tests _ConvertReportPath with flattened source dir.""" 108 _report_file_util = report_file_utils.ReportFileUtil( 109 flatten_source_dir=True, 110 use_destination_date_dir=False, 111 destination_dir="vts-log", 112 url_prefix="https://storage.cloud.google.com/vts-log/") 113 _report_file_util._use_gcs = True 114 115 src_path = "dir1/dir1_1/dir_1_1_1/file1" 116 root_dir = "dir1/dir1_1" 117 file_name_prefix = "" 118 dest_path, url = _report_file_util._ConvertReportPath( 119 src_path=src_path, 120 root_dir=root_dir, 121 file_name_prefix=file_name_prefix) 122 123 self.assertEqual(dest_path, os.path.basename(src_path)) 124 self.assertEqual( 125 url, 126 os.path.join(_report_file_util._url_prefix, 127 os.path.basename(src_path))) 128 129 def testConvertReportPathFlattenDateDir(self): 130 """Tests _ConvertReportPath with flattened source dir and date dir.""" 131 _report_file_util = report_file_utils.ReportFileUtil( 132 flatten_source_dir=True, 133 use_destination_date_dir=True, 134 destination_dir="vts-log", 135 url_prefix="https://storage.cloud.google.com/vts-log/") 136 _report_file_util._use_gcs = True 137 138 src_path = "dir1/dir1_1/dir_1_1_1/file1" 139 root_dir = "dir1/dir1_1" 140 file_name_prefix = "" 141 dest_path, url = _report_file_util._ConvertReportPath( 142 src_path=src_path, 143 root_dir=root_dir, 144 file_name_prefix=file_name_prefix) 145 146 self.assertEqual( 147 dest_path, 148 os.path.join(datetime.datetime.now().strftime('%Y-%m-%d'), 149 os.path.basename(src_path))) 150 self.assertEqual( 151 url, 152 os.path.join( 153 _report_file_util._url_prefix, 154 os.path.join(datetime.datetime.now().strftime('%Y-%m-%d'), 155 os.path.basename(src_path)))) 156 157 @mock.patch('tempfile.mkdtemp', side_effect=simple_tempfile_mkdtemp) 158 def testPushReportFileGcs(self, simple_tempfile_mkdtemp): 159 """Tests the _PushReportFileGcs function with mocking""" 160 _report_file_util = report_file_utils.ReportFileUtil() 161 _report_file_util._gcs_api_utils = mock.MagicMock() 162 _report_file_util._gcs_available = True 163 report_file_utils.shutil = mock.MagicMock() 164 _report_file_util._PushReportFileGcs("src_path", "dest_path") 165 _report_file_util._gcs_api_utils.UploadFile.assert_called_with( 166 "temp_dir/src_path", "dest_path") 167 report_file_utils.shutil.copy.assert_called_with( 168 'src_path', 'temp_dir') 169 report_file_utils.shutil.rmtree.assert_called_with('temp_dir') 170 171 @mock.patch( 172 'vts.utils.python.reporting.report_file_utils.ReportFileUtil._PushReportFileGcs', 173 side_effect=simple_PushReportFileGcs) 174 @mock.patch('os.walk', side_effect=simple_os_walk) 175 def testSaveReportsFromDirectory(self, simple_os_walk, 176 simple_PushReportFileGcs): 177 """Tests the SaveReportsFromDirectory function with mocking""" 178 _report_file_util = report_file_utils.ReportFileUtil( 179 flatten_source_dir=False, 180 use_destination_date_dir=False, 181 destination_dir="vts-log") 182 _report_file_util._use_gcs = True 183 184 source_dir = "/root" 185 file_name_prefix = "prefix_" 186 urls = _report_file_util.SaveReportsFromDirectory( 187 source_dir=source_dir, 188 file_name_prefix=file_name_prefix, 189 file_path_filters=None) 190 simple_os_walk.assert_called_with(source_dir, followlinks=False) 191 self.assertEqual(simple_PushReportFileGcs.call_count, 4) 192 193 194if __name__ == "__main__": 195 unittest.main() 196