Example #1
0
    def test_submittedjob_get_final_output(self):
        """Test _get_final_output"""

        _callback = mock.Mock()
        resp_a = mock.create_autospec(Response)
        resp_a.success = False
        resp_a.result = RestCallException(None, "test", None)
        resp_b = mock.create_autospec(Response)
        resp_b.success = False
        resp_b.result = RestCallException(None, "test", None)

        api = mock.create_autospec(BatchAppsApi)
        api.props_output.return_value = resp_a
        api.get_output.return_value = resp_b

        job = SubmittedJob(api, None, None, None)
        output = job._get_final_output("", True)
        api.props_output.assert_called_with(url=None)
        self.assertFalse(api.get_output.called)

        job = SubmittedJob(api,
                           None,
                           None,
                           None,
                           outputLink={'href': 'http://output'})
        output = job._get_final_output("", True)
        api.props_output.assert_called_with(url='http://output')
        self.assertFalse(api.get_output.called)

        resp_a.success = True
        resp_a.result = 42
        output = job._get_final_output("", True)
        api.props_output.assert_called_with(url='http://output')
        api.get_output.assert_called_with("",
                                          42,
                                          None,
                                          True,
                                          url='http://output',
                                          callback=None,
                                          block=4096)

        self.assertEqual(output, resp_b)
        output = job._get_final_output("", True, callback=_callback, block=111)
        api.get_output.assert_called_with("",
                                          42,
                                          None,
                                          True,
                                          url='http://output',
                                          callback=_callback,
                                          block=111)
Example #2
0
    def test_pool_update(self):
        """Test delete"""

        api = mock.create_autospec(BatchAppsApi)
        pool = Pool(api)
        api.get_pool.return_value = mock.create_autospec(Response)
        api.get_pool.return_value.success = True
        api.get_pool.return_value.result = {
            'targetDedicated': '5',
            'currentDedicated': '4',
            'state': 'active',
            'allocationState': 'test',
        }

        self.assertEqual(pool.target_size, 0)
        self.assertEqual(pool.current_size, 0)
        self.assertEqual(pool.state, None)
        self.assertEqual(pool.allocation_state, None)
        self.assertEqual(pool.resize_error, '')
        pool.update()
        api.get_pool.assert_called_with(pool_id=None)
        self.assertEqual(pool.target_size, 5)
        self.assertEqual(pool.current_size, 4)
        self.assertEqual(pool.state, 'active')
        self.assertEqual(pool.allocation_state, 'test')
        self.assertEqual(pool.resize_error, '')

        api.get_pool.return_value.success = False
        api.get_pool.return_value.result = RestCallException(
            None, "Test", None)

        with self.assertRaises(RestCallException):
            pool.update()
