コード例 #1
0
    def test_stackdriver_upsert_alert_policy(self, mock_channel_client,
                                             mock_policy_client,
                                             mock_get_creds_and_project_id):
        hook = stackdriver.StackdriverHook()
        existing_alert_policy = ParseDict(
            TEST_ALERT_POLICY_1, monitoring_v3.types.alert_pb2.AlertPolicy())
        alert_policy_to_create = ParseDict(
            TEST_ALERT_POLICY_2, monitoring_v3.types.alert_pb2.AlertPolicy())

        mock_policy_client.return_value.list_alert_policies.return_value = [
            existing_alert_policy
        ]
        mock_channel_client.return_value.list_notification_channels.return_value = []

        hook.upsert_alert(
            alerts=json.dumps({
                "policies": [TEST_ALERT_POLICY_1, TEST_ALERT_POLICY_2],
                "channels": []
            }),
            project_id=PROJECT_ID,
        )
        mock_channel_client.return_value.list_notification_channels.assert_called_once_with(
            name='projects/{project}'.format(project=PROJECT_ID),
            filter_=None,
            retry=DEFAULT,
            timeout=DEFAULT,
            order_by=None,
            page_size=None,
            metadata=None,
        )
        mock_policy_client.return_value.list_alert_policies.assert_called_once_with(
            name='projects/{project}'.format(project=PROJECT_ID),
            filter_=None,
            retry=DEFAULT,
            timeout=DEFAULT,
            order_by=None,
            page_size=None,
            metadata=None,
        )
        alert_policy_to_create.ClearField('name')
        alert_policy_to_create.ClearField('creation_record')
        alert_policy_to_create.ClearField('mutation_record')
        alert_policy_to_create.conditions[0].ClearField('name')  # pylint: disable=no-member
        mock_policy_client.return_value.create_alert_policy.assert_called_once_with(
            name='projects/{project}'.format(project=PROJECT_ID),
            alert_policy=alert_policy_to_create,
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=None,
        )
        existing_alert_policy.ClearField('creation_record')
        existing_alert_policy.ClearField('mutation_record')
        mock_policy_client.return_value.update_alert_policy.assert_called_once_with(
            alert_policy=existing_alert_policy,
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=None)
コード例 #2
0
 def test_stackdriver_upsert_channel(self, mock_channel_client, mock_get_creds_and_project_id):
     hook = stackdriver.StackdriverHook()
     existing_notification_channel = ParseDict(
         TEST_NOTIFICATION_CHANNEL_1, monitoring_v3.types.notification_pb2.NotificationChannel()
     )
     notification_channel_to_be_created = ParseDict(
         TEST_NOTIFICATION_CHANNEL_2, monitoring_v3.types.notification_pb2.NotificationChannel()
     )
     mock_channel_client.return_value.list_notification_channels.return_value = [
         existing_notification_channel
     ]
     hook.upsert_channel(
         channels=json.dumps({"channels": [TEST_NOTIFICATION_CHANNEL_1, TEST_NOTIFICATION_CHANNEL_2]}),
         project_id=PROJECT_ID,
     )
     mock_channel_client.return_value.list_notification_channels.assert_called_once_with(
         name='projects/{project}'.format(project=PROJECT_ID),
         filter_=None,
         order_by=None,
         page_size=None,
         retry=DEFAULT,
         timeout=DEFAULT,
         metadata=None,
     )
     mock_channel_client.return_value.update_notification_channel.assert_called_once_with(
         notification_channel=existing_notification_channel, retry=DEFAULT, timeout=DEFAULT, metadata=None
     )
     notification_channel_to_be_created.ClearField('name')
     mock_channel_client.return_value.create_notification_channel.assert_called_once_with(
         name='projects/{project}'.format(project=PROJECT_ID),
         notification_channel=notification_channel_to_be_created,
         retry=DEFAULT,
         timeout=DEFAULT,
         metadata=None,
     )
コード例 #3
0
    def test_stackdriver_upsert_alert_policy_without_channel(
            self, mock_channel_client, mock_policy_client,
            mock_get_creds_and_project_id):
        hook = stackdriver.StackdriverHook()
        existing_alert_policy = ParseDict(
            TEST_ALERT_POLICY_1, monitoring_v3.types.alert_pb2.AlertPolicy())

        mock_policy_client.return_value.list_alert_policies.return_value = [
            existing_alert_policy
        ]
        mock_channel_client.return_value.list_notification_channels.return_value = []

        hook.upsert_alert(
            alerts=json.dumps(
                {"policies": [TEST_ALERT_POLICY_1, TEST_ALERT_POLICY_2]}),
            project_id=PROJECT_ID,
        )
        mock_channel_client.return_value.list_notification_channels.assert_called_once_with(
            name=f'projects/{PROJECT_ID}',
            filter_=None,
            retry=DEFAULT,
            timeout=DEFAULT,
            order_by=None,
            page_size=None,
            metadata=None,
        )
        mock_policy_client.return_value.list_alert_policies.assert_called_once_with(
            name=f'projects/{PROJECT_ID}',
            filter_=None,
            retry=DEFAULT,
            timeout=DEFAULT,
            order_by=None,
            page_size=None,
            metadata=None,
        )

        existing_alert_policy.ClearField('creation_record')
        existing_alert_policy.ClearField('mutation_record')
        mock_policy_client.return_value.update_alert_policy.assert_called_once_with(
            alert_policy=existing_alert_policy,
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=None)