-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrclone_test.py
106 lines (92 loc) · 4.31 KB
/
rclone_test.py
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
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# pylint: disable=W0212,C0111
import unittest
import tempfile
import os
import logging
import json
import rclone
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(name)s [%(levelname)s]: %(message)s")
class RSyncTest(unittest.TestCase):
def setUp(self):
self.cfg = """[local]
type = local
nounc = true"""
def test_execute_with_wrong_command(self):
result = rclone.with_config(self.cfg)._execute(
["command_not_valid", "some", "args"])
self.assertEqual(result.get('code'), -20)
self.assertIsInstance(result.get('error'), FileNotFoundError)
def test_execute_with_correct_command(self):
result = rclone.with_config(self.cfg)._execute(["echo", "123"])
self.assertEqual(result.get('code'), 0)
self.assertIsNotNone(result.get('out'))
def test_listremoted(self):
result = rclone.with_config(self.cfg).listremotes()
self.assertEqual(result.get('code'), 0)
self.assertEqual(result.get('out'), b'local:\n')
def test_copy_and_ls(self):
source = "local:" + os.getcwd() + "/README.md"
with tempfile.TemporaryDirectory() as dest:
result = rclone.with_config(self.cfg).copy(
source, "local:" + dest)
self.assertEqual(result.get('code'), 0)
self.assertEqual(result.get('out'), b'')
result = rclone.with_config(self.cfg).ls("local:"+dest)
self.assertEqual(result.get('code'), 0)
self.assertRegex(result.get('out').decode("utf-8"),
r'.*\sREADME.md.*', "README.md was not listed.")
def test_sync_and_lsjson(self):
source = "local:" + os.getcwd() + "/README.md"
with tempfile.TemporaryDirectory() as dest:
result = rclone.with_config(self.cfg).sync(
source, "local:" + dest)
self.assertEqual(result.get('code'), 0)
self.assertEqual(result.get('out'), b'')
result = rclone.with_config(self.cfg).lsjson("local:"+dest)
self.assertEqual(result.get('code'), 0)
result_json = json.loads(result.get('out').decode("utf-8"))
self.assertGreater(len(result_json), 0)
self.assertEqual(result_json[0].get('Path'), 'README.md')
self.assertFalse(result_json[0].get('IsDir'))
def test_copy_lsjson_and_delete(self):
source = "local:" + os.getcwd() + "/README.md"
with tempfile.TemporaryDirectory() as dest:
# copy
result = rclone.with_config(self.cfg).copy(
source, "local:" + dest)
self.assertEqual(result.get('code'), 0)
self.assertEqual(result.get('out'), b'')
# lsjson
result = rclone.with_config(self.cfg).lsjson("local:"+dest)
self.assertEqual(result.get('code'), 0)
result_json = json.loads(result.get('out').decode("utf-8"))
self.assertGreater(len(result_json), 0)
self.assertEqual(result_json[0].get('Path'), 'README.md')
self.assertFalse(result_json[0].get('IsDir'))
# delete
result = rclone.with_config(self.cfg).delete(
"local:" + dest + "/README.md")
self.assertEqual(result.get('code'), 0)
# lsjson to check that the file is gone
result = rclone.with_config(self.cfg).lsjson("local:"+dest)
self.assertEqual(result.get('code'), 0)
result_json = json.loads(result.get('out').decode("utf-8"))
self.assertEqual(len(result_json), 0)