Example #3
0
    def test_task_get_output(self, mock_get):
        """Test get_output"""

        _callback = mock.Mock()
        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "test", None)
        api = mock.create_autospec(BatchAppsApi)
        mock_get.return_value = resp

        task = Task(api, None)
        with self.assertRaises(RestCallException):
            task.get_output(None, None)
        resp.success = True
        output = task.get_output({}, "dir")
        mock_get.assert_called_with({},
                                    "dir",
                                    False,
                                    callback=None,
                                    block=4096)
        self.assertEqual(output, "dir\\")

        output = task.get_output({'name': 'test.txt'},
                                 "dir",
                                 overwrite=True,
                                 callback=_callback,
                                 block=111)
        mock_get.assert_called_with({'name': 'test.txt'},
                                    "dir",
                                    True,
                                    callback=_callback,
                                    block=111)
        self.assertEqual(output, "dir\\test.txt")
    def test_rest_client_put(self, mock_open, mock_call):
        """Test put"""

        auth = mock.create_autospec(Credentials)
        u_file = mock.create_autospec(UserFile)
        u_file.name = "test.jpg"
        u_file.path = "testfile"

        def _callback(progress, data, total):
            self.assertEqual(progress, 0.0)
            self.assertIsInstance(progress, float)
            self.assertIsInstance(data, int)
            self.assertIsInstance(total, int)

        with self.assertRaises(RestCallException):
            rest_client.put(auth, "http://test//{0}",
                            {"Content-Type": "application/json"}, u_file, {})
        with self.assertRaises(RestCallException):
            rest_client.put(auth, "http://test//{0}",
                            {"Content-Type": "application/json"}, u_file, {
                                'timestamp': 'a',
                                'originalFilePath': 'b'
                            })

        val = rest_client.put(auth,
                              "http://test//{name}",
                              {"Content-Type": "application/json"},
                              u_file, {
                                  'timestamp': 'a',
                                  'originalFilePath': 'b'
                              },
                              callback=_callback,
                              block_size=1111)
        mock_open.assert_called_with("testfile", 'rb')
        mock_call.assert_called_with(
            auth,
            'PUT',
            "http://test//test.jpg",
            data=mock.ANY,
            params={
                'timestamp': 'a',
                'originalFilePath': 'b'
            },
            headers={'Content-Type': 'application/octet-stream'})
        self.assertIsNotNone(val)

        mock_open.side_effect = OSError("test")
        with self.assertRaises(RestCallException):
            rest_client.put(auth, "http://test//{name}",
                            {"Content-Type": "application/json"}, u_file, {
                                'timestamp': 'a',
                                'originalFilePath': 'b'
                            })

        mock_open.side_effect = None
        mock_call.side_effect = RestCallException(None, "Boom!", None)

        with self.assertRaises(RestCallException):
            rest_client.put(auth, "http://test//{name}",
                            {"Content-Type": "application/json"}, u_file, {})
    def test_rest_client_post(self, mock_call):
        """Test post"""

        auth = mock.create_autospec(Credentials)

        with self.assertRaises(RestCallException):
            rest_client.post(auth, "http://test", {})

        mock_call.return_value.text = '{"key":"value"}'
        val = rest_client.post(auth, "http://test", {})
        mock_call.assert_called_with(auth,
                                     'POST',
                                     "http://test",
                                     headers={},
                                     data=None)
        self.assertEqual(val, {"key": "value"})

        val = rest_client.post(auth,
                               "http://test", {},
                               message={"msg": "test"})
        mock_call.assert_called_with(auth,
                                     'POST',
                                     "http://test",
                                     headers={},
                                     data='{"msg": "test"}')

        del mock_call.return_value.text
        with self.assertRaises(RestCallException):
            rest_client.post(auth, "http://test", {})

        mock_call.side_effect = RestCallException(None, "Boom!", None)
        with self.assertRaises(RestCallException):
            rest_client.post(auth, "http://test", {})
def get(auth, url, headers, params=None):
    """
    Call GET.

    :Args:
        - auth (:class:`.Credentials`): The session credentials object.
        - url (str): The complete endpoint URL.
        - headers (dict): The headers to be used in the request.

    :Kwargs:
        - params (dict): Any additional parameters to be added to the URI as
          required by the specified call.

    :Returns:
        - The data retrieved by the GET call, after json decoding.

    :Raises:
        - :exc:`.RestCallException` is the call failed,
          or returned a non-200 status.
    """
    LOG.debug("GET call URL: {0}, params: {1}".format(url, params))

    try:
        response = _call(auth, 'GET', url, headers=headers, params=params)
        return response.json()

    except RestCallException:
        raise

    except ValueError as exp:
        raise RestCallException(ValueError,
                                "No json object to be decoded from GET call.",
                                exp)
    def test_poolmgr_get_pool(self, mock_pool, mock_api, mock_creds, mock_cfg):
        """Test get_pool"""

        mgr = PoolManager(mock_creds, cfg=mock_cfg)

        with self.assertRaises(ValueError):
            mgr.get_pool()

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "test", None)
        mgr._client.get_pool.return_value = resp

        with self.assertRaises(RestCallException):
            mgr.get_pool(url="http://test")
        mgr._client.get_pool.assert_called_with(url="http://test")

        resp.success = True
        resp.result = {'id': '1', 'autoPool': False, 'state': 'test'}
        job = mgr.get_pool(url="http://test")
        mgr._client.get_pool.assert_called_with(url="http://test")
        mock_pool.assert_called_with(mgr._client,
                                     id='1',
                                     autoPool=False,
                                     state="test")

        resp.result = {'id': '1', 'name': '2', 'type': '3', 'other': '4'}
        job = mgr.get_pool(poolid="test_id")
        mgr._client.get_pool.assert_called_with(pool_id="test_id")
