File: ci_test_wb_server_management.py

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (535 lines) | stat: -rw-r--r-- 26,814 bytes parent folder | download
1
2
3
4
5
6
7
8
9
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
69
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import os
import sys

old_test_case = False

if sys.version_info < (2, 7, 0):
    from unittest2 import TestCase
    old_test_case = True
else:
    from unittest import TestCase

import wb_server_management as target_module
from wb_common import Users, InvalidPasswordError, PermissionDeniedError

class TestGlobalModuleCode(TestCase):
    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)
        
        if old_test_case:
            self.setUpClass()

    @classmethod
    def setUpClass(self):
        self.sudo_pwd = None
        if 'WB_SUDO_PASSWD' in os.environ:
            self.sudo_pwd = os.environ['WB_SUDO_PASSWD']
        else:
            print '---> WB_SUDO_PASSWD is not defined, skipping TestGlobalModuleCode Admin Tests'
    
    def test_reset_sudo_prefix(self):
        target_module.reset_sudo_prefix()
        self.assertEquals(target_module.default_sudo_prefix, '/usr/bin/sudo -S -p EnterPasswordHere')
    
    def test_quote_path(self):
        self.assertEqual(target_module.quote_path("/hello/world"), '"/hello/world"')
        self.assertEqual(target_module.quote_path("~/hello/world"), '~/"hello/world"')
        self.assertEqual(target_module.quote_path("/hello world/file.txt"), '"/hello world/file.txt"')
        self.assertEqual(target_module.quote_path('/tmp/"hello world".txt'), '"/tmp/\\"hello world\\".txt"')
        self.assertEqual(target_module.quote_path("/tmp/'hello world'.txt"), "\"/tmp/'hello world'.txt\"")
        self.assertEqual(target_module.quote_path("/tmp/hello world.txt"), '"/tmp/hello world.txt"')
        self.assertEqual(target_module.quote_path("/tmp/foo\bar"), '"/tmp/foo\bar"')
        self.assertEqual(target_module.quote_path("/tmp/$hello.txt"), '"/tmp/$hello.txt"')
        
        
    def test_wrap_for_sudo(self):
    
        #1) Test passing no command
        with self.assertRaisesRegexp(Exception, 'Empty command passed to execution routine'):
            target_module.wrap_for_sudo("", "")
        
        #2) Test wrapping command for current user, no change should be done
        self.assertEquals(target_module.wrap_for_sudo('ls -l', '', Users.CURRENT), 'ls -l')
        
        #3) Test wrapping command for admin user, the default sudo prefix should be added
        self.assertEquals(target_module.wrap_for_sudo('ls -l', ''), '/usr/bin/sudo -S -p EnterPasswordHere /bin/bash -c "ls -l"')
        
        #4) Test wrapping command for admin user, passing a custom sudo prefix
        self.assertEquals(target_module.wrap_for_sudo('ls -l', 'sudo'), 'sudo /bin/bash -c "ls -l"')

        #5) Repeast previous 2 test cases but using a user different than admin or the current user
        self.assertEquals(target_module.wrap_for_sudo('ls -l', '', 'guest'), '/usr/bin/sudo -u guest -S -p EnterPasswordHere /bin/bash -c "ls -l"')
        self.assertEquals(target_module.wrap_for_sudo('ls -l', 'sudo', 'guest'), 'sudo -u guest /bin/bash -c "ls -l"')
        
        #6) Test sudo for a spawned command
        self.assertEquals(target_module.wrap_for_sudo('ls -l', '', to_spawn=True), '/usr/bin/sudo -S -p EnterPasswordHere /usr/bin/nohup /bin/bash -c "ls -l &"')

        
    def test_local_run_cmd_linux(self):
        custom_sudo = '/usr/bin/sudo -S -k -p EnterPasswordHere'
        # Creates a folder as the current user
        self.assertEquals(target_module.local_run_cmd_linux('mkdir __testing_folder'), 0)
        
        self.assertEquals(target_module.local_run_cmd_linux('rmdir __testing_folder'), 0)

        # Attempts creating a folder using sudo with the incorrect password
        with self.assertRaisesRegexp(InvalidPasswordError, 'Incorrect password for sudo'):
            self.assertEquals(target_module.local_run_cmd_linux('mkdir __testing_admin', Users.ADMIN, sudo_prefix = custom_sudo), 1)
            
        if self.sudo_pwd:
            # Creates a folder as the root user
            self.assertEquals(target_module.local_run_cmd_linux('mkdir __testing_admin', Users.ADMIN, self.sudo_pwd, custom_sudo), 0)
            
            # Deletes both folders
            self.assertEquals(target_module.local_run_cmd_linux('rmdir __testing_admin', Users.ADMIN, self.sudo_pwd, custom_sudo), 0)
        

