Пример #1
0
    def test_start(self):
        self._subject._setup_sockets = MagicMock()
        self._subject._recv_thread = MagicMock()
        self._subject._send_thread = MagicMock()
        self._subject._expire_thread = MagicMock()

        self._subject.start()

        assert_that(
            self._subject._setup_sockets.mock_calls,
            equal_to([
                call()
            ])
        )

        assert_that(
            self._subject._recv_thread.mock_calls,
            equal_to([
                call.start()
            ])
        )

        assert_that(
            self._subject._send_thread.mock_calls,
            equal_to([
                call.start()
            ])
        )

        assert_that(
            self._subject._expire_thread.mock_calls,
            equal_to([
                call.start()
            ])
        )
Пример #2
0
def test_lifecycle(capture, machine, strokes):
    # Start machine.
    machine.start_capture()
    assert capture.mock_calls == [
        call.suppress_keyboard(()),
        call.start(),
    ]
    capture.reset_mock()
    machine.set_suppression(True)
    suppressed_keys = dict(machine.keymap.get_bindings())
    del suppressed_keys['space']
    assert strokes == []
    assert capture.mock_calls == [
        call.suppress_keyboard(suppressed_keys.keys()),
    ]
    # Trigger some strokes.
    capture.reset_mock()
    send_input(capture, '+a +h -a -h space w')
    assert strokes == [
        {'S-', '*'},
        {'T-'},
    ]
    assert capture.mock_calls == []
    # Stop machine.
    del strokes[:]
    machine.stop_capture()
    assert strokes == []
    assert capture.mock_calls == [
        call.suppress_keyboard(()),
        call.cancel(),
    ]
Пример #3
0
def test_lifecycle(capture, machine, strokes):
    # Start machine.
    machine.start_capture()
    assert capture.mock_calls == [
        call.suppress_keyboard(()),
        call.start(),
    ]
    capture.reset_mock()
    machine.set_suppression(True)
    suppressed_keys = dict(machine.keymap.get_bindings())
    del suppressed_keys['space']
    assert strokes == []
    assert capture.mock_calls == [
        call.suppress_keyboard(suppressed_keys.keys()),
    ]
    # Trigger some strokes.
    capture.reset_mock()
    send_input(capture, '+a +h -a -h space w')
    assert strokes == [
        {'S-', '*'},
        {'T-'},
    ]
    assert capture.mock_calls == []
    # Stop machine.
    del strokes[:]
    machine.stop_capture()
    assert strokes == []
    assert capture.mock_calls == [
        call.suppress_keyboard(()),
        call.cancel(),
    ]
Пример #4
0
 def test_reparent(self, m_driver_cls, m_logging, m_socket, m_ppid):
     m_ppid.side_effect = iter([123, 123, 1])
     m_driver = m_driver_cls.return_value
     m_driver.join.return_value = False
     self._import_main()
     self.assertEqual(m_driver.mock_calls,
                      [call.start(),
                       call.join(timeout=1),
                       call.join(timeout=1),
                       call.stop()])
Пример #5
0
 def test_create(self):
     self.m_etcd_watcher.assert_has_calls([
         call.link(self.api._on_worker_died),
         call.start(),
     ])
     self.m_spawn.assert_has_calls([
         call(self.api._periodically_resync),
         call(self.api._periodically_resync).link_exception(
             self.api._on_worker_died)
     ])
Пример #6
0
 def test_create(self):
     self.m_etcd_watcher.assert_has_calls([
         call.link(self.api._on_worker_died),
         call.start(),
     ])
     self.m_spawn.assert_has_calls([
         call(self.api._periodically_resync),
         call(self.api._periodically_resync).link_exception(
             self.api._on_worker_died)
     ])
Пример #7
0
 def test_reparent(self, m_driver_cls, m_logging, m_socket, m_ppid):
     m_ppid.side_effect = iter([123, 123, 1])
     m_driver = m_driver_cls.return_value
     m_driver.join.return_value = False
     self._import_main()
     self.assertEqual(m_driver.mock_calls, [
         call.start(),
         call.join(timeout=1),
         call.join(timeout=1),
         call.stop()
     ])
Пример #8
0
 def test_wait(self):
     """
     Does the decorator call the right methods?
     """
     self.cow.return_value = 'boy'
     output = self.dummy('pie')
     self.cow.assert_called_with('pie')
     self.assertEqual('boy', output)
     calls = [call.wait(self.sleep), call.clear(), call.start()]
     self.assertEqual(calls, self.timer.mock_calls)
     return
Пример #9
0
 def test_start(self):
     """
     Does it clear the event then start the timer?
     """
     self.timer.start()
     # first call - create the event and set
     # second call - clear the call so users will block
     # third call - start the timer
     calls = [call.set(), call.clear(), call.start()]
     self.assertEqual(calls, self.timer.event.mock_calls + self.timer.timer.mock_calls)
     return
Пример #10
0
def assert_stuff_about_keep_alive(slot, mock_timer, exception, logging_called):
    mock_timer.reset()
    slot._repl_cursor.reset()

    with patch('logging.Logger.exception') as mock_log:
        slot._repl_cursor.send_feedback = Mock(side_effect=exception)
        thread = slot._send_keepalive()

    assert mock_log.called == logging_called
    assert call.send_feedback() in slot._repl_cursor.method_calls
    assert mock_timer.called
    assert thread.daemon == True, 'It is a daemon'
    assert call.start() in thread.method_calls, 'It has been started'
Пример #11
0
 def test_mainline(self, m_driver_cls, m_logging, m_socket, m_ppid):
     m_ppid.return_value = 123
     m_driver = m_driver_cls.return_value
     m_driver.join.side_effect = iter([False, True])
     self._import_main()
     self.assertEqual(
         m_driver.mock_calls,
         [call.start(),
          call.join(timeout=1),
          call.join(timeout=1)])
     self.assertEqual(m_logging.mock_calls, [
         call(gevent_in_use=False,
              syslog_executable_name="calico-felix-etcd")
     ])