Example #8
0
    def test_submittedjob_get_thumbnail(self, mock_prev):
        """Test get_thumbnail"""

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "test", None)
        mock_prev.return_value = resp
        api = mock.create_autospec(BatchAppsApi)

        job = SubmittedJob(api, "abc", None, None)

        with self.assertRaises(FileDownloadException):
            job.get_thumbnail()
        self.assertFalse(mock_prev.called)

        job = SubmittedJob(api,
                           "abc",
                           None,
                           None,
                           previewLink={'href': 'http://'})

        with self.assertRaises(RestCallException):
            job.get_thumbnail()
        self.assertTrue(mock_prev.called)

        resp.success = True
        thumb = job.get_thumbnail(filename="thumb.png")
        mock_prev.assert_called_with(tempfile.gettempdir(), "thumb.png", True)

        thumb = job.get_thumbnail(download_dir="dir",
                                  filename="thumb.png",
                                  overwrite=False)
        mock_prev.assert_called_with("dir", "thumb.png", False)
        self.assertEqual(thumb, "dir\\thumb.png")
    def test_filemgr_list_files(self, mock_file, mock_api, mock_creds,
                                mock_cfg):
        """Test list_files"""

        mgr = FileManager(mock_creds, cfg=mock_cfg)

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "test", None)
        mgr._client.list_files.return_value = resp

        with self.assertRaises(RestCallException):
            test = mgr.list_files()
        self.assertTrue(mgr._client.list_files.called)
        self.assertFalse(mock_file.called)

        resp.success = True
        resp.result = ["test", True, 42, None]
        test = mgr.list_files()
        self.assertIsInstance(test, list)
        mock_file.assert_any_call(mgr._client, "test")
        mock_file.assert_any_call(mgr._client, True)
        mock_file.assert_any_call(mgr._client, 42)
        mock_file.assert_any_call(mgr._client, None)
        self.assertEqual(mock_file.call_count, 4)
    def test_filemgr_find_files(self, mock_file, mock_api, mock_creds,
                                mock_cfg):
        """Test find_files"""

        mgr = FileManager(mock_creds, cfg=mock_cfg)

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "test", None)
        mgr._client.query_files.return_value = resp

        with self.assertRaises(RestCallException):
            res = mgr.find_files("test")
        mgr._client.query_files.assert_called_with("test")

        with self.assertRaises(RestCallException):
            res = mgr.find_files([None])
        mgr._client.query_files.assert_called_with([None])

        resp.success = True
        resp.result = []
        res = mgr.find_files("test")
        self.assertEqual(res, [])
        self.assertFalse(mock_file.called)

        resp.result = ["testFile", None]
        res = mgr.find_files("test")
        self.assertEqual(len(res), 2)
        mock_file.assert_any_call(mgr._client, "testFile")
        mock_file.assert_any_call(mgr._client, None)
    def test_api_list_tasks(self, mock_url, mock_creds, mock_config, mock_get):
        """Test list_tasks"""

        _api = BatchAppsApi(mock_creds, mock_config)
        mock_url.return_value = "https://test.com/{jobid}"
        mock_get.return_value = {}

        val = _api.list_tasks()
        self.assertFalse(val.success)
        self.assertFalse(mock_get.called)

        val = _api.list_tasks(url="http://test")
        self.assertFalse(mock_url.called)
        self.assertFalse(val.success)
        mock_get.assert_called_with(mock_creds, "http://test", self.headers)

        mock_get.return_value = {'tasks': None}
        val = _api.list_tasks(job_id="test")
        mock_get.assert_called_with(mock_creds, "https://test.com/test",
                                    self.headers)
        self.assertFalse(val.success)

        mock_get.return_value = {'tasks': []}
        val = _api.list_tasks(job_id="test")
        self.assertTrue(val.success)
        self.assertEqual(val.result, [])

        mock_get.side_effect = RestCallException(None, "Boom!", None)
        val = _api.list_tasks(job_id="test")
        self.assertFalse(val.success)
    def test_api_get_job(self, mock_url, mock_creds, mock_config, mock_get):
        """Test get_job"""

        _api = BatchAppsApi(mock_creds, mock_config)
        mock_url.return_value = "https://test_endpoint.com/api/jobs"
        mock_get.return_value = None
        val = _api.get_job()
        self.assertIsInstance(val, Response)
        self.assertFalse(val.success)

        val = _api.get_job(url="https://job_url")
        mock_get.assert_called_with(mock_creds, "https://job_url",
                                    self.headers)
        self.assertFalse(val.success)

        mock_get.return_value = {'id': '1', 'name': '2', 'type': '3'}
        val = _api.get_job(url="https://job_url")
        mock_get.assert_called_with(mock_creds, "https://job_url",
                                    self.headers)
        self.assertTrue(val.success)

        mock_url.return_value = "https://test_endpoint.com/api/{jobid}"
        val = _api.get_job(job_id="abcdef")
        mock_get.assert_called_with(mock_creds,
                                    "https://test_endpoint.com/api/abcdef",
                                    self.headers)
        self.assertTrue(val.success)
        self.assertTrue(mock_url.called)

        mock_get.side_effect = RestCallException(None, "Boom~", None)
        val = _api.get_job(job_id="abcdef")
        self.assertFalse(val.success)
    def test_rest_client_head(self, mock_call):
        """Test head"""

        auth = mock.create_autospec(Credentials)
        val = rest_client.head(auth, "http://test", {})
        mock_call.assert_called_with(auth, 'HEAD', "http://test", headers={})

        with self.assertRaises(RestCallException):
            rest_client.head(auth, "http://test/{0}", {})

        val = rest_client.head(auth, "http://test/{name}", {})
        mock_call.assert_called_with(auth, 'HEAD', "http://test/", headers={})

        val = rest_client.head(auth,
                               "http://test/{name}", {},
                               filename="test file.jpg")

        mock_call.assert_called_with(auth,
                                     'HEAD',
                                     "http://test/test%20file.jpg",
                                     headers={})

        mock_call.return_value.headers = {}
        with self.assertRaises(RestCallException):
            rest_client.head(auth, "http://test", {})

        mock_call.return_value.headers = {"content-length": "10"}
        val = rest_client.head(auth, "http://test", {})
        self.assertEqual(val, 10)

        mock_call.side_effect = RestCallException(None, "Boom!", None)
        with self.assertRaises(RestCallException):
            rest_client.head(auth, "http://test", {})
    def test_userfile_is_uploaded(self, mock_mod, mock_query, mock_ufile):
        """Test is_uploaded"""

        mock_mod.return_value = True
        result = mock.create_autospec(UserFile)
        result.name = "1"
        mock_ufile.return_value = result
        api = mock.create_autospec(batchapps.api.BatchAppsApi)

        ufile = UserFile(api, {'name':'1'})

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "Boom", None)
        api.query_files.return_value = resp

        with self.assertRaises(RestCallException):
            ufile.is_uploaded()

        resp.success = True
        resp.result = ['1', '2', '3']
        self.assertIsInstance(ufile.is_uploaded(), UserFile)
        self.assertTrue(api.query_files.called)
        self.assertTrue(mock_query.called)
        self.assertEqual(mock_ufile.call_count, 3)
        mock_ufile.assert_called_with(mock.ANY, '3')

        result.name = "4"
        self.assertIsNone(ufile.is_uploaded())
    def test_rest_client_get(self, mock_call):
        """Test get"""

        auth = mock.create_autospec(Credentials)
        rest_client.get(auth, "http://test", {})
        mock_call.assert_called_with(auth,
                                     'GET',
                                     "http://test",
                                     headers={},
                                     params=None)

        rest_client.get(auth, "http://test", {}, params={'a': 1})
        mock_call.assert_called_with(auth,
                                     'GET',
                                     "http://test",
                                     headers={},
                                     params={'a': 1})

        mock_call.return_value.json.side_effect = ValueError("Value Error!")
        with self.assertRaises(RestCallException):
            rest_client.get(auth, "http://test", {})

        mock_call.side_effect = RestCallException(None, "Boom!", None)
        with self.assertRaises(RestCallException):
            rest_client.get(auth, "http://test", {})
