Exemple #1
0
def test_http_errors(monkeypatch):
    resp = MagicMock()
    resp.status_code = 404
    resp.raise_for_status.side_effect = requests.HTTPError("Not Found")
    get = MagicMock()
    get.return_value = resp
    monkeypatch.setattr("requests.get", get)
    http = HttpWrapper("http://example.org")
    # the code method will not raise an exception..
    assert 404 == http.code()
    for meth in ("time", "json", "cookies", "headers"):
        with pytest.raises(HttpError) as ex:
            # ..but other methods will!
            getattr(http, meth)()
        assert "Not Found" == ex.value.message

    get.side_effect = requests.Timeout("timed out")
    http = HttpWrapper("http://example.org")
    with pytest.raises(HttpError) as ex:
        http.time()
    assert "timeout" == ex.value.message

    get.side_effect = requests.ConnectionError("connfail")
    http = HttpWrapper("http://example.org")
    with pytest.raises(HttpError) as ex:
        http.code()
    assert "connection failed" == ex.value.message

    get.side_effect = Exception("foofail")
    http = HttpWrapper("http://example.org")
    with pytest.raises(HttpError) as ex:
        http.code()
    assert "foofail" == ex.value.message
def test_http_errors(monkeypatch):
    resp = MagicMock()
    resp.status_code = 404
    resp.raise_for_status.side_effect = requests.HTTPError('Not Found')
    get = MagicMock()
    get.return_value = resp
    monkeypatch.setattr('requests.get', get)
    http = HttpWrapper('http://example.org')
    # the code method will not raise an exception..
    assert 404 == http.code()
    for meth in ('time', 'json', 'cookies', 'headers'):
        with pytest.raises(HttpError) as ex:
            # ..but other methods will!
            getattr(http, meth)()
        assert 'Not Found' == ex.value.message

    get.side_effect = requests.Timeout('timed out')
    http = HttpWrapper('http://example.org')
    with pytest.raises(HttpError) as ex:
        http.time()
    assert 'timeout' == ex.value.message

    get.side_effect = requests.ConnectionError('connfail')
    http = HttpWrapper('http://example.org')
    with pytest.raises(HttpError) as ex:
        http.code()
    assert 'connection failed' == ex.value.message

    get.side_effect = Exception('foofail')
    http = HttpWrapper('http://example.org')
    with pytest.raises(HttpError) as ex:
        http.code()
    assert 'foofail' == ex.value.message
    def test_check_connection(self):
        """
        Does it only ping the server if the option was set?
        """
        ping = MagicMock()
        dut = MagicMock()
        configuration_mock = MagicMock()
        dut.operating_system = 'linux'
        self.tester._dut = dut
        self.tester._server = MagicMock()
        self.tester._server.TestInterface = 'server'

        # check that it doesn't run
        self.parser.has_section.return_value = False
        #self.tester.configuration.ping = None
        outcome = self.tester.connected()
        with self.assertRaises(AssertionError):
            ping.assert_called_with()
        self.assertTrue(outcome)

        # check that it does run
        self.tester._ping = ping
        mock_time = MagicMock()
        times = [500,4,3, 2, 1]
        # pinged
        mock_time.side_effect = lambda: times.pop()
        with patch('time.time', mock_time):
            ping.return_value = True
            outcome = self.tester.connected()
            ping.assert_called_with()
            
        times = [700, 600, 500,400 ,300, 40,30, 20, 10]
        def time_effects():
            output =  times.pop()
            return output
        
        # timed out
        mock_time.side_effect = time_effects
        #mock_time_2 = MagicMock()
        self.tester._result_location = 'cache'
        with patch('time.time', mock_time):
            ping.return_value = False
            outcome = self.tester.connected()
            ping.assert_called_with()
            self.assertFalse(outcome)
        
        # timed out
        times = [700, 600, 100, 10]
        
        mock_time.side_effect = lambda: times.pop()
        # raises exception if asked to
        start = 0
        attenuation = 0
        with self.assertRaises(CameraobscuraError):
            with patch('time.time', mock_time):
                ping.return_value = False
                self.tester.connected(raise_error=start==attenuation)
        return
 def test_wait_for_state_boto3_error(self, mock_sleep):
     """Test wait_for_state_boto3 with RuntimeError and returned RuntimeError"""
     mock_describe_func = MagicMock()
     mock_describe_func.side_effect = RuntimeError
     self.assertRaises(RuntimeError, wait_for_state_boto3, mock_describe_func, {"param1": "p1"},
                       "myresource", 'available', state_attr='status', timeout=30)
     self.assertEqual(1, mock_describe_func.call_count)
def mock_connect(connect_error=False, fetch_error=False, no_login=False, no_group=False, results={}):
    conn = MagicMock()
    cursor = MagicMock()
    row = MagicMock()

    row.can_login = False if no_login else True
    row.member_of = [] if no_group else [REQUIRED_GROUP]

    row._asdict.return_value = results

    if not fetch_error:
        cursor.fetchone.return_value = row
        cursor.fetchmany.return_value = [row] * RESULT_COUNT
    else:
        cursor.fetchone.side_effect = Exception()
        cursor.fetchmany.side_effect = Exception()

    conn.cursor = MagicMock()
    conn.cursor.return_value = cursor

    connect = MagicMock()
    if not connect_error:
        connect.return_value = conn
    else:
        connect.side_effect = Exception()

    return connect