def test_profile_start_decorator_starts_profiling_for_target_function():
  profiler = Mock()
  profiler.start = Mock()
  profiler.intermediate = Mock()

  @profile_start(profiler)
  def fn():
    profiler.intermediate()

  fn()

  expect(profiler.mock_calls[0]).to.equal(call.start('fn'))
  expect(profiler.mock_calls[1]).to.equal(call.intermediate())
  expect(profiler.mock_calls).to.have.length_of(2)
Пример #13
0
 def test_mainline(self, m_driver_cls, m_logging, m_socket, m_ppid):
     m_ppid.return_value = 123
     m_driver = m_driver_cls.return_value
     m_driver.join.side_effect = iter([
         False,
         True
     ])
     self._import_main()
     self.assertEqual(m_driver.mock_calls,
                      [call.start(),
                       call.join(timeout=1),
                       call.join(timeout=1)])
     self.assertEqual(m_logging.mock_calls,
                      [call(gevent_in_use=False,
                            syslog_executable_name="calico-felix-etcd")])
Пример #14
0
def test_awsec2_class():

    with patch('boto3.resource') as client, \
            patch.object(SSH, 'get_session', return_value='started_session'), \
            swallow_logs(new_level=logging.DEBUG) as log:

        # Test connecting when a resource doesn't exist.
        client.return_value = MagicMock(instances=MagicMock(
            filter=lambda Filters: []))
        config = {
            'name': 'non-existent-instance',
            'type': 'aws-ec2',
            'access_key_id': 'my-aws-access-key-id',
            'secret_access_key': 'my-aws-secret-access-key-id'
        }
        resource = ResourceManager.factory(config)
        resource.connect()
        assert resource.id is None
        assert resource.status is None

        # Test catching exception when multiple resources are found at connection.
        client.return_value = MagicMock(instances=MagicMock(
            filter=lambda Filters: [
                MagicMock(instance_id='i-22221ddf096c22bb0',
                          state={'Name': 'running'}),
                MagicMock(instance_id='i-3333f40de2b9b8967',
                          state={'Name': 'running'})
            ]))
        config = {
            'name': 'duplicate-instance-name',
            'type': 'aws-ec2',
            'access_key_id': 'my-aws-access-key-id',
            'secret_access_key': 'my-aws-secret-access-key-id'
        }
        resource = ResourceManager.factory(config)
        try:
            resource.connect()
        except Exception as e:
            assert e.args[0] == "Multiple container matches found"

        # Test connecting to an existing resource.
        client.return_value = MagicMock(Instance=lambda id: MagicMock(
            instance_id='i-00002777d52482d9c', state={'Name': 'running'}))
        config = {
            'name': 'my-instance-name',
            'id': 'i-00002777d52482d9c',
            'type': 'aws-ec2',
            'access_key_id': 'my-aws-access-key-id',
            'secret_access_key': 'my-aws-secret-access-key-id',
            'key_name': 'my-ssh-key',
            'key_filename': '/home/me/.ssh/id_rsa'
        }
        resource = ResourceManager.factory(config)
        resource.connect()
        assert resource.image == 'ami-c8580bdf'
        assert resource.id == 'i-00002777d52482d9c'
        assert resource.instance_type == 't2.micro'
        assert resource.key_filename == '/home/me/.ssh/id_rsa'
        assert resource.key_name == 'my-ssh-key'
        assert resource.name == 'my-instance-name'
        assert resource.region_name == 'us-east-1'
        assert resource.access_key_id == 'my-aws-access-key-id'
        assert resource.secret_access_key == 'my-aws-secret-access-key-id'
        assert resource.security_group == 'default'
        assert resource.status == 'running'
        assert resource.type == 'aws-ec2'

        # Test creating an existing resource and catch the exception.
        try:
            resource.create()
        except Exception as e:
            assert e.args[
                0] == "Instance 'i-00002777d52482d9c' already exists in AWS subscription"

        # Test creating resource.
        client.return_value = MagicMock(
            instances=MagicMock(filter=lambda Filters: []),
            Instance=lambda id: MagicMock(instance_id='i-11112777d52482d9c',
                                          state={'Name': 'running'}))
        config = {
            'name': 'my-instance-name',
            'type': 'aws-ec2',
            'access_key_id': 'my-aws-access-key-id',
            'secret_access_key': 'my-aws-secret-access-key-id',
            'key_name': 'my-ssh-key',
            'key_filename': '/home/me/.ssh/id_rsa'
        }
        resource = ResourceManager.factory(config)
        resource.connect()
        results = resource.create()
        assert results['id'] == 'i-11112777d52482d9c'
        assert results['status'] == 'running'
        assert_in('EC2 instance i-11112777d52482d9c initialized!', log.lines)
        assert_in('EC2 instance i-11112777d52482d9c to start running!',
                  log.lines)
        assert_in(
            'Waiting for EC2 instance i-11112777d52482d9c to complete initialization...',
            log.lines)
        assert_in('EC2 instance i-11112777d52482d9c initialized!', log.lines)

        # Test stopping resource.
        resource.stop()
        calls = [call.stop()]
        resource._ec2_instance.assert_has_calls(calls, any_order=True)

        # Test starting resource.
        resource.start()
        calls = [call.start()]
        resource._ec2_instance.assert_has_calls(calls, any_order=True)

        # Test deleting resource.
        resource.delete()
        calls = [call.terminate()]
        resource._ec2_instance.assert_has_calls(calls, any_order=True)

        session = resource.get_session()
        assert session == 'started_session'