def test_check_rest_version(self, mock_request):
        mock_request.return_value = self.rest_version_data
        ex_args = [
            self.array, "GET",
            "https://{0}/api/api_version".format(self.target)
        ]
        ex_kwargs = {"reestablish_session": False}
        result = self.array._check_rest_version("1.0")
        assert result == "1.0"
        mock_request.assert_called_once_with(*ex_args, **ex_kwargs)
        mock_request.reset_mock()

        result = self.array._check_rest_version(1.0)
        assert result == "1.0"
        mock_request.assert_called_once_with(*ex_args, **ex_kwargs)
        mock_request.reset_mock()

        self.assert_raises(ValueError, self.array._check_rest_version, "0.1")
        assert mock_request.call_count == 0
        mock_request.reset_mock()

        self.assert_raises(ValueError, self.array._check_rest_version, "1.2")
        mock_request.assert_called_once_with(*ex_args, **ex_kwargs)
        mock_request.reset_mock()

        mock_request.side_effect = PureError("reason")
        self.assert_raises(PureError, self.array._check_rest_version, "1.0")
        mock_request.assert_called_once_with(*ex_args, **ex_kwargs)
    def assert_error_propagates(cls, mocks, func, *args, **kwargs):
        """Assert that errors from mocks propogate to func.

        Fail if exceptions raised by mocks are not seen when calling
        func(*args, **kwargs). Ensure that we are really seeing exceptions
        from the mocks by failing if just running func(*args, **kargs) raises
        an exception itself.
        """
        func(*args, **kwargs)
        for mock_func in mocks:
            mock_func.side_effect = PureError("reason")
            cls.assert_raises(PureError, func, *args, **kwargs)
            mock_func.side_effect = None
    def test_request_450_error(self, mock_request):
        choose_rest_version_call = self.make_call(
            "GET", "https://{0}/api/api_version".format(self.target), "null")
        mock_request.side_effect = iter([
            self.make_response(450),
            self.make_response(200, self.rest_version_data),
            self.make_response(200),
        ])
        expected = [
            self.default_call, choose_rest_version_call,
            self.make_call(
                path=self.path_template.format(self.target, "1.1", self.path))
        ]
        real_result = self.array._request(self.method, self.path, self.data)
        assert self.result == real_result
        assert mock_request.call_args_list == expected
        mock_request.reset_mock()
        self.array._rest_version = self.rest_version

        mock_request.side_effect = iter([
            self.make_response(450),
            self.make_response(200,
                               {"version": ["1.1", self.rest_version, "1.3"]}),
        ])
        expected = [self.default_call, choose_rest_version_call]
        self.assert_raises(PureHTTPError, self.array._request, self.method,
                           self.path, self.data)
        assert mock_request.call_args_list == expected
        mock_request.reset_mock()

        mock_request.side_effect = iter(
            [self.make_response(450),
             PureError("reason")])
        expected = [self.default_call, choose_rest_version_call]
        self.assert_raises(PureError, self.array._request, self.method,
                           self.path, self.data)
        assert mock_request.call_args_list == expected
        mock_request.reset_mock()

        self.array._renegotiate_rest_version = False
        mock_request.return_value = self.make_response(450)
        mock_request.side_effect = None
        expected = [self.default_call]
        self.assert_raises(PureHTTPError, self.array._request, self.method,
                           self.path, self.data)
        assert mock_request.call_args_list == expected
    def test_choose_rest_version(self, mock_request):
        mock_request.return_value = self.rest_version_data
        ex_args = [
            self.array, "GET",
            "https://{0}/api/api_version".format(self.target)
        ]
        ex_kwargs = {"reestablish_session": False}
        result = self.array._choose_rest_version()
        assert result == "1.1"
        mock_request.assert_called_once_with(*ex_args, **ex_kwargs)
        mock_request.reset_mock()

        mock_request.return_value = {"version": ["0.1", "1.3"]}
        self.assert_raises(PureError, self.array._choose_rest_version)
        mock_request.assert_called_once_with(*ex_args, **ex_kwargs)
        mock_request.reset_mock()

        mock_request.side_effect = PureError("reason")
        self.assert_raises(PureError, self.array._choose_rest_version)
        mock_request.assert_called_once_with(*ex_args, **ex_kwargs)