def test_run_policy_for_event(self, add_job_mock, _0, _1, _2, _3): host = Host(DEFAULT_EVENT_QUEUE_ID, DEFAULT_EVENT_QUEUE_NAME, DEFAULT_POLICY_STORAGE) host.policies = { 'one': { 'policy': ContainerHostTest.get_mock_policy({ 'name': 'one', 'mode': { 'type': 'container-event', 'events': ['ResourceGroupWrite', 'VnetWrite'] } }) } } message = QueueMessage() message.id = 1 message.dequeue_count = 0 message.content = \ """eyAgCiAgICJzdWJqZWN0IjoiL3N1YnNjcmlwdGlvbnMvZWE5ODk3NGItNWQyYS00ZDk4LWE3OGEt MzgyZjM3MTVkMDdlL3Jlc291cmNlR3JvdXBzL3Rlc3RfY29udGFpbmVyX21vZGUiLAogICAiZXZl bnRUeXBlIjoiTWljcm9zb2Z0LlJlc291cmNlcy5SZXNvdXJjZVdyaXRlU3VjY2VzcyIsCiAgICJl dmVudFRpbWUiOiIyMDE5LTA3LTE2VDE4OjMwOjQzLjM1OTUyNTVaIiwKICAgImlkIjoiNjE5ZDI2 NzQtYjM5Ni00MzU2LTk2MTktNmM1YTUyZmU0ZTg4IiwKICAgImRhdGEiOnsgICAgICAgIAogICAg ICAiY29ycmVsYXRpb25JZCI6IjdkZDVhNDc2LWUwNTItNDBlMi05OWU0LWJiOTg1MmRjMWY4NiIs CiAgICAgICJyZXNvdXJjZVByb3ZpZGVyIjoiTWljcm9zb2Z0LlJlc291cmNlcyIsCiAgICAgICJy ZXNvdXJjZVVyaSI6Ii9zdWJzY3JpcHRpb25zL2VhOTg5NzRiLTVkMmEtNGQ5OC1hNzhhLTM4MmYz NzE1ZDA3ZS9yZXNvdXJjZUdyb3Vwcy90ZXN0X2NvbnRhaW5lcl9tb2RlIiwKICAgICAgIm9wZXJh dGlvbk5hbWUiOiJNaWNyb3NvZnQuUmVzb3VyY2VzL3N1YnNjcmlwdGlvbnMvcmVzb3VyY2VHcm91 cHMvd3JpdGUiLAogICAgICAic3RhdHVzIjoiU3VjY2VlZGVkIgogICB9LAogICAidG9waWMiOiIv c3Vic2NyaXB0aW9ucy9hYTk4OTc0Yi01ZDJhLTRkOTgtYTc4YS0zODJmMzcxNWQwN2UiCn0=""" # run with real match host.run_policies_for_event(message) add_job_mock.assert_called_with( ANY, id='one619d2674-b396-4356-9619-6c5a52fe4e88', name='one', args=ANY, misfire_grace_time=ANY) add_job_mock.reset_mock() # run with no match host.policies = {} host.run_policies_for_event(message) self.assertFalse(add_job_mock.called)
def test_poll_queue(self, run_policy_mock, storage_mock, _1, _2, _3): host = Host(DEFAULT_EVENT_QUEUE_ID, DEFAULT_EVENT_QUEUE_NAME, DEFAULT_POLICY_STORAGE) host.policies = { 'one': { 'policy': ContainerHostTest.get_mock_policy({ 'name': 'one', 'mode': { 'type': 'container-event', 'events': ['ResourceGroupWrite', 'VnetWrite'] } }) } } q1 = QueueMessage() q1.id = 1 q1.dequeue_count = 0 q1.content = """eyAgCiAgICJzdWJqZWN0IjoiL3N1YnNjcmlwdGlvbnMvZWE5ODk3NGItNWQyYS00ZDk4LWE3 OGEtMzgyZjM3MTVkMDdlL3Jlc291cmNlR3JvdXBzL3Rlc3RfY29udGFpbmVyX21vZGUiLAogICAiZXZlbnRUeXBlIj oiTWljcm9zb2Z0LlJlc291cmNlcy5SZXNvdXJjZVdyaXRlU3VjY2VzcyIsCiAgICJldmVudFRpbWUiOiIyMDE5LTA3 LTE2VDE4OjMwOjQzLjM1OTUyNTVaIiwKICAgImlkIjoiNjE5ZDI2NzQtYjM5Ni00MzU2LTk2MTktNmM1YTUyZmU0ZT g4IiwKICAgImRhdGEiOnsgICAgICAgIAogICAgICAiY29ycmVsYXRpb25JZCI6IjdkZDVhNDc2LWUwNTItNDBlMi05 OWU0LWJiOTg1MmRjMWY4NiIsCiAgICAgICJyZXNvdXJjZVByb3ZpZGVyIjoiTWljcm9zb2Z0LlJlc291cmNlcyIsCi AgICAgICJyZXNvdXJjZVVyaSI6Ii9zdWJzY3JpcHRpb25zL2VhOTg5NzRiLTVkMmEtNGQ5OC1hNzhhLTM4MmYzNzE1 ZDA3ZS9yZXNvdXJjZUdyb3Vwcy90ZXN0X2NvbnRhaW5lcl9tb2RlIiwKICAgICAgIm9wZXJhdGlvbk5hbWUiOiJNaW Nyb3NvZnQuUmVzb3VyY2VzL3N1YnNjcmlwdGlvbnMvcmVzb3VyY2VHcm91cHMvd3JpdGUiLAogICAgICAic3RhdHVz IjoiU3VjY2VlZGVkIiwKICAgfSwKICAgInRvcGljIjoiL3N1YnNjcmlwdGlvbnMvYWE5ODk3NGItNWQyYS00ZDk4LW E3OGEtMzgyZjM3MTVkMDdlIgp9""" q2 = QueueMessage() q2.id = 2 q2.dequeue_count = 0 q2.content = q1.content # Return 2 messages on first call, then none storage_mock.get_queue_messages.side_effect = [[q1, q2], []] host.poll_queue() self.assertEqual(2, run_policy_mock.call_count) run_policy_mock.reset_mock() # Return 5 messages on first call, then 2, then 0 storage_mock.get_queue_messages.side_effect = [[q1, q1, q1, q1, q1], [q1, q2], []] host.poll_queue() self.assertEqual(7, run_policy_mock.call_count) run_policy_mock.reset_mock() # High dequeue count q1.dequeue_count = 100 storage_mock.get_queue_messages.side_effect = [[q1, q2], []] host.poll_queue() self.assertEqual(1, run_policy_mock.call_count)
def test_update_event_subscriptions(self, event_filter_mock, _0, _1, _2, _3, _4): host = Host(DEFAULT_EVENT_QUEUE_ID, DEFAULT_EVENT_QUEUE_NAME, DEFAULT_POLICY_STORAGE) host.event_queue_name = 'testq' host.policies = { 'one': { 'policy': ContainerHostTest.get_mock_policy({ 'name': 'one', 'mode': { 'type': 'container-event', 'events': ['ResourceGroupWrite', 'VnetWrite'] } }) }, 'two': { 'policy': ContainerHostTest.get_mock_policy({ 'name': 'two', 'mode': { 'type': 'container-event', 'events': ['ResourceGroupWrite'] } }) }, 'three': { 'policy': ContainerHostTest.get_mock_policy({ 'name': 'three', 'mode': { 'type': 'container-event', 'events': [{ 'resourceProvider': 'Microsoft.KeyVault/vaults', 'event': 'write' }] } }) } } # Verify we get all three events with no duplicates host.update_event_subscriptions() event_filter_mock.assert_called_with( key='Data.OperationName', values={ 'Microsoft.KeyVault/vaults/write', 'Microsoft.Network/virtualNetworks/write', 'Microsoft.Resources/subscriptions/resourceGroups/write' })