class TestProcessOpsLinuxLocal(TestCase):
    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)
        
        if old_test_case:
            self.setUpClass()

    @classmethod
    def setUpClass(self):
        self.sudo_pwd = None
        if 'WB_SUDO_PASSWD' in os.environ:
            self.sudo_pwd = os.environ['WB_SUDO_PASSWD']
        else:
            print '---> WB_SUDO_PASSWD is not defined, skipping TestFileOpsLinuxBase Admin Tests'
    
        self.target_class = target_module.ProcessOpsLinuxLocal(sudo_prefix = '/usr/bin/sudo -S -k -p EnterPasswordHere', ssh=None)
        
    def test_exec_cmd(self):
        # Creates a folder as the current user
        self.assertEquals(self.target_class.exec_cmd('mkdir __testing_folder'), 0)
        self.assertEquals(self.target_class.exec_cmd('rmdir __testing_folder'), 0)
        
        # Attempts creating a folder using sudo with the incorrect password
        with self.assertRaisesRegexp(InvalidPasswordError, 'Incorrect password for sudo'):
            self.assertEquals(self.target_class.exec_cmd('mkdir __testing_admin', Users.ADMIN), 1)
                        
        # Creates a folder as the root user
        if self.sudo_pwd:
            self.assertEquals(self.target_class.exec_cmd('mkdir __testing_admin', Users.ADMIN, self.sudo_pwd), 0)
            
            # Deletes both folders
            self.assertEquals(self.target_class.exec_cmd('rmdir __testing_admin', Users.ADMIN, self.sudo_pwd), 0)


