def test_dashboard_mult_match_valid(): dash = Dashboard() res = dash.__has_multiple_matches__('foo', [{ 'name': 'foo' }, { 'name': 'foo' }]) assert res is True
def test_find_match_none(): response = { 'count': 1, 'results': [ { 'name': 'bar' } ] } dash = Dashboard().with_name('foo') with pytest.raises(ResourceMatchNotFoundError): dash.__find_existing_match__(response)
def test_find_match_exact(): response = { 'count': 1, 'results': [ { 'name': 'foo' } ] } dash = Dashboard().with_name('foo') with pytest.raises(ResourceAlreadyExistsError): dash.__find_existing_match__(response)
def test_dashboard_with_charts(chart): chart1 = chart('chart1') chart2 = chart('chart2') expected_values = [chart1, chart2] dashboard = Dashboard() dashboard.with_charts(chart1, chart2) list_charts = dashboard.options['charts'] assert len(list_charts) == 2 assert set(map(lambda x: x.__get__('name'), list_charts)) \ == set(map(lambda x: x.__get__('name'), expected_values))
def test_dashboard_create(): chart1 = TimeSeriesChart() chart1.with_name('chart1') chart1.with_program("data('requests.min').publish()") chart1.with_default_plot_type(PlotType.area_chart) chart2 = TimeSeriesChart() chart2.with_name('chart2') chart2.with_program("data('requests.min').publish()") chart2.with_default_plot_type(PlotType.line_chart) dashboard_name = 'removeme111' dashboard = Dashboard() dashboard.with_charts(chart1, chart2) dashboard.with_name(dashboard_name) f = StringIO() with stdout_redirected(f): dashboard.create(dry_run=True) response = f.getvalue() result_string = response[response.find('{'):]\ .replace('\'', '\"')\ .replace('("', '(\\"')\ .replace('")', '\\")') result = json.loads(result_string) assert 'charts' in result assert 'name' in result assert len(result['charts']) == 2 assert result['name'] == dashboard_name assert result['charts'][0]['options']['defaultPlotType'] \ == PlotType.area_chart.value assert result['charts'][1]['options']['defaultPlotType'] \ == PlotType.line_chart.value
def test_find_match_duplicate_matches(): response = { 'count': 1, 'results': [ { 'name': 'foo' }, { 'name': 'foo' } ] } dash = Dashboard().with_name('foo') with pytest.raises(ResourceHasMultipleExactMatchesError): dash.__find_existing_match__(response)
def test_dashboard_create_with_invalid_dashboard_group_id_failure(session, chart): with pytest.raises(RuntimeError): Dashboard(session=session)\ .with_name('testy mctesterson')\ .with_api_token('foo')\ .with_charts(chart('lol'))\ .create(group_id='asdf;lkj')
def test_dashboard_create_with_dashboard_group_id_success(sfx_recorder, session, chart): with sfx_recorder.use_cassette('dashboard_create_with_dashboard_group_id_success', serialize_with='prettyjson'): Dashboard(session=session)\ .with_name('testy mctesterson')\ .with_api_token('foo')\ .with_charts(chart('lol'))\ .create(group_id='DnogQGhAcAA')
def test_dashboard_create_success(): with global_recorder.use_cassette('dashboard_create_success', serialize_with='prettyjson'): Dashboard(session=global_session)\ .with_name('testy mctesterson')\ .with_api_token('foo')\ .with_charts(mk_chart('lol'))\ .create()
def test_dashboard_update_child_chart(sfx_recorder, session, chart): chart = chart('rawr') dashboard = Dashboard(session=session)\ .with_name('foobarium')\ .with_api_token('foo')\ .with_charts(chart) with sfx_recorder.use_cassette('dashboard_update_child_chart', serialize_with='prettyjson'): # We expect that updating the chart immediately shouldn't have # any effect on the state of the chart. dashboard.update() resp = dashboard.update() # We should only have one chart assert len(resp['charts']) == 1
def test_create_signal_analog_error(input, session, sfx_recorder): """Test the cases we expect to fail.""" with sfx_recorder.use_cassette(input.lower().replace(' ', '_'), serialize_with='prettyjson'): with pytest.raises(SignalAnalogError): Dashboard(session=session)\ .with_name(input)\ .with_api_token('foo')\ .create()
def test_dashboard_create_child_chart(sfx_recorder, session, chart): chart1 = chart('rawr') chart2 = chart('roar') dashboard = Dashboard(session=session)\ .with_name('bariumfoo')\ .with_api_token('foo')\ .with_charts(chart1) with sfx_recorder.use_cassette('dashboard_create_child_chart', serialize_with='prettyjson'): resp = dashboard.create() assert len(resp['charts']) == 1 # Simulate updating a configuration file. dashboard.options['charts'].append(chart2) resp_update = dashboard.update() assert len(resp_update['charts']) == 2
def test_dashboard_create_interactive_failure(confirm, sfx_recorder, session, chart): confirm.__getitem__.return_value = 'n' dashboard = Dashboard(session=session) \ .with_name('testy mctesterson') \ .with_api_token('foo') \ .with_charts(chart('lol')) with sfx_recorder.use_cassette('dashboard_create_failure_interactive', serialize_with='prettyjson'): # Create our first dashboard dashboard.create() with pytest.raises(SignalAnalogError): # Verify that we can't create it again dashboard.create() dashboard.create(interactive=True)
def test_dashboard_delete_child_chart(sfx_recorder, session, chart): chart1 = chart('rawr') chart2 = chart('roar') dashboard = Dashboard(session=session)\ .with_name('isley brothers')\ .with_api_token('foo')\ .with_charts(chart1, chart2) with sfx_recorder.use_cassette('dashboard_delete_child_chart', serialize_with='prettyjson'): resp = dashboard.create() assert len(resp['charts']) == 2 # Simulate removing a chart from a user's config. dashboard.options['charts'] = list(filter( lambda x: x.options != chart2.options, dashboard.options['charts'])) resp_delete = dashboard.update() # We should only have one chart assert len(resp_delete['charts']) == 1
def test_find_existing_resources(sfx_recorder, session): with sfx_recorder.use_cassette('get_existing_dashboards', serialize_with='prettyjson'): name = 'Riposte Template Dashboard' resp = Dashboard(session=session)\ .with_name('Riposte Template Dashboard')\ .with_api_token('foo')\ .__find_existing_resources__() assert resp['count'] > 0 for r in resp['results']: assert name in r['name']
def test_dashboard_create_with_filters_source_with_missing_property_and_value_failure(session, chart): with pytest.raises(ValueError): aws_src = FilterSource() dashboard_filter = DashboardFilters() \ .with_sources(aws_src) Dashboard(session=session) \ .with_name('testy mctesterson') \ .with_api_token('foo') \ .with_charts(chart('lol')) \ .with_filters(dashboard_filter) \ .create()
def test_dashboard_create_with_filters_time_missing_start_and_end_time_failure(session, chart): with pytest.raises(ValueError): time = FilterTime() dashboard_filter = DashboardFilters() \ .with_time(time) Dashboard(session=session) \ .with_name('testy mctesterson') \ .with_api_token('foo') \ .with_charts(chart('lol')) \ .with_filters(dashboard_filter) \ .create()
def test_dashboard_create_interactive_success(confirm): confirm.__getitem__.return_value = 'y' dashboard = Dashboard(session=global_session) \ .with_name('testy mctesterson') \ .with_api_token('foo') \ .with_charts(mk_chart('lol')) with global_recorder.use_cassette('dashboard_create_success_interactive', serialize_with='prettyjson'): # Create our first dashboard dashboard.create() with pytest.raises(SignalAnalogError): # Verify that we can't create it again dashboard.create() # Force the dashboard to create itself again dashboard.create(interactive=True)
def test_dashboard_create_force_success(sfx_recorder, session, chart): dashboard = Dashboard(session=session)\ .with_name('testy mctesterson')\ .with_api_token('foo')\ .with_charts(chart('lol')) with sfx_recorder.use_cassette('dashboard_create_success_force', serialize_with='prettyjson'): # Create our first dashboard dashboard.create() with pytest.raises(SignalAnalogError): # Verify that we can't create it again dashboard.create() # Force the dashboard to create itself again dashboard.create(force=True)
def test_dashboard_create_with_filters_time_start_time_is_not_less_than_end_time_failure(session, chart): with pytest.raises(ValueError): time = FilterTime()\ .with_start(1523894400000)\ .with_end(1523808000000) dashboard_filter = DashboardFilters() \ .with_time(time) Dashboard(session=session) \ .with_name('testy mctesterson') \ .with_api_token('foo') \ .with_charts(chart('lol')) \ .with_filters(dashboard_filter) \ .create()
def test_dashboard_create_with_filters_time_start_no_integer_after_negative_sign_failure(session, chart): with pytest.raises(ValueError): time = FilterTime()\ .with_start("-zh")\ .with_end("Now") dashboard_filter = DashboardFilters() \ .with_time(time) Dashboard(session=session) \ .with_name('testy mctesterson') \ .with_api_token('foo') \ .with_charts(chart('lol')) \ .with_filters(dashboard_filter) \ .create()
def test_dashboard_create_with_filters_time_start_unexpected_last_character_failure(chart): with pytest.raises(ValueError): time = FilterTime()\ .with_start("-1z")\ .with_end("Now") dashboard_filter = DashboardFilters() \ .with_time(time) Dashboard(session=session) \ .with_name('testy mctesterson') \ .with_api_token('foo') \ .with_charts(chart('lol')) \ .with_filters(dashboard_filter) \ .create()
def test_dashboard_create_with_filters_time_start_and_end_time_as_unsupported_data_type_failure(session, chart): with pytest.raises(ValueError): time = FilterTime()\ .with_start(True)\ .with_end(False) dashboard_filter = DashboardFilters() \ .with_time(time) Dashboard(session=session) \ .with_name('testy mctesterson') \ .with_api_token('foo') \ .with_charts(chart('lol')) \ .with_filters(dashboard_filter) \ .create()
def test_dashboard_create_with_filters_time_start_and_end_time_as_different_data_types_failure(session, chart): with pytest.raises(ValueError): time = FilterTime()\ .with_start(1523808000000)\ .with_end("Now") dashboard_filter = DashboardFilters() \ .with_time(time) Dashboard(session=session) \ .with_name('testy mctesterson') \ .with_api_token('foo') \ .with_charts(chart('lol')) \ .with_filters(dashboard_filter) \ .create()
def test_dashboard_create_with_filters_source_with_empty_value_failure(): with pytest.raises(ValueError): aws_src = FilterSource() \ .with_property("aws_region") \ .with_value("") dashboard_filter = DashboardFilters() \ .with_sources(aws_src) Dashboard(session=global_session) \ .with_name('testy mctesterson') \ .with_api_token('foo') \ .with_charts(mk_chart('lol')) \ .with_filters(dashboard_filter) \ .create()
def test_dashboard_create_with_filters_with_unexpected_data_type_failure(session, chart): with pytest.raises(ValueError): app_var = FilterVariable().with_alias('app') \ .with_is_required("THIS SHOULD BE BOOLEAN NOT A STRING") \ .with_value('bar') dashboard_filter = DashboardFilters() \ .with_variables(app_var) Dashboard(session=session)\ .with_name('testy mctesterson')\ .with_api_token('foo')\ .with_charts(chart('lol'))\ .with_filters(dashboard_filter)\ .create()
def test_dashboard_create_with_filters_with_missing_property_failure(session, chart): with pytest.raises(ValueError): app_var = FilterVariable().with_alias('app') \ .with_is_required(True) \ .with_value('bar') dashboard_filter = DashboardFilters() \ .with_variables(app_var) Dashboard(session=session)\ .with_name('testy mctesterson')\ .with_api_token('foo')\ .with_charts(chart('lol'))\ .with_filters(dashboard_filter)\ .create()
def test_dashboard_create_with_filters_time_end_time_is_not_Now_failure(): with pytest.raises(ValueError): time = FilterTime()\ .with_start("-1h")\ .with_end("Not Now") dashboard_filter = DashboardFilters() \ .with_time(time) Dashboard(session=global_session) \ .with_name('testy mctesterson') \ .with_api_token('foo') \ .with_charts(mk_chart('lol')) \ .with_filters(dashboard_filter) \ .create()
def test_cli_create_force_success(): program = Data('cpu.utilization').publish() chart = TimeSeriesChart().with_name('lol').with_program(program) with global_recorder.use_cassette('cli_create_force_success', serialize_with='prettyjson'): dashboard = Dashboard(session=global_session)\ .with_name('testy mctesterson')\ .with_charts(chart) cli = CliBuilder().with_resources(dashboard).build() runner = CliRunner() result = runner.invoke(cli, args=['--api-key', 'foo', 'create', '-f']) assert result.exit_code == 0
def test_dashboard_create_with_filters_time_as_integer_success(sfx_recorder, session, chart): time = FilterTime()\ .with_start(1523721600000)\ .with_end(1523808000000) dashboard_filter = DashboardFilters()\ .with_time(time) with sfx_recorder.use_cassette('dashboard_create_with_filters_time_as_integer_success', serialize_with='prettyjson'): Dashboard(session=session)\ .with_name('testy mctesterson')\ .with_api_token('foo')\ .with_charts(chart('lol'))\ .with_filters(dashboard_filter)\ .create()