summaryrefslogtreecommitdiff
path: root/testing/mozharness/test/test_base_transfer.py
blob: f3f907254d4e35caabb4cf640d46b3e1203eb949 (plain)
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
import unittest
import mock

from mozharness.base.transfer import TransferMixin


class GoodMockMixin(object):
    def query_abs_dirs(self):
        return {'abs_work_dir': ''}

    def query_exe(self, exe):
        return exe

    def info(self, msg):
        pass

    def log(self, msg, level):
        pass

    def run_command(*args, **kwargs):
        return 0


class BadMockMixin(GoodMockMixin):
    def run_command(*args, **kwargs):
        return 1


class GoodTransferMixin(TransferMixin, GoodMockMixin):
    pass


class BadTransferMixin(TransferMixin, BadMockMixin):
    pass


class TestTranferMixin(unittest.TestCase):
    @mock.patch('mozharness.base.transfer.os')
    def test_rsync_upload_dir_not_a_dir(self, os_mock):
        # simulates upload dir but dir is a file
        os_mock.path.isdir.return_value = False
        tm = GoodTransferMixin()
        self.assertEqual(tm.rsync_upload_directory(
                         local_path='',
                         ssh_key='my ssh key',
                         ssh_user='my ssh user',
                         remote_host='remote host',
                         remote_path='remote path',), -1)

    @mock.patch('mozharness.base.transfer.os')
    def test_rsync_upload_fails_create_remote_dir(self, os_mock):
        # we cannot create the remote directory
        os_mock.path.isdir.return_value = True
        tm = BadTransferMixin()
        self.assertEqual(tm.rsync_upload_directory(
                         local_path='',
                         ssh_key='my ssh key',
                         ssh_user='my ssh user',
                         remote_host='remote host',
                         remote_path='remote path',
                         create_remote_directory=True), -2)

    @mock.patch('mozharness.base.transfer.os')
    def test_rsync_upload_fails_do_not_create_remote_dir(self, os_mock):
        # upload fails, remote directory is not created
        os_mock.path.isdir.return_value = True
        tm = BadTransferMixin()
        self.assertEqual(tm.rsync_upload_directory(
                         local_path='',
                         ssh_key='my ssh key',
                         ssh_user='my ssh user',
                         remote_host='remote host',
                         remote_path='remote path',
                         create_remote_directory=False), -3)

    @mock.patch('mozharness.base.transfer.os')
    def test_rsync_upload(self, os_mock):
        # simulates an upload with no errors
        os_mock.path.isdir.return_value = True
        tm = GoodTransferMixin()
        self.assertEqual(tm.rsync_upload_directory(
                         local_path='',
                         ssh_key='my ssh key',
                         ssh_user='my ssh user',
                         remote_host='remote host',
                         remote_path='remote path',
                         create_remote_directory=False), None)

    @mock.patch('mozharness.base.transfer.os')
    def test_rsync_download_in_not_a_dir(self, os_mock):
        # local path is not a directory
        os_mock.path.isdir.return_value = False
        tm = GoodTransferMixin()
        self.assertEqual(tm.rsync_download_directory(
                         local_path='',
                         ssh_key='my ssh key',
                         ssh_user='my ssh user',
                         remote_host='remote host',
                         remote_path='remote path',), -1)

    @mock.patch('mozharness.base.transfer.os')
    def test_rsync_download(self, os_mock):
        # successful rsync
        os_mock.path.isdir.return_value = True
        tm = GoodTransferMixin()
        self.assertEqual(tm.rsync_download_directory(
                         local_path='',
                         ssh_key='my ssh key',
                         ssh_user='my ssh user',
                         remote_host='remote host',
                         remote_path='remote path',), None)

    @mock.patch('mozharness.base.transfer.os')
    def test_rsync_download_fail(self, os_mock):
        # ops download has failed
        os_mock.path.isdir.return_value = True
        tm = BadTransferMixin()
        self.assertEqual(tm.rsync_download_directory(
                         local_path='',
                         ssh_key='my ssh key',
                         ssh_user='my ssh user',
                         remote_host='remote host',
                         remote_path='remote path',), -3)


if __name__ == '__main__':
    unittest.main()