def test_load_wrong_extension(tmp_path):
    dash = tmp_path / 'dash.py'
    dash.write_text(sample_dash.format(title='dash'))

    dashboards = builder.load_dashboards(str(tmp_path))

    assert len(dashboards) == 0
def test_load_without_dashboard(tmp_path):
    dash = tmp_path / 'dash.dashboard.py'
    dash.write_text('import grafanalib')

    dashboards = builder.load_dashboards(str(tmp_path))

    assert len(dashboards) == 0
def test_load_empty_file(tmp_path):
    dash = tmp_path / 'dash.dashboard.py'
    dash.write_text('')

    dashboards = builder.load_dashboards(str(tmp_path))

    assert len(dashboards) == 0
def test_load_not_python(tmp_path):
    dash = tmp_path / 'dash.dashboard.py'
    dash.write_text('I am not a python file')

    dashboards = builder.load_dashboards(str(tmp_path))

    assert len(dashboards) == 0
def test_load_valid_dashboard(tmp_path):
    dash = tmp_path / 'dash.dashboard.py'
    dash.write_text(sample_dash.format(title='dash'))

    dashboards = builder.load_dashboards(str(tmp_path))

    assert len(dashboards) == 1
    assert len(dashboards[builder.DEFAULT_FOLDER]) == 1
    assert isinstance(dashboards[builder.DEFAULT_FOLDER][0], Dashboard)
示例#6
0
def test_generating_multiple_dirs(tmp_path):
    """
    Check that for a following input dir...

        input_dir/
            dash0.dashboard.py
            dir1/
                dash1.dashboard.py
            dir2/
                dash2.dashboard.py
                dir3/
                    dash3.dashboard.py

    ...the output dir looks like this:

        output_dir/
            DEFAULT_FOLDER/
                dash0.json
            dir1/
                dash1.json
            dir2/
                dash2.json
                dash3.json
    """

    out_path = tmp_path / builder.DEFAULT_OUT
    in_path = tmp_path / 'in'
    in_path.mkdir()

    dir1 = in_path / 'dir1'
    dir2 = in_path / 'dir2'
    dir3 = in_path / 'dir2' / 'dir3'
    dir1.mkdir()
    dir2.mkdir()
    dir3.mkdir()

    dash0 = in_path / 'dash0.dashboard.py'
    dash1 = dir1 / 'dash1.dashboard.py'
    dash2 = dir2 / 'dash2.dashboard.py'
    dash3 = dir3 / 'dash3.dashboard.py'

    dash0.write_text(sample_dash.format(title='dash0'))
    dash1.write_text(sample_dash.format(title='dash1'))
    dash2.write_text(sample_dash.format(title='dash2'))
    dash3.write_text(sample_dash.format(title='dash3'))

    dashboards = builder.load_dashboards(str(in_path))
    builder.generate_dashboards(dashboards, str(out_path))

    assert (out_path / builder.DEFAULT_FOLDER / 'dash0.json').is_file()
    assert (out_path / 'dir1').is_dir()
    assert (out_path / 'dir2').is_dir()
    assert not (out_path / 'dir3').is_dir()
    assert (out_path / 'dir1' / 'dash1.json').is_file()
    assert (out_path / 'dir2' / 'dash2.json').is_file()
    assert (out_path / 'dir2' / 'dash3.json').is_file()
def test_load_multiple_files(tmp_path):
    dash1 = tmp_path / 'dash1.dashboard.py'
    dash2 = tmp_path / 'dash2.dashboard.py'
    dash1.write_text(sample_dash.format(title='dash1'))
    dash2.write_text(sample_dash.format(title='dash2'))

    dashboards = builder.load_dashboards(str(tmp_path))

    assert len(dashboards) == 1
    assert len(dashboards[builder.DEFAULT_FOLDER]) == 2
def test_load_from_configmap(tmp_path):
    dash0 = tmp_path / 'dash0.dashboard.py'
    dash1 = tmp_path / f'dir1{builder.DIR_SEPARATOR}dash1.dashboard.py'
    dash2 = tmp_path / f'dir2{builder.DIR_SEPARATOR}-dash2.dashboard.py'
    dash3 = tmp_path / f'dir2{builder.DIR_SEPARATOR}dash3.dashboard.py'

    dash0.write_text(sample_dash.format(title='dash0'))
    dash1.write_text(sample_dash.format(title='dash1'))
    dash2.write_text(sample_dash.format(title='dash2'))
    dash3.write_text(sample_dash.format(title='dash3'))

    dashboards = builder.load_dashboards(str(tmp_path), from_configmap=True)

    assert len(dashboards) == 3
    assert len(dashboards[builder.DEFAULT_FOLDER]) == 1
    assert len(dashboards['dir1']) == 1
    assert len(dashboards['dir2']) == 2

    dashboards = builder.load_dashboards(str(tmp_path), from_configmap=False)

    assert len(dashboards) == 1
    assert len(dashboards[builder.DEFAULT_FOLDER]) == 4
