test.py 15.4 KB
Newer Older
1
2
3
4
5
6
7
8
import subprocess
import unittest
import os
import shutil

import OSDManager
import div_util
import verify
Felix Seibert's avatar
Felix Seibert committed
9
import dirstatuspageparser
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import dataDistribution
import folder


class TestDataDistribution(unittest.TestCase):
    def setUp(self):
        self.osdList = ['osd1', 'osd2', 'osd3']

        self.sixty_folders = []
        for i in range(0, 60):
            self.sixty_folders.append(folder.Folder('id' + str(i), 1, 'None'))

        self.six_folders = []
        for i in range(0, 6):
            self.six_folders.append(folder.Folder('id' + str(i), 1, 'None'))

        self.six_hundred_folders = []
        for i in range(0, 600):
            self.six_hundred_folders.append(folder.Folder('id' + str(i), 1, 'None'))

    def test_add_folders_no_osd_information(self):
        data_distribution = dataDistribution.DataDistribution()
        data_distribution.add_osd_list(self.osdList)

        data_distribution.add_folders(self.sixty_folders)
        total_osd_folder_sizes = []
        for osd in data_distribution.OSDs.values():
            total_osd_folder_sizes.append(osd.totalFolderSize)

        expected_osd_folder_sizes = [20, 20, 20]
        self.assertListEqual(expected_osd_folder_sizes, total_osd_folder_sizes)

    def test_add_folders(self):

        data_distribution = dataDistribution.DataDistribution()
        data_distribution.add_osd_list(self.osdList)

        osd_information = {}
        osd_1 = {'usable_space': 10, 'total_space': 10}
        osd_2 = {'usable_space': 20, 'total_space': 20}
        osd_3 = {'usable_space': 30, 'total_space': 30}

        osd_information['osd1'] = osd_1
        osd_information['osd2'] = osd_2
        osd_information['osd3'] = osd_3

        data_distribution.add_folders(self.sixty_folders, osd_information, 'total_space')

        total_osd_folder_sizes = {}
        for osd in data_distribution.OSDs.values():
            total_osd_folder_sizes[osd.uuid] = osd.totalFolderSize

        expected_osd_folder_sizes = {'osd1': 10, 'osd2': 20, 'osd3': 30}

        self.assertEqual(expected_osd_folder_sizes, total_osd_folder_sizes)


class TestDirStatusPageParser(unittest.TestCase):
    def test_feed(self):
Felix Seibert's avatar
Felix Seibert committed
69
        parser = dirstatuspageparser.DIRStatusPageParser()
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400

        html_data = open('../test_data/dir_status_page.html', 'r').read()

        parser.feed(html_data)

        filtered_data_sets = list(filter(lambda x: int(x['last updated'].split()[0]) != 0, parser.dataSets))
        filtered_data_sets = list(filter(lambda x: x['type'] == 'SERVICE_TYPE_OSD', filtered_data_sets))

        self.assertEqual(len(filtered_data_sets), 24)

        total_usable_space = 0
        total_space = 0

        for dataSet in filtered_data_sets:
            total_usable_space += int(dataSet['usable'])
            total_space += int(dataSet['total'].split()[0])

        self.assertGreaterEqual(total_space, total_usable_space)


class TestDivUtil(unittest.TestCase):
    def test_remove_leading_trailing_slashes(self):
        s = '/hello/world////'
        self.assertEqual(div_util.remove_leading_trailing_slashes(s), 'hello/world')

        s = '/just_one_tile'
        self.assertEqual(div_util.remove_leading_trailing_slashes(s), 'just_one_tile')

    def test_extract_volume_information(self):
        test_string = open('../test_data/example_volume_information', 'r').read()
        volume_information = div_util.extract_volume_information(test_string)

        self.assertEqual('xfs_volume', volume_information[0])

        expected_osd_list = []
        expected_osd_list.append(('flx-local-osd-1', '130.73.78.157'))
        expected_osd_list.append(('flx-local-osd-2', '130.73.78.157'))
        expected_osd_list.append(('flx-local-osd-3', '130.73.78.157'))
        self.assertEqual(expected_osd_list, volume_information[1])

        self.assertEqual('1000,1004', volume_information[2])

        self.assertEqual('localhost:32638', volume_information[3])

    def test_get_http_address(self):
        test_address = 'localhost:32638'

        self.assertEqual('http://localhost:30638/', div_util.get_http_address(test_address))

        test_address = '192.168.227.11:32638'
        self.assertEqual('http://192.168.227.11:30638/', div_util.get_http_address(test_address))


