예제 #1
0
    def test_stackdriver_disable_notification_channel(
        self, mock_channel_client, mock_get_creds_and_project_id
    ):
        hook = stackdriver.StackdriverHook()
        notification_channel_enabled = ParseDict(
            TEST_NOTIFICATION_CHANNEL_1, monitoring_v3.types.notification_pb2.NotificationChannel()
        )
        notification_channel_disabled = ParseDict(
            TEST_NOTIFICATION_CHANNEL_2, monitoring_v3.types.notification_pb2.NotificationChannel()
        )
        mock_channel_client.return_value.list_notification_channels.return_value = [
            notification_channel_enabled,
            notification_channel_disabled,
        ]

        hook.disable_notification_channels(
            filter_=TEST_FILTER,
            project_id=PROJECT_ID,
        )

        notification_channel_enabled.enabled.value = False  # pylint: disable=no-member
        mask = monitoring_v3.types.field_mask_pb2.FieldMask()
        mask.paths.append('enabled')  # pylint: disable=no-member
        mock_channel_client.return_value.update_notification_channel.assert_called_once_with(
            notification_channel=notification_channel_enabled,
            update_mask=mask,
            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(self, mock_channel_client,
                                             mock_policy_client,
                                             mock_get_creds_and_project_id):
        hook = stackdriver.StackdriverHook()
        existing_alert_policy = AlertPolicy(**TEST_ALERT_POLICY_1)
        alert_policy_to_create = AlertPolicy(**TEST_ALERT_POLICY_2)

        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(
            request=dict(
                name=f'projects/{PROJECT_ID}',
                filter=None,
                order_by=None,
                page_size=None,
            ),
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=(),
        )
        mock_policy_client.return_value.list_alert_policies.assert_called_once_with(
            request=dict(name=f'projects/{PROJECT_ID}',
                         filter=None,
                         order_by=None,
                         page_size=None),
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=(),
        )
        alert_policy_to_create.name = None
        alert_policy_to_create.creation_record = None
        alert_policy_to_create.mutation_record = None
        # pylint: disable=unsubscriptable-object
        alert_policy_to_create.conditions[0].name = None
        mock_policy_client.return_value.create_alert_policy.assert_called_once_with(
            request=dict(
                name=f'projects/{PROJECT_ID}',
                alert_policy=alert_policy_to_create,
            ),
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=(),
        )
        existing_alert_policy.creation_record = None
        existing_alert_policy.mutation_record = None
        mock_policy_client.return_value.update_alert_policy.assert_called_once_with(
            request=dict(alert_policy=existing_alert_policy),
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=())
예제 #4
0
    def test_stackdriver_disable_alert_policy(self, mock_policy_client, mock_get_creds_and_project_id):
        hook = stackdriver.StackdriverHook()
        alert_policy_enabled = ParseDict(TEST_ALERT_POLICY_1, monitoring_v3.types.alert_pb2.AlertPolicy())
        alert_policy_disabled = ParseDict(TEST_ALERT_POLICY_2, monitoring_v3.types.alert_pb2.AlertPolicy())

        mock_policy_client.return_value.list_alert_policies.return_value = [
            alert_policy_enabled,
            alert_policy_disabled,
        ]
        hook.disable_alert_policies(
            filter_=TEST_FILTER,
            project_id=PROJECT_ID,
        )
        mock_policy_client.return_value.list_alert_policies.assert_called_once_with(
            name='projects/{project}'.format(project=PROJECT_ID),
            filter_=TEST_FILTER,
            retry=DEFAULT,
            timeout=DEFAULT,
            order_by=None,
            page_size=None,
            metadata=None,
        )
        mask = monitoring_v3.types.field_mask_pb2.FieldMask()
        alert_policy_enabled.enabled.value = False  # pylint: disable=no-member
        mask.paths.append('enabled')  # pylint: disable=no-member
        mock_policy_client.return_value.update_alert_policy.assert_called_once_with(
            alert_policy=alert_policy_enabled,
            update_mask=mask,
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=None,
        )
예제 #5
0
    def test_stackdriver_disable_notification_channel(
            self, mock_channel_client, mock_get_creds_and_project_id):
        hook = stackdriver.StackdriverHook()
        notification_channel_enabled = NotificationChannel(
            **TEST_NOTIFICATION_CHANNEL_1)
        notification_channel_disabled = NotificationChannel(
            **TEST_NOTIFICATION_CHANNEL_2)
        mock_channel_client.return_value.list_notification_channels.return_value = [
            notification_channel_enabled,
            notification_channel_disabled,
        ]

        hook.disable_notification_channels(
            filter_=TEST_FILTER,
            project_id=PROJECT_ID,
        )

        notification_channel_enabled.enabled = False  # pylint: disable=no-member
        mask = FieldMask(paths=['enabled'])
        mock_channel_client.return_value.update_notification_channel.assert_called_once_with(
            request=dict(notification_channel=notification_channel_enabled,
                         update_mask=mask),
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=(),
        )
예제 #6
0
    def test_stackdriver_disable_alert_policy(self, mock_policy_client,
                                              mock_get_creds_and_project_id):
        hook = stackdriver.StackdriverHook()
        alert_policy_enabled = AlertPolicy(**TEST_ALERT_POLICY_1)
        alert_policy_disabled = AlertPolicy(**TEST_ALERT_POLICY_2)

        mock_policy_client.return_value.list_alert_policies.return_value = [
            alert_policy_enabled,
            alert_policy_disabled,
        ]
        hook.disable_alert_policies(
            filter_=TEST_FILTER,
            project_id=PROJECT_ID,
        )
        mock_policy_client.return_value.list_alert_policies.assert_called_once_with(
            request=dict(name=f'projects/{PROJECT_ID}',
                         filter=TEST_FILTER,
                         order_by=None,
                         page_size=None),
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=(),
        )
        mask = FieldMask(paths=["enabled"])
        alert_policy_enabled.enabled = False  # pylint: disable=no-member
        mock_policy_client.return_value.update_alert_policy.assert_called_once_with(
            request=dict(alert_policy=alert_policy_enabled, update_mask=mask),
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=(),
        )
예제 #7
0
 def test_stackdriver_delete_notification_channel(
     self, mock_channel_client, mock_get_creds_and_project_id
 ):
     hook = stackdriver.StackdriverHook()
     hook.delete_notification_channel(name='test-channel',)
     mock_channel_client.return_value.delete_notification_channel.assert_called_once_with(
         name='test-channel', retry=DEFAULT, timeout=DEFAULT, metadata=None
     )
예제 #8
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)
예제 #9
0
 def test_stackdriver_delete_alert_policy(self, mock_policy_client,
                                          mock_get_creds_and_project_id):
     hook = stackdriver.StackdriverHook()
     hook.delete_alert_policy(name='test-alert', )
     mock_policy_client.return_value.delete_alert_policy.assert_called_once_with(
         request=dict(name='test-alert'),
         retry=DEFAULT,
         timeout=DEFAULT,
         metadata=(),
     )