def test_load_nested_dirs(tmp_path):
    dir1 = tmp_path / 'dir1'
    dir2 = tmp_path / 'dir2'
    dir1.mkdir()
    dir2.mkdir()

    dash1 = dir1 / 'dash1.dashboard.py'
    dash2 = dir2 / 'dash2.dashboard.py'
    dash1.write_text(sample_dash.format(title='dash1'))
    dash2.write_text(sample_dash.format(title='dash2'))

    dashboards = builder.load_dashboards(str(tmp_path))

    assert len(dashboards) == 2
    assert len(dashboards['dir1']) == 1
    assert len(dashboards['dir2']) == 1
    assert builder.DEFAULT_FOLDER not in dashboards
示例#10
0
def test_generate_single_dashboard(tmp_path):
    """
    Check if out_path contains a generated dashboard json with correct title.
    """

    out_path = tmp_path / builder.DEFAULT_OUT

    title = 'dash1'

    dash = tmp_path / 'dash.dashboard.py'
    dash.write_text(sample_dash.format(title=title))

    dashboards = builder.load_dashboards(str(tmp_path))
    builder.generate_dashboards(dashboards, str(out_path))

    generated_json = out_path / builder.DEFAULT_FOLDER / f'{title}.json'

    assert Path(generated_json).is_file()
    assert f'"title": "{title}"' in generated_json.read_text()
def test_load_general_folder(tmp_path):
    """
    If there are dashboards in the root of input_dir and inside the "General" default folder,
    they all should be loaded under the same DEFAULT_FOLDER key
    """

    general = tmp_path / 'General'
    general.mkdir()

    dash0 = tmp_path / 'dash0.dashboard.py'
    dash1 = general / 'dash1.dashboard.py'

    dash0.write_text(sample_dash.format(title='dash0'))
    dash1.write_text(sample_dash.format(title='dash1'))

    dashboards = builder.load_dashboards(str(tmp_path))

    assert len(dashboards) == 1
    assert len(dashboards[builder.DEFAULT_FOLDER]) == 2
示例#12
0
def test_out_dir_non_empty(tmp_path):
    """
    Check if out_path is correctly cleaned and contains only the newly generated files.
    """

    out_path = tmp_path / builder.DEFAULT_OUT
    out_path.mkdir()

    some_file = out_path / 'some.file'
    some_file.write_text('I should be removed')

    title = 'dash1'

    dash = tmp_path / 'dash.dashboard.py'
    dash.write_text(sample_dash.format(title=title))

    dashboards = builder.load_dashboards(str(tmp_path))
    builder.generate_dashboards(dashboards, str(out_path))

    generated_json = out_path / builder.DEFAULT_FOLDER / f'{title}.json'

    assert Path(generated_json).is_file()
    assert not Path(some_file).exists()
    assert f'"title": "{title}"' in generated_json.read_text()
def test_load_multiple_nested_dirs(tmp_path):
    dir1 = tmp_path / 'dir1'
    dir2 = tmp_path / 'dir2'
    dir3 = tmp_path / 'dir2' / 'dir3'
    dir1.mkdir()
    dir2.mkdir()
    dir3.mkdir()

    dash0 = tmp_path / 'dash0.dashboard.py'
    dash1 = dir1 / 'dash1.dashboard.py'
    dash2 = dir2 / 'dash2.dashboard.py'
    dash3 = dir3 / 'dash3.dashboard.py'

    dash0.write_text(sample_dash.format(title='dash0'))
    dash1.write_text(sample_dash.format(title='dash1'))
    dash2.write_text(sample_dash.format(title='dash2'))
    dash3.write_text(sample_dash.format(title='dash3'))

    dashboards = builder.load_dashboards(str(tmp_path))

    assert len(dashboards) == 3
    assert len(dashboards[builder.DEFAULT_FOLDER]) == 1
    assert len(dashboards['dir1']) == 1
    assert len(dashboards['dir2']) == 2