class TestXOSDManager(unittest.TestCase):
    def setUp(self):
        # fields for unit testing without an xtreemfs instance
        self.volume_name = 'volume'
        self.path_to_mount = '/mnt/x_mnt'
        self.path_on_volume = 'one/two/three'

        self.some_folder = 'some/folder'
        self.some_sub_folder = 'subfolder'

        self.absolute_path_to_managed_folder = os.path.join(self.path_to_mount, self.path_on_volume)

        self.absolute_path_random = '/la/la/la/la/la/la/la'

        self.volume_address = '1.1.1.1:30000'

        self.value_map = {'path_on_volume': self.path_on_volume, 'path_to_mount': self.path_to_mount,
                          'volume_name': self.volume_name, 'osd_selection_policy': None, 'data_distribution': None,
                          'volume_address': self.volume_address}

    def test_init_no_xtreemfs_volume(self):
        dummy_path = '/'
        with self.assertRaises(OSDManager.NotAXtreemFSVolume):
            OSDManager.OSDManager(dummy_path)

    def test_path_on_volume(self):
        osd_man = OSDManager.OSDManager(self.absolute_path_to_managed_folder, value_map=self.value_map)

        self.assertEqual(os.path.join(self.volume_name, self.path_on_volume, self.some_folder),
                         osd_man.path_on_volume(
                             os.path.join(self.path_to_mount, self.path_on_volume, self.some_folder)))

        with self.assertRaises(OSDManager.PathNotManagedException):
            osd_man.path_on_volume(self.absolute_path_random)

    def test_get_target_dir(self):
        osd_man = OSDManager.OSDManager(self.absolute_path_to_managed_folder, value_map=self.value_map)

        folder_id = os.path.join(self.volume_name, self.path_on_volume, self.some_folder, self.some_sub_folder)
        target_dir = osd_man.get_target_dir(folder_id)
        expected_target_dir = os.path.join(self.path_to_mount, self.path_on_volume, self.some_folder)

        self.assertEqual(expected_target_dir, target_dir)


