-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_sync.py
More file actions
222 lines (157 loc) · 8.61 KB
/
Copy pathtest_cli_sync.py
File metadata and controls
222 lines (157 loc) · 8.61 KB
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
import shutil
import time
import unittest
from pathlib import Path
from unittest.mock import patch
from click.testing import CliRunner
from cloudinary_cli.cli import cli
from test.helper_test import unique_suffix, RESOURCES_DIR, TEST_FILES_DIR, delete_cld_folder_if_exists, retry_assertion, \
get_request_url, get_params, URLLIB3_REQUEST
from test.test_modules.test_cli_upload_dir import UPLOAD_MOCK_RESPONSE
from cloudinary_cli.utils.api_utils import get_folder_mode
class TestCLISync(unittest.TestCase):
runner = CliRunner()
LOCAL_PARTIAL_SYNC_DIR = str(Path.joinpath(RESOURCES_DIR, "test_sync_partial"))
LOCAL_SYNC_PULL_DIR = str(Path.joinpath(RESOURCES_DIR, unique_suffix("test_sync_pull")))
CLD_SYNC_DIR = unique_suffix("test_sync")
DUPLICATE_NAME = unique_suffix("duplicate_name")
GRACE_PERIOD = 3 # seconds
folder_mode = "fixed"
def setUp(self) -> None:
self.folder_mode = get_folder_mode()
delete_cld_folder_if_exists(self.CLD_SYNC_DIR, self.folder_mode)
time.sleep(1)
def tearDown(self) -> None:
delete_cld_folder_if_exists(self.CLD_SYNC_DIR, self.folder_mode)
time.sleep(1)
shutil.rmtree(self.LOCAL_SYNC_PULL_DIR, ignore_errors=True)
@retry_assertion
def test_cli_sync_push(self):
result = self.runner.invoke(cli, ['sync', '--push', '-F', TEST_FILES_DIR, self.CLD_SYNC_DIR])
self.assertEqual(0, result.exit_code)
self.assertIn("Synced | 12", result.output)
self.assertIn("Done!", result.output)
# the upload banner names both the destination folder and the active cloud
self.assertIn(f"to Cloudinary folder '{self.CLD_SYNC_DIR}'", result.output)
self.assertIn("in cloud '", result.output)
def test_cli_sync_push_non_existing_folder(self):
non_existing_dir = self.LOCAL_SYNC_PULL_DIR + "non_existing"
result = self.runner.invoke(cli, ['sync', '--push', non_existing_dir, self.CLD_SYNC_DIR])
self.assertIn(f"Cannot push a non-existent local folder '{non_existing_dir}'", result.output)
self.assertIn("Aborting...", result.output)
@retry_assertion
def test_cli_sync_push_twice(self):
self._upload_sync_files(TEST_FILES_DIR)
# wait for indexing to be updated
time.sleep(self.GRACE_PERIOD)
result = self.runner.invoke(cli, ['sync', '--push', '-F', TEST_FILES_DIR, self.CLD_SYNC_DIR])
self.assertEqual(0, result.exit_code)
self.assertIn("Skipping 12 items", result.output)
self.assertIn("Done!", result.output)
@retry_assertion
def test_cli_sync_push_out_of_sync(self):
self._upload_sync_files(TEST_FILES_DIR)
# wait for indexing to be updated
time.sleep(self.GRACE_PERIOD)
result = self.runner.invoke(cli, ['sync', '--push', '-F', self.LOCAL_PARTIAL_SYNC_DIR, self.CLD_SYNC_DIR])
self.assertEqual(0, result.exit_code)
self.assertIn("Found 2 items in local folder", result.output)
self.assertIn("Skipping 1 items", result.output)
self.assertIn("Deleting 11 resources", result.output)
self.assertIn("In Sync| 1", result.output)
self.assertIn("Synced | 1", result.output)
self.assertIn("Done!", result.output)
@retry_assertion
def test_cli_sync_pull(self):
self._upload_sync_files(TEST_FILES_DIR)
# wait for indexing to be updated
time.sleep(self.GRACE_PERIOD)
result = self.runner.invoke(cli, ['sync', '--pull', '-F', self.LOCAL_SYNC_PULL_DIR, self.CLD_SYNC_DIR])
self.assertEqual(0, result.exit_code, result.output)
self.assertIn("Synced | 12", result.output)
self.assertIn("Done!", result.output)
def test_cli_sync_pull_non_existing_folder(self):
non_existing_dir = self.CLD_SYNC_DIR + "non_existing"
result = self.runner.invoke(cli, ['sync', '--pull', self.LOCAL_SYNC_PULL_DIR, non_existing_dir])
self.assertIn(f"Cannot pull from a non-existent Cloudinary folder '{non_existing_dir}'", result.output)
self.assertIn("Aborting...", result.output)
@retry_assertion
def test_cli_sync_pull_twice(self):
self._upload_sync_files(TEST_FILES_DIR)
# wait for indexing to be updated
time.sleep(self.GRACE_PERIOD)
result = self.runner.invoke(cli, ['sync', '--pull', '-F', self.LOCAL_SYNC_PULL_DIR, self.CLD_SYNC_DIR])
self.assertEqual(0, result.exit_code)
self.assertIn("Done!", result.output)
result = self.runner.invoke(cli, ['sync', '--pull', '-F', self.LOCAL_SYNC_PULL_DIR, self.CLD_SYNC_DIR])
self.assertEqual(0, result.exit_code)
self.assertIn("Skipping 12 items", result.output)
self.assertIn("Done!", result.output)
@retry_assertion
def test_cli_sync_pull_out_of_sync(self):
self._upload_sync_files(TEST_FILES_DIR)
# wait for indexing to be updated
time.sleep(self.GRACE_PERIOD)
shutil.copytree(self.LOCAL_PARTIAL_SYNC_DIR, self.LOCAL_SYNC_PULL_DIR)
result = self.runner.invoke(cli, ['sync', '--pull', '-F', self.LOCAL_SYNC_PULL_DIR, self.CLD_SYNC_DIR])
self.assertEqual(0, result.exit_code)
self.assertIn("Found 2 items in local folder", result.output)
self.assertIn("Skipping 1 items", result.output)
self.assertIn("Deleting 1 local files", result.output)
self.assertIn("Downloading 11 files", result.output)
self.assertIn("In Sync| 1", result.output)
self.assertIn("Synced | 11", result.output)
self.assertIn("Done!", result.output)
def _upload_sync_files(self, dir, optional_params=None):
if optional_params is None:
optional_params = []
result = self.runner.invoke(cli, ['sync', '--push', '-F', dir, self.CLD_SYNC_DIR] + optional_params)
self.assertEqual(0, result.exit_code)
self.assertIn("Synced | 12", result.output)
self.assertIn("Done!", result.output)
@patch(URLLIB3_REQUEST)
def test_sync_override_defaults(self, mocker):
mocker.return_value = UPLOAD_MOCK_RESPONSE
result = self.runner.invoke(cli, ['sync', '--push', '-fm', 'fixed', '-F', TEST_FILES_DIR, self.CLD_SYNC_DIR,
"-o", "resource_type", "raw", "-O", "unique_filename", "True"])
self.assertEqual(0, result.exit_code)
self.assertIn("raw/upload", get_request_url(mocker))
self.assertTrue(get_params(mocker)['unique_filename'])
@unittest.skipUnless(get_folder_mode() == "dynamic", "requires dynamic folder mode")
@retry_assertion
def test_cli_sync_duplicate_file_names_dynamic_folder_mode(self):
self._upload_sync_files(TEST_FILES_DIR, ['-o', 'display_name', self.DUPLICATE_NAME])
# wait for indexing to be updated
time.sleep(self.GRACE_PERIOD)
result = self.runner.invoke(cli, ['sync', '--pull', '-F', self.LOCAL_SYNC_PULL_DIR, self.CLD_SYNC_DIR])
self.assertEqual(0, result.exit_code)
self.assertIn("Found 0 items in local folder", result.output)
self.assertIn("Downloading 12 files", result.output)
for index in range(1, 6):
self.assertIn(f"{self.DUPLICATE_NAME} ({index})", result.output)
self.assertIn("Done!", result.output)
result = self.runner.invoke(cli, ['sync', '--push', '-F', self.LOCAL_SYNC_PULL_DIR, self.CLD_SYNC_DIR])
self.assertEqual(0, result.exit_code)
self.assertIn("Skipping 12 items", result.output)
self.assertIn("Done!", result.output)
@retry_assertion
def test_cli_sync_push_dry_run(self):
self._upload_sync_files(TEST_FILES_DIR)
# wait for indexing to be updated
time.sleep(self.GRACE_PERIOD)
result = self.runner.invoke(cli, ['sync', '--push', '-F', self.LOCAL_PARTIAL_SYNC_DIR, self.CLD_SYNC_DIR, '--dry-run'])
# check that no files were uploaded
self.assertEqual(0, result.exit_code)
self.assertIn("Dry run mode enabled. The following files would be uploaded:", result.output)
self.assertIn("Done!", result.output)
@retry_assertion
def test_cli_sync_pull_dry_run(self):
self._upload_sync_files(TEST_FILES_DIR)
# wait for indexing to be updated
time.sleep(self.GRACE_PERIOD)
shutil.copytree(self.LOCAL_PARTIAL_SYNC_DIR, self.LOCAL_SYNC_PULL_DIR)
result = self.runner.invoke(cli, ['sync', '--pull', '-F', self.LOCAL_SYNC_PULL_DIR, self.CLD_SYNC_DIR, '--dry-run'])
# check that no files were downloaded
self.assertEqual(0, result.exit_code)
self.assertIn("Dry run mode enabled. The following files would be downloaded:", result.output)
self.assertIn("Done!", result.output)