Ejemplo n.º 1
0
 def test_handle_resource_ttl_annotation_notification_event(
         self, mocked_create_event, mocked_utcnow):
     # Resource was created: 2019-03-11T11:05:00Z
     # ttl is 10 minutes, so it will expire: 2019-03-11T11:15:00Z
     # Current datetime is: 2019-03-11T11:13:09Z
     # Notification is 3 minutes: 180s. Has to notify after: 2019-03-11T11:12:00Z
     resource = MockedNamespace(
         None,
         {
             "metadata": {
                 "name": "foo",
                 "annotations": {
                     "janitor/ttl": "10m"
                 },
                 "creationTimestamp": "2019-03-11T11:05:00Z",
             }
         },
     )
     delete_notification = 180
     handle_resource_on_ttl(
         resource,
         [],
         delete_notification,
         deployment_time_annotation=None,
         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/ttl 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)
Ejemplo n.º 2
0
 def test_handle_resource_ttl_annotation_with_notification_not_triggered(
         self, mocked_add_notification_flag, mocked_utcnow):
     # Resource was created: 2019-03-11T11:05:00Z
     # ttl is 10 minutes, so it will expire: 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/ttl": "10m"
                 },
                 "creationTimestamp": "2019-03-11T11:05:00Z",
             }
         },
     )
     delete_notification = 180
     handle_resource_on_ttl(
         resource,
         [],
         delete_notification,
         deployment_time_annotation=None,
         dry_run=True,
     )
     mocked_add_notification_flag.assert_not_called()
Ejemplo n.º 3
0
 def test_handle_resource_ttl_annotation_notification_event(
         self, mocked_create_event, mocked_utcnow):
     # Resource was created: 2019-03-11T11:05:00Z
     # ttl is 10 minutes, so it will expire: 2019-03-11T11:15:00Z
     # Current datetime is: 2019-03-11T11:13:09Z
     # Notification is 3 minutes: 180s. Has to notify after: 2019-03-11T11:12:00Z
     resource = MockedNamespace(
         None, {
             'metadata': {
                 'name': 'foo',
                 'annotations': {
                     'janitor/ttl': '10m'
                 },
                 'creationTimestamp': '2019-03-11T11:05:00Z'
             }
         })
     delete_notification = 180
     handle_resource_on_ttl(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/ttl 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_ttl_annotation_with_notification_triggered(self, mocked_add_notification_flag, mocked_utcnow):
     # Resource was created: 2019-03-11T11:05:00Z
     # ttl is 10 minutes, so it will expire: 2019-03-11T11:15:00Z
     # Current datetime is: 2019-03-11T11:13:09Z
     # Notification is 3 minutes: 180s. Has to notify after: 2019-03-11T11:12:00Z
     resource = Namespace(None, {'metadata': {'name': 'foo', 'annotations': {'janitor/ttl': '10m'}, 'creationTimestamp': '2019-03-11T11:05:00Z'}})
     delete_notification = 180
     handle_resource_on_ttl(resource, [], delete_notification, dry_run=True)
     mocked_add_notification_flag.assert_called()
Ejemplo n.º 5
0
def test_handle_resource_no_ttl():
    resource = Namespace(None, {'metadata': {'name': 'foo'}})
    counter = handle_resource_on_ttl(resource, [],
                                     None,
                                     dry_run=True,
                                     tiller=None)
    assert counter == {'resources-processed': 1}
Ejemplo n.º 6
0
def test_handle_resource_no_ttl():
    resource = Namespace(None, {"metadata": {"name": "foo"}})
    counter = handle_resource_on_ttl(resource, [],
                                     None,
                                     deployment_time_annotation=None,
                                     dry_run=True)
    assert counter == {"resources-processed": 1}
Ejemplo n.º 7
0
def test_handle_resource_deployment_time_both_expired():
    # both creation time + TTL and deployment time + TTL are in the past
    resource = Namespace(
        None,
        {
            "metadata": {
                "name": "foo",
                "annotations": {
                    "janitor/ttl": "1w",
                    "deploymentTimestamp": "2019-03-02T11:13:09Z",
                },
                "creationTimestamp": "2019-03-01T11:13:09Z",
            }
        },
    )
    counter = handle_resource_on_ttl(
        resource, [],
        0,
        deployment_time_annotation="deploymentTimestamp",
        dry_run=True)
    assert counter == {
        "resources-processed": 1,
        "namespaces-with-ttl": 1,
        "namespaces-deleted": 1,
    }
Ejemplo n.º 8
0
 def test_handle_resource_ttl_annotation_with_forever_value_not_triggered(
         self, mocked_add_notification_flag, mocked_utcnow):
     # Resource was created: 2019-03-11T11:05:00Z
     # ttl is `forever`, so it will not expire
     # Current datetime is: 2019-03-11T11:13:09Z
     # Notification is 3 minutes: 180s. Has to notify after: 2019-03-11T11:12:00Z
     resource = Namespace(
         None,
         {
             "metadata": {
                 "name": "foo",
                 "annotations": {
                     "janitor/ttl": "forever"
                 },
                 "creationTimestamp": "2019-03-11T11:05:00Z",
             }
         },
     )
     delete_notification = 180
     counter = handle_resource_on_ttl(
         resource,
         [],
         delete_notification,
         deployment_time_annotation=None,
         dry_run=True,
     )
     self.assertEqual(1, counter["resources-processed"])
     self.assertEqual(1, len(counter))
     mocked_add_notification_flag.assert_not_called()
 def test_handle_resource_ttl_annotation_with_forever_value_not_triggered(self, mocked_add_notification_flag, mocked_utcnow):
     # Resource was created: 2019-03-11T11:05:00Z
     # ttl is `forever`, so it will not expire
     # Current datetime is: 2019-03-11T11:13:09Z
     # Notification is 3 minutes: 180s. Has to notify after: 2019-03-11T11:12:00Z
     resource = Namespace(None, {'metadata': {'name': 'foo', 'annotations': {'janitor/ttl': 'forever'}, 'creationTimestamp': '2019-03-11T11:05:00Z'}})
     delete_notification = 180
     counter = handle_resource_on_ttl(resource, [], delete_notification, dry_run=True)
     self.assertEqual(1, counter['resources-processed'])
     self.assertEqual(1, len(counter))
     mocked_add_notification_flag.assert_not_called()
Ejemplo n.º 10
0
def test_handle_resource_ttl_annotation():
    # TTL is far in the future
    resource = Namespace(
        None, {
            'metadata': {
                'name': 'foo',
                'annotations': {
                    'janitor/ttl': '999w'
                },
                'creationTimestamp': '2019-01-17T20:59:12Z'
            }
        })
    counter = handle_resource_on_ttl(resource, [], dry_run=True)
    assert counter == {'resources-processed': 1, 'namespaces-with-ttl': 1}
Ejemplo n.º 11
0
def test_handle_resource_ttl_annotation():
    # TTL is in the future
    resource = Namespace(
        None,
        {
            "metadata": {
                "name": "foo",
                "annotations": {"janitor/ttl": "2w"},
                "creationTimestamp": "2019-03-01T11:13:09Z",
            }
        },
    )
    counter = handle_resource_on_ttl(
        resource, [], 0, deployment_time_annotation=None, dry_run=True
    )
    assert counter == {"resources-processed": 1, "namespaces-with-ttl": 1}
Ejemplo n.º 12
0
def test_handle_resource_ttl_annotation():
    # TTL is far in the future
    resource = Namespace(
        None,
        {
            "metadata": {
                "name": "foo",
                "annotations": {
                    "janitor/ttl": "999w"
                },
                "creationTimestamp": "2019-01-17T20:59:12Z",
            }
        },
    )
    counter = handle_resource_on_ttl(resource, [], None, dry_run=True)
    assert counter == {"resources-processed": 1, "namespaces-with-ttl": 1}
Ejemplo n.º 13
0
def test_handle_resource_ttl_expired():
    resource = Namespace(
        None, {
            'metadata': {
                'name': 'foo',
                'annotations': {
                    'janitor/ttl': '1s'
                },
                'creationTimestamp': '2019-01-17T20:59:12Z'
            }
        })
    counter = handle_resource_on_ttl(resource, [], dry_run=True)
    assert counter == {
        'resources-processed': 1,
        'namespaces-with-ttl': 1,
        'namespaces-deleted': 1
    }
Ejemplo n.º 14
0
def test_handle_resource_ttl_expired():
    resource = Namespace(
        None,
        {
            "metadata": {
                "name": "foo",
                "annotations": {"janitor/ttl": "1s"},
                "creationTimestamp": "2019-01-17T20:59:12Z",
            }
        },
    )
    counter = handle_resource_on_ttl(
        resource, [], None, deployment_time_annotation=None, dry_run=True
    )
    assert counter == {
        "resources-processed": 1,
        "namespaces-with-ttl": 1,
        "namespaces-deleted": 1,
    }
Ejemplo n.º 15
0
def test_handle_resource_deployment_time_no_expiry():
    # creation time + TTL is in the past, but deployment time + TTL is in the future
    resource = Namespace(
        None,
        {
            "metadata": {
                "name": "foo",
                "annotations": {
                    "janitor/ttl": "1w",
                    "deploymentTimestamp": "2019-03-10T11:13:09Z",
                },
                "creationTimestamp": "2019-03-01T11:13:09Z",
            }
        },
    )
    counter = handle_resource_on_ttl(
        resource, [], 0, deployment_time_annotation="deploymentTimestamp", dry_run=True
    )
    assert counter == {
        "resources-processed": 1,
        "namespaces-with-ttl": 1,
    }