class TestFileOpsLinuxBase(TestCase):
    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)
        
        if old_test_case:
            self.setUpClass()

    @classmethod
    def setUpClass(self):
        self.sudo_pwd = None
        if 'WB_SUDO_PASSWD' in os.environ:
            self.sudo_pwd = os.environ['WB_SUDO_PASSWD']
        else:
            print '---> WB_SUDO_PASSWD is not defined, skipping TestFileOpsLinuxBase Admin Tests'
    
        self.process_ops = target_module.ProcessOpsLinuxLocal(sudo_prefix = '/usr/bin/sudo -S -k -p EnterPasswordHere', ssh=None)
        self.target_class = target_module.FileOpsLinuxBase(self.process_ops, None, 'linux')

        self.user_home = os.path.expanduser('~')
        
        self.test_files = []
        self.test_files.append(os.path.join(self.user_home, '__testing_file.txt'))
        self.test_files.append(os.path.join(self.user_home, '__testing file.txt'))
        
        self.test_folders = []
        self.test_folders.append(os.path.join(self.user_home, '__testing_folder'))
        self.test_folders.append(os.path.join(self.user_home, '__testing folder'))
        
    def test_file_exists(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "file_exists", parameter "filename" must be an absolute path'):
            self.target_class.file_exists('sample.txt')
        
        for file in self.test_files:
            # Verifies the non existence of a file
            self.assertFalse(self.target_class.file_exists(file))

            # Creates a temporary file
            self.process_ops.exec_cmd('touch "%s"' % file)
            
            # Ensures the file_exists only accepts absolute paths
            self.assertTrue(self.target_class.file_exists(file))

            # Removes the temporary file
            self.process_ops.exec_cmd('rm "%s"' % file)
        

    def test_get_available_space(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "get_available_space", parameter "path" must be an absolute path'):
            self.target_class.get_available_space('folder_name')
        
        for folder in self.test_folders:
            # Gets availalbe space of unexisting folder
            self.assertEquals(self.target_class.get_available_space(folder), 'Could not determine')
            
            # Gets availalbe space of existing folder
            self.process_ops.exec_cmd('mkdir "%s"' % folder)
            self.assertRegexpMatches(self.target_class.get_available_space(folder), '.*of.*available')
            self.process_ops.exec_cmd('rmdir "%s"' % folder)
        
    def test_get_file_owner(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "get_file_owner", parameter "path" must be an absolute path'):
            self.target_class.get_file_owner('test.txt')
        
        
        for file in self.test_files:
            # Gets availalbe space of unexisting file
            with self.assertRaisesRegexp(Exception, 'No such file or directory'):
                self.target_class.get_file_owner(file)


            if self.sudo_pwd:
                # Creates a temporary file
                self.process_ops.exec_cmd('touch "%s"' % file, Users.ADMIN, self.sudo_pwd)
                
                # Ensures the file_exists
                self.assertEquals(self.target_class.get_file_owner(file), 'root')

                # Removes the temporary file
                self.process_ops.exec_cmd('rm "%s"' % file, Users.ADMIN, self.sudo_pwd)


    def test_create_directory(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "create_directory", parameter "path" must be an absolute path'):
            self.target_class.create_directory('test_folder')
        
        for folder in self.test_folders:
            # Creates a directory
            self.target_class.create_directory(folder + '_1')
            self.assertTrue(self.target_class.file_exists(folder + '_1'))
            
            # Attempts creating an existing directory
            with self.assertRaisesRegexp(Exception, 'cannot create directory.*File exists'):
                self.target_class.create_directory(folder + '_1')        
                
            self.process_ops.exec_cmd('rmdir "%s_1"' % folder)

            # Attempts creating a directory with owned but using the current user
            with self.assertRaisesRegexp(PermissionDeniedError, 'Cannot set owner of directory'):
                self.target_class.create_directory(folder + '_2', with_owner = 'other')        

            if self.sudo_pwd:
                # Attempts creating a directory with the admin user
                self.target_class.create_directory(folder + '_2', Users.ADMIN, self.sudo_pwd, with_owner = 'root')
                self.assertTrue(self.target_class.file_exists(folder + '_2'))
                
                # Removes folder
                self.process_ops.exec_cmd('rmdir "%s_2"' % folder, Users.ADMIN, self.sudo_pwd)


    def test_create_directory_recursive(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "create_directory_recursive", parameter "path" must be an absolute path'):
            self.target_class.create_directory_recursive('test_folder')
            
        for folder in self.test_folders:
            # Creates a directory
            self.target_class.create_directory_recursive(os.path.join(folder, 'one', 'two', 'three'))
            
            self.assertTrue(self.target_class.file_exists(os.path.join(folder, 'one', 'two', 'three')))
            self.assertTrue(self.target_class.file_exists(os.path.join(folder, 'one', 'two')))
            self.assertTrue(self.target_class.file_exists(os.path.join(folder, 'one')))
            self.assertTrue(self.target_class.file_exists(os.path.join(folder)))
            
            # Creates a directory tree that aalready exists, nothing is expected to happen
            with self.assertRaisesRegexp(Exception, 'cannot create directory.*File exists'):
                self.target_class.create_directory_recursive(os.path.join(folder, 'one', 'two', 'three'))

            self.process_ops.exec_cmd('rm -R "%s"' % folder)
        
        
    def test_remove_directory(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "remove_directory", parameter "path" must be an absolute path'):
            self.target_class.remove_directory('test_folder')
        
        for folder in self.test_folders:
            # Creates a directory
            self.process_ops.exec_cmd('mkdir "%s"' % folder)
            self.assertTrue(self.target_class.file_exists(folder))
            
            self.target_class.remove_directory(folder)
            self.assertFalse(self.target_class.file_exists(folder))
            
            # Attempts creating an existing directory
            with self.assertRaisesRegexp(Exception, 'No such file or directory'):
                self.target_class.remove_directory(folder)
        
    def test_remove_directory_recursive(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "remove_directory_recursive", parameter "path" must be an absolute path'):
            self.target_class.remove_directory_recursive('test_folder')
        
        for folder in self.test_folders:
            # Creates a directory
            self.process_ops.exec_cmd('mkdir "%s"' % folder)
            self.process_ops.exec_cmd('mkdir "%s"' % os.path.join(folder, 'uno'))
            self.process_ops.exec_cmd('mkdir "%s"' % os.path.join(folder, 'uno', 'dos'))
            self.process_ops.exec_cmd('mkdir "%s"' % os.path.join(folder, 'uno', 'dos', 'tres'))
            self.assertTrue(self.target_class.file_exists(os.path.join(folder, 'uno', 'dos', 'tres')))
            
            self.target_class.remove_directory_recursive(folder)
            self.assertFalse(self.target_class.file_exists(folder))
            
            # Attempts creating an existing directory
            with self.assertRaisesRegexp(Exception, 'No such file or directory'):
                self.target_class.remove_directory_recursive(folder)
            
    def test_delete_file(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "delete_file", parameter "path" must be an absolute path'):
            self.target_class.delete_file('test.txt')
        
        for file in self.test_files:
            # Creates a temporary file
            self.process_ops.exec_cmd('touch "%s"' % file)
            self.assertTrue(self.target_class.file_exists(file))
            
            # Deletes the file
            self.target_class.delete_file(file)
            self.assertFalse(self.target_class.file_exists(file))
            
            # Attempts to delete the file that no longer exists
            with self.assertRaisesRegexp(Exception, 'No such file or directory'):
                self.target_class.delete_file(file)


    def test_get_file_content(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "get_file_content", parameter "filename" must be an absolute path'):
            self.target_class.get_file_content('test.txt')
        
        for file in self.test_files:
            # Creates a temporary file
            self.process_ops.exec_cmd('echo "1234\n6789" >  "%s"' % file)
            
            # gets the file content
            self.assertEquals(self.target_class.get_file_content(file), '1234\n6789')
            
            # gets the file content skipping the first line
            self.assertEquals(self.target_class.get_file_content(file, skip_lines = 1), '6789')

            # Deletes the file
            self.process_ops.exec_cmd('rm "%s"' % file)

            # Attempts to get the content of a file that no longer exists
            with self.assertRaisesRegexp(Exception, 'No such file or directory'):
                self.target_class.get_file_content(file)


    def test_check_dir_writable(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "check_dir_writable", parameter "path" must be an absolute path'):
            self.target_class.check_dir_writable('test_folder')
        
        for folder in self.test_folders:
            # Creates temporary folders
            self.process_ops.exec_cmd('mkdir "%s"' % folder + '_1')
            
            if self.sudo_pwd:
                self.process_ops.exec_cmd('mkdir "%s"' % folder + '_2', Users.ADMIN, self.sudo_pwd)
            
            # Checks writability
            self.assertTrue(self.target_class.check_dir_writable(folder + '_1'))
            if self.sudo_pwd:
                self.assertFalse(self.target_class.check_dir_writable(folder + '_2'))
            
            self.process_ops.exec_cmd('rmdir "%s_1"' % folder)
            
            if self.sudo_pwd:
                self.assertTrue(self.target_class.check_dir_writable(folder + '_2', Users.ADMIN, self.sudo_pwd))
                self.process_ops.exec_cmd('rmdir "%s_2"' % folder, Users.ADMIN, self.sudo_pwd)
            
            # Attempts to check dir writability on unexisting folder
            with self.assertRaisesRegexp(Exception, 'The path.*does not exist'):
                self.target_class.check_dir_writable(folder)

            # Attempts to check dir writability on a file
            self.process_ops.exec_cmd('touch "%s"' % folder)
            with self.assertRaisesRegexp(Exception, 'The path.*is not a directory'):
                self.target_class.check_dir_writable(folder)
            self.process_ops.exec_cmd('rm "%s"' % folder)


    def test_listdir(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "listdir", parameter "path" must be an absolute path'):
            self.target_class.listdir('test_folder')
        
        for folder in self.test_folders:
            # Attempts listing unexisting folder
            with self.assertRaisesRegexp(Exception, 'cannot access.*No such file or directory'):
                self.target_class.listdir(folder)
                
                
            # Creates temporary folders
            self.process_ops.exec_cmd('mkdir "%s"' % folder)
            self.process_ops.exec_cmd('touch "%s"' % os.path.join(folder, 'test.txt'))
            
            directory = self.target_class.listdir(folder)
            self.assertIn('test.txt', directory)

            directory = self.target_class.listdir(folder, include_size=True)
            self.assertIn(('test.txt', 0), directory)
            
            self.process_ops.exec_cmd('rm -R "%s"' % folder)
        
    def test_save_file_content_and_backup(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "save_file_content_and_backup", parameter "filename" must be an absolute path'):
            self.target_class.save_file_content_and_backup('test.txt', '12345678')
        
        for folder in self.test_folders:
            # Tries to save a file on an unexisting folder
            with self.assertRaisesRegexp(OSError, 'The path.*does not exist'):
                self.target_class.save_file_content_and_backup(os.path.join(folder, 'test.txt'), '12345678', '.bak')
                
            # Tries to save a file on a folder with no write permissions
            if self.sudo_pwd:
                self.process_ops.exec_cmd('mkdir "%s"' % folder, Users.ADMIN, self.sudo_pwd)
                with self.assertRaisesRegexp(PermissionDeniedError, 'Cannot write to target folder'):
                    self.target_class.save_file_content_and_backup(os.path.join(folder, 'test.txt'), '12345678', '.bak')
                    
                self.target_class.save_file_content_and_backup(os.path.join(folder, 'test.txt'), '12345678', '.bak', Users.ADMIN, self.sudo_pwd)
                
                self.process_ops.exec_cmd('rm -R "%s"' % folder, Users.ADMIN, self.sudo_pwd)
            
            # Saves the file on a folder, first time
            self.process_ops.exec_cmd('mkdir "%s"' % folder)
            self.target_class.save_file_content_and_backup(os.path.join(folder, 'test.txt'), '1', '.bak')
            self.assertEquals(self.target_class.get_file_content(os.path.join(folder, 'test.txt')), '1')
            self.assertFalse(self.target_class.file_exists(os.path.join(folder, 'test.txt.bak')))
            

            # Saves the file on a folder, second time
            self.target_class.save_file_content_and_backup(os.path.join(folder, 'test.txt'), '2', '.bak')
            self.assertEquals(self.target_class.get_file_content(os.path.join(folder, 'test.txt')), '2')
            self.assertEquals(self.target_class.get_file_content(os.path.join(folder, 'test.txt.bak')), '1')

            self.process_ops.exec_cmd('rm -R "%s"' % folder)
        
class TestFileOpsLocalUnix(TestCase):
    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)
        
        if old_test_case:
            self.setUpClass()
    # These test cases also exercise the variants of the methods inherited from
    # from FileOpsLinuxBase using a ProcessOpeLinuxLocal instance
    
    @classmethod
    def setUpClass(self):
        self.sudo_pwd = None
        if 'WB_SUDO_PASSWD' in os.environ:
            self.sudo_pwd = os.environ['WB_SUDO_PASSWD']
        else:
            print '---> WB_SUDO_PASSWD is not defined, skipping TestFileOpsLinuxBase Admin Tests'
    
        self.process_ops = target_module.ProcessOpsLinuxLocal(sudo_prefix = '/usr/bin/sudo -S -k -p EnterPasswordHere', ssh=None)
        self.target_class = target_module.FileOpsLocalUnix(self.process_ops, None, 'linux')
        
        self.user_home = os.path.expanduser('~')
        
        self.test_files = []
        self.test_files.append(os.path.join(self.user_home, '__testing_file.txt'))
        self.test_files.append(os.path.join(self.user_home, '__testing file.txt'))
        
        self.test_folders = []
        self.test_folders.append(os.path.join(self.user_home, '__testing_folder'))
        self.test_folders.append(os.path.join(self.user_home, '__testing folder'))
        
    
    def test_save_file_content(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "save_file_content", parameter "filename" must be an absolute path'):
            self.target_class.save_file_content('test.txt', '12345678')
    
        for file in self.test_files:
            self.target_class.save_file_content(file, "1")
            
            self.process_ops.exec_cmd('rm "%s"' % file)
        
    def test_get_file_content(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "get_file_content", parameter "filename" must be an absolute path'):
            self.target_class.get_file_content('test.txt')

        for file in self.test_files:
            # Creates a temporary file
            self.target_class.save_file_content(file, "1234\n6789")
            
            # Tests loading the full file
            self.assertEquals(self.target_class.get_file_content(file), "1234\n6789")
            
            # Tests skipping the first line
            self.assertEquals(self.target_class.get_file_content(file, skip_lines = 1), "6789")
            
            # Tests loading the file as ADMIN
            if self.sudo_pwd:
                self.assertEquals(self.target_class.get_file_content(file, Users.ADMIN, self.sudo_pwd), "1234\n6789")
            
            self.process_ops.exec_cmd('rm "%s"' % file)

    def test_delete_file(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "delete_file", parameter "path" must be an absolute path'):
            self.target_class.delete_file('test.txt')
        
        for file in self.test_files:
            self.process_ops.exec_cmd('touch "%s"' % file)
            self.assertTrue(self.target_class.file_exists(file))
            self.target_class.delete_file(file)
            self.assertFalse(self.target_class.file_exists(file))
            
            if self.sudo_pwd:
                self.process_ops.exec_cmd('touch "%s"' % file, Users.ADMIN, self.sudo_pwd)
                self.assertTrue(self.target_class.file_exists(file))
                self.target_class.delete_file(file, Users.ADMIN, self.sudo_pwd)
                self.assertFalse(self.target_class.file_exists(file))

        
    def test_listdir(self):
        # Ensures the function requires an absolute path
        with self.assertRaisesRegexp(ValueError, 'Error on path validation for function "listdir", parameter "path" must be an absolute path'):
            self.target_class.listdir('test_folder')
        
        for folder in self.test_folders:
            # Attempts listing unexisting folder
            with self.assertRaisesRegexp(Exception, 'No such file or directory'):
                self.target_class.listdir(folder)
                
            # Attempts listing unexisting folder
            with self.assertRaisesRegexp(Exception, 'No such file or directory'):
                self.target_class.listdir(folder)
                
            # Creates temporary folders
            self.process_ops.exec_cmd('mkdir "%s"' % folder)
            self.process_ops.exec_cmd('touch "%s"' % os.path.join(folder, 'test.txt'))
            
            directory = self.target_class.listdir(folder)
            self.assertIn('test.txt', directory)

            directory = self.target_class.listdir(folder, include_size=True)
            self.assertIn(('test.txt', 0), directory)

            if self.sudo_pwd:
                directory = self.target_class.listdir(folder, Users.ADMIN, self.sudo_pwd, include_size=True)
                self.assertIn(('test.txt', 0), directory)

            self.process_ops.exec_cmd('rm -R "%s"' % folder)