def test_change_panel_name(self, send_email): """Don't update the check if the check name changes""" self.status_check.name = '44' self.status_check.save() sync_grafana_check(self.status_check.id, str(datetime(2017, 2, 1, 0, 0, 1, 1231))) self.assertEqual(ElasticsearchStatusCheck.objects.get(id=self.status_check.id).name, '44') self.assertFalse(send_email.called)
def test_dashboard_deleted(self, send_email): sync_grafana_check(self.status_check.id, str(datetime(2017, 2, 1, 0, 0, 1, 1231))) send_email.assert_called_once_with(args=(['*****@*****.**'], 'http://localhost/check/{}/\n\n' 'Dashboard "{}" has been deleted, so check "{}" has been ' 'deactivated. If you would like to keep the check, re-enable it ' 'with auto_sync: False.' .format(self.status_check.id, '42', 'Also Great Dashboard: 42'), 'Also Great Dashboard: 42')) check = ElasticsearchStatusCheck.objects.get(id=self.status_check.id) self.assertFalse(check.active)
def test_change_time_range(self, send_email): """Time range changes on the dashboard shouldn't be reflected in the check""" self.status_check.time_range = 12345 self.status_check.save() queries = self.status_check.queries sync_grafana_check(self.status_check.id, str(datetime(2017, 2, 1, 0, 0, 1, 12312))) check = ElasticsearchStatusCheck.objects.get(id=self.status_check.id) self.assertEqual(queries, check.queries) self.assertFalse(send_email.called)
def test_change_queries(self, send_email): self.status_check.queries = self.old_queries self.status_check.save() # careful copy/pasting in an IDE - there is whitespace at the end of some lines diff = u"""\ { "0": { "aggs": { "agg": { "$delete": [ "terms" ], "aggs": { "$replace": { "sum: 42": { "sum": { "field": "value" } } } }, "date_histogram": { "extended_bounds": { "max": "now", "min": "now-180m" }, "field": "@timestamp", "interval": "1m" } } }, "query": { "bool": { "must": { "0": { "query_string": { "query": "query:life-the-universe-and-everything" } } } } } } }""" # noqa: W291 (suppress trailing whitespace warning) sync_grafana_check(self.status_check.id, str(datetime(2017, 2, 1, 0, 0, 1, 123))) send_email.assert_called_once_with(args=(['*****@*****.**', '*****@*****.**', '*****@*****.**'], 'http://localhost/check/{}/\n\n' 'The queries have changed from:\n\n{}\n\nto:\n\n{}\n\nDiff:\n{}' .format(self.status_check.id, self.old_queries, self.queries, diff), 'Also Great Dashboard: 42')) self.assertEqual(ElasticsearchStatusCheck.objects.get(id=self.status_check.id).queries, str(self.queries))
def test_change_series_ids(self, send_email): self.panel.series_ids = 'B,E' self.panel.save() sync_grafana_check(self.status_check.id, str(datetime(2017, 2, 1, 0, 0, 1, 12))) send_email.assert_called_once_with(args=(['*****@*****.**', '*****@*****.**', '*****@*****.**'], 'http://localhost/check/{}/\n\n' 'The panel series ids have changed from B,E to B. The check has not ' 'been changed.'.format(self.status_check.id), 'Also Great Dashboard: 42')) self.assertEqual(GrafanaPanel.objects.get(id=self.panel.id).series_ids, 'B')
def test_change_datasource(self, send_email): self.grafana_data_source.grafana_source_name = 'hello' self.grafana_data_source.save() sync_grafana_check(self.status_check.id, str(datetime(2017, 2, 1, 0, 0, 1, 231))) send_email.assert_called_once_with(args=(['*****@*****.**', '*****@*****.**', '*****@*****.**'], 'http://localhost/check/{}/\n\n' 'The Grafana data source has changed from "hello" to "deep-thought". ' 'The new source is not configured in Cabot, so the status check will ' 'continue to use the old source.'.format(self.status_check.id), 'Also Great Dashboard: 42'))
def test_sync_multiple_sources(self, send_email, fake_get_dashboard_info): """If there are multiple sources with the same name, get data from the correct one""" grafana_instance2 = GrafanaInstance.objects.create( name='graf2', url='graf2', api_key='graf2' ) self.panel.grafana_instance = grafana_instance2 self.panel.save() fake_get_dashboard_info.side_effect = raise_validation_error sync_grafana_check(self.status_check.id, str(datetime(2017, 2, 1, 0, 0, 1, 123))) # Sync based on the correct Grafana instance fake_get_dashboard_info.assert_called_once_with(grafana_instance2, 'db/42')
def test_dashboard_no_changes(self, send_email): sync_grafana_check(self.status_check.id, str(datetime(2017, 2, 1, 0, 0, 1, 123))) # No emails should be sent since nothing relevant in the dashboard has changed self.assertFalse(send_email.called)
def test_dashboard_not_updated_time(self, send_email): sync_grafana_check(self.status_check.id, str(datetime(2017, 3, 1, 0, 0, 0, 1231))) # No emails should be sent since it hasn't been updated recently self.assertFalse(send_email.called)