libtest.py 4.72 KB
Newer Older
jvoisin's avatar
jvoisin committed
1
2
#!/usr/bin/env python
# -*- coding: utf-8 -*
3
4
5
6
7

'''
    Unit test for the library
'''

8
9
import unittest
import test
jvoisin's avatar
jvoisin committed
10
import sys
11
import tempfile
jvoisin's avatar
jvoisin committed
12
13
import tarfile

jvoisin's avatar
jvoisin committed
14
sys.path.append('..')
15
import MAT
16

jvoisin's avatar
jvoisin committed
17

jvoisin's avatar
jvoisin committed
18
19
20
21
class TestRemovelib(test.MATTest):
    '''
        test the remove_all() method
    '''
22
23
    def test_remove(self):
        '''make sure that the lib remove all compromizing meta'''
jvoisin's avatar
jvoisin committed
24
        for _, dirty in self.file_list:
25
            current_file = MAT.mat.create_class_file(dirty, False, add2archive=True)
26
            current_file.remove_all()
27
28
            current_file = MAT.mat.create_class_file(dirty, False, add2archive=True)
            self.assertTrue(current_file.is_clean())
29
30
31

    def test_remove_empty(self):
        '''Test removal with clean files'''
jvoisin's avatar
jvoisin committed
32
        for clean, _ in self.file_list:
33
            current_file = MAT.mat.create_class_file(clean, False, add2archive=True)
34
            current_file.remove_all()
35
            current_file = MAT.mat.create_class_file(clean, False, add2archive=True)
36
37
38
            self.assertTrue(current_file.is_clean())


jvoisin's avatar
jvoisin committed
39
40
41
42
class TestListlib(test.MATTest):
    '''
        test the get_meta() method
    '''
43
    def test_list(self):
44
        '''check if get_meta returns metadata'''
jvoisin's avatar
jvoisin committed
45
        for _, dirty in self.file_list:
46
            current_file = MAT.mat.create_class_file(dirty, False, add2archive=True)
jvoisin's avatar
jvoisin committed
47
            self.assertIsNotNone(current_file.get_meta())
48
49

    def testlist_list_empty(self):
50
        '''check that a listing of a clean file returns an empty dict'''
jvoisin's avatar
jvoisin committed
51
        for clean, _ in self.file_list:
52
            current_file = MAT.mat.create_class_file(clean, False, add2archive=True)
jvoisin's avatar
jvoisin committed
53
            self.assertEqual(current_file.get_meta(), dict())
54
55


jvoisin's avatar
jvoisin committed
56
57
58
59
class TestisCleanlib(test.MATTest):
    '''
        test the is_clean() method
    '''
60
    def test_dirty(self):
61
        '''test is_clean on dirty files'''
jvoisin's avatar
jvoisin committed
62
        for _, dirty in self.file_list:
63
            current_file = MAT.mat.create_class_file(dirty, False, add2archive=True)
jvoisin's avatar
jvoisin committed
64
            self.assertFalse(current_file.is_clean())
65
66

    def test_clean(self):
67
        '''test is_clean on clean files'''
jvoisin's avatar
jvoisin committed
68
        for clean, _ in self.file_list:
69
            current_file = MAT.mat.create_class_file(clean, False, add2archive=True)
jvoisin's avatar
jvoisin committed
70
            self.assertTrue(current_file.is_clean())
71

jvoisin's avatar
jvoisin committed
72

jvoisin's avatar
jvoisin committed
73
74
75
76
77
class TestFileAttributes(unittest.TestCase):
    '''
        test various stuffs about files (readable, writable, exist, ...)
    '''
    def test_not_writtable(self):
jvoisin's avatar
jvoisin committed
78
        ''' test MAT's behaviour on non-writable file'''
79
        self.assertFalse(MAT.mat.create_class_file('not_writtable', False, add2archive=True))
jvoisin's avatar
jvoisin committed
80
81

    def test_not_exist(self):
jvoisin's avatar
jvoisin committed
82
        ''' test MAT's behaviour on non-existent file'''
83
        self.assertFalse(MAT.mat.create_class_file('ilikecookies', False, add2archive=True))
84

jvoisin's avatar
jvoisin committed
85
86
    def test_empty(self):
        ''' test MAT's behaviour on empty file'''
87
        self.assertFalse(MAT.mat.create_class_file('empty_file', False, add2archive=True))
88

jvoisin's avatar
jvoisin committed
89

90
91
92
93
94
95
96
97
98
99
100
101
class TestSecureRemove(unittest.TestCase):
    ''' Test the secure_remove function
    '''
    def test_remove_existing(self):
        ''' test the secure removal of an existing file
        '''
        _, file_to_remove = tempfile.mkstemp()
        self.assertTrue(MAT.mat.secure_remove(file_to_remove))

    def test_remove_fail(self):
        ''' test the secure removal of an non-removable file
        '''
102
        self.assertRaises(MAT.exceptions.UnableToRemoveFile, MAT.mat.secure_remove, '/NOTREMOVABLE')
103

jvoisin's avatar
jvoisin committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class TestArchiveProcessing(test.MATTest):
    ''' Test archives cleaning
    '''
    def test_remove_bz2(self):
        tar = tarfile.open("test.tar.bz2", "w:bz2")
        for _,dirty in self.file_list:
            tar.add(dirty)
        tar.close()
        current_file = MAT.mat.create_class_file("test.tar.bz2", False, add2archive=False)
        current_file.remove_all()
        current_file = MAT.mat.create_class_file("test.tar.bz2", False, add2archive=False)
        self.assertTrue(current_file.is_clean())

    def test_remove_tar(self):
        tar = tarfile.open("test.tar", "w")
        for _,dirty in self.file_list:
            tar.add(dirty)
        tar.close()
        current_file = MAT.mat.create_class_file("test.tar", False, add2archive=False)
        current_file.remove_all()
        current_file = MAT.mat.create_class_file("test.tar", False, add2archive=False)
        self.assertTrue(current_file.is_clean())
jvoisin's avatar
jvoisin committed
126

127
def get_tests():
128
    ''' Returns every libtests'''
jvoisin's avatar
jvoisin committed
129
130
131
132
133
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestRemovelib))
    suite.addTest(unittest.makeSuite(TestListlib))
    suite.addTest(unittest.makeSuite(TestisCleanlib))
    suite.addTest(unittest.makeSuite(TestFileAttributes))
134
    suite.addTest(unittest.makeSuite(TestSecureRemove))
jvoisin's avatar
jvoisin committed
135
    suite.addTest(unittest.makeSuite(TestArchiveProcessing))
jvoisin's avatar
jvoisin committed
136
    return suite