def test_modlue_configure_core_configured_not_added(mock_setup_module,
                                                    mock_from_file,
                                                    mock_add_core,
                                                    mock_core_added):
    mock_setup_module.return_value = setup_module_with_params(
        configure_core_device={
            "id": "1",
            "cache_id": "2",
            "cached_volume": "/dev/dummy",
        })
    mock_config = h.CopyableMock()
    mock_config.mock_add_spec(opencas.cas_config)
    mock_config.insert_core.side_effect = (
        opencas.cas_config.AlreadyConfiguredException())
    mock_from_file.return_value = mock_config
    mock_core_added.return_value = False

    with pytest.raises(AnsibleExitJson) as e:
        cas.main()

    e.match("'changed': True")

    mock_config.write.assert_not_called()

    mock_add_core.assert_called_once()
    (args, kwargs) = mock_add_core.call_args
    core_arg = args[0]
    assert type(core_arg) == opencas.cas_config.core_config
    assert core_arg.cache_id == 2
    assert core_arg.core_id == 1
    assert core_arg.device == "/dev/dummy"
def test_modlue_configure_cache_not_configured_not_started_start_failed(
    mock_setup_module,
    mock_from_file,
    mock_configure_cache,
    mock_start_cache,
    mock_cache_started,
):
    mock_setup_module.return_value = setup_module_with_params(
        configure_cache_device={
            "id": "1",
            "cache_device": "/dev/dummy",
            "cache_mode": "WT",
        })
    mock_config = h.CopyableMock()
    mock_config.mock_add_spec(opencas.cas_config)
    mock_from_file.return_value = mock_config
    mock_cache_started.return_value = False
    mock_start_cache.side_effect = Exception()

    with pytest.raises(AnsibleFailJson) as e:
        cas.main()

    mock_config.write.assert_called()

    mock_start_cache.assert_called_once()

    e.match("'failed': True")
    mock_configure_cache.assert_not_called()
    assert len(mock_config.copies) == 1
    mock_config.copies[0].write.assert_called_once()
def test_modlue_configure_core_insert_failed(mock_setup_module,
                                             mock_from_file):
    mock_setup_module.return_value = setup_module_with_params(
        configure_core_device={
            "id": "1",
            "cache_id": "1",
            "cached_volume": "/dev/dummy",
        })
    mock_config = h.CopyableMock()
    mock_config.mock_add_spec(opencas.cas_config)
    mock_config.insert_core.side_effect = Exception()
    mock_from_file.return_value = mock_config

    with pytest.raises(AnsibleFailJson) as e:
        cas.main()

    mock_config.insert_core.assert_called_once()
    e.match("'failed': True")
def test_modlue_configure_core_already_added(mock_setup_module, mock_from_file,
                                             mock_core_added, mock_add_core):
    mock_setup_module.return_value = setup_module_with_params(
        configure_core_device={
            "id": "1",
            "cache_id": "1",
            "cached_volume": "/dev/dummy",
        })
    mock_config = h.CopyableMock()
    mock_config.mock_add_spec(opencas.cas_config)
    mock_config.insert_core.side_effect = (
        opencas.cas_config.AlreadyConfiguredException())
    mock_from_file.return_value = mock_config
    mock_core_added.return_value = True

    with pytest.raises(AnsibleExitJson) as e:
        cas.main()

    e.match("'changed': False")
    mock_add_core.assert_not_called()
def test_modlue_configure_cache_not_configured_not_started(
    mock_setup_module,
    mock_from_file,
    mock_configure_cache,
    mock_start_cache,
    mock_cache_started,
):
    mock_setup_module.return_value = setup_module_with_params(
        configure_cache_device={
            "id": "1",
            "cache_device": "/dev/dummy",
            "cache_mode": "WT",
        })
    mock_config = h.CopyableMock()
    mock_config.mock_add_spec(opencas.cas_config)
    mock_from_file.return_value = mock_config
    mock_cache_started.return_value = False

    with pytest.raises(AnsibleExitJson) as e:
        cas.main()

    e.match("'changed': True")

    mock_config.write.assert_called()

    mock_start_cache.assert_called_once()
    (args, kwargs) = mock_start_cache.call_args
    cache_arg = args[0]
    assert kwargs["load"] == False
    assert kwargs["force"] == None
    assert type(cache_arg) == opencas.cas_config.cache_config
    assert cache_arg.cache_id == 1
    assert cache_arg.device == "/dev/dummy"
    assert cache_arg.cache_mode == "WT"

    mock_configure_cache.assert_called_once()
    (args, kwargs) = mock_configure_cache.call_args
    assert args[0] == cache_arg