示例#1
0
def test_file_source_directory(tmpdir):
    dp = tmpdir.join('testing.txt')
    sp = tmpdir.mkdir('directory')

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    with pytest.raises(ActionError):
        file.process()
示例#2
0
def test_file_source_and_path_not_writable(tmpdir, monkeypatch):
    dp = tmpdir.join('testing.txt').ensure()
    sp = tmpdir.join('source.txt')
    sp.write('Hello there')

    builtins_open = open

    def open_(  # pylint: disable=unused-argument
            file,
            mode='r',
            buffering=-1,
            encoding=None,
            errors=None,
            newline=None,
            closefd=True,
            opener=None):
        if file == dp.strpath and mode == 'wb':
            raise PermissionError(13, 'Permission denied', file)
        else:
            return builtins_open(file, mode, buffering, encoding, errors,
                                 newline, closefd, opener)

    monkeypatch.setattr('builtins.open', open_)

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    with pytest.raises(ActionError):
        file.process()
示例#3
0
def test_alias_not_writable(tmpdir):
    sp = tmpdir.join('source.txt').ensure()

    # TODO: Determine a suitable way to mock the appropriate PyObjC methods to do this better
    file = File(path='/alias', source=sp.strpath, state='alias')
    with pytest.raises(ActionError):
        file.process()
示例#4
0
def test_file_empty_exists(tmpdir):
    p = tmpdir.join('testing.txt')
    p.write('Hello there')

    file = File(path=p.strpath, state='file')
    assert file.process() == ActionResponse(changed=False,
                                            data={'path': p.strpath})
示例#5
0
def test_absent_exists(tmpdir):
    p = tmpdir.join('testing.txt').ensure()

    file = File(path=p.strpath, state='absent')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': p.strpath})
    assert not p.exists()
示例#6
0
def test_directory_inexistent(tmpdir):
    p = tmpdir.join('directory')

    file = File(path=p.strpath, state='directory')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': p.strpath})
    assert p.isdir()
示例#7
0
def test_file_empty_inexistent(tmpdir):
    p = tmpdir.join('testing.txt')

    file = File(path=p.strpath, state='file')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': p.strpath})
    assert p.read() == ''
示例#8
0
def test_symlink_exists_same(tmpdir):
    p = tmpdir.join('testing.txt')
    p.mksymlinkto('source.txt')

    file = File(path=p.strpath, source='source.txt', state='symlink')
    assert file.process() == ActionResponse(changed=False,
                                            data={'path': p.strpath})
示例#9
0
def test_symlink_inexistent(tmpdir):
    p = tmpdir.join('testing.txt')

    file = File(path=p.strpath, source='source.txt', state='symlink')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': p.strpath})
    assert p.islink()
    assert p.readlink() == 'source.txt'
示例#10
0
def test_symlink_destination_path_is_directory(tmpdir):
    p = tmpdir.mkdir('directory')

    file = File(path=p.strpath, source='source.txt', state='symlink')
    assert file.process() == ActionResponse(
        changed=True, data={'path': p.join('source.txt').strpath})
    assert p.join('source.txt').islink()
    assert p.join('source.txt').readlink() == 'source.txt'
示例#11
0
def test_file_empty_not_writable(tmpdir, monkeypatch):
    p = tmpdir.join('testing.txt')

    monkeypatch.setattr('builtins.open',
                        build_open_with_permission_error(p.strpath))

    file = File(path=p.strpath, state='file')
    with pytest.raises(ActionError):
        file.process()
示例#12
0
def test_file_source_exists_same(tmpdir):
    dp = tmpdir.join('testing.txt')
    dp.write('Hello there')
    sp = tmpdir.join('source.txt')
    sp.write('Hello there')

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    assert file.process() == ActionResponse(changed=False,
                                            data={'path': dp.strpath})
示例#13
0
def test_file_source_inexistent(tmpdir):
    dp = tmpdir.join('testing.txt')
    sp = tmpdir.join('source.txt')
    sp.write('Hello there')

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': dp.strpath})
    assert dp.read() == 'Hello there'
示例#14
0
def test_file_source_destination_path_is_directory(tmpdir):
    dp = tmpdir.mkdir('directory')
    sp = tmpdir.join('source.txt')
    sp.write('Hello there')

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    assert file.process() == ActionResponse(
        changed=True, data={'path': dp.join('source.txt').strpath})
    assert dp.join('source.txt').read() == 'Hello there'
示例#15
0
def test_file_source_and_path_not_readable(tmpdir, monkeypatch):
    dp = tmpdir.join('testing.txt').ensure()
    sp = tmpdir.join('source.txt').ensure()

    monkeypatch.setattr('builtins.open',
                        build_open_with_permission_error(dp.strpath))

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    with pytest.raises(ActionError):
        file.process()