def test_process_function_logs_error(mock_loop, mock_resource, mock_log, mock_get_task):
    "test ..."
    # Arrange
    Q = QueueProcessor(run=False)

    fake_task_name = fake.pystr()
    fake_params = fake.pylist()

    fake_message = {
        'task': fake_task_name,
        'parameters': fake_params
    }
    mock_task = MagicMock()
    def raise_exception(*args, **kwargs):
        raise Exception()
    mock_task.side_effect = raise_exception
    mock_get_task.return_value = mock_task

    # Act
    Q.process(fake_message)

    # Assert
    mock_get_task.assert_called_once_with(fake_task_name)
    mock_log.exception.assert_called()
    mock_task.assert_called_once_with(Q, *fake_params)
    def test_get_request_exception_and_retry_four_times(self, Session):
        request_exception = RequestException('I raise RequestException!')
        def exception_raiser(method, url, data, params, headers=None,
                             allow_redirects=None):
            raise request_exception

        Session.return_value = MagicMock(request=exception_raiser)

        ex = HttpExecutor('http://api.stormpath.com/v1', ('user', 'pass'))

        def try_four_times(retries, status):
            return retries <= 3

        ShouldRetry = MagicMock()
        ShouldRetry.side_effect = try_four_times

        with patch('stormpath.http.HttpExecutor.should_retry', ShouldRetry):
            with self.assertRaises(Error):
                ex.get('/test')

        should_retry_calls = [
            call(0, request_exception), call(1, request_exception),
            call(2, request_exception), call(3, request_exception),
            call(4, request_exception)
        ]
        ShouldRetry.assert_has_calls(should_retry_calls)
    def _extra_context_in_huddle_missed_stream_messages_two_others(self, send_as_user: bool,
                                                                   mock_random_token: MagicMock,
                                                                   show_message_content: bool=True) -> None:
        tokens = self._get_tokens()
        mock_random_token.side_effect = tokens

        msg_id = self.send_huddle_message(
            self.example_email('othello'),
            [
                self.example_email('hamlet'),
                self.example_email('iago'),
            ],
            'Group personal message!',
        )

        if show_message_content:
            body = ('You and Iago, Othello, the Moor of Venice Othello,'
                    ' the Moor of Venice Group personal message')
            email_subject = 'Group PMs with Iago and Othello, the Moor of Venice'
            verify_body_does_not_include = []  # type: List[str]
        else:
            body = 'While you were away you received 1 new group private message!'
            email_subject = 'New missed message'
            verify_body_does_not_include = ['Iago', 'Othello, the Moor of Venice Othello, the Moor of Venice',
                                            'Group personal message!', 'mentioned',
                                            'Or just reply to this email.']
        self._test_cases(tokens, msg_id, body, email_subject, send_as_user,
                         show_message_content=show_message_content,
                         verify_body_does_not_include=verify_body_does_not_include)
 def test_throttled_call_clienterror_timeout(self, mock_sleep):
     """Test throttle_call using ClientError and timeout"""
     mock_func = MagicMock()
     error_response = {"Error": {"Code": "Throttling"}}
     client_error = ClientError(error_response, "test")
     mock_func.side_effect = client_error
     self.assertRaises(ClientError, throttled_call, mock_func)
    def _deleted_message_in_huddle_missed_stream_messages(self, send_as_user: bool,
                                                          mock_random_token: MagicMock) -> None:
        tokens = self._get_tokens()
        mock_random_token.side_effect = tokens

        msg_id = self.send_huddle_message(
            self.example_email('othello'),
            [
                self.example_email('hamlet'),
                self.example_email('iago'),
            ],
            'Group personal message!',
        )

        hamlet = self.example_user('hamlet')
        iago = self.example_user('iago')
        email = self.example_email('othello')
        self.login(email)
        result = self.client_patch('/json/messages/' + str(msg_id),
                                   {'message_id': msg_id, 'content': ' '})
        self.assert_json_success(result)
        handle_missedmessage_emails(hamlet.id, [{'message_id': msg_id}])
        self.assertEqual(len(mail.outbox), 0)
        handle_missedmessage_emails(iago.id, [{'message_id': msg_id}])
        self.assertEqual(len(mail.outbox), 0)
    def test_call(self):
        """
        Does the call try to ping the device correctly?
        """
        time_mock = MagicMock()
        logger = Mock()
        self.ping._logger = logger

        # case 1 : success
        test_line = '64 bytes from pc-in-f147.1e100.net (74.125.28.147): icmp_req=1 ttl=44 time=13.7 ms'
        self.assertIsNotNone(self.ping.command.data_expression.search(test_line))
        self.connection.exec_command.return_value = None, [test_line], StringIO('')
        
        time_mock.return_value = 0
        with patch('time.time', time_mock):
            self.assertTrue(self.ping())

        ## case 2: failure
        ## this fails sometimes -- the actual path needs to be worked out
        self.connection.exec_command.return_value = None, '', ''
        times = [self.timeout] * 2 + [0] * (self.threshold + 1)
        time_mock.side_effect = lambda: times.pop()
        with patch('time.time', time_mock):
            self.assertFalse(self.ping())

        # case 3: connection problem
        self.connection.exec_command.side_effect = socket.timeout("socket timed out-- catch it")
        with self.assertRaises(CameraobscuraError):
            self.ping()
        return
    def test_mangle_prompt(self):
        """
        Does it mangle the prompt if not given?
        """
        # default for prompt is None
        self.assertIsNone(self.client._prompt)

        # if set, use the given
        prompt = random_string_of_letters()
        client = TelnetClient(hostname=None, prompt=prompt)
        self.assertEqual(prompt, client.prompt)

        # if not given, set a random prompt       
        randrange_mock = MagicMock()
        randrange_mock.return_value = len(prompt)

        choice_mock = MagicMock()
        prompt_source = list(prompt[:])

        def side_effect(*args): return prompt_source.pop(0)
        choice_mock.side_effect = side_effect
        
        with patch('random.randrange', randrange_mock):
                with patch('random.choice', choice_mock):
                        self.assertEqual(self.client.prompt, prompt)
        return
    def test_extra_kwargs(self):

        # create and connect a pre and post fake receiver to the desired signal
        pre_receiver = MagicMock(return_value=None)
        post_receiver = MagicMock(return_value=None)

        # set the return value of the pre_receiver to the uuid value
        # so we can use it in later test.
        def side_effect(*args, **kwargs):
            pre_receiver.return_value = kwargs['uuid']
            return kwargs

        pre_receiver.side_effect = side_effect
        pre_transition_event.connect(
            pre_receiver, dispatch_uid='blah kwargs pre'
        )
        post_transition_event.connect(
            post_receiver, dispatch_uid='blah kwargs post'
        )

        # call the transition that will trigger the signal
        self.new_instance.test_some_event()

        # ensure the receiver was only called once with the correct args
        post_receiver.assert_called_once_with(
            instance=self.new_instance, from_state=FakeStates.NEW,
            to_state=FakeStates.WHATEVER, state='fake_state',
            transition_name='test_some_event', signal=post_transition_event,
            sender=self.new_instance.__class__, uuid=pre_receiver.return_value,
            some_event_name="Yes An Event"
        )

        # cleanup
        pre_transition_event.disconnect(pre_receiver)
        post_transition_event.disconnect(post_receiver)
    def test_stream_mentions_multiple_people(self, mock_random_token: MagicMock) -> None:
        """A mention should take precedence over regular stream messages for email subjects.

        Each sender who has mentioned a user should appear in the email subject line.
        """
        tokens = self._get_tokens()
        mock_random_token.side_effect = tokens

        hamlet = self.example_user('hamlet')
        msg_id_1 = self.send_stream_message(self.example_email('iago'),
                                            "Denmark",
                                            '@**King Hamlet**')
        msg_id_2 = self.send_stream_message(self.example_email('othello'),
                                            "Denmark",
                                            '@**King Hamlet**')
        msg_id_3 = self.send_stream_message(self.example_email('cordelia'),
                                            "Denmark",
                                            'Regular message')

        handle_missedmessage_emails(hamlet.id, [
            {'message_id': msg_id_1, "trigger": "mentioned"},
            {'message_id': msg_id_2, "trigger": "mentioned"},
            {'message_id': msg_id_3, "trigger": "stream_email_notify"},
        ])
        self.assertEqual(len(mail.outbox), 1)
        email_subject = 'Iago, Othello, the Moor of Venice mentioned you'
        self.assertEqual(mail.outbox[0].subject, email_subject)
