|
1 | 1 | import copy |
2 | | -import getpass |
3 | 2 | import logging |
4 | 3 | import os |
5 | | -import platform |
6 | 4 | import shutil |
7 | 5 | import uuid |
8 | | -from subprocess import CalledProcessError |
9 | | -from subprocess import check_output |
10 | | -from subprocess import Popen |
11 | 6 | from unittest import SkipTest |
12 | 7 |
|
13 | 8 | import pytest |
|
29 | 24 | from dvc.remote.base import STATUS_DELETED |
30 | 25 | from dvc.remote.base import STATUS_NEW |
31 | 26 | from dvc.remote.base import STATUS_OK |
32 | | -from dvc.utils import env2bool |
33 | 27 | from dvc.utils import file_md5 |
34 | 28 | from dvc.utils.compat import str |
35 | 29 | from dvc.utils.stage import dump_stage_file |
36 | 30 | from dvc.utils.stage import load_stage_file |
37 | 31 | from tests.basic_env import TestDvc |
38 | 32 | from tests.utils import spy |
39 | 33 |
|
40 | | - |
41 | | -TEST_REMOTE = "upstream" |
42 | | -TEST_SECTION = 'remote "{}"'.format(TEST_REMOTE) |
43 | | -TEST_CONFIG = { |
44 | | - Config.SECTION_CACHE: {}, |
45 | | - Config.SECTION_CORE: {Config.SECTION_CORE_REMOTE: TEST_REMOTE}, |
46 | | - TEST_SECTION: {Config.SECTION_REMOTE_URL: ""}, |
47 | | -} |
48 | | - |
49 | | -TEST_AWS_REPO_BUCKET = os.environ.get("DVC_TEST_AWS_REPO_BUCKET", "dvc-test") |
50 | | -TEST_GCP_REPO_BUCKET = os.environ.get("DVC_TEST_GCP_REPO_BUCKET", "dvc-test") |
51 | | -TEST_OSS_REPO_BUCKET = "dvc-test" |
52 | | - |
53 | | -TEST_GCP_CREDS_FILE = os.path.abspath( |
54 | | - os.environ.get( |
55 | | - "GOOGLE_APPLICATION_CREDENTIALS", |
56 | | - os.path.join("scripts", "ci", "gcp-creds.json"), |
57 | | - ) |
58 | | -) |
59 | | -# Ensure that absolute path is used |
60 | | -os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = TEST_GCP_CREDS_FILE |
61 | | - |
62 | | -TEST_GDRIVE_CLIENT_ID = ( |
63 | | - "719861249063-v4an78j9grdtuuuqg3lnm0sugna6v3lh.apps.googleusercontent.com" |
| 34 | +from tests.remotes import ( |
| 35 | + _should_test_aws, |
| 36 | + _should_test_azure, |
| 37 | + _should_test_gcp, |
| 38 | + _should_test_gdrive, |
| 39 | + _should_test_hdfs, |
| 40 | + _should_test_oss, |
| 41 | + _should_test_ssh, |
| 42 | + TEST_CONFIG, |
| 43 | + TEST_SECTION, |
| 44 | + TEST_GCP_CREDS_FILE, |
| 45 | + TEST_GDRIVE_CLIENT_ID, |
| 46 | + TEST_GDRIVE_CLIENT_SECRET, |
| 47 | + TEST_REMOTE, |
| 48 | + get_aws_url, |
| 49 | + get_azure_url, |
| 50 | + get_gcp_url, |
| 51 | + get_gdrive_url, |
| 52 | + get_hdfs_url, |
| 53 | + get_local_url, |
| 54 | + get_oss_url, |
| 55 | + get_ssh_url, |
| 56 | + get_ssh_url_mocked, |
64 | 57 | ) |
65 | | -TEST_GDRIVE_CLIENT_SECRET = "2fy_HyzSwkxkGzEken7hThXb" |
66 | | - |
67 | | - |
68 | | -def _should_test_aws(): |
69 | | - do_test = env2bool("DVC_TEST_AWS", undefined=None) |
70 | | - if do_test is not None: |
71 | | - return do_test |
72 | | - |
73 | | - if os.getenv("AWS_ACCESS_KEY_ID") and os.getenv("AWS_SECRET_ACCESS_KEY"): |
74 | | - return True |
75 | | - |
76 | | - return False |
77 | | - |
78 | | - |
79 | | -def _should_test_gdrive(): |
80 | | - if os.getenv(RemoteGDrive.GDRIVE_USER_CREDENTIALS_DATA): |
81 | | - return True |
82 | | - |
83 | | - return False |
84 | | - |
85 | | - |
86 | | -def _should_test_gcp(): |
87 | | - do_test = env2bool("DVC_TEST_GCP", undefined=None) |
88 | | - if do_test is not None: |
89 | | - return do_test |
90 | | - |
91 | | - if not os.path.exists(TEST_GCP_CREDS_FILE): |
92 | | - return False |
93 | | - |
94 | | - try: |
95 | | - check_output( |
96 | | - [ |
97 | | - "gcloud", |
98 | | - "auth", |
99 | | - "activate-service-account", |
100 | | - "--key-file", |
101 | | - TEST_GCP_CREDS_FILE, |
102 | | - ] |
103 | | - ) |
104 | | - except (CalledProcessError, OSError): |
105 | | - return False |
106 | | - return True |
107 | | - |
108 | | - |
109 | | -def _should_test_azure(): |
110 | | - do_test = env2bool("DVC_TEST_AZURE", undefined=None) |
111 | | - if do_test is not None: |
112 | | - return do_test |
113 | | - |
114 | | - return os.getenv("AZURE_STORAGE_CONTAINER_NAME") and os.getenv( |
115 | | - "AZURE_STORAGE_CONNECTION_STRING" |
116 | | - ) |
117 | | - |
118 | | - |
119 | | -def _should_test_oss(): |
120 | | - do_test = env2bool("DVC_TEST_OSS", undefined=None) |
121 | | - if do_test is not None: |
122 | | - return do_test |
123 | | - |
124 | | - return ( |
125 | | - os.getenv("OSS_ENDPOINT") |
126 | | - and os.getenv("OSS_ACCESS_KEY_ID") |
127 | | - and os.getenv("OSS_ACCESS_KEY_SECRET") |
128 | | - ) |
129 | | - |
130 | | - |
131 | | -def _should_test_ssh(): |
132 | | - do_test = env2bool("DVC_TEST_SSH", undefined=None) |
133 | | - if do_test is not None: |
134 | | - return do_test |
135 | | - |
136 | | - # FIXME: enable on windows |
137 | | - if os.name == "nt": |
138 | | - return False |
139 | | - |
140 | | - try: |
141 | | - check_output(["ssh", "-o", "BatchMode=yes", "127.0.0.1", "ls"]) |
142 | | - except (CalledProcessError, IOError): |
143 | | - return False |
144 | | - |
145 | | - return True |
146 | | - |
147 | | - |
148 | | -def _should_test_hdfs(): |
149 | | - if platform.system() != "Linux": |
150 | | - return False |
151 | | - |
152 | | - try: |
153 | | - check_output( |
154 | | - ["hadoop", "version"], shell=True, executable=os.getenv("SHELL") |
155 | | - ) |
156 | | - except (CalledProcessError, IOError): |
157 | | - return False |
158 | | - |
159 | | - p = Popen( |
160 | | - "hadoop fs -ls hdfs://127.0.0.1/", |
161 | | - shell=True, |
162 | | - executable=os.getenv("SHELL"), |
163 | | - ) |
164 | | - p.communicate() |
165 | | - if p.returncode != 0: |
166 | | - return False |
167 | | - |
168 | | - return True |
169 | | - |
170 | | - |
171 | | -def get_local_storagepath(): |
172 | | - return TestDvc.mkdtemp() |
173 | | - |
174 | | - |
175 | | -def get_local_url(): |
176 | | - return get_local_storagepath() |
177 | | - |
178 | | - |
179 | | -def get_ssh_url(): |
180 | | - return "ssh://{}@127.0.0.1:22{}".format( |
181 | | - getpass.getuser(), get_local_storagepath() |
182 | | - ) |
183 | | - |
184 | | - |
185 | | -def get_ssh_url_mocked(user, port): |
186 | | - path = get_local_storagepath() |
187 | | - if os.name == "nt": |
188 | | - # NOTE: On Windows get_local_storagepath() will return an ntpath |
189 | | - # that looks something like `C:\some\path`, which is not compatible |
190 | | - # with SFTP paths [1], so we need to convert it to a proper posixpath. |
191 | | - # To do that, we should construct a posixpath that would be relative |
192 | | - # to the server's root. In our case our ssh server is running with |
193 | | - # `c:/` as a root, and our URL format requires absolute paths, so the |
194 | | - # resulting path would look like `/some/path`. |
195 | | - # |
196 | | - # [1]https://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-6 |
197 | | - drive, path = os.path.splitdrive(path) |
198 | | - assert drive.lower() == "c:" |
199 | | - path = path.replace("\\", "/") |
200 | | - url = "ssh://{}@127.0.0.1:{}{}".format(user, port, path) |
201 | | - return url |
202 | | - |
203 | | - |
204 | | -def get_hdfs_url(): |
205 | | - return "hdfs://{}@127.0.0.1{}".format( |
206 | | - getpass.getuser(), get_local_storagepath() |
207 | | - ) |
208 | | - |
209 | | - |
210 | | -def get_aws_storagepath(): |
211 | | - return TEST_AWS_REPO_BUCKET + "/" + str(uuid.uuid4()) |
212 | | - |
213 | | - |
214 | | -def get_aws_url(): |
215 | | - return "s3://" + get_aws_storagepath() |
216 | | - |
217 | | - |
218 | | -def get_gdrive_url(): |
219 | | - return "gdrive://root/" + str(uuid.uuid4()) |
220 | | - |
221 | | - |
222 | | -def get_gcp_storagepath(): |
223 | | - return TEST_GCP_REPO_BUCKET + "/" + str(uuid.uuid4()) |
224 | | - |
225 | | - |
226 | | -def get_gcp_url(): |
227 | | - return "gs://" + get_gcp_storagepath() |
228 | | - |
229 | | - |
230 | | -def get_azure_url(): |
231 | | - container_name = os.getenv("AZURE_STORAGE_CONTAINER_NAME") |
232 | | - assert container_name is not None |
233 | | - return "azure://{}/{}".format(container_name, str(uuid.uuid4())) |
234 | | - |
235 | | - |
236 | | -def get_oss_storagepath(): |
237 | | - return "{}/{}".format(TEST_OSS_REPO_BUCKET, (uuid.uuid4())) |
238 | | - |
239 | | - |
240 | | -def get_oss_url(): |
241 | | - return "oss://{}".format(get_oss_storagepath()) |
242 | 58 |
|
243 | 59 |
|
244 | 60 | class TestDataCloud(TestDvc): |
|
0 commit comments