def delete(auth, url, headers):
    """
    Call DELETE.
    Currently only used to delete pools.

    :Args:
        - auth (:class:`.Credentials`): The session credentials object.
        - url (str): The complete endpoint URL.
        - headers (dict): The headers to be used in the request.

    :Returns:
        - The raw server response.

    :Raises:
        - :exc:`.RestCallException` if the call failed or returned a
          non-200 status.
    """
    try:
        LOG.debug("DELETE call URL: {0}".format(url))
        response = _call(auth, 'DELETE', url, headers=headers)
        return response

    except RestCallException:
        raise

    except (ValueError, AttributeError) as exp:
        raise RestCallException(
            ValueError, "No json object to be decoded from DELETE call.", exp)
    def test_api_get_pool(self, mock_url, mock_creds, mock_config, mock_get):
        """Test get_pool"""

        _api = BatchAppsApi(mock_creds, mock_config)
        mock_url.return_value = "https://test_endpoint.com/api/pools"
        mock_get.return_value = None
        val = _api.get_pool()
        self.assertIsInstance(val, Response)
        self.assertFalse(val.success)

        val = _api.get_pool(url="https://pool_url")
        mock_get.assert_called_with(mock_creds, "https://pool_url",
                                    self.headers)
        self.assertTrue(val.success)

        mock_url.return_value = "https://test_endpoint.com/api/{poolid}"
        val = _api.get_pool(pool_id="abcdef")
        mock_get.assert_called_with(mock_creds,
                                    "https://test_endpoint.com/api/abcdef",
                                    self.headers)
        self.assertTrue(val.success)
        self.assertTrue(mock_url.called)

        mock_get.side_effect = RestCallException(None, "Boom~", None)
        val = _api.get_pool(pool_id="abcdef")
        self.assertFalse(val.success)
    def test_poolmgr_get_pools(self, mock_pool, mock_api, mock_creds,
                               mock_cfg):
        """Test get_pools"""

        mgr = PoolManager(mock_creds, cfg=mock_cfg)

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "test", None)
        mgr._client.list_pools.return_value = resp

        with self.assertRaises(RestCallException):
            mgr.get_pools()
        mgr._client.list_pools.assert_called_with()

        resp.success = True
        resp.result = {'pools': []}
        pools = mgr.get_pools()
        mgr._client.list_pools.assert_called_with()
        self.assertEqual(pools, [])
        self.assertEqual(len(mgr), 0)

        resp.result = {
            'totalCount': 1,
            'pools': [{
                'id': 'abc',
                'targetDedicated': '1',
                'state': 'active'
            }]
        }

        pools = mgr.get_pools()
        mock_pool.assert_called_with(mgr._client, **resp.result['pools'][0])
        self.assertEqual(len(pools), 1)
