From 37b9d32e04fb5d2228d71f135860a73ac5f03ad3 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Tue, 14 Jan 2020 15:09:43 +0545 Subject: [PATCH] test: refactor: use helper remotes in TestCases --- tests/func/test_data_cloud.py | 97 ++++++++--------------------------- tests/remotes.py | 8 +-- 2 files changed, 26 insertions(+), 79 deletions(-) diff --git a/tests/func/test_data_cloud.py b/tests/func/test_data_cloud.py index cfe0febfc9..9da9b11899 100644 --- a/tests/func/test_data_cloud.py +++ b/tests/func/test_data_cloud.py @@ -77,17 +77,19 @@ class TestDataCloudBase(TestDvc): def _get_cloud_class(self): return None - def _should_test(self): + @staticmethod + def should_test(): return False - def _get_url(self): - return "" + @staticmethod + def get_url(): + return NotImplementedError def _get_keyfile(self): return None def _ensure_should_run(self): - if not self._should_test(): + if not self.should_test(): raise SkipTest( "Test {} is disabled".format(self.__class__.__name__) ) @@ -95,7 +97,7 @@ def _ensure_should_run(self): def _setup_cloud(self): self._ensure_should_run() - repo = self._get_url() + repo = self.get_url() keyfile = self._get_keyfile() config = copy.deepcopy(TEST_CONFIG) @@ -188,25 +190,16 @@ def test(self): self._test_cloud() -class TestRemoteS3(TestDataCloudBase): - def _should_test(self): - return S3.should_test() - - def _get_url(self): - return S3.get_url() - +class TestRemoteS3(S3, TestDataCloudBase): def _get_cloud_class(self): return RemoteS3 -class TestRemoteGDrive(TestDataCloudBase): - def _should_test(self): - return GDrive.should_test() - +class TestRemoteGDrive(GDrive, TestDataCloudBase): def _setup_cloud(self): self._ensure_should_run() - repo = self._get_url() + repo = self.get_url() config = copy.deepcopy(TEST_CONFIG) config[TEST_SECTION][Config.SECTION_REMOTE_URL] = repo @@ -221,21 +214,15 @@ def _setup_cloud(self): self.assertIsInstance(self.cloud.get_remote(), self._get_cloud_class()) - def _get_url(self): - return GDrive.get_url() - def _get_cloud_class(self): return RemoteGDrive -class TestRemoteGS(TestDataCloudBase): - def _should_test(self): - return GCP.should_test() - +class TestRemoteGS(GCP, TestDataCloudBase): def _setup_cloud(self): self._ensure_should_run() - repo = self._get_url() + repo = self.get_url() config = copy.deepcopy(TEST_CONFIG) config[TEST_SECTION][Config.SECTION_REMOTE_URL] = repo @@ -247,53 +234,27 @@ def _setup_cloud(self): self.assertIsInstance(self.cloud.get_remote(), self._get_cloud_class()) - def _get_url(self): - return GCP.get_url() - def _get_cloud_class(self): return RemoteGS -class TestRemoteAZURE(TestDataCloudBase): - def _should_test(self): - return Azure.should_test() - - def _get_url(self): - return Azure.get_url() - +class TestRemoteAZURE(Azure, TestDataCloudBase): def _get_cloud_class(self): return RemoteAZURE -class TestRemoteOSS(TestDataCloudBase): - def _should_test(self): - return OSS.should_test() - - def _get_url(self): - return OSS.get_url() - +class TestRemoteOSS(OSS, TestDataCloudBase): def _get_cloud_class(self): return RemoteOSS -class TestRemoteLOCAL(TestDataCloudBase): - def _should_test(self): - return Local.should_test() - - def _get_url(self): - self.dname = Local.get_url() - return self.dname - +class TestRemoteLOCAL(Local, TestDataCloudBase): def _get_cloud_class(self): return RemoteLOCAL - def test(self): - super().test() - self.assertTrue(os.path.isdir(self.dname)) - @pytest.mark.usefixtures("ssh_server") -class TestRemoteSSHMocked(TestDataCloudBase): +class TestRemoteSSHMocked(SSHMocked, TestDataCloudBase): @pytest.fixture(autouse=True) def setup_method_fixture(self, request, ssh_server): self.ssh_server = ssh_server @@ -302,7 +263,7 @@ def setup_method_fixture(self, request, ssh_server): def _setup_cloud(self): self._ensure_should_run() - repo = self._get_url() + repo = self.get_url() keyfile = self._get_keyfile() config = copy.deepcopy(TEST_CONFIG) @@ -314,27 +275,18 @@ def _setup_cloud(self): self.assertIsInstance(self.cloud.get_remote(), self._get_cloud_class()) - def _get_url(self): + def get_url(self): user = self.ssh_server.test_creds["username"] - return SSHMocked.get_url(user, self.ssh_server.port) + return super().get_url(user, self.ssh_server.port) def _get_keyfile(self): return self.ssh_server.test_creds["key_filename"] - def _should_test(self): - return SSHMocked.should_test() - def _get_cloud_class(self): return RemoteSSH -class TestRemoteHDFS(TestDataCloudBase): - def _should_test(self): - return HDFS.should_test() - - def _get_url(self): - return HDFS.get_url() - +class TestRemoteHDFS(HDFS, TestDataCloudBase): def _get_cloud_class(self): return RemoteHDFS @@ -571,18 +523,11 @@ def test(self): self._test() -class TestRecursiveSyncOperations(TestDataCloudBase): +class TestRecursiveSyncOperations(Local, TestDataCloudBase): def main(self, args): ret = main(args) self.assertEqual(ret, 0) - def _get_url(self): - self.dname = Local.get_url() - return self.dname - - def _should_test(self): - return Local.should_test() - def _get_cloud_class(self): return RemoteLOCAL diff --git a/tests/remotes.py b/tests/remotes.py index cf80fd1605..2cf1c19b1b 100644 --- a/tests/remotes.py +++ b/tests/remotes.py @@ -42,9 +42,11 @@ ) TEST_GDRIVE_CLIENT_SECRET = "2fy_HyzSwkxkGzEken7hThXb" +always_test = staticmethod(lambda: True) + class Local: - should_test = lambda: True # noqa: E731 + should_test = always_test @staticmethod def get_storagepath(): @@ -79,7 +81,7 @@ def get_url(): class S3Mocked(S3): - should_test = lambda: True # noqa: E731 + should_test = always_test @classmethod @contextmanager @@ -219,7 +221,7 @@ def get_url(): class SSHMocked: - should_test = lambda: True # noqa: E731 + should_test = always_test @staticmethod def get_url(user, port):