예제 #10
0
 def test_stackdriver_list_notification_channel(
         self, mock_channel_client, mock_get_creds_and_project_id):
     hook = stackdriver.StackdriverHook()
     hook.list_notification_channels(filter_=TEST_FILTER)
     mock_channel_client.return_value.list_notification_channels.assert_called_once_with(
         name='projects/{project}'.format(project=PROJECT_ID),
         filter_=TEST_FILTER,
         order_by=None,
         page_size=None,
         retry=DEFAULT,
         timeout=DEFAULT,
         metadata=None)
예제 #11
0
 def test_stackdriver_list_alert_policies(self, mock_policy_client,
                                          mock_get_creds_and_project_id):
     method = mock_policy_client.return_value.list_alert_policies
     hook = stackdriver.StackdriverHook()
     hook.list_alert_policies(filter_=TEST_FILTER)
     method.assert_called_once_with(
         name='projects/{project}'.format(project=PROJECT_ID),
         filter_=TEST_FILTER,
         retry=DEFAULT,
         timeout=DEFAULT,
         order_by=None,
         page_size=None,
         metadata=None)
예제 #12
0
 def test_stackdriver_list_notification_channel(
         self, mock_channel_client, mock_get_creds_and_project_id):
     hook = stackdriver.StackdriverHook()
     hook.list_notification_channels(
         filter_=TEST_FILTER,
         project_id=PROJECT_ID,
     )
     mock_channel_client.return_value.list_notification_channels.assert_called_once_with(
         request=dict(name=f'projects/{PROJECT_ID}',
                      filter=TEST_FILTER,
                      order_by=None,
                      page_size=None),
         retry=DEFAULT,
         timeout=DEFAULT,
         metadata=(),
     )
예제 #13
0
 def test_stackdriver_list_alert_policies(self, mock_policy_client,
                                          mock_get_creds_and_project_id):
     method = mock_policy_client.return_value.list_alert_policies
     hook = stackdriver.StackdriverHook()
     hook.list_alert_policies(
         filter_=TEST_FILTER,
         project_id=PROJECT_ID,
     )
     method.assert_called_once_with(
         request=dict(name=f'projects/{PROJECT_ID}',
                      filter=TEST_FILTER,
                      order_by=None,
                      page_size=None),
         retry=DEFAULT,
         timeout=DEFAULT,
         metadata=(),
     )
예제 #14
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)
예제 #15
0
    def test_stackdriver_upsert_channel(self, mock_channel_client,
                                        mock_get_creds_and_project_id):
        hook = stackdriver.StackdriverHook()
        existing_notification_channel = NotificationChannel(
            **TEST_NOTIFICATION_CHANNEL_1)
        notification_channel_to_be_created = NotificationChannel(
            **TEST_NOTIFICATION_CHANNEL_2)

        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(
            request=dict(name=f'projects/{PROJECT_ID}',
                         filter=None,
                         order_by=None,
                         page_size=None),
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=(),
        )
        mock_channel_client.return_value.update_notification_channel.assert_called_once_with(
            request=dict(notification_channel=existing_notification_channel),
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=(),
        )
        notification_channel_to_be_created.name = None
        mock_channel_client.return_value.create_notification_channel.assert_called_once_with(
            request=dict(
                name=f'projects/{PROJECT_ID}',
                notification_channel=notification_channel_to_be_created),
            retry=DEFAULT,
            timeout=DEFAULT,
            metadata=(),
        )