def _call(auth, *args, **kwargs):
    """Internal method to open Requests session."""

    try:
        conn_session = auth.get_session()
        conn_adptr = requests.adapters.HTTPAdapter(max_retries=RETRIES)
        conn_session.mount('https://', conn_adptr)

        LOG.info("About to make REST call with args {0}".format(args))
        LOG.debug("About to make REST call with kwargs {0}".format(kwargs))
        LOG.debug(
            "Opened requests session with max retries: {0}".format(RETRIES))

        response = conn_session.request(*args, **kwargs)
        return _check_code(response)

    except (oauth2.rfc6749.errors.InvalidGrantError,
            oauth2.rfc6749.errors.TokenExpiredError) as exp:

        LOG.info("Token expired. Attempting to refresh and continue.")
        refreshed_session = auth.refresh_session()
        if not refreshed_session:
            raise SessionExpiredException("Please log in again. "
                                          "{0}".format(str(exp)))

        try:
            conn_adptr = requests.adapters.HTTPAdapter(max_retries=RETRIES)
            refreshed_session.mount('https://', conn_adptr)
            response = refreshed_session.request(*args, **kwargs)
            return _check_code(response)

        except Exception as exp:
            raise RestCallException(
                type(exp),
                "An {type} error occurred: {error}".format(type=type(exp),
                                                           error=str(exp)),
                exp)

    except (requests.RequestException,
            oauth2.rfc6749.errors.OAuth2Error) as exp:

        raise RestCallException(
            type(exp),
            "An {type} error occurred: {error}".format(type=type(exp),
                                                       error=str(exp)), exp)
    def test_userfile_download(self, mock_size, mock_is_uploaded):
        """Test download"""

        _callback = mock.Mock()
        mock_size.return_value = 0
        api = mock.create_autospec(batchapps.api.BatchAppsApi)
        ufile = UserFile(api, {})
        download_dir = "test"

        mock_is_uploaded.side_effect = RestCallException(None, "Boom", None)
        with self.assertRaises(RestCallException):
            ufile.download(download_dir)
        
        mock_is_uploaded.side_effect = None
        mock_is_uploaded.return_value = None
        ufile.download(download_dir)
        self.assertFalse(api.props_file.called)

        ufile._exists = True
        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "Boom", None)
        api.props_file.return_value = resp
        mock_is_uploaded.return_value = ufile
        with self.assertRaises(RestCallException):
            ufile.download(download_dir)
            self.assertTrue(api.props_file.called)

        resp.success = True
        resp.result = 123
        api.props_file.return_value = resp
        r = mock.create_autospec(Response)
        r.success = False
        r.result = RestCallException(None, "Boom", None)
        api.get_file.return_value = r
        with self.assertRaises(RestCallException):
            ufile.download(download_dir)
        api.get_file.assert_called_with(ufile, resp.result, download_dir, callback=None, block=4096)
        
        r.success = True
        r.result = "test"
        ufile.download(download_dir, callback=_callback, block=1)
        api.get_file.assert_called_with(ufile, resp.result, download_dir, callback=_callback, block=1)
