def test_update_file(mocker, mock_open, exception, reraise, calls):
    mock_tempfile = mocker.patch("octane.util.tempfile.get_tempname")
    mock_tempfile.return_value = "/temp/filename"

    mock_old = mock.MagicMock()
    mock_new = mock.MagicMock()

    mock_open.side_effect = [mock_old, mock_new]

    mock_os = mock.Mock()
    os_methods = ["unlink", "stat", "chmod", "chown", "rename"]
    for method in os_methods:
        mocker.patch("os." + method, new=getattr(mock_os, method))

    mock_os.stat.return_value.configure_mock(
        st_mode=0o640,
        st_uid=2,
        st_gid=3,
    )

    if reraise:
        with pytest.raises(exception):
            with subprocess.update_file("/fake/filename"):
                raise exception
    else:
        with subprocess.update_file("/fake/filename"):
            if exception is not None:
                raise exception

    assert mock_os.mock_calls == calls
Esempio n. 2
0
def replace_pipeline_items(filename, pipelines):
    def iterate_pipeline_items(old, new):
        for line, section, parameter, value in helpers.iterate_parameters(old):
            if section in pipelines and parameter == "pipeline":
                items = value.split()
                yield items
                new_value = " ".join(items)
                line = "{0} = {1}\n".format(parameter, new_value)
            new.write(line)

    with subprocess.update_file(filename) as (old, new):
        yield iterate_pipeline_items(old, new)
Esempio n. 3
0
def unset_default_domain_id(filename):
    with subprocess.update_file(filename) as (old, new):
        for line, section, parameter, value in helpers.iterate_parameters(old):
            if section == "identity" and parameter == "default_domain_id":
                line = "#{0}".format(line)
            new.write(line)