class TestXOSDManagerWithXtreemFS(unittest.TestCase):
    def setUp(self):
        # fields for unit testing with a running xtreemfs instance and real files
        # two mount points of the same xtreemfs volume
        self.mount_point_1 = '/dev/shm/xfs_mnt_1'
        self.mount_point_2 = '/dev/shm/xfs_mnt_2'
        self.path_on_mount_point = 'x/y/folder'

        self.tmp_folder = '/tmp/python-1298324809321934'
        self.test_files_folder = 'test_folder'

        self.depth_1_name = 'dir_'
        self.depth_2_name = 'FOLDER_'
        self.depth_3_name = 'img_'
        self.file_name = 'file_'

        self.num_depth_1_dirs = 2
        self.num_depth_2_dirs = 3
        self.num_depth_3_dirs = 2
        self.num_files = 5

        self.depth_1_dirs = []
        self.depth_2_dirs = []
        self.depth_3_dirs = []

        self.file_sizes = {1: 1, 2: 2, 3: 4, 4: 8, 5: 16}
        self.file_size_multiplier = 1024 * 8

        self.create_test_files()

    def tearDown(self):
        clean_up_volume(self.mount_point_1)
        clean_up_folder(os.path.join(self.tmp_folder, self.test_files_folder))

    def create_test_files(self):
        clean_up_volume(self.mount_point_1)
        clean_up_folder(os.path.join(self.tmp_folder, self.test_files_folder))

        for i in range(0, self.num_depth_1_dirs):
            depth_1_dir = os.path.join(self.tmp_folder, self.test_files_folder, self.depth_1_name + str(i))
            self.depth_1_dirs.append(depth_1_dir)
            for j in range(0, self.num_depth_2_dirs):
                depth_2_dir = os.path.join(depth_1_dir, self.depth_2_name + str(j))
                self.depth_2_dirs.append(depth_2_dir)
                for k in range(0, self.num_depth_3_dirs):
                    depth_3_dir = os.path.join(self.tmp_folder, self.test_files_folder,
                                               self.depth_1_name + str(i),
                                               self.depth_2_name + str(j),
                                               self.depth_3_name + str(k))
                    self.depth_3_dirs.append(depth_3_dir)
                    os.makedirs(depth_3_dir)

                    for l in range(0, self.num_files):
                        file = open(os.path.join(depth_3_dir, self.file_name + str(l)), 'w')
                        for m in range(0, self.file_sizes[j + 1] * self.file_size_multiplier):
                            file.write("1")

    def test_copy_folders(self):
        managed_folder = os.path.join(self.mount_point_1, self.path_on_mount_point)
        os.makedirs(managed_folder)

        x_man = OSDManager.OSDManager(managed_folder)
        x_man.copy_folders(self.depth_2_dirs)

        self.assertTrue(verify.verify_gms_folder(managed_folder))

        self.assertEqual(count_folder_and_files(managed_folder),
                         count_folder_and_files(os.path.join(self.tmp_folder, self.test_files_folder)))

    def test_create_empty_folders(self):
        managed_folder = os.path.join(self.mount_point_1, self.path_on_mount_point)
        os.makedirs(managed_folder)

        new_dirs = []
        copy_tuples = []
        for depth_2_dir in self.depth_2_dirs:
            new_dir = os.path.join(os.path.split(os.path.split(depth_2_dir)[0])[1],
                                   os.path.split(depth_2_dir)[1])
            new_dir = os.path.join(managed_folder, new_dir)
            new_dirs.append(new_dir)
            copy_tuples.append((depth_2_dir, new_dir))

        x_man = OSDManager.OSDManager(managed_folder)
        x_man.create_empty_folders(new_dirs)

        # now copy files manually and check whether the data layout is good

        for src, dst in copy_tuples:
            shutil.rmtree(dst, ignore_errors=True)
            # shutil.copytree requires that the target directory does not exist
            shutil.copytree(src, dst)

        self.assertEqual(count_folder_and_files(managed_folder),
                         count_folder_and_files(os.path.join(self.tmp_folder, self.test_files_folder)))
        self.assertTrue(verify.verify_gms_folder(managed_folder))

    def test_update(self):
        managed_folder = os.path.join(self.mount_point_1, self.path_on_mount_point)
        os.makedirs(managed_folder)

        new_dirs = []
        copy_tuples = []
        for depth_2_dir in self.depth_2_dirs:
            new_dir = os.path.join(os.path.split(os.path.split(depth_2_dir)[0])[1],
                                   os.path.split(depth_2_dir)[1])
            new_dir = os.path.join(managed_folder, new_dir)
            new_dirs.append(new_dir)
            copy_tuples.append((depth_2_dir, new_dir))

        x_man = OSDManager.OSDManager(managed_folder)
        x_man.create_empty_folders(new_dirs)

        for src, dst in copy_tuples:
            shutil.rmtree(dst, ignore_errors=True)
            # shutil.copytree requires that the target directory does not exist
            shutil.copytree(src, dst)

        du_source = subprocess.run(["du", "-s", os.path.join(self.tmp_folder, self.test_files_folder)],
                                   stdout=subprocess.PIPE, universal_newlines=True)
        size_source = int(str(du_source.stdout).split()[0])

        total_size_in_distribution = 0
        for osd in x_man.distribution.OSDs.values():
            total_size_in_distribution += osd.totalFolderSize

        self.assertNotEqual(size_source, total_size_in_distribution)

        x_man.update()

        du_source = subprocess.run(["du", "-s", os.path.join(self.tmp_folder, self.test_files_folder)],
                                   stdout=subprocess.PIPE, universal_newlines=True)
        size_source = int(str(du_source.stdout).split()[0])

        total_size_in_distribution = 0
        for osd in x_man.distribution.OSDs.values():
            total_size_in_distribution += osd.totalFolderSize

        self.assertEqual(size_source, total_size_in_distribution)

    def test_read_write(self):
        managed_folder_1 = os.path.join(self.mount_point_1, self.path_on_mount_point)
        os.makedirs(managed_folder_1)

        x_man_1 = OSDManager.OSDManager(managed_folder_1)
        x_man_1.copy_folders(self.depth_2_dirs)

        total_size_in_distribution_1 = 0
        for osd in x_man_1.distribution.OSDs.values():
            total_size_in_distribution_1 += osd.totalFolderSize

        managed_folder_2 = os.path.join(self.mount_point_2, self.path_on_mount_point)
        x_man_2 = OSDManager.OSDManager(managed_folder_2)

        total_size_in_distribution_2 = 0
        for osd in x_man_2.distribution.OSDs.values():
            total_size_in_distribution_2 += osd.totalFolderSize

        self.assertEqual(total_size_in_distribution_1, total_size_in_distribution_2)

    def test_remove_folder(self):
        managed_folder_1 = os.path.join(self.mount_point_1, self.path_on_mount_point)
        os.makedirs(managed_folder_1)

        x_man_1 = OSDManager.OSDManager(managed_folder_1)
        x_man_1.copy_folders(self.depth_2_dirs)

        removed_folder_id = 'xfs_volume/x/y/folder/dir_0/FOLDER_1'
        x_man_1.remove_folder(removed_folder_id)

        new_files_target_dir = os.path.join(x_man_1.get_target_dir(removed_folder_id),
                                            os.path.split(removed_folder_id)[1])

        self.assertIsNotNone(verify.verify_tile_folder(new_files_target_dir, False))

        for i in range(1, 100):
            file_path = os.path.join(new_files_target_dir, 'new_file_' + str(i))
            open(file_path, 'a').close()

        self.assertIsNone(verify.verify_tile_folder(new_files_target_dir, False))

    def test_move_folder_to_OSD(self):
        managed_folder_1 = os.path.join(self.mount_point_1, self.path_on_mount_point)
        os.makedirs(managed_folder_1)

        x_man_1 = OSDManager.OSDManager(managed_folder_1)
        x_man_1.copy_folders(self.depth_2_dirs)

        moved_folder_id = 'xfs_volume/x/y/folder/dir_0/FOLDER_1'
        new_osd_id = 'flx-local-osd-2'

        moved_folder_path = os.path.join(x_man_1.get_target_dir(moved_folder_id),
                                         os.path.split(moved_folder_id)[1])

        old_num_dirs_and_files = count_folder_and_files(moved_folder_path)

        x_man_1.move_folder_to_osd(moved_folder_id, new_osd_id)

        self.assertEqual('flx-local-osd-2',
                         verify.verify_tile_folder(moved_folder_path, False))
        self.assertEqual(old_num_dirs_and_files, count_folder_and_files(moved_folder_path))


def count_folder_and_files(top_dir):
    # calculate and return some number, based on a directory tree rooted at top_dir
    num = 0
    for item in os.walk(top_dir):
        for directory in item[1]:
            if not directory.startswith('.'):
                num += 1
        for file in item[2]:
            if not file.startswith('.'):
                num += 1
    return num


def clean_up_volume(mount_point):
    clean_up_folder(mount_point)
    subprocess.run(["xtfsutil",
                    "--set-pattr", "1004.filenamePrefix", "--value", "clear", mount_point],
                   stdout=subprocess.PIPE, universal_newlines=True)


def clean_up_folder(folder):
    try:
        for item in os.listdir(folder):
            if os.path.isdir(os.path.join(folder, item)):
                shutil.rmtree(os.path.join(folder, item), ignore_errors=True)
            else:
                os.remove(os.path.join(folder, item))

        assert len(os.listdir(folder)) == 0
    except FileNotFoundError:
        pass