Example #21
0
    def test_track_job_progress(self, mock_check_job, mock_track_tasks):
        """Test _track_job_progress"""

        job_mgr = mock.create_autospec(JobManager)
        sub = {"jobId": "test", "url": "url_test"}

        with self.assertRaises(RuntimeError):
            client.track_job_progress(job_mgr, [])

        job_mgr.get_job.side_effect = RestCallException(
            None, "GetJobFailed", None)

        with self.assertRaises(RuntimeError):
            client.track_job_progress(job_mgr, sub)
        job_mgr.get_job.assert_called_with(jobid="test")

        job = mock.create_autospec(SubmittedJob)
        job_mgr.get_job.side_effect = None
        job_mgr.get_job.return_value = None
        with self.assertRaises(RuntimeError):
            client.track_job_progress(job_mgr, sub)

        client.TIMEOUT = "test"
        with self.assertRaises(RuntimeError):
            client.track_job_progress(job_mgr, sub)

        job_mgr.get_job.return_value = job
        job.status = "test"
        client.TIMEOUT = 3600
        with self.assertRaises(RuntimeError):
            client.track_job_progress(job_mgr, sub)

        job.name = "MyJob"
        job.status = "NotStarted"
        job.update.return_value = True
        job.percentage = 10

        mock_check_job.return_value = True
        client.track_job_progress(job_mgr, sub)
        self.assertTrue(mock_check_job.called_with(job))

        job.status = "Complete"
        client.track_job_progress(job_mgr, sub)

        job.status = "NotStarted"
        client.TIMEOUT = 20
        mock_check_job.reset_mock()
        mock_check_job.return_value = False
        with self.assertRaises(RuntimeError):
            client.track_job_progress(job_mgr, sub)

        self.assertTrue(mock_check_job.called_with(job))
        self.assertEqual(mock_check_job.call_count, 2)
        self.assertTrue(mock_track_tasks.called_with(job))
    def test_filecoll_is_uploaded(self,
                                  mock_rem,
                                  mock_mess,
                                  mock_ufile,
                                  mock_api):
        """Test is_uploaded"""

        def user_file_gen(u_name):
            """Mock UserFile generator"""
            ugen = mock.create_autospec(UserFile)
            ugen.name = str(u_name)
            ugen.compare_lastmodified.return_value = True
            return ugen

        def add(col, itm):
            """Mock add UserFile to collection"""
            col._collection.append(itm)

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "Boom", None)
        mock_ufile.return_value = user_file_gen("1")
        FileCollection.add = add
        mock_api.query_files.return_value = resp
        mock_mess.return_value = ["1", "2", "3", "4", "5"]

        col = FileCollection(mock_api)
        upl = col.is_uploaded()
        self.assertIsInstance(upl, FileCollection)
        self.assertEqual(upl._collection, col._collection)
        self.assertFalse(mock_api.query_files.called)

        col._collection = [1, 2, 3, 4, 5]
        with self.assertRaises(RestCallException):
            col.is_uploaded()
        mock_api.query_files.assert_called_once_with(["1", "2", "3", "4", "5"])

        with self.assertRaises(RestCallException):
            col.is_uploaded(per_call=2)
        mock_api.query_files.assert_called_with(["1", "2"])

        col._collection = [user_file_gen("1"), user_file_gen("2")]
        mock_api.reset()
        resp.success = True
        resp.result = ["test1", "test2", "test3"]
        upl = col.is_uploaded()
        mock_api.query_files.assert_called_with(["1", "2", "3", "4", "5"])
        mock_rem.assert_called_with([mock.ANY])
        self.assertEqual(upl._collection, col._collection)

        col._collection = [user_file_gen("test1"), user_file_gen("test2")]
        upl = col.is_uploaded()
        mock_rem.assert_called_with([])
        self.assertEqual(upl._collection, col._collection)
    def test_api_query_files(self, mock_url, mock_creds, mock_config,
                             mock_post):
        """Test query_files"""

        _api = BatchAppsApi(mock_creds, mock_config)
        mock_url.return_value = "http://test.com/{queryby}"
        mock_post.return_value = {}

        val = _api.query_files(0)
        self.assertFalse(val.success)
        self.assertFalse(mock_post.called)

        val = _api.query_files([0])
        self.assertFalse(val.success)
        self.assertFalse(mock_post.called)

        val = _api.query_files([])
        self.assertFalse(val.success)
        self.assertFalse(mock_post.called)

        val = _api.query_files({})
        mock_post.assert_called_with(mock_creds,
                                     "http://test.com/byspecification",
                                     self.headers, {"Specifications": [{}]})
        self.assertFalse(val.success)

        val = _api.query_files([{}])
        mock_post.assert_called_with(mock_creds,
                                     "http://test.com/byspecification",
                                     self.headers, {"Specifications": [{}]})
        self.assertFalse(val.success)

        val = _api.query_files("")
        mock_post.assert_called_with(mock_creds, "http://test.com/byname",
                                     self.headers, {"Names": [""]})
        self.assertFalse(val.success)

        val = _api.query_files([""])
        mock_post.assert_called_with(mock_creds, "http://test.com/byname",
                                     self.headers, {"Names": [""]})
        self.assertFalse(val.success)

        mock_post.return_value = {'files': None}
        val = _api.query_files([""])
        self.assertFalse(val.success)

        mock_post.return_value = {'files': []}
        val = _api.query_files([""])
        self.assertTrue(val.success)
        self.assertEqual(val.result, [])

        mock_post.side_effect = RestCallException(None, "test", None)
        val = _api.query_files([""])
        self.assertFalse(val.success)
    def test_api_list_outputs(self, mock_url, mock_creds, mock_config,
                              mock_get):
        """Test list_outputs"""

        _api = BatchAppsApi(mock_creds, mock_config)
        mock_url.return_value = "https://test_endpoint.com/{jobid}"
        mock_get.return_value = {}

        val = _api.list_outputs("test_id")
        mock_get.assert_called_with(mock_creds,
                                    "https://test_endpoint.com/test_id",
                                    self.headers)
        self.assertFalse(val.success)

        mock_get.return_value = {'jobOutputs': None}
        val = _api.list_outputs("test_id")
        mock_get.assert_called_with(mock_creds,
                                    "https://test_endpoint.com/test_id",
                                    self.headers)
        self.assertFalse(val.success)

        mock_get.return_value = {'jobOutputs': []}
        val = _api.list_outputs("test_id")
        self.assertTrue(val.success)
        self.assertEqual(val.result, [])

        mock_get.return_value = {
            'jobOutputs': [{
                'name': 'output.zip',
                'link': {
                    'href': 'http://url'
                },
                'kind': 'output'
            }]
        }
        val = _api.list_outputs("test_id")
        self.assertTrue(val.success)
        self.assertEqual(val.result, [{
            'name': 'output.zip',
            'link': 'http://url',
            'type': 'output'
        }])
        mock_get.return_value = {'jobOutputs': [{'name': 'output.zip'}]}
        val = _api.list_outputs("test_id")
        self.assertTrue(val.success)
        self.assertEqual(val.result, [{
            'name': 'output.zip',
            'link': None,
            'type': None
        }])

        mock_get.side_effect = RestCallException(None, "Boom~", None)
        val = _api.list_outputs("test_id")
        self.assertFalse(val.success)
