def test_long_running_delete(self):
     # Test polling from azure-asyncoperation header
     response = TestArmPolling.mock_send(
         'DELETE', 202, {'azure-asyncoperation': ASYNC_URL}, body="")
     poll = LROPoller(CLIENT, response, TestArmPolling.mock_outputs,
                      ARMPolling(0))
     poll.wait()
     assert poll.result() is None
     assert poll._polling_method._response.randomFieldFromPollAsyncOpHeader is None
def wait_poller(service_client, operation_config, response):
    def get_long_running_output(response):
        return response

    poller = LROPoller(service_client, ClientRawResponse(None, response),
                       get_long_running_output,
                       ARMPolling(30, **operation_config))
    try:
        poller.wait(timeout=600)
        response = poller.result()
    except Exception as exc:
        raise
    return response
    def test_long_running_negative(self):
        global LOCATION_BODY
        global POLLING_STATUS

        # Test LRO PUT throws for invalid json
        LOCATION_BODY = '{'
        response = TestArmPolling.mock_send('POST', 202,
                                            {'location': LOCATION_URL})
        poll = LROPoller(CLIENT, response, TestArmPolling.mock_outputs,
                         ARMPolling(0))
        with pytest.raises(DeserializationError):
            poll.wait()

        LOCATION_BODY = '{\'"}'
        response = TestArmPolling.mock_send('POST', 202,
                                            {'location': LOCATION_URL})
        poll = LROPoller(CLIENT, response, TestArmPolling.mock_outputs,
                         ARMPolling(0))
        with pytest.raises(DeserializationError):
            poll.wait()

        LOCATION_BODY = '{'
        POLLING_STATUS = 203
        response = TestArmPolling.mock_send('POST', 202,
                                            {'location': LOCATION_URL})
        poll = LROPoller(CLIENT, response, TestArmPolling.mock_outputs,
                         ARMPolling(0))
        with pytest.raises(
                CloudError):  # TODO: Node.js raises on deserialization
            poll.wait()

        LOCATION_BODY = json.dumps({'name': TEST_NAME})
        POLLING_STATUS = 200
    def test_long_running_post(self):

        # Test POST LRO with both Location and Azure-AsyncOperation

        # The initial response contains both Location and Azure-AsyncOperation, a 202 and no Body
        response = TestArmPolling.mock_send(
            'POST', 202, {
                'location': 'http://example.org/location',
                'azure-asyncoperation': 'http://example.org/async_monitor',
            }, '')

        class TestServiceClient(ServiceClient):
            def __init__(self):
                ServiceClient.__init__(self, None,
                                       Configuration("http://example.org"))

            def send(self, request, headers=None, content=None, **config):
                assert request.method == 'GET'

                if request.url == 'http://example.org/location':
                    return TestArmPolling.mock_send(
                        'GET', 200, body={'location_result': True})
                elif request.url == 'http://example.org/async_monitor':
                    return TestArmPolling.mock_send(
                        'GET', 200, body={'status': 'Succeeded'})
                else:
                    pytest.fail("No other query allowed")

        def deserialization_cb(response):
            return response.json()

        # Test 1, LRO options with Location final state
        poll = LROPoller(
            TestServiceClient(), response, deserialization_cb,
            ARMPolling(0, lro_options={"final-state-via": "location"}))
        result = poll.result()
        assert result['location_result'] == True

        # Test 2, LRO options with Azure-AsyncOperation final state
        poll = LROPoller(
            TestServiceClient(), response, deserialization_cb,
            ARMPolling(
                0, lro_options={"final-state-via": "azure-async-operation"}))
        result = poll.result()
        assert result['status'] == 'Succeeded'

        # Test 3, backward compat (no options, means "azure-async-operation")
        poll = LROPoller(TestServiceClient(), response, deserialization_cb,
                         ARMPolling(0))
        result = poll.result()
        assert result['status'] == 'Succeeded'

        # Test 4, location has no body

        class TestServiceClientNoBody(ServiceClient):
            def __init__(self):
                ServiceClient.__init__(self, None,
                                       Configuration("http://example.org"))

            def send(self, request, headers=None, content=None, **config):
                assert request.method == 'GET'

                if request.url == 'http://example.org/location':
                    return TestArmPolling.mock_send('GET', 200, body="")
                elif request.url == 'http://example.org/async_monitor':
                    return TestArmPolling.mock_send(
                        'GET', 200, body={'status': 'Succeeded'})
                else:
                    pytest.fail("No other query allowed")

        poll = LROPoller(
            TestServiceClientNoBody(), response, deserialization_cb,
            ARMPolling(0, lro_options={"final-state-via": "location"}))
        result = poll.result()
        assert result is None

        # Former oooooold tests to refactor one day to something more readble

        # Test throw on non LRO related status code
        response = TestArmPolling.mock_send('POST', 201, {})
        op = LongRunningOperation(response, lambda x: None)
        with pytest.raises(BadStatus):
            op.set_initial_status(response)
        with pytest.raises(CloudError):
            LROPoller(CLIENT, response, TestArmPolling.mock_outputs,
                      ARMPolling(0)).result()

        # Test polling from azure-asyncoperation header
        response = TestArmPolling.mock_send(
            'POST',
            202, {'azure-asyncoperation': ASYNC_URL},
            body={'properties': {
                'provisioningState': 'Succeeded'
            }})
        poll = LROPoller(CLIENT, response, TestArmPolling.mock_outputs,
                         ARMPolling(0))
        poll.wait()
        #self.assertIsNone(poll.result())
        assert poll._polling_method._response.randomFieldFromPollAsyncOpHeader is None

        # Test polling from location header
        response = TestArmPolling.mock_send(
            'POST',
            202, {'location': LOCATION_URL},
            body={'properties': {
                'provisioningState': 'Succeeded'
            }})
        poll = LROPoller(CLIENT, response, TestArmPolling.mock_outputs,
                         ARMPolling(0))
        assert poll.result().name == TEST_NAME
        assert poll._polling_method._response.randomFieldFromPollLocationHeader is None

        # Test fail to poll from azure-asyncoperation header
        response = TestArmPolling.mock_send('POST', 202,
                                            {'azure-asyncoperation': ERROR})
        with pytest.raises(BadEndpointError):
            poll = LROPoller(CLIENT, response, TestArmPolling.mock_outputs,
                             ARMPolling(0)).result()

        # Test fail to poll from location header
        response = TestArmPolling.mock_send('POST', 202, {'location': ERROR})
        with pytest.raises(BadEndpointError):
            poll = LROPoller(CLIENT, response, TestArmPolling.mock_outputs,
                             ARMPolling(0)).result()