def test_module_check_cache_device(
    mock_cache_config,
    mock_setup_module,
):
    mock_setup_module.return_value = setup_module_with_params(
        check_cache_config={
            "id": "1000",
            "cache_device": "test_cache_device",
            "cache_mode": "test_cache_mode",
            "cleaning_policy": "test_cleaning_policy",
            "promotion_policy": "test_promotion_policy",
            "line_size": "test_cache_line_size",
            "io_class": "test_ioclass_file",
        })

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

    mock_cache_config.assert_called_with(
        1000,
        "test_cache_device",
        "test_cache_mode",
        cache_line_size="test_cache_line_size",
        ioclass_file="/etc/opencas/ansible/test_ioclass_file",
        cleaning_policy="test_cleaning_policy",
        promotion_policy="test_promotion_policy",
    )
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_module_zap_config_empty(mock_setup_module, mock_config_from_file):
    mock_setup_module.return_value = setup_module_with_params(zap=True)
    mock_config_from_file.return_value = opencas.cas_config()

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

    e.match("'changed': False")
def test_module_get_facts_not_installed(mock_setup_module, mock_get_version):
    mock_get_version.side_effect = opencas.casadm.CasadmError("casadm error")
    mock_setup_module.return_value = setup_module_with_params(
        gather_facts=True)

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

    e.match("'opencas_installed': False")
def test_module_zap_no_file(mock_setup_module, mock_config_from_file):
    mock_setup_module.return_value = setup_module_with_params(zap=True)
    mock_config_from_file.side_effect = IOError()

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

    mock_setup_module.assert_called_once()
    e.match("'changed': False")
def test_module_check_core_device_missing_params(mock_setup_module,
                                                 core_params):
    mock_setup_module.return_value = setup_module_with_params(
        check_core_config=core_params)

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

    e.match("Missing")
    e.match("'failed': True")
def test_module_stop_no_devices(mock_setup_module, mock_stop, mock_get_list):
    mock_setup_module.return_value = setup_module_with_params(
        stop={"flush": True})
    mock_get_list.return_value = []

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

    mock_stop.assert_not_called()
    e.match("'changed': False")
def test_module_stop_exception(mock_setup_module, mock_stop, mock_get_list):
    mock_setup_module.return_value = setup_module_with_params(
        stop={"flush": True})
    mock_get_list.return_value = [{}]
    mock_stop.side_effect = Exception()

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

    mock_stop.assert_called_with(True)
    e.match("'failed': True")
def test_module_stop_all_devices_stopped(mock_setup_module, mock_stop,
                                         mock_get_list):
    mock_setup_module.return_value = setup_module_with_params(
        stop={"flush": False})
    mock_get_list.side_effect = [[{}, {}, {}], []]

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

    mock_stop.assert_called_with(False)
    e.match("'changed': True")
def test_module_zap_config(mock_setup_module, mock_new_config,
                           mock_config_from_file):
    mock_setup_module.return_value = setup_module_with_params(zap=True)
    mock_config_from_file.return_value = mock_config_file
    new_config = Mock()
    mock_new_config.return_value = new_config

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

    new_config.write.assert_called_once()
    mock_new_config.assert_called_with(version_tag="DEADBEEF")
    e.match("'changed': True")
def test_module_stop_no_devices_stopped(mock_setup_module, mock_stop,
                                        mock_get_list):
    mock_setup_module.return_value = setup_module_with_params(
        stop={"flush": False})

    # We're using empty dictionaries just to indicate devices count
    # Here no caches were stopped between mock calls
    mock_get_list.side_effect = [[{}, {}, {}], [{}, {}, {}]]

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

    mock_stop.assert_called_with(False)
    e.match("'failed': True")
def test_modlue_configure_core_no_config(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_from_file.side_effect = ValueError()

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

    mock_from_file.assert_called()
    e.match("'failed': True")
def test_module_get_facts_installed(mock_setup_module, mock_get_caches_list,
                                    mock_get_version):
    mock_get_version.return_value = {
        "Open CAS Kernel Module": "03.08.00.01131011",
        "Open CAS Disk Kernel Module": "03.08.00.01131011",
        "Open CAS CLI Utility": "03.08.00.01131011",
    }
    mock_setup_module.return_value = setup_module_with_params(
        gather_facts=True)

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

    e.match("'opencas_installed': True")
    e.match("03.08.00.01131011")
def test_module_check_cache_device_validate_failed(mock_setup_module,
                                                   mock_validate):
    mock_setup_module.return_value = setup_module_with_params(
        check_core_config={
            "id": "1",
            "cache_id": "1",
            "cached_volume": "/dev/dummy",
        })
    mock_validate.side_effect = Exception()

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

    mock_validate.assert_called()
    e.match("'failed': True")
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