def post(auth, url, headers, message=None):
    """
    Call to POST data to the Batch Apps service.
    Used for job submission, job commands and file queries.

    :Args:
        - auth (:class:`.Credentials`): The session credentials object.
        - url (str): The complete endpoint URL.
        - headers (dict): The headers to be used in the request.

    :Kwargs:
        - message (dict): Data to be acted on e.g. job submission
          specification, file query parameters. The format and contents will
          depend on the specific API call.

    :Returns:
        - The data in the service response, after json decoding.

    :Raises:
        - :exc:`.RestCallException` is the call failed, or returned a
          non-200 status.
    """
    try:
        if message:
            message = json.dumps(message)

        LOG.debug("POST call URL: {0}, message: {1}".format(url, message))

        response = _call(auth, 'POST', url, headers=headers, data=message)
        return json.loads(response.text)

    except RestCallException:
        raise

    except (ValueError, TypeError) as exp:
        raise RestCallException(
            type(exp), "No json object to be decoded from POST call.", exp)

    except AttributeError as exp:
        raise RestCallException(AttributeError,
                                "Response object has no text attribute.", exp)
Example #26
0
    def test_submittedjob_reprocess(self):
        """Test reprocess"""

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "test", None)
        api = mock.create_autospec(BatchAppsApi)
        api.reprocess.return_value = resp

        job = SubmittedJob(api, "abc", None, None)
        working = job.reprocess()
        api.reprocess.assert_called_with("abc")
        self.assertFalse(working)

        resp.result = RestCallException(TypeError, "Boom!", None)
        with self.assertRaises(RestCallException):
            job.reprocess()

        resp.success = True
        working = job.reprocess()
        self.assertTrue(working)
