コード例 #1
0
def test_redis_data_without_arguments(capfd):
    set_module_args({})
    with pytest.raises(SystemExit) as results:
        redis_data.main()
    out, err = capfd.readouterr()
    assert not err
    assert json.loads(out)['failed']
コード例 #2
0
def test_redis_delete_key_non_existent_key(capfd, mocker):
    set_module_args({
        'login_host': 'localhost',
        'login_password': '******',
        'key': 'foo',
        'state': 'absent',
        '_ansible_check_mode': False
    })
    mocker.patch('redis.Redis.get', return_value=None)
    mocker.patch('redis.Redis.delete', return_value=0)
    with pytest.raises(SystemExit):
        redis_data.main()
    out, err = capfd.readouterr()
    print(out)
    assert not err
    assert json.loads(out)['msg'] == 'Key: foo not present'
    assert json.loads(out)['changed'] is False
コード例 #3
0
def test_redis_data_delete_present_key(capfd, mocker):
    set_module_args({
        'login_host': 'localhost',
        'login_user': '******',
        'login_password': '******',
        'key': 'foo',
        'state': 'absent'
    })
    mocker.patch('redis.Redis.get', return_value='bar')
    mocker.patch('redis.Redis.delete', return_value=1)
    with pytest.raises(SystemExit):
        redis_data.main()
    out, err = capfd.readouterr()
    print(out)
    assert not err
    assert json.loads(out)['msg'] == 'Deleted key: foo'
    assert json.loads(out)['changed'] is True
コード例 #4
0
def test_redis_set_key_check_mode(capfd, mocker):
    set_module_args({
        'login_host': 'localhost',
        'login_password': '******',
        'key': 'foo',
        'state': 'present',
        'value': 'baz',
        '_ansible_check_mode': True
    })
    mocker.patch('redis.Redis.get', return_value='bar')
    with pytest.raises(SystemExit):
        redis_data.main()
    out, err = capfd.readouterr()
    print(out)
    assert not err
    assert json.loads(out)['msg'] == 'Set key: foo'
    assert json.loads(out)['value'] == 'baz'
    assert json.loads(out)['old_value'] == 'bar'
コード例 #5
0
def test_redis_data_fail_username(capfd, mocker):
    set_module_args({
        'login_host': 'localhost',
        'login_user': '******',
        'login_password': '******',
        'key': 'foo',
        'value': 'baz',
        '_ansible_check_mode': False
    })
    with pytest.raises(SystemExit):
        redis_data.main()
    out, err = capfd.readouterr()
    print(out)
    assert not err
    assert json.loads(out)['failed']
    assert json.loads(
        out
    )['msg'] == 'The option `username` in only supported with redis >= 3.4.0.'
コード例 #6
0
def test_redis_data_non_existing_key_xx(capfd, mocker):
    set_module_args({
        'login_host': 'localhost',
        'login_user': '******',
        'login_password': '******',
        'key': 'foo',
        'value': 'baz',
        'existing': True,
        '_ansible_check_mode': False
    })
    mocker.patch('redis.Redis.get', return_value=None)
    mocker.patch('redis.Redis.set', return_value=None)
    with pytest.raises(SystemExit):
        redis_data.main()
    out, err = capfd.readouterr()
    print(out)
    assert not err
    assert json.loads(out)['old_value'] is None
    assert 'value' not in json.loads(out)
    assert json.loads(out)['msg'] == 'Could not set key: foo. Key not present.'
    assert json.loads(out)['changed'] is False
    assert json.loads(out)['failed'] is True