Exemple #15
0
    def test_retry_decorator(self, m_logger, m_config):
        """
        Raise AutoReconnect once (simulate no connection to db), hijack the logger to fix the mock
        so it is not an infinite loop.
        """
        mock_r = MagicMock()
        mock_r.side_effect = AutoReconnect

        def restart_mongo(*args):
            """
            Simulate turning Mongo back on.
            """
            mock_r.side_effect = None
            mock_r.return_value = 'final'

        @connection.UnsafeRetry.retry_decorator(full_name='mock_coll')
        def mock_func():
            """
            Simple function to decorate.
            """
            return mock_r()

        m_config.getboolean.return_value = True
        m_logger.error.side_effect = restart_mongo

        final_answer = mock_func()
        m_logger.error.assert_called_once_with('mock_func operation failed on mock_coll')
        self.assertTrue(final_answer is 'final')
    def _extra_context_in_missed_stream_messages_mention(self, send_as_user: bool,
                                                         mock_random_token: MagicMock,
                                                         show_message_content: bool=True) -> None:
        tokens = self._get_tokens()
        mock_random_token.side_effect = tokens

        for i in range(0, 11):
            self.send_stream_message(self.example_email('othello'), "Denmark", content=str(i))
        self.send_stream_message(
            self.example_email('othello'), "Denmark",
            '11', topic_name='test2')
        msg_id = self.send_stream_message(
            self.example_email('othello'), "denmark",
            '@**King Hamlet**')

        if show_message_content:
            body = 'Denmark > test Othello, the Moor of Venice 1 2 3 4 5 6 7 8 9 10 @**King Hamlet**'
            email_subject = 'Othello, the Moor of Venice mentioned you'
            verify_body_does_not_include = []  # type: List[str]
        else:
            # Test in case if message content in missed email message are disabled.
            body = 'While you were away you received 1 new message in which you were mentioned!'
            email_subject = 'New missed message'
            verify_body_does_not_include = ['Denmark > test', 'Othello, the Moor of Venice',
                                            '1 2 3 4 5 6 7 8 9 10 @**King Hamlet**', 'private', 'group',
                                            'Or just reply to this email.']
        self._test_cases(tokens, msg_id, body, email_subject, send_as_user,
                         show_message_content=show_message_content,
                         verify_body_does_not_include=verify_body_does_not_include,
                         trigger='mentioned')
def get_boto_client(monkeypatch, *args):
    client = MagicMock()
    client.side_effect = args

    monkeypatch.setattr('boto3.client', client)

    return client