Example #27
0
    def test_submittedjob_cancel(self):
        """Test cancel"""

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "test", None)
        api = mock.create_autospec(BatchAppsApi)
        api.cancel.return_value = resp

        job = SubmittedJob(api, "abc", None, None)
        cancelled = job.cancel()
        api.cancel.assert_called_with("abc")
        self.assertFalse(cancelled)

        resp.result = RestCallException(TypeError, "Boom!", None)
        with self.assertRaises(RestCallException):
            job.cancel()

        resp.success = True
        cancelled = job.cancel()
        self.assertTrue(cancelled)
Example #28
0
    def test_task_cancel(self):
        """Test cancel_task"""

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "test", None)
        api = mock.create_autospec(BatchAppsApi)
        api.cancel_task.return_value = resp

        task = Task(api, "abc")
        cancelled = task.cancel()
        api.cancel_task.assert_called_with("abc", 0)
        self.assertFalse(cancelled)

        resp.result = RestCallException(TypeError, "Boom!", None)
        with self.assertRaises(RestCallException):
            task.cancel()

        resp.success = True
        cancelled = task.cancel()
        self.assertTrue(cancelled)
def head(auth, url, headers, filename=""):
    """
    Call HEAD.
    This call is only used to retrieve the content-length response
    header to get a file size.

    :Args:
        - auth (:class:`.Credentials`): The session credentials object.
        - url (str): The complete endpoint URL.
        - headers (dict): The headers to be used in the request.

    :Kwargs:
        - filename (str): Used to add a filename to the end of the URL if
          doesn't already have one. The default is an empty string.

    :Returns:
        - The content-length header, as an integer.

    :Raises:
        - :exc:`.RestCallException` if the call failed, returned a non200 status,
          or the content-length header was not present in the response object.
    """
    try:
        url = url.format(name=url_from_filename(filename))
        LOG.debug("HEAD call URL: {0}".format(url))

        response = _call(auth, 'HEAD', url, headers=headers)
        return int(response.headers["content-length"])

    except RestCallException:
        raise

    except KeyError as exp:
        raise RestCallException(KeyError,
                                "No content-length key in response headers.",
                                exp)

    except IndexError as exp:
        raise RestCallException(IndexError,
                                "Incorrectly formatted URL supplied.", exp)
    def test_filecoll_upload_thread(self, mock_pik, mock_api):
        """Test upload"""

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "Boom", None)

        col = FileCollection(mock_api)
        col._api = None
        failed = col.upload(force=True, threads=1)
        self.assertFalse(mock_pik.called)
        self.assertEqual(failed, [])

        col._collection = [1, 2, 3, 4]
        failed = col.upload(force=True, threads=1)
        self.assertEqual(mock_pik.call_count, 1)
        self.assertEqual(failed, 
                         [(1, "'int' object has no attribute 'upload'"),
                          (2, "'int' object has no attribute 'upload'"),
                          (3, "'int' object has no attribute 'upload'"),
                          (4, "'int' object has no attribute 'upload'")])

        mock_pik.call_count = 0
        col._collection = [UFile()]
        failed = col.upload(force=True, threads=1)
        self.assertEqual(mock_pik.call_count, 1)
        self.assertEqual(len(failed), 1)
        self.assertIsInstance(failed[0], tuple)

        mock_pik.call_count = 0
        col._collection = [UFile(arg_a=True)]
        failed = col.upload(force=True, threads=1)
        self.assertEqual(mock_pik.call_count, 1)
        self.assertEqual(failed, [])

        mock_pik.call_count = 0
        col._collection = [UFile(arg_a=True)]
        failed = col.upload(force=True, threads=3)
        self.assertEqual(mock_pik.call_count, 1)
        self.assertEqual(failed, [])

        mock_pik.call_count = 0
        col._collection = [UFile() for a in range(15)]
        failed = col.upload(force=True, threads=3)
        self.assertEqual(mock_pik.call_count, 5)
        self.assertEqual(len(failed), 15)

        mock_pik.call_count = 0
        col._collection = [UFile(arg_a=True) for a in range(20)]
        failed = col.upload(force=True, threads=20)
        self.assertEqual(mock_pik.call_count, 2)
        self.assertEqual(failed, [])