def test_handle_deployment(self, mock_send): deployment = mock_data.get_deployment_sample_enriched() del os.environ[environment.SLACK_CHANNEL_OVERRIDE] os.environ[environment.SLACK_WEB_HOOK] = ( 'https://hooks.slack.com/services/' 'T02KE/B4PH1/VmEd2c7') os.environ[environment.SLACK_CHANNELS] = '#zermatt,#developers' slack_deployment.handle_deployment(deployment) calls = [ call('https://hooks.slack.com/services/T02KE/B4PH1/VmEd2c7', '#zermatt', deployment), call('https://hooks.slack.com/services/T02KE/B4PH1/VmEd2c7', '#developers', deployment), call('https://hooks.slack.com/services/T02KE/B4PH1/VmEd2c7', '#team-studadm', deployment), ] # Print actual calls - for debugging #for c in mock_send.call_args_list: # print(c) mock_send.assert_has_calls(calls, any_order=True) mock_send.reset_mock() os.environ[environment.SLACK_CHANNEL_OVERRIDE] = '#override' calls = [ call('https://hooks.slack.com/services/T02KE/B4PH1/VmEd2c7', '#override', deployment) ] slack_deployment.handle_deployment(deployment) mock_send.assert_has_calls(calls, any_order=True)
def test_create_file_name(self): deployment = mock_data.get_deployment_sample_enriched() lighthouse.get_current_date_time = mock.MagicMock() lighthouse.get_current_date_time.return_value = '2000-01-02_13:40' result = lighthouse.create_file_name(deployment) self.assertEqual(result, 'report_kth-azure-app_active_2000-01-02_13:40.html')
def test_select_alert_contact(self): studadm_id = 321 uptimerobot.get_alert_contacts = Mock(return_value=[ { "id": "123", "friendly_name": "#team-pipeline", "type": 11, "status": 2, "value": "https://hooks.slack.com/services/abc/123/xzy" }, { "id": studadm_id, "friendly_name": "#team-studadm-alert", "type": 11, "status": 2, "value": "https://hooks.slack.com/services/abc/321/xzy" }, { "id": "231", "friendly_name": "#team-kth-webb-alert", "type": 11, "status": 2, "value": "https://hooks.slack.com/services/abc/231/xzy" } ]) deployment = mock_data.get_deployment_sample_enriched() self.assertEqual(studadm_id, uptimerobot.select_alert_contact(deployment))
def test_create_deployment_message(self): deployment = mock_data.get_deployment_sample_enriched() message = slack_deployment.create_deployment_message(deployment) self.assertEqual( message, '3 replica(s) of *Monitor application* are being deployed in *active*, this may take up to 30 seconds.' )
def test_process_api_key(self): api_key = 'xyz' deployment = mock_data.get_deployment_sample_enriched() responses.add(responses.GET, 'https://api.detectify.com/rest/v2/profiles/', json=[{ "name": "Test", "endpoint": "www.test.com", "status": "verified", "created": "2018-01-10T08:34:15Z", "token": "abc123" }, { "name": "Test2", "endpoint": "www.test2.com", "status": "verified", "created": "2018-01-10T08:34:15Z", "token": "abc1234" }], status=200) responses.add(responses.GET, 'https://api.detectify.com/rest/v2/profiles/', json={}, status=400) detectify.process_api_key(api_key, deployment) self.assertRaises(detectify.DetectifyError, detectify.process_api_key, api_key, deployment)
def test_modify_or_add_monitor(self, call_endpoint, _): deployment = mock_data.get_deployment_sample_enriched() # New monitor uptimerobot.modify_or_add_monitor(deployment) call_endpoint.assert_called_once() call_endpoint.assert_called_with( '/newMonitor', uptimerobot.get_api_payload(deployment)) # Delete monitor call_endpoint.reset_mock() os.environ[ feature_flags.FEATURE_FLAG_UTR_DELETE_ON_ZERO_REPLICAS] = 'True' deployment['replicas'] = '0' uptimerobot.modify_or_add_monitor(deployment, 1) call_endpoint.assert_called_once() call_endpoint.assert_called_with( '/deleteMonitor', uptimerobot.get_api_payload(deployment, 1)) del os.environ[feature_flags.FEATURE_FLAG_UTR_DELETE_ON_ZERO_REPLICAS] # Do nothing call_endpoint.reset_mock() uptimerobot.modify_or_add_monitor(deployment) call_endpoint.assert_not_called() # Edit monitor call_endpoint.reset_mock() deployment['replicas'] = 'global' uptimerobot.modify_or_add_monitor(deployment, 1) call_endpoint.assert_called_once() call_endpoint.assert_called_with( '/editMonitor', uptimerobot.get_api_payload(deployment, 1))
def test_should_use_cluster(self): os.environ[environment.DETECTIFY_CLUSTERS] = '' deployment = mock_data.get_deployment_sample_enriched() result = detectify.should_use_cluster(deployment) self.assertFalse(result) os.environ[environment.DETECTIFY_CLUSTERS] = 'active' result = result = detectify.should_use_cluster(deployment) self.assertTrue(result)
def test_get_attachment(self): deployment = mock_data.get_deployment_sample_enriched() attachment = slack_util.get_attachment(deployment) log_link = ('https://graycloud.ite.kth.se/search?rangetype=relative&' 'fields=message%2Csource&width=2560&highlightMessage=&' 'relative=3600&q=source%3Aactive+AND+image_name%3A' '/.%2Akth-azure-app%3A2.0.11_abc123.%2A/') self.assertEqual(attachment['author_name'], ':mag: Search Logs') self.assertEqual(attachment['text'], 'Image: kth-azure-app:2.0.11_abc123') self.assertEqual(attachment['title'], 'Name: kth-azure-app') self.assertEqual(attachment['author_link'], log_link)
def test_handle_deployment(self, mock_start_scan): os.environ[environment.DETECTIFY_CLUSTERS] = 'active' os.environ[environment.DETECTIFY_API_KEYS] = 'xyz1,xyz2' responses.add(responses.GET, 'https://api.detectify.com/rest/v2/scans/abc123xyz456/', json={ "scan_profile_token": "abc123xyz456", "created": "2018-01-10T08:34:15Z", "started": "2018-01-16T16:01:38Z", "phase": "general", "state": "stopped" }, status=200) responses.add(responses.GET, 'https://api.detectify.com/rest/v2/profiles/', json=[{ "name": "Test", "endpoint": "www.test.com", "status": "verified", "created": "2018-01-10T08:34:15Z", "token": "abc123xyz456" }, { "name": "Test2", "endpoint": "www.test2.com", "status": "verified", "created": "2018-01-10T08:34:15Z", "token": "abc1234" }], status=200) responses.add(responses.GET, 'https://api.detectify.com/rest/v2/profiles/', json=[], status=200) responses.add(responses.POST, 'https://api.detectify.com/rest/v2/scans/abc123xyz456/', json={}, status=200) deployment = mock_data.get_deployment_sample_enriched() detectify.handle_deployment(deployment) mock_start_scan.assert_called_once() mock_start_scan.reset_mock() detectify.handle_deployment(deployment) mock_start_scan.assert_not_called()
def test_get_deployment_channels(self): deployment = mock_data.get_deployment_sample_enriched() channels = slack_util.get_deployment_channels(deployment) self.assertEqual(channels, ['#developers', '#team-studadm']) os.environ[environment.SLACK_CHANNELS] = '#team-ateam' channels = slack_util.get_deployment_channels(deployment) self.assertEqual(channels, ['#team-ateam', '#developers', '#team-studadm']) os.environ[environment.SLACK_CHANNELS] = '#team-ateam, #general' channels = slack_util.get_deployment_channels(deployment) self.assertEqual( channels, ['#team-ateam', '#general', '#developers', '#team-studadm']) os.environ[environment.SLACK_CHANNEL_OVERRIDE] = '#override' channels = slack_util.get_deployment_channels(deployment) self.assertEqual(channels, ['#override'])
def test_build_information_link(self): self.assertEqual(application_endpoint.build_information_link(mock_data.get_deployment_sample_enriched()), 'https://app.kth.se/pipeline/#kth-azure-app')
def test_call_endpoint_without_api_key(self): flottsbro.call_endpoint(flottsbro.get_add_endpoint('my-cluster'), mock_data.get_deployment_sample_enriched())
def test_should_monitor_cluster(self): os.environ[environment.UTR_CLUSTERS] = 'stage, development' deployment = mock_data.get_deployment_sample_enriched() self.assertFalse(uptimerobot.should_monitor_cluster(deployment)) os.environ[environment.UTR_CLUSTERS] = 'stage, active' self.assertTrue(uptimerobot.should_monitor_cluster(deployment))
def test_app_is_excluded(self): os.environ[environment.UTR_EXCLUDED_APPS] = 'kth-azure-app' deployment = mock_data.get_deployment_sample_enriched() self.assertTrue(uptimerobot.app_is_excluded(deployment)) os.environ[environment.UTR_EXCLUDED_APPS] = 'tamarack' self.assertFalse(uptimerobot.app_is_excluded(deployment))
def test_has_application_path(self): deployment = mock_data.get_deployment_sample_enriched() self.assertTrue(uptimerobot.has_application_path(deployment)) deployment['applicationPath'] = None self.assertFalse(uptimerobot.has_application_path(deployment))