Exemple #18
0
    def mock_controller(self, plugin_label, method, return_value = None,
            side_effect = None, mock = None):
        """
        Mocks controller by label. Can only be used to test controllers
        that get instantiated automatically by cement.
        @param plugin_label: e.g. 'drupal'
        @param method: e.g. 'enumerate_plugins'
        @param return_value: what to return. Default is None, unless the
            method starts with enumerate_*, in which case the result is a
            tuple as expected by BasePlugin.
        @param mock: the MagicMock to place. If None, a blank MagicMock is
            created.
        @param side_effect: if set to an exception, it will raise an
            exception.
        """
        if mock:
            m = mock
        else:
            m = MagicMock()

        if return_value != None:
            m.return_value = return_value
        else:
            if method.startswith("enumerate_"):
                m.return_value = ({"a":[]}, True)

        if side_effect:
            m.side_effect = side_effect

        setattr(self.controller_get(plugin_label), method, m)
        return m
 def test_wait_for_state_boto3_clienterror(self, mock_sleep):
     """Test wait_for_state_boto3 with ClientError and returned Timeout"""
     mock_describe_func = MagicMock()
     error_response = {"Error": {"Code": "MyError"}}
     mock_describe_func.side_effect = ClientError(error_response, "test")
     self.assertRaises(TimeoutError, wait_for_state_boto3, mock_describe_func, {"param1": "p1"},
                       "myresource", 'available', state_attr='status', timeout=30)
    def test_export_full_json(self):
        mock_warc_iter_cls = MagicMock()
        mock_warc_iter = MagicMock()
        mock_warc_iter_cls.side_effect = [mock_warc_iter]
        mock_warc_iter.iter.return_value = [
            IterItem(None, None, None, None, {"key1": "k1v1", "key2": "k2v1", "key3": "k3v1"}),
            IterItem(None, None, None, None, {"key1": "k1v2", "key2": "k2v2", "key3": "k3v2"}),
        ]

        export_filepath = os.path.join(self.export_path, "test")
        now = datetime_now()
        limit_uids = [11, 14]

        exporter = BaseExporter(
            None, mock_warc_iter_cls, None, self.working_path, warc_base_path=self.warc_base_path, host="testhost"
        )

        exporter._full_json_export(self.warcs, export_filepath, True, now, None, limit_uids, None)

        mock_warc_iter_cls.assert_called_once_with(self.warcs, limit_uids)
        mock_warc_iter.iter.assert_called_once_with(
            dedupe=True, item_date_start=now, item_date_end=None, limit_item_types=None
        )

        file_path = export_filepath + "_001.json"
        self.assertTrue(os.path.exists(file_path))
        with open(file_path, "r") as f:
            lines = f.readlines()
        self.assertEqual(2, len(lines))
        self.assertDictEqual({"key1": "k1v1", "key2": "k2v1", "key3": "k3v1"}, json.loads(lines[0]))
 def test_throttled_call_timeout(self, mock_sleep):
     """Test throttle_call with timeout"""
     mock_func = MagicMock()
     boto_server_error = BotoServerError('error', None)
     boto_server_error.error_code = "Throttling"
     mock_func.side_effect = boto_server_error
     self.assertRaises(BotoServerError, throttled_call, mock_func)
        def test_results_set(self, mock_algorithms, model):
            # Mock out algorithm return value.
            first_algo = MagicMock()
            first_algo.algorithm.return_value = sentinel.results
            first_algo.name = sentinel.algo_name
            mock_algorithms.registry = [first_algo]

            # Mock out timing.
            class DefaultTimer(object):
                RETVALS = [24, 35]

                def __init__(self):
                    self.times_called = 0

                def __call__(self):
                    retval = self.RETVALS[self.times_called]
                    self.times_called += 1
                    return retval

            mock_default_timer = MagicMock()
            mock_default_timer.side_effect = DefaultTimer()

            with patch('timeit.default_timer', mock_default_timer):
                model.compute_islands(sentinel.seq_record,
                                      sentinel.island_size,
                                      sentinel.min_gc_ratio,
                                      sentinel.min_obs_exp_cpg_ratio,
                                      0)
            assert mock_default_timer.mock_calls == [call() for _ in xrange(2)]
            assert (model.results_model.mock_calls ==
                    [call.set_results(
                        sentinel.results, sentinel.algo_name, 11)])
 def test_throttled_call_noerr(self, mock_sleep):
     """Test throttle_call with no error"""
     mock_func = MagicMock()
     boto_server_error = BotoServerError('error', None)
     boto_server_error.error_code = "Throttling"
     mock_func.side_effect = [boto_server_error, boto_server_error, True]
     throttled_call(mock_func)
     self.assertEqual(3, mock_func.call_count)
Exemple #24
0
 def test_get_lan_ip_should_return_localhost(self):
     io_error = MagicMock()
     io_error.side_effect = IOError
     with patch.object(sys_assist, 'get_interface_ip',  io_error), \
         patch.object(sys_assist.socket, 'gethostbyname',
                      return_value='127.0.0.1'):
         ip = sys_assist.get_lan_ip()
         self.assertEqual(ip, '127.0.0.1')
def make_alternating_condition():
    """
    Create a function that simulates a standby condition that alternates.

    """
    condition = MagicMock()
    condition.side_effect = cycle([True, False])
    return condition
 def test_throttled_call_error(self, mock_sleep):
     """Test throttle_call with error"""
     mock_func = MagicMock()
     boto_server_error = BotoServerError('error', None)
     boto_server_error.error_code = "MyError"
     mock_func.side_effect = boto_server_error
     self.assertRaises(BotoServerError, throttled_call, mock_func)
     self.assertEqual(1, mock_func.call_count)
 def test_throttled_call_clienterror_noerr(self, mock_sleep):
     """Test throttle_call using ClientError and no error"""
     mock_func = MagicMock()
     error_response = {"Error": {"Code": "Throttling"}}
     client_error = ClientError(error_response, "test")
     mock_func.side_effect = [client_error, client_error, True]
     throttled_call(mock_func)
     self.assertEqual(3, mock_func.call_count)
 def test_throttled_call_clienterror_error(self, mock_sleep):
     """Test throttle_call using ClientError and error"""
     mock_func = MagicMock()
     error_response = {"Error": {"Code": "MyError"}}
     client_error = ClientError(error_response, "test")
     mock_func.side_effect = client_error
     self.assertRaises(ClientError, throttled_call, mock_func)
     self.assertEqual(1, mock_func.call_count)
def requests_mock(resp, failure=None):
    req = MagicMock()

    if failure is not None:
        req.side_effect = failure
    else:
        req.return_value = resp

    return req
Exemple #30
0
 def test_enqueue_batch_does_not_add_redundant_tile_in_flight(self):
     coords = [Coordinate(row=1, column=1, zoom=1),
               Coordinate(row=2, column=2, zoom=2)]
     mock = MagicMock()
     mock.side_effect = [True, False]
     self.mockRedis.sismember = mock
     self.sqs.enqueue_batch(coords)
     self.assertEqual(1, len(self.message_tuples))
     self.assertEqual(self.message_tuples[0][1], "2/2/2")
def test_run_cell_command_exception():
    run_cell_method = MagicMock()
    run_cell_method.side_effect = HttpClientException('meh')
    spark_controller.run_command = run_cell_method

    command = "-s"
    name = "sessions_name"
    line = " ".join([command, name])
    cell = "cell code"

    result = magic.spark(line, cell)

    run_cell_method.assert_called_once_with(Command(cell), name)
    assert result is None
    ipython_display.send_error.assert_called_once_with(
        EXPECTED_ERROR_MSG.format(run_cell_method.side_effect))
