async def test_play_sound_file_stops_if_cancelled(self, example_sound_manager, monkeypatch): """ Test that the `_play_sound_file()` method will call `stop()` on the `pygame.mixer.Sound` instance if cancelled. """ sound_instance_mock = MagicMock() monkeypatch.setattr("src.sound.sound_manager.pygame.mixer.Sound", MagicMock(return_value=sound_instance_mock)) # Waiting for the sound to end (aka. sleeping) will cause a CancelledError via a side effect monkeypatch.setattr("src.sound.sound_manager.asyncio.sleep", CoroutineMock(side_effect=asyncio.CancelledError)) example_sound_manager.volume = 0.5 group = example_sound_manager.groups[0] sound = group.sounds[0] sound.volume = 0.5 assert len( sound.files ) == 1 # make sure it is only one since they are chosen at random sound_file = sound.files[0] sound_file.end_at = None # Test without end_at with pytest.raises(asyncio.CancelledError): await example_sound_manager._play_sound_file(0, 0) expected_calls = [ call.set_volume(example_sound_manager.volume * sound.volume), call.play(), call.get_length(), # to sleep for this length call.stop(), ] assert sound_instance_mock.mock_calls == expected_calls
async def test_observer_discovery(hass, hass_ws_client, venv): """Test that observer can discover a device without raising an exception.""" new_usb = [{"domain": "test1", "vid": "3039"}] mock_comports = [ MagicMock( device=slae_sh_device.device, vid=12345, pid=12345, serial_number=slae_sh_device.serial_number, manufacturer=slae_sh_device.manufacturer, description=slae_sh_device.description, ) ] mock_observer = None async def _mock_monitor_observer_callback(callback): await hass.async_add_executor_job( callback, MagicMock(action="create", device_path="/dev/new") ) def _create_mock_monitor_observer(monitor, callback, name): nonlocal mock_observer hass.async_create_task(_mock_monitor_observer_callback(callback)) mock_observer = MagicMock() return mock_observer with patch("pyudev.Context"), patch( "pyudev.MonitorObserver", new=_create_mock_monitor_observer ), patch("pyudev.Monitor.filter_by"), patch( "homeassistant.components.usb.async_get_usb", return_value=new_usb ), patch( "homeassistant.components.usb.comports", return_value=mock_comports ), patch.object( hass.config_entries.flow, "async_init" ) as mock_config_flow: assert await async_setup_component(hass, "usb", {"usb": {}}) await hass.async_block_till_done() hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) await hass.async_block_till_done() assert len(mock_config_flow.mock_calls) == 1 assert mock_config_flow.mock_calls[0][1][0] == "test1" hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP) await hass.async_block_till_done() assert mock_observer.mock_calls == [call.start(), call.stop()]
def test_awsec2_class(resman): 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 = resman.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 = resman.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 = resman.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: list(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': 'pending'})) 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 = resman.factory(config) resource.connect() # Test retrieving more than one yield from the create method create_generator = resource.create() result_1 = next(create_generator) assert result_1['id'] == 'i-11112777d52482d9c' assert result_1['status'] == 'pending' resource._ec2_instance.state['Name'] = 'running' result_2 = next(create_generator) assert result_2['status'] == 'running' assert_in('EC2 instance i-11112777d52482d9c initialized!', log.lines) assert_in('EC2 instance i-11112777d52482d9c is 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'