Exemplo n.º 1
0
    def test_handle_resource_expiry_annotation_notification_event(
            self, mocked_create_event, mocked_utcnow):
        # Resource was created: 2019-03-11T11:05:00Z
        # Expire is set to: 2019-03-11T11:15:00Z
        # Current datetime is: 2019-03-11T11:10:09Z
        # Notification is 3 minutes: 180s. Has to notify after: 2019-03-11T11:12:00Z
        resource = MockedNamespace(
            None, {
                'metadata': {
                    'name': 'foo',
                    'annotations': {
                        'janitor/expires': '2019-03-11T11:15:00Z'
                    },
                    'creationTimestamp': '2019-03-11T11:05:00Z'
                }
            })
        delete_notification = 180
        handle_resource_on_expiry(resource, [],
                                  delete_notification,
                                  dry_run=True)

        expire = datetime.datetime.strptime('2019-03-11T11:15:00Z',
                                            '%Y-%m-%dT%H:%M:%SZ')
        formatted_expire_datetime = expire.strftime('%Y-%m-%dT%H:%M:%SZ')
        reason = 'annotation janitor/expires is set'
        message = f'{resource.kind} {resource.name} will be deleted at {formatted_expire_datetime} ({reason})'
        mocked_create_event.assert_called_with(resource,
                                               message,
                                               'DeleteNotification',
                                               dry_run=True)
Exemplo n.º 2
0
    def test_handle_resource_expiry_annotation_notification_event(
            self, mocked_create_event, mocked_utcnow):
        # Resource was created: 2019-03-11T11:05:00Z
        # Expire is set to: 2019-03-11T11:15:00Z
        # Current datetime is: 2019-03-11T11:10:09Z
        # Notification is 3 minutes: 180s. Has to notify after: 2019-03-11T11:12:00Z
        resource = MockedNamespace(
            None,
            {
                "metadata": {
                    "name": "foo",
                    "annotations": {
                        "janitor/expires": "2019-03-11T11:15:00Z"
                    },
                    "creationTimestamp": "2019-03-11T11:05:00Z",
                }
            },
        )
        delete_notification = 180
        handle_resource_on_expiry(resource, [],
                                  delete_notification,
                                  dry_run=True)

        expire = datetime.datetime.strptime("2019-03-11T11:15:00Z",
                                            "%Y-%m-%dT%H:%M:%SZ")
        formatted_expire_datetime = expire.strftime("%Y-%m-%dT%H:%M:%SZ")
        reason = "annotation janitor/expires is set"
        message = f"{resource.kind} {resource.name} will be deleted at {formatted_expire_datetime} ({reason})"
        mocked_create_event.assert_called_with(resource,
                                               message,
                                               "DeleteNotification",
                                               dry_run=True)
 def test_handle_resource_expiry_annotation_with_notification_triggered(self, mocked_add_notification_flag, mocked_utcnow):
     # Resource was created: 2019-03-11T11:05:00Z
     # Expire is set to: 2019-03-11T11:15:00Z
     # Current datetime is: 2019-03-11T11:10:09Z
     # Notification is 3 minutes: 180s. Has to notify after: 2019-03-11T11:12:00Z
     resource = Namespace(None, {'metadata': {'name': 'foo', 'annotations': {
                          'janitor/expires': '2019-03-11T11:15:00Z'}, 'creationTimestamp': '2019-03-11T11:05:00Z'}})
     delete_notification = 180
     handle_resource_on_expiry(resource, [], delete_notification, dry_run=True)
     mocked_add_notification_flag.assert_called()
Exemplo n.º 4
0
def test_handle_resource_no_expiry():
    resource = Namespace(None, {'metadata': {'name': 'foo'}})
    counter = handle_resource_on_expiry(resource, [],
                                        None,
                                        dry_run=True,
                                        tiller=None)
    assert counter == {}
Exemplo n.º 5
0
def test_handle_resource_no_expiry():
    resource = Namespace(None, {"metadata": {"name": "foo"}})
    counter = handle_resource_on_expiry(resource, [],
                                        None,
                                        wait_after_delete=0,
                                        dry_run=True)
    assert counter == {}
Exemplo n.º 6
0
def test_handle_resource_expiry_expired():
    resource = Namespace(
        None, {
            'metadata': {
                'name': 'foo',
                'annotations': {
                    'janitor/expires': '2001-09-26T01:51:42Z'
                }
            }
        })
    counter = handle_resource_on_expiry(resource, [], None, dry_run=True)
    assert counter == {'namespaces-with-expiry': 1, 'namespaces-deleted': 1}
Exemplo n.º 7
0
def test_handle_resource_expiry_expired():
    resource = Namespace(
        None,
        {
            "metadata": {
                "name": "foo",
                "annotations": {"janitor/expires": "2001-09-26T01:51:42Z"},
            }
        },
    )
    counter = handle_resource_on_expiry(resource, [], None, dry_run=True)
    assert counter == {"namespaces-with-expiry": 1, "namespaces-deleted": 1}
Exemplo n.º 8
0
def test_handle_resource_expiry_annotation():
    # TTL is far in the future
    resource = Namespace(
        None, {
            'metadata': {
                'name': 'foo',
                'annotations': {
                    'janitor/expires': '2050-09-26T01:51:42Z'
                }
            }
        })
    counter = handle_resource_on_expiry(resource, [], None, dry_run=True)
    assert counter == {'namespaces-with-expiry': 1}
Exemplo n.º 9
0
def test_handle_resource_expiry_annotation():
    # TTL is far in the future
    resource = Namespace(
        None,
        {
            "metadata": {
                "name": "foo",
                "annotations": {"janitor/expires": "2050-09-26T01:51:42Z"},
            }
        },
    )
    counter = handle_resource_on_expiry(resource, [], None, dry_run=True)
    assert counter == {"namespaces-with-expiry": 1}
 def test_handle_resource_expiry_annotation_with_notification_triggered(
     self, mocked_add_notification_flag, mocked_utcnow
 ):
     # Resource was created: 2019-03-11T11:05:00Z
     # Expire is set to: 2019-03-11T11:15:00Z
     # Current datetime is: 2019-03-11T11:10:09Z
     # Notification is 3 minutes: 180s. Has to notify after: 2019-03-11T11:12:00Z
     resource = Namespace(
         None,
         {
             "metadata": {
                 "name": "foo",
                 "annotations": {"janitor/expires": "2019-03-11T11:15:00Z"},
                 "creationTimestamp": "2019-03-11T11:05:00Z",
             }
         },
     )
     delete_notification = 180
     handle_resource_on_expiry(
         resource, [], delete_notification, wait_after_delete=0, dry_run=True
     )
     mocked_add_notification_flag.assert_called()
Exemplo n.º 11
0
def test_handle_resource_no_expiry():
    resource = Namespace(None, {'metadata': {'name': 'foo'}})
    counter = handle_resource_on_expiry(resource, [], dry_run=True)
    assert counter == {'resources-processed-expiry': 1}