blob: 062c38c9097258adb75dd43f34636faea5f49bb9 [file] [log] [blame]
Dan Shi4c90cb92013-03-07 15:48:02 -08001#!/usr/bin/python
2
3"""Tests for site_sysinfo."""
4
5__author__ = 'dshi@google.com (Dan Shi)'
6
7import common
8import os
9import random
10import unittest
11from autotest_lib.client.bin import site_sysinfo
12from autotest_lib.client.common_lib import autotemp
13
14
15class diffable_logdir_test(unittest.TestCase):
16 """Tests for methods in class diffable_logdir."""
17
18
19 def setUp(self):
20 """Initialize a temp direcotry with test files."""
21 self.tempdir = autotemp.tempdir(unique_id='diffable_logdir')
22 self.src_dir = os.path.join(self.tempdir.name, 'src')
23 self.dest_dir = os.path.join(self.tempdir.name, 'dest')
24
25 self.existing_files = ['existing_file_'+str(i) for i in range(3)]
26 self.existing_files_folder = ['', 'sub', 'sub/sub2']
27 self.existing_files_path = [os.path.join(self.src_dir, folder, f)
28 for f,folder in zip(self.existing_files,
29 self.existing_files_folder)]
30 self.new_files = ['new_file_'+str(i) for i in range(2)]
31 self.new_files_folder = ['sub', 'sub/sub3']
32 self.new_files_path = [os.path.join(self.src_dir, folder, f)
33 for f,folder in zip(self.new_files,
34 self.new_files_folder)]
35
36 # Create some file with random data in source directory.
37 for p in self.existing_files_path:
38 self.append_text_to_file(str(random.random()), p)
39
40
41 def tearDown(self):
42 """Clearn up."""
43 self.tempdir.clean()
44
45
46 def append_text_to_file(self, text, file_path):
47 """Append text to the end of a file, create the file if not existed.
48
49 @param text: text to be appended to a file.
50 @param file_path: path to the file.
51
52 """
53 dir_name = os.path.dirname(file_path)
54 if not os.path.exists(dir_name):
55 os.makedirs(dir_name)
56 with open(file_path, 'a') as f:
57 f.write(text)
58
59
60 def test_diffable_logdir_success(self):
61 """Test the diff function to save new data from a directory."""
62 info = site_sysinfo.diffable_logdir(self.src_dir,
63 keep_file_hierarchy=False,
64 append_diff_in_name=False)
65 # Run the first time to collect file status.
66 info.run(self.dest_dir)
67
68 # Add new files to the test directory.
69 for file_name, file_path in zip(self.new_files,
70 self.new_files_path):
71 self.append_text_to_file(file_name, file_path)
72
73 # Temp file for existing_file_2, used to hold on the inode. If the
74 # file is deleted and recreated, its inode might not change.
75 existing_file_2 = self.existing_files_path[2]
76 existing_file_2_tmp = existing_file_2 + '_tmp'
77 os.rename(existing_file_2, existing_file_2_tmp)
78
79 # Append data to existing file.
80 for file_name, file_path in zip(self.existing_files,
81 self.existing_files_path):
82 self.append_text_to_file(file_name, file_path)
83
84 # Remove the tmp file.
85 os.remove(existing_file_2_tmp)
86
87 # Run the second time to do diff.
88 info.run(self.dest_dir)
89
90 # Validate files in dest_dir.
91 for file_name, file_path in zip(self.existing_files+self.new_files,
92 self.existing_files_path+self.new_files_path):
93 file_path = file_path.replace('src', 'dest')
94 with open(file_path, 'r') as f:
95 self.assertEqual(file_name, f.read())
96
97
98if __name__ == '__main__':
99 unittest.main()