Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

import unittest 

import testlib 

import os 

import mock 

import errno 

 

import refcounter 

 

 

class TestRefCounter(unittest.TestCase): 

    @testlib.with_context 

    def test_get_whencalled_creates_namespace(self, context): 

        os.makedirs(refcounter.RefCounter.BASE_DIR) 

 

        refcounter.RefCounter.get('not-important', False, 'somenamespace') 

 

        self.assertEquals( 

            ['somenamespace'], 

            os.listdir(os.path.join(refcounter.RefCounter.BASE_DIR))) 

 

    @testlib.with_context 

    def test_get_whencalled_returns_counters(self, context): 

        os.makedirs(refcounter.RefCounter.BASE_DIR) 

 

        result = refcounter.RefCounter.get( 

            'not-important', False, 'somenamespace') 

 

        self.assertEquals(1, result) 

 

    @testlib.with_context 

    def test_get_whencalled_creates_refcounter_file(self, context): 

        os.makedirs(refcounter.RefCounter.BASE_DIR) 

 

        refcounter.RefCounter.get('someobject', False, 'somenamespace') 

 

        self.assertEquals( 

            ['someobject'], 

            os.listdir(os.path.join( 

                refcounter.RefCounter.BASE_DIR, 'somenamespace'))) 

 

    @testlib.with_context 

    def test_get_whencalled_refcounter_file_contents(self, context): 

        os.makedirs(refcounter.RefCounter.BASE_DIR) 

 

        refcounter.RefCounter.get('someobject', False, 'somenamespace') 

 

        path_to_refcounter = os.path.join( 

            refcounter.RefCounter.BASE_DIR, 'somenamespace', 'someobject') 

 

        refcounter_file = open(path_to_refcounter, 'r') 

        contents = refcounter_file.read() 

        refcounter_file.close() 

 

        self.assertEquals('1 0\n', contents) 

 

    @testlib.with_context 

    def test_put_is_noop_if_already_zero(self, context): 

        os.makedirs(refcounter.RefCounter.BASE_DIR) 

 

        result = refcounter.RefCounter.put( 

            'someobject', False, 'somenamespace') 

 

        self.assertEquals(0, result) 

 

    @testlib.with_context 

    def test_writeCount_returns_true_if_file_found(self, context): 

        os.makedirs('/existing') 

 

        result = refcounter.RefCounter._writeCount('/existing/file', 1, 1) 

 

        self.assertTrue(result) 

 

    @testlib.with_context 

    def test_writeCount_returns_false_if_file_not_found(self, context): 

        result = refcounter.RefCounter._writeCount('/nonexisting/file', 1, 1) 

 

        self.assertFalse(result) 

 

    @mock.patch('os.rmdir', autospec=True) 

    @mock.patch('os.unlink', autospec=True) 

    @mock.patch('util.pathexists', autospec=True) 

    def test_removeObject_ignores_if_directory_already_removed(self, 

                                                               pathexists, 

                                                               unlink, 

                                                               rmdir): 

        rmdir.side_effect = OSError(errno.ENOENT, 'ignored') 

 

        refcounter.RefCounter._removeObject('namespace', 'obj') 

 

        rmdir.assert_called_once_with( 

            os.path.join(refcounter.RefCounter.BASE_DIR, 'namespace')) 

 

    @mock.patch('os.rmdir', autospec=True) 

    @mock.patch('os.unlink', autospec=True) 

    @mock.patch('util.pathexists', autospec=True) 

    def test_removeObject_ignores_if_directory_not_empty(self, 

                                                         pathexists, 

                                                         unlink, 

                                                         rmdir): 

        rmdir.side_effect = OSError(errno.ENOTEMPTY, 'ignored') 

 

        refcounter.RefCounter._removeObject('namespace', 'obj') 

 

        rmdir.assert_called_once_with( 

            os.path.join(refcounter.RefCounter.BASE_DIR, 'namespace')) 

 

 

# Re-use legacy tests embedded in refcounter 

testcase = unittest.FunctionTestCase(refcounter.RefCounter._runTests) 

with mock.patch.object(refcounter.RefCounter, "BASE_DIR", "./fakesm/refcount"): 

        unittest.TextTestRunner().run(testcase)