def test_pillar_updater_component_pillar(monkeypatch, tmpdir_function):
    mock_res = []

    some_pillar = {
        1: {
            2: 3,
            4: [5, 6]
        }
    }

    default_pillar_dir = tmpdir_function / 'default'
    user_pillar_dir = tmpdir_function / 'user'

    monkeypatch.setattr(pillar, 'PRVSNR_PILLAR_DIR', default_pillar_dir)
    monkeypatch.setattr(
        pillar, 'PRVSNR_USER_PILLAR_ALL_HOSTS_DIR', user_pillar_dir
    )

    monkeypatch.setattr(
        pillar, 'dump_yaml', mock_fun_echo(mock_res, 'dump_yaml')
    )

    monkeypatch.setattr(
        pillar, 'load_yaml', mock_fun_echo(mock_res, 'load_yaml')
    )

    component = 'component1'
    default_pillar_path = (
        default_pillar_dir / 'components/{}.sls'.format(component)
    )
    user_pillar_path = user_pillar_dir / '{}.sls'.format(component)

    # show (no user pillar)
    _ = PillarUpdater().component_pillar(component, show=True)
    assert [res.key for res in mock_res] == ['load_yaml']
    assert mock_res[0].args_all == ((default_pillar_path,), {})

    # show (with user pillar)
    mock_res[:] = []
    PillarUpdater.ensure_exists(user_pillar_path)
    _ = PillarUpdater().component_pillar(component, show=True)
    assert [res.key for res in mock_res] == ['load_yaml']
    assert mock_res[0].args_all == ((user_pillar_path,), {})

    # reset for existent
    _ = PillarUpdater().component_pillar(component, reset=True)
    assert not user_pillar_path.exists()

    # reset for non-existent won't fail
    _ = PillarUpdater().component_pillar(component, reset=True)

    # set
    mock_res[:] = []
    component = 'component2'
    user_pillar_path = user_pillar_dir / '{}.sls'.format(component)
    _ = PillarUpdater().component_pillar(component, pillar=some_pillar)
    assert user_pillar_path.exists()
    assert [res.key for res in mock_res] == ['dump_yaml']
    assert mock_res[0].args_all == ((user_pillar_path, some_pillar), {})
Esempio n. 2
0
def test_pillar_updater_ensure_exists(tmpdir_function):
    pu = PillarUpdater()

    f1 = tmpdir_function / 'aaa'
    pu.ensure_exists(f1)
    assert f1.exists()

    # parent dir not exists
    f2 = tmpdir_function / 'some-dir' / 'aaa'
    pu.ensure_exists(f2)
    assert f2.parent.exists()
    assert f2.exists()