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

import errno 

import mock 

import nfs 

import NFSSR # Without this the FileSR won't import 

import FileSR 

import os 

import stat 

import unittest 

import uuid 

 

 

class FakeFileVDI(FileSR.FileVDI): 

     def __init__(self, srcmd, none): 

         pass 

 

 

class TestFileVDI(unittest.TestCase): 

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

    def test_find_vhd_path(self, mock_os_stat): 

        vdi_uuid=uuid.uuid4() 

        vdi = FakeFileVDI("a command", None) 

        sr = mock.MagicMock() 

        sr.path = "sr_path" 

        vdi.sr = sr 

        mock_os_stat.side_effect = [os.stat_result((stat.S_IFREG, 0, 0, 0, 0, 0, 1024, 0, 0, 0)) ] 

 

        found = vdi._find_path_with_retries(vdi_uuid) 

 

        self.assertTrue(found) 

        expected_path = 'sr_path/%s.vhd' % vdi_uuid 

        mock_os_stat.assert_called_with(expected_path) 

        self.assertEqual(vdi.path, expected_path) 

 

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

    def test_find_raw_path(self, mock_os_stat): 

        vdi_uuid=uuid.uuid4() 

        vdi = FakeFileVDI("a command", None) 

        sr = mock.MagicMock() 

        sr.path = "sr_path" 

        vdi.sr = sr 

        mock_os_stat.side_effect = [OSError(errno.ENOENT), 

                            os.stat_result((stat.S_IFREG, 0, 0, 0, 0, 0, 1024, 0, 0, 0)) ] 

 

        found = vdi._find_path_with_retries(vdi_uuid) 

 

        self.assertTrue(found) 

        expected_path = 'sr_path/%s.raw' % vdi_uuid 

        mock_os_stat.assert_called_with(expected_path) 

        self.assertEqual(vdi.path, expected_path) 

 

    @mock.patch('time.sleep', autospec=True) 

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

    def test_find_retry_vhd_path(self, mock_os_stat, sleep): 

        vdi_uuid=uuid.uuid4() 

        vdi = FakeFileVDI("a command", None) 

        sr = mock.MagicMock() 

        sr.path = "sr_path" 

        vdi.sr = sr 

        mock_os_stat.side_effect = [OSError(errno.ENOENT), 

                            OSError(errno.ENOENT), 

                            OSError(errno.ENOENT), 

                            os.stat_result((stat.S_IFREG, 0, 0, 0, 0, 0, 1024, 0, 0, 0)) ] 

 

        found = vdi._find_path_with_retries(vdi_uuid) 

 

        self.assertTrue(found) 

        expected_path = 'sr_path/%s.vhd' % vdi_uuid 

        mock_os_stat.assert_called_with(expected_path) 

        self.assertEqual(vdi.path, expected_path) 

 

    @mock.patch('time.sleep', autospec=True) 

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

    def test_find_not_found(self, mock_os_stat, sleep): 

        vdi_uuid=uuid.uuid4() 

        vdi = FakeFileVDI("a command", None) 

        sr = mock.MagicMock() 

        sr.path = "sr_path" 

        vdi.sr = sr 

        mock_os_stat.side_effect = OSError(errno.ENOENT) 

 

        found = vdi._find_path_with_retries(vdi_uuid) 

 

        self.assertFalse(found)