示例#16
0
def test_alias_exists_different(tmpdir):
    dp = tmpdir.join('test.alias')
    shutil.copy(os.path.join(FIXTURE_PATH, 'file', 'test.alias'), dp.strpath)
    sp = tmpdir.join('source.txt').ensure()

    file = File(path=dp.strpath, source=sp.strpath, state='alias')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': dp.strpath})
    alias_data = dp.read_binary()
    assert alias_data.startswith(b'book')
    assert b'source.txt' in alias_data
示例#17
0
def test_alias_path_inexistent(tmpdir):
    dp = tmpdir.join('testing.txt')
    sp = tmpdir.join('source.txt').ensure()

    file = File(path=dp.strpath, source=sp.strpath, state='alias')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': dp.strpath})
    assert dp.isfile()
    alias_data = dp.read_binary()
    assert alias_data.startswith(b'book')
    assert b'source.txt' in alias_data
示例#18
0
def test_directory_not_writable(tmpdir, monkeypatch):
    p = tmpdir.join('directory')

    os_mkdir = os.mkdir

    def mkdir(path, *args, **kwargs):
        if path == p.strpath:
            raise PermissionError(13, 'Permission denied', file)
        else:
            return os_mkdir(path, *args, **kwargs)

    monkeypatch.setattr('os.mkdir', mkdir)

    file = File(path=p.strpath, state='directory')
    with pytest.raises(ActionError):
        file.process()
示例#19
0
def test_symlink_not_writable(tmpdir, monkeypatch):
    p = tmpdir.join('testing.txt')

    os_symlink = os.symlink

    def symlink(src, dst, *args, **kwargs):
        if dst == p.strpath:
            raise PermissionError(13, 'Permission denied', file)
        else:
            return os_symlink(src, dst, *args, **kwargs)

    monkeypatch.setattr('os.symlink', symlink)

    file = File(path=p.strpath, source='source.txt', state='symlink')
    with pytest.raises(ActionError):
        file.process()
示例#20
0
def test_alias_exists_same(tmpdir):
    p = tmpdir.join('test.alias')
    shutil.copy(os.path.join(FIXTURE_PATH, 'file', 'test.alias'), p.strpath)

    try:
        # Create a file at the source path as it must exist for the alias to work
        with open('/private/var/tmp/test.txt', 'w'):
            pass

        file = File(path=p.strpath,
                    source='/private/var/tmp/test.txt',
                    state='alias')
        assert file.process() == ActionResponse(changed=False,
                                                data={'path': p.strpath})
    finally:
        os.remove('/private/var/tmp/test.txt')
示例#21
0
def test_file_empty_directory(tmpdir):
    p = tmpdir.mkdir('directory')

    file = File(path=p.strpath, state='file')
    with pytest.raises(ActionError):
        file.process()
示例#22
0
def test_directory_exists(tmpdir):
    p = tmpdir.mkdir('directory')

    file = File(path=p.strpath, state='directory')
    assert file.process() == ActionResponse(changed=False,
                                            data={'path': p.strpath})
示例#23
0
def test_argument_state_symlink_after_init_invalid():
    file = File(path='boo.txt')
    with pytest.raises(ValueError):
        file.state = 'symlink'
示例#24
0
def test_argument_state_directory_after_init_invalid():
    file = File(path='boo.txt', source='source')
    with pytest.raises(ValueError):
        file.state = 'directory'
示例#25
0
def test_argument_state_absent_after_init_invalid():
    file = File(path='boo.txt', source='source.txt')
    with pytest.raises(ValueError):
        file.state = 'absent'
示例#26
0
def test_argument_source_directory_symlink_combination_invalid():
    with pytest.raises(ValueError):
        File(path='boo.txt', state='symlink')
示例#27
0
def test_absent_inexistent(tmpdir):
    p = tmpdir.join('testing.txt')

    file = File(path=p.strpath, state='absent')
    assert file.process() == ActionResponse(changed=False,
                                            data={'path': p.strpath})
示例#28
0
def test_argument_source_absent_state_combination_invalid():
    with pytest.raises(ValueError):
        File(path='boo.txt', source='source.txt', state='absent')
示例#29
0
def test_alias_source_inexistent(tmpdir):
    p = tmpdir.join('testing.txt')

    file = File(path=p.strpath, source='source.txt', state='alias')
    with pytest.raises(ActionError):
        file.process()
示例#30
0
def test_argument_state_invalid():
    with pytest.raises(ValueError):
        File(path='boo.txt', state='hmmm')