Exemple #32
0
def test_get_collection_version_metadata_no_version(api_version, token_type, version, token_ins, monkeypatch):
    api = get_test_galaxy_api('https://galaxy.server.com/api/', api_version, token_ins=token_ins)

    if token_ins:
        mock_token_get = MagicMock()
        mock_token_get.return_value = 'my token'
        monkeypatch.setattr(token_ins, 'get', mock_token_get)

    mock_open = MagicMock()
    mock_open.side_effect = [
        StringIO(to_text(json.dumps({
            'href': 'https://galaxy.server.com/api/{api}/namespace/name/versions/{version}/'.format(api=api_version, version=version),
            'download_url': 'https://downloadme.com',
            'artifact': {
                'sha256': 'ac47b6fac117d7c171812750dacda655b04533cf56b31080b82d1c0db3c9d80f',
            },
            'namespace': {
                'name': 'namespace',
            },
            'collection': {
                'name': 'collection',
            },
            'version': version,
            'metadata': {
                'dependencies': {},
            }
        }))),
    ]
    monkeypatch.setattr(galaxy_api, 'open_url', mock_open)

    actual = api.get_collection_version_metadata('namespace', 'collection', version)

    assert isinstance(actual, CollectionVersionMetadata)
    assert actual.namespace == u'namespace'
    assert actual.name == u'collection'
    assert actual.download_url == u'https://downloadme.com'
    assert actual.artifact_sha256 == u'ac47b6fac117d7c171812750dacda655b04533cf56b31080b82d1c0db3c9d80f'
    assert actual.version == version
    assert actual.dependencies == {}

    assert mock_open.call_count == 1
    assert mock_open.mock_calls[0][1][0] == '%s%s/collections/namespace/collection/versions/%s/' \
        % (api.api_server, api_version, version)

    # v2 calls dont need auth, so no authz header or token_type
    if token_type:
        assert mock_open.mock_calls[0][2]['headers']['Authorization'] == '%s my token' % token_type
    def test_export_full_json(self):
        mock_warc_iter_cls = MagicMock()
        mock_warc_iter = MagicMock()
        mock_warc_iter_cls.side_effect = [mock_warc_iter]
        mock_warc_iter.iter.return_value = [
            IterItem(None, None, None, None, {
                "key1": "k1v1",
                "key2": "k2v1",
                "key3": "k3v1"
            }),
            IterItem(None, None, None, None, {
                "key1": "k1v2",
                "key2": "k2v2",
                "key3": "k3v2"
            })
        ]

        export_filepath = os.path.join(self.export_path, "test")
        now = datetime_now()
        limit_uids = [11, 14]

        exporter = BaseExporter(None,
                                mock_warc_iter_cls,
                                None,
                                self.working_path,
                                warc_base_path=self.warc_base_path,
                                host="testhost")

        exporter._full_json_export(self.warcs, export_filepath, True, now,
                                   None, limit_uids, None)

        mock_warc_iter_cls.assert_called_once_with(self.warcs, limit_uids)
        mock_warc_iter.iter.assert_called_once_with(dedupe=True,
                                                    item_date_start=now,
                                                    item_date_end=None,
                                                    limit_item_types=None)

        file_path = export_filepath + '_001.json'
        self.assertTrue(os.path.exists(file_path))
        with open(file_path, "r") as f:
            lines = f.readlines()
        self.assertEqual(2, len(lines))
        self.assertDictEqual({
            "key1": "k1v1",
            "key2": "k2v1",
            "key3": "k3v1"
        }, json.loads(lines[0]))
Exemple #34
0
def test_paginated_single_page():
    mock_responder = MagicMock()
    mock_responder.side_effect = [
        {
            "NextToken": None,
            "Data": ["a", "b"]
        },
    ]

    @paginated("Data", request_pagination_marker="NextToken", response_pagination_marker="NextToken")
    def retrieve_letters(**kwargs):
        return mock_responder(**kwargs)

    result = retrieve_letters()
    assert result == ["a", "b"]
    assert mock_responder.call_count == 1
    mock_responder.assert_has_calls([call()])
Exemple #35
0
    def _deleted_message_in_personal_missed_stream_messages(self, send_as_user: bool,
                                                            mock_random_token: MagicMock) -> None:
        tokens = self._get_tokens()
        mock_random_token.side_effect = tokens

        msg_id = self.send_personal_message(self.example_email('othello'),
                                            self.example_email('hamlet'),
                                            'Extremely personal message! to be deleted!')

        hamlet = self.example_user('hamlet')
        email = self.example_email('othello')
        self.login(email)
        result = self.client_patch('/json/messages/' + str(msg_id),
                                   {'message_id': msg_id, 'content': ' '})
        self.assert_json_success(result)
        handle_missedmessage_emails(hamlet.id, [{'message_id': msg_id}])
        self.assertEqual(len(mail.outbox), 0)
Exemple #36
0
    def test_device_init(self):
        mock = MagicMock()
        mock.side_effect = [
            device_list_xml,
        ]

        fritz = Fritzhome('10.0.0.1', 'user', 'pass')
        fritz._request = mock
        element = fritz.get_device_element('08761 0000434')
        device = FritzhomeDevice(node=element)

        eq_(device.ain, '08761 0000434')
        eq_(device.fw_version, '03.33')
        assert_true(device.present)
        assert_true(device.has_switch)
        assert_true(device.has_temperature_sensor)
        assert_true(device.has_powermeter)
Exemple #37
0
def test_get_repo_with_github_api_down(
    mocked_get: mock.MagicMock, app_test_client: FlaskClient
):
    """
    Test the behavior of the application if connection with Github fails.

    A 500 response must be returned.

    Args:
        mocked_get (mock.MagicMock): Mocker for the get request. It will always
            throw an exception.
        app_test_client (FlaskClient): Test client for the application
            provided out-of-the-box by the Flask framework.
    """
    mocked_get.side_effect = Exception("Exception from Github")
    response = app_test_client.get("pallets/flask")
    assert_internal_server_response(response)
Exemple #38
0
    def test_device_hkr_fw_03_50(self):
        mock = MagicMock()
        mock.side_effect = [
            device_hkr_fw_03_50_xml,
        ]

        fritz = Fritzhome('10.0.0.1', 'user', 'pass')
        fritz._request = mock
        element = fritz.get_device_element('12345')
        device = FritzhomeDevice(node=element)

        eq_(device.ain, '12345')
        assert_true(device.present)
        eq_(device.device_lock, None)
        eq_(device.lock, None)
        eq_(device.error_code, None)
        eq_(device.battery_low, None)
Exemple #39
0
def test_cache_get_unpack_error():
    unit = get_unit()
    test_cache_get = "testingCacheGet"
    test_value = "testing"

    unit.get.return_value = test_cache_get
    unpack = MagicMock()
    unpack.side_effect = Exception("test")

    @unit.cached(unpack=unpack)
    def test(a):
        return a

    assert test(test_value) == test_value
    unit.get.assert_called_with(KEY)
    unpack.assert_called_with(test_cache_get)
    unit.put.assert_called_with(KEY, test_value, None)
Exemple #40
0
    def _realm_name_in_missed_message_email_subject(self,
                                                    realm_name_in_notifications: bool,
                                                    mock_random_token: MagicMock) -> None:
        tokens = self._get_tokens()
        mock_random_token.side_effect = tokens

        msg_id = self.send_personal_message(
            self.example_email('othello'),
            self.example_email('hamlet'),
            'Extremely personal message!',
        )
        body = 'You and Othello, the Moor of Venice Extremely personal message!'
        email_subject = 'Othello, the Moor of Venice sent you a message'

        if realm_name_in_notifications:
            email_subject = 'Othello, the Moor of Venice sent you a message in Zulip Dev'
        self._test_cases(tokens, msg_id, body, email_subject, False)
Exemple #41
0
def failing_kinesis_client():  # type: ()->MagicMock
    mock = MagicMock()
    paginate_mock = MagicMock()
    paginate_mock.side_effect = [[  # Each line is a response page
        {
            "StreamDescription": {
                "Shards": [{
                    "ShardId": "shard1"
                }]
            }
        }
    ]]
    mock.get_paginator.return_value.paginate = paginate_mock
    mock.get_shard_iterator.return_value = {"ShardIterator": "iterator1"}
    mock.get_records.side_effect = ClientError(error_response={},
                                               operation_name="testing")
    return mock
Exemple #42
0
    def test_cam_wrapper(self):
        model = MagicMock()
        wrapper = imaging.inside_cnns._CAMWrapper((3, 10, 10), model)

        self.assertTrue(wrapper.input_image.requires_grad)
        self.assertTrue(wrapper.input_image.shape == torch.Size([3, 10, 10]))
        wrapper('', 'state')
        self.assertTrue(model.call_count == 1)
        self.assertTrue(model.call_args[0][0].shape == torch.Size([1, 3, 10, 10]))
        self.assertTrue(model.call_args[0][1] == 'state')
        model.reset_mock()

        model.side_effect = lambda x: x
        wrapper('', 'state')
        self.assertTrue(model.call_count == 2)
        self.assertTrue(model.call)
        self.assertTrue(model.call_args[0][0].shape == torch.Size([1, 3, 10, 10]))
    def test_add_available_channel_with_available_base_channel(self):
        """ Test adding an available channel whose parent is available.

        Should add both of them."""

        base_channel = "rhel-i386-es-4"
        channel = "res4-es-i386"
        options = get_options("add channel {0}".format(channel).split())

        self.mgr_sync._fetch_remote_channels = MagicMock(
            return_value=parse_channels(
                read_data_from_fixture("list_channels.data"),
                self.mgr_sync.log))

        stubbed_xmlrpm_call = MagicMock()
        stubbed_xmlrpm_call.side_effect = xmlrpc_sideeffect
        self.mgr_sync._execute_xmlrpc_method = stubbed_xmlrpm_call

        with ConsoleRecorder() as recorder:
            self.assertEqual(0, self.mgr_sync.run(options))

        expected_xmlrpc_calls = [
            call._execute_xmlrpc_method(self.mgr_sync.conn.sync.content,
                                        "addChannels", self.fake_auth_token,
                                        base_channel, ''),
            call._execute_xmlrpc_method(self.mgr_sync.conn.channel.software,
                                        "syncRepo", self.fake_auth_token,
                                        [base_channel]),
            call._execute_xmlrpc_method(self.mgr_sync.conn.sync.content,
                                        "addChannels", self.fake_auth_token,
                                        channel, ''),
            call._execute_xmlrpc_method(self.mgr_sync.conn.channel.software,
                                        "syncRepo", self.fake_auth_token,
                                        [channel])
        ]
        stubbed_xmlrpm_call.assert_has_calls(expected_xmlrpc_calls)

        expected_output = """'res4-es-i386' depends on channel 'rhel-i386-es-4' which has not been added yet
Going to add 'rhel-i386-es-4'
Added 'rhel-i386-es-4' channel
Scheduling reposync for following channels:
- rhel-i386-es-4
Added 'res4-es-i386' channel
Scheduling reposync for following channels:
- res4-es-i386"""
        self.assertEqual(expected_output.split("\n"), recorder.stdout)
Exemple #44
0
def test_add_log_target_no_src(monkeypatch, env, fx_scalyr):
    patch_os(monkeypatch)
    patch_env(monkeypatch, env)

    target = fx_scalyr['target']

    exists = MagicMock()
    exists.side_effect = (True, True, False)
    monkeypatch.setattr('os.path.exists', exists)

    agent = ScalyrAgent(CLUSTER_ID, load_template)

    assert_agent(agent, env)

    agent.add_log_target(target)

    assert agent.logs == []
    def _extra_context_in_huddle_missed_stream_messages_many_others(
            self, send_as_user: bool, mock_random_token: MagicMock) -> None:
        tokens = self._get_tokens()
        mock_random_token.side_effect = tokens

        msg_id = self.send_huddle_message(self.example_email('othello'), [
            self.example_email('hamlet'),
            self.example_email('iago'),
            self.example_email('cordelia'),
            self.example_email('prospero')
        ], 'Group personal message!')

        body = (
            'You and Cordelia Lear, Iago, Othello, the Moor of Venice, Prospero from The Tempest'
            ' Othello, the Moor of Venice Group personal message')
        email_subject = 'Group PMs with Cordelia Lear, Iago, and 2 others'
        self._test_cases(tokens, msg_id, body, email_subject, send_as_user)
Exemple #46
0
def test_cache_put_pack_error():
    unit = get_unit()
    test_value = "testing"
    pack_return_value = "packed"

    unit.get.return_value = None
    pack = MagicMock()
    pack.side_effect = Exception("test")

    @unit.cached(pack=pack)
    def test(a):
        return a

    assert test(test_value) == test_value
    unit.get.assert_called_with(KEY)
    pack.assert_called_with(test_value)
    unit.put.assert_has_calls([])
Exemple #47
0
    def test_verbose_exception(self, _readline: MagicMock, getLogger: MagicMock):
        config = Configuration({
            'verbose': 1
        })
        stderr = MagicMock()
        signal = MagicMock()
        signal.running = MagicMock()
        signal.running.side_effect = [True, False]
        _readline.side_effect = self.raise_exception
        getLogger.return_value = logger = MagicMock()

        _log_output(config, stderr, signal)

        getLogger.assert_called_once_with("ffmpeg")
        logger.info.assert_not_called()
        _readline.assert_called_with(stderr)
        signal.running.assert_called()
Exemple #48
0
 def test_getting_list_of_containers(self):
     mocked_lxc_list_containers = MagicMock()
     mocked_lxc_list_containers.return_value = [
         "nova_api_container-2f22c87b", "stackforce_base_container"
     ]
     mocked_lxc_container = MagicMock()
     get_interfaces = MagicMock()
     get_interfaces.side_effect = [("eth0", "lo"), False]
     mocked_lxc_container.get_interfaces = get_interfaces
     with patch("lxc.list_containers", mocked_lxc_list_containers):
         with patch("lxc.Container", mocked_lxc_container):
             res = dynlxc.list_containers()
             assert res["nova"] == \
                 {'hosts': ['nova_api_container-2f22c87b']}
             assert res["all"] == \
                 ['nova_api_container-2f22c87b',
                  'stackforce_base_container']
Exemple #49
0
def test_get_new_containers_log_targets_not_found_pods(monkeypatch,
                                                       fx_containers_sync):
    containers, pods, _, _, _ = fx_containers_sync

    get_pod = MagicMock()
    get_pod.side_effect = PodNotFound

    monkeypatch.setattr('kube_log_watcher.kube.get_pod', get_pod)
    monkeypatch.setattr('kube_log_watcher.main.CLUSTER_NODE_NAME', 'node-1')

    targets = get_new_containers_log_targets(
        containers,
        CONTAINERS_PATH,
        CLUSTER_ID,
        strict_labels=['application', 'version'])

    assert targets == []
Exemple #50
0
    def test_sender_name_in_missed_message(
            self, mock_random_token: MagicMock) -> None:
        tokens = self._get_tokens()
        mock_random_token.side_effect = tokens

        hamlet = self.example_user('hamlet')
        msg_id_1 = self.send_stream_message(self.example_email('iago'),
                                            "Denmark", '@**King Hamlet**')
        msg_id_2 = self.send_stream_message(self.example_email('iago'),
                                            "Verona", '* 1\n *2')
        msg_id_3 = self.send_personal_message(self.example_email('iago'),
                                              hamlet.email, 'Hello')

        handle_missedmessage_emails(hamlet.id, [
            {
                'message_id': msg_id_1,
                "trigger": "mentioned"
            },
            {
                'message_id': msg_id_2,
                "trigger": "stream_email_notify"
            },
            {
                'message_id': msg_id_3
            },
        ])

        self.assertIn('Iago: @**King Hamlet**\n\n--\nYou are',
                      mail.outbox[0].body)
        # If message content starts with <p> tag the sender name is appended inside the <p> tag.
        self.assertIn('<p><b>Iago</b>: <span class="user-mention"',
                      mail.outbox[0].alternatives[0][0])

        self.assertIn('Iago: * 1\n *2\n\n--\nYou are receiving',
                      mail.outbox[1].body)
        # If message content does not starts with <p> tag sender name is appended before the <p> tag
        self.assertIn(
            '       <b>Iago</b>: <ul>\n<li>1<br/>\n *2</li>\n</ul>\n',
            mail.outbox[1].alternatives[0][0])

        self.assertEqual('Hello\n\n--\n\nReply', mail.outbox[2].body[:16])
        # Sender name is not appended to message for PM missed messages
        self.assertIn(
            '>\n                    \n                        <p>Hello</p>\n',
            mail.outbox[2].alternatives[0][0])
Exemple #51
0
    def _extra_context_in_huddle_missed_stream_messages_three_others(
            self, send_as_user: bool, mock_random_token: MagicMock) -> None:
        tokens = self._get_tokens()
        mock_random_token.side_effect = tokens

        msg_id = self.send_huddle_message(
            self.example_email('othello'),
            [
                self.example_email('hamlet'),
                self.example_email('iago'),
                self.example_email('cordelia'),
            ],
            'Group personal message!',
        )

        body = 'Othello, the Moor of Venice --- Group personal message! Manage email preferences'
        email_subject = 'Group PMs with Cordelia Lear, Iago, and Othello, the Moor of Venice'
        self._test_cases(tokens, msg_id, body, email_subject, send_as_user)
Exemple #52
0
 def test_retry_failure_reduced_set(self):
     sleep = MagicMock()
     self.patch(time, "sleep", sleep)
     method = MagicMock()
     method.side_effect = [
         {"FailedResourcesMap": {"arn:abc": {"ErrorCode": "ThrottlingException"}}},
         {"Result": 32},
     ]
     self.assertEqual(
         universal_retry(method, ["arn:abc", "arn:def"]), {"Result": 32}
     )
     sleep.assert_called_once()
     self.assertTrue(
         method.call_args_list == [
             call(ResourceARNList=["arn:abc", "arn:def"]),
             call(ResourceARNList=["arn:abc"]),
         ]
     )
    def _extra_context_in_missed_stream_messages_mention_two_senders(
            self, send_as_user: bool, mock_random_token: MagicMock) -> None:
        tokens = self._get_tokens()
        mock_random_token.side_effect = tokens

        for i in range(0, 3):
            self.send_stream_message(self.example_email('cordelia'), "Denmark",
                                     str(i))
        msg_id = self.send_stream_message(self.example_email('othello'),
                                          "Denmark", '@**King Hamlet**')
        body = 'Denmark > test Cordelia Lear 0 1 2 Othello, the Moor of Venice @**King Hamlet**'
        email_subject = 'Othello, the Moor of Venice mentioned you'
        self._test_cases(tokens,
                         msg_id,
                         body,
                         email_subject,
                         send_as_user,
                         trigger='mentioned')
Exemple #54
0
def test_get_new_containers_log_targets_failuer(monkeypatch,
                                                fx_containers_sync):
    containers, pods, _, _, _ = fx_containers_sync

    is_pause = MagicMock()
    is_pause.side_effect = Exception

    # Force exception
    monkeypatch.setattr('kube_log_watcher.kube.is_pause_container', is_pause)
    monkeypatch.setattr('kube_log_watcher.main.CLUSTER_NODE_NAME', 'node-1')

    targets = get_new_containers_log_targets(
        containers,
        CONTAINERS_PATH,
        CLUSTER_ID,
        strict_labels=['application', 'version'])

    assert targets == []
Exemple #55
0
def test_tournament_negative():
    from EvoDAG import RootGP
    gp = RootGP(generations=1, tournament_size=4, classifier=False, popsize=4)
    gp.X = X
    y = cl.copy()
    mask = y == 0
    y[mask] = 1
    y[~mask] = -1
    gp.y = y
    randint = np.random.randint
    mock = MagicMock()
    mock.side_effect = list(range(gp.popsize))
    np.random.randint = mock
    gp.create_population()
    j = gp.population.tournament(negative=True)
    index = np.argsort([x.fitness for x in gp.population.population])[0]
    assert j == index
    np.random.randint = randint
    def test_some_fast_work(self):
        work = MagicMock()
        work.side_effect = short_sleep()

        before = datetime.datetime.now()

        for i in range(0, 20):
            self._subject.submit(work)

        while len(work.mock_calls) < 20:
            time.sleep(0.01)

        after = datetime.datetime.now()

        assert_that(work.mock_calls, equal_to([call()] * 20))

        assert_that((after - before).total_seconds(),
                    less_than_or_equal_to(0.2))
Exemple #57
0
    def test_disk_ops_disk_not_found(self):
        """Attaching/detaching a disk that doesn't exist should report error"""
        def _raise_disk_not_found_exception(disk_id):
            raise DiskNotFoundException

        req = VmDisksDetachRequest(vm_id="vm.id", disk_ids=["disk.id"])

        handler = HostHandler(MagicMock())
        get_resource = MagicMock()
        get_resource.side_effect = _raise_disk_not_found_exception
        handler.hypervisor.disk_manager.get_resource = get_resource

        response = handler.attach_disks(req)
        assert_that(response.result,
                    equal_to(VmDiskOpResultCode.DISK_NOT_FOUND))
        response = handler.detach_disks(req)
        assert_that(response.result,
                    equal_to(VmDiskOpResultCode.DISK_NOT_FOUND))
Exemple #58
0
 def setup(self):
     self.mocks = dict()
     self.patchers = dict()
     exists, listdir, isfile, isdir, open = make_fake_fstools(realistic_fs)
     for ppoint, fn in {
          'os.listdir': listdir,
          'os.path.isdir': isdir,
          'teuthology.describe_tests.open': open,
          'os.path.exists': exists,
          'os.listdir': listdir,
          'os.path.isfile': isfile,
      }.items():
         mockobj = MagicMock()
         patcher = patch(ppoint, mockobj)
         mockobj.side_effect = fn
         patcher.start()
         self.mocks[ppoint] = mockobj
         self.patchers[ppoint] = patcher
Exemple #59
0
def test_get_new_containers_log_targets_no_strict_labels(
        monkeypatch, fx_containers_sync):
    containers, pods, result_labels, result_no_labels, _ = fx_containers_sync

    result = result_labels + result_no_labels

    get_pod = MagicMock()
    get_pod.side_effect = [pod_mock(p) for p in pods]

    monkeypatch.setattr('kube_log_watcher.kube.get_pod', get_pod)
    monkeypatch.setattr('kube_log_watcher.main.CLUSTER_NODE_NAME', 'node-1')

    targets = get_new_containers_log_targets(containers, CONTAINERS_PATH,
                                             CLUSTER_ID)

    assert sorted(targets,
                  key=lambda k: k['id']) == sorted(result,
                                                   key=lambda k: k['id'])
Exemple #60
0
    def test_exception_logging(self, mock_logger):
        payment_method = PaymentMethodFactory.create(
            payment_processor=triggered_processor, verified=True)

        TransactionFactory.create(payment_method=payment_method)

        mock_execute = MagicMock()
        mock_execute.side_effect = Exception('This happened.')

        with patch.multiple(TriggeredProcessor,
                            execute_transaction=mock_execute):
            call_command('execute_transactions')
            expected_call = call(
                'Encountered exception while executing transaction with id=%s.',
                1,
                exc_info=True)

            self.assertEqual(expected_call, mock_logger.call_args)