Skip to content
Permalink
Browse files

tests: add tests for `_prune_droplet_snapshots`

  • Loading branch information...
etienne-napoleone committed Dec 20, 2018
1 parent 4d680c2 commit ca35c7464cf9397d8a9920d39dc5d30c37c36cb4
Showing with 47 additions and 10 deletions.
  1. +17 −1 tests/mock.py
  2. +30 −9 tests/test_goutte.py
@@ -1,11 +1,21 @@
class Snapshot:
def __init__(self, created_at=None, name=None):
def __init__(self, created_at=None, name=None, id=None):
self.created_at = created_at
self.name = name
self.id = id

def destroy(self):
pass

@staticmethod
def get_object(api_token=None, snapshot_id=None):
if snapshot_id == '1337':
return Snapshot(name=f'snapshot{snapshot_id}', id=snapshot_id,
created_at=f'{snapshot_id}')
else:
return Snapshot(name=f'goutte-snapshot{snapshot_id}',
id=snapshot_id, created_at=f'{snapshot_id}')


class Volume:
def __init__(self, name=None, snapshots=None, throw=None):
@@ -23,6 +33,12 @@ def snapshot(self, name):
raise self.throw


class Droplet:
def __init__(self, name=None, snapshot_ids=None):
self.name = name
self.snapshot_ids = snapshot_ids


class Manager:
def __init__(self, token=None):
self.token = token
@@ -9,12 +9,36 @@ def test_version():
assert __version__ == '1.0.0'


def test_get_volumes(caplog, monkeypatch):
def test_prune_droplet_snapshots(caplog, monkeypatch):
monkeypatch.setattr(digitalocean, 'Snapshot', mock.Snapshot)
droplet = mock.Droplet(name='testdroplet', snapshot_ids=['3', '2', '1'])
with caplog.at_level('INFO'):
main._prune_droplet_snapshots(droplet, 1)
assert len(caplog.records) == 2
for record in caplog.records:
assert record.levelname == 'INFO'
assert "goutte-snapshot3" not in record.message


def test_prune_droplet_snapshots_goutte_prefix_only(caplog, monkeypatch):
monkeypatch.setattr(digitalocean, 'Snapshot', mock.Snapshot)
droplet = mock.Droplet(name='testdroplet', snapshot_ids=['1337', '2', '1'])
with caplog.at_level('INFO'):
main._prune_droplet_snapshots(droplet, 1)
assert len(caplog.records) == 1
for record in caplog.records:
assert record.levelname == 'INFO'
assert "snapshot1337" not in record.message
assert "goutte-snapshot2" not in record.message


def test_get_volumes(caplog):
exceptions = [
digitalocean.baseapi.TokenError,
digitalocean.baseapi.DataReadError,
digitalocean.baseapi.JSONReadError,
digitalocean.baseapi.NotFoundError
digitalocean.baseapi.NotFoundError,
Exception,
]
for exception in exceptions:
volume = mock.Volume(name='testvol', throw=exception)
@@ -25,11 +49,6 @@ def test_get_volumes(caplog, monkeypatch):
caplog.clear()


def test_get_volumes_raise(monkeypatch):
monkeypatch.setattr(digitalocean, 'Manager', mock.Manager)
assert 'testvol' in main._get_volumes(['testvol'])[0].name


def test_snapshot_volume(caplog):
volume = mock.Volume('testvol')
with caplog.at_level('INFO'):
@@ -44,7 +63,8 @@ def test_snapshot_volume_raise(caplog):
digitalocean.baseapi.TokenError,
digitalocean.baseapi.DataReadError,
digitalocean.baseapi.JSONReadError,
digitalocean.baseapi.NotFoundError
digitalocean.baseapi.NotFoundError,
Exception,
]
for exception in exceptions:
volume = mock.Volume(name='testvol', throw=exception)
@@ -84,7 +104,8 @@ def test_prune_volume_snapshots_raise(caplog):
digitalocean.baseapi.TokenError,
digitalocean.baseapi.DataReadError,
digitalocean.baseapi.JSONReadError,
digitalocean.baseapi.NotFoundError
digitalocean.baseapi.NotFoundError,
Exception,
]
for exception in exceptions:
volume = mock.Volume(name='testvol', throw=exception)

0 comments on commit ca35c74

Please sign in to comment.
You can’t perform that action at this time.