result_info_unittest.py revision 393fc8c903d61bad72ad0ab13d56955ac2888912
1#!/usr/bin/python 2# Copyright 2017 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""unittest for result_info.py 7""" 8 9import copy 10import os 11import shutil 12import tempfile 13import unittest 14 15import common 16from autotest_lib.client.bin.result_tools import utils_lib 17from autotest_lib.client.bin.result_tools import result_info 18 19_SIZE = 100 20_EXPECTED_SUMMARY = { 21 '': {utils_lib.ORIGINAL_SIZE_BYTES: 13 * _SIZE, 22 utils_lib.TRIMMED_SIZE_BYTES: 4 * _SIZE, 23 utils_lib.DIRS: [ 24 {'file1': {utils_lib.ORIGINAL_SIZE_BYTES: _SIZE}}, 25 {'folder1': {utils_lib.ORIGINAL_SIZE_BYTES: 11 * _SIZE, 26 utils_lib.TRIMMED_SIZE_BYTES: 2 * _SIZE, 27 utils_lib.DIRS: [ 28 {'file2': { 29 utils_lib.ORIGINAL_SIZE_BYTES: 10 * _SIZE, 30 utils_lib.TRIMMED_SIZE_BYTES: _SIZE}}, 31 {'file3': { 32 utils_lib.ORIGINAL_SIZE_BYTES: _SIZE}}]}}, 33 {'folder2': {utils_lib.ORIGINAL_SIZE_BYTES: _SIZE, 34 utils_lib.DIRS: 35 [{'file2': { 36 utils_lib.ORIGINAL_SIZE_BYTES: _SIZE} 37 }], 38 } 39 } 40 ] 41 } 42 } 43 44class ResultInfoUnittest(unittest.TestCase): 45 """Test class for ResultInfo. 46 """ 47 48 def setUp(self): 49 """Setup directory for test.""" 50 self.test_dir = tempfile.mkdtemp() 51 52 def tearDown(self): 53 """Cleanup the test directory.""" 54 shutil.rmtree(self.test_dir, ignore_errors=True) 55 56 def testLoadJson(self): 57 """Test method load_summary_json_file and related update methods.""" 58 summary_file = os.path.join(self.test_dir, 'summary.json') 59 result_info.save_summary(_EXPECTED_SUMMARY, summary_file) 60 summary = result_info.load_summary_json_file(summary_file) 61 62 self.assertEqual(_EXPECTED_SUMMARY, summary, 63 'Failed to match the loaded json file.') 64 # Save the json of the new summary, confirm it matches the old one. 65 summary_file_new = os.path.join(self.test_dir, 'summary_new.json') 66 result_info.save_summary(summary, summary_file_new) 67 summary_new = result_info.load_summary_json_file(summary_file_new) 68 self.assertEqual( 69 _EXPECTED_SUMMARY, summary_new, 70 'Failed to match the loaded json file after it is saved again.') 71 72 # Validate the details of result_info. 73 self.assertEqual(summary.path, self.test_dir) 74 self.assertEqual(summary.get_file('file1').path, 75 os.path.join(self.test_dir, 'file1')) 76 self.assertEqual(summary.get_file('folder1').get_file('file2').path, 77 os.path.join(self.test_dir, 'folder1', 'file2')) 78 79 # Validate the details of a deep copy of result_info. 80 new_summary = copy.deepcopy(summary) 81 # Modify old summary, to make sure the clone is not changed. 82 summary._path = None 83 summary.get_file('file1')._path = None 84 summary.get_file('file1').original_size = 0 85 summary.get_file('folder1').get_file('file2')._path = None 86 87 self.assertEqual(new_summary.path, self.test_dir) 88 self.assertEqual(new_summary.get_file('file1').path, 89 os.path.join(self.test_dir, 'file1')) 90 self.assertEqual(new_summary.get_file('file1').original_size, _SIZE) 91 self.assertEqual(id(new_summary.get_file('file1')._parent_result_info), 92 id(new_summary)) 93 self.assertNotEqual(id(new_summary), id(summary)) 94 self.assertEqual(new_summary.get_file('folder1').get_file('file2').path, 95 os.path.join(self.test_dir, 'folder1', 'file2')) 96 97 def testInit(self): 98 """Test __init__ method fails when both name and original_info are set. 99 """ 100 self.assertRaises(result_info.ResultInfoError, result_info.ResultInfo, 101 'parent_dir', 'file_name', None, 102 {'name': 'file_name'}) 103 104 def testUpdateSize(self): 105 """Test sizes updated in all parent nodes after leaf node is updated. 106 """ 107 summary_file = os.path.join(self.test_dir, 'summary.json') 108 result_info.save_summary(_EXPECTED_SUMMARY, summary_file) 109 summary = result_info.load_summary_json_file(summary_file) 110 file2 = summary.get_file('folder1').get_file('file2') 111 file2.trimmed_size = 2 * _SIZE 112 113 # Check all parent result info have size updated. 114 self.assertEqual(file2.trimmed_size, 2 * _SIZE) 115 self.assertEqual(summary.get_file('folder1').trimmed_size, 3 * _SIZE) 116 self.assertEqual(summary.trimmed_size, 5 * _SIZE) 117 118 file2.original_size = 11 * _SIZE 119 self.assertEqual(summary.get_file('folder1').original_size, 12 * _SIZE) 120 self.assertEqual(summary.original_size, 14 * _SIZE) 121 122 file2.collected_size = 20 * _SIZE 123 self.assertEqual(summary.get_file('folder1').collected_size, 21 * _SIZE) 124 self.assertEqual(summary.collected_size, 23 * _SIZE) 125 126 127# this is so the test can be run in standalone mode 128if __name__ == '__main__': 129 """Main""" 130 unittest.main() 131