flake8 now automated (#5)

* flake8 now automated
* 120 chars max, auto-reformat intellij
This commit is contained in:
Marx314 2016-04-15 07:49:15 -04:00 committed by Maxime Belanger
parent ca830c05b2
commit b013e96aff
8 changed files with 78 additions and 56 deletions

View File

@ -14,7 +14,6 @@
class Entity(object): class Entity(object):
def __init__(self, entity_id, project_id, start, end, last_event, name, entity_type): def __init__(self, entity_id, project_id, start, end, last_event, name, entity_type):
self.entity_id = entity_id self.entity_id = entity_id
self.project_id = project_id self.project_id = project_id
@ -28,13 +27,13 @@ class Entity(object):
return todict(self) return todict(self)
def __eq__(self, other): def __eq__(self, other):
return (other.entity_id == self.entity_id return (other.entity_id == self.entity_id and
and other.project_id == self.project_id other.project_id == self.project_id and
and other.start == self.start other.start == self.start and
and other.end == self.end other.end == self.end and
and other.last_event == self.last_event other.last_event == self.last_event and
and other.name == self.name other.name == self.name and
and other.entity_type == self.entity_type) other.entity_type == self.entity_type)
class Instance(Entity): class Instance(Entity):
@ -47,43 +46,42 @@ class Instance(Entity):
self.os = OS(**os) self.os = OS(**os)
def __eq__(self, other): def __eq__(self, other):
return (super(Instance, self).__eq__(other) return (super(Instance, self).__eq__(other) and
and other.flavor == self.flavor other.flavor == self.flavor and
and other.os == self.os other.os == self.os and
and other.metadata == self.metadata) other.metadata == self.metadata)
class OS(object): class OS(object):
def __init__(self, os_type, distro, version): def __init__(self, os_type, distro, version):
self.os_type = os_type self.os_type = os_type
self.distro = distro self.distro = distro
self.version = version self.version = version
def __eq__(self, other): def __eq__(self, other):
return (other.os_type == self.os_type return (other.os_type == self.os_type and
and other.distro == self.distro other.distro == self.distro and
and other.version == self.version) other.version == self.version)
class Volume(Entity): class Volume(Entity):
TYPE = "volume" TYPE = "volume"
def __init__(self, entity_id, project_id, start, end, volume_type, size, last_event, name, attached_to=None, entity_type=TYPE): def __init__(self, entity_id, project_id, start, end, volume_type, size, last_event, name, attached_to=None,
entity_type=TYPE):
super(Volume, self).__init__(entity_id, project_id, start, end, last_event, name, entity_type) super(Volume, self).__init__(entity_id, project_id, start, end, last_event, name, entity_type)
self.volume_type = volume_type self.volume_type = volume_type
self.size = size self.size = size
self.attached_to = attached_to or [] self.attached_to = attached_to or []
def __eq__(self, other): def __eq__(self, other):
return (super(Volume, self).__eq__(other) return (super(Volume, self).__eq__(other) and
and other.volume_type == self.volume_type other.volume_type == self.volume_type and
and other.size == self.size other.size == self.size and
and other.attached_to == self.attached_to) other.attached_to == self.attached_to)
class VolumeType(object): class VolumeType(object):
def __init__(self, volume_type_id, volume_type_name): def __init__(self, volume_type_id, volume_type_name):
self.volume_type_id = volume_type_id self.volume_type_id = volume_type_id
self.volume_type_name = volume_type_name self.volume_type_name = volume_type_name

View File

@ -15,8 +15,6 @@
import logging import logging
import pkg_resources import pkg_resources
from logging import config
def get_config_file(): def get_config_file():
logging_conf = pkg_resources.resource_filename("almanach", "resources/config/logging.cfg") logging_conf = pkg_resources.resource_filename("almanach", "resources/config/logging.cfg")

View File

@ -12,8 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
#!/usr/bin/env python
from setuptools import setup from setuptools import setup
setup( setup(

View File

@ -7,3 +7,4 @@ nose-blockage==0.1.2
flexmock==0.9.4 flexmock==0.9.4
mongomock==2.0.0 mongomock==2.0.0
PyHamcrest==1.8.1 PyHamcrest==1.8.1
flake8==2.5.4

View File

@ -13,13 +13,12 @@
# limitations under the License. # limitations under the License.
import unittest import unittest
import mongomock
from datetime import datetime from datetime import datetime
import mongomock
from flexmock import flexmock, flexmock_teardown from flexmock import flexmock, flexmock_teardown
from hamcrest import assert_that, contains_inanyorder from hamcrest import assert_that, contains_inanyorder
from pymongo import MongoClient from pymongo import MongoClient
from almanach.adapters.database_adapter import DatabaseAdapter from almanach.adapters.database_adapter import DatabaseAdapter
from almanach.common.volume_type_not_found_exception import VolumeTypeNotFoundException from almanach.common.volume_type_not_found_exception import VolumeTypeNotFoundException
from almanach.common.almanach_exception import AlmanachException from almanach.common.almanach_exception import AlmanachException
@ -29,7 +28,6 @@ from tests.builder import a, instance, volume, volume_type
class DatabaseAdapterTest(unittest.TestCase): class DatabaseAdapterTest(unittest.TestCase):
def setUp(self): def setUp(self):
config.read(config_file="resources/config/test.cfg") config.read(config_file="resources/config/test.cfg")
mongo_connection = mongomock.Connection() mongo_connection = mongomock.Connection()
@ -166,7 +164,8 @@ class DatabaseAdapterTest(unittest.TestCase):
def test_list_entities_in_period(self): def test_list_entities_in_period(self):
fake_entities_in_period = [ fake_entities_in_period = [
a(instance().with_id("in_the_period").with_start(2014, 1, 1, 7, 0, a(instance().with_id("in_the_period").with_start(2014, 1, 1, 7, 0,
0).with_end(2014, 1, 1, 8, 0, 0).with_project_id("project_id")), 0).with_end(2014, 1, 1, 8, 0, 0).with_project_id(
"project_id")),
a(instance().with_id("running_has_started_before").with_start( a(instance().with_id("running_has_started_before").with_start(
2014, 1, 1, 1, 0, 0).with_no_end().with_project_id("project_id")), 2014, 1, 1, 1, 0, 0).with_no_end().with_project_id("project_id")),
a(instance().with_id("running_has_started_during").with_start( a(instance().with_id("running_has_started_during").with_start(
@ -174,9 +173,11 @@ class DatabaseAdapterTest(unittest.TestCase):
] ]
fake_entities_out_period = [ fake_entities_out_period = [
a(instance().with_id("before_the_period").with_start(2014, 1, 1, 0, a(instance().with_id("before_the_period").with_start(2014, 1, 1, 0,
0, 0).with_end(2014, 1, 1, 1, 0, 0).with_project_id("project_id")), 0, 0).with_end(2014, 1, 1, 1, 0, 0).with_project_id(
"project_id")),
a(instance().with_id("after_the_period").with_start(2014, 1, 1, 10, a(instance().with_id("after_the_period").with_start(2014, 1, 1, 10,
0, 0).with_end(2014, 1, 1, 11, 0, 0).with_project_id("project_id")), 0, 0).with_end(2014, 1, 1, 11, 0, 0).with_project_id(
"project_id")),
a(instance().with_id("running_has_started_after").with_start( a(instance().with_id("running_has_started_after").with_start(
2014, 1, 1, 10, 0, 0).with_no_end().with_project_id("project_id")), 2014, 1, 1, 10, 0, 0).with_no_end().with_project_id("project_id")),
] ]

View File

@ -13,13 +13,12 @@
# limitations under the License. # limitations under the License.
import unittest import unittest
import pytz
from datetime import datetime, timedelta from datetime import datetime, timedelta
import pytz
from dateutil import parser as date_parser from dateutil import parser as date_parser
from flexmock import flexmock, flexmock_teardown from flexmock import flexmock, flexmock_teardown
from nose.tools import assert_raises from nose.tools import assert_raises
from almanach import config from almanach import config
from almanach.common.date_format_exception import DateFormatException from almanach.common.date_format_exception import DateFormatException
from almanach.core.controller import Controller from almanach.core.controller import Controller
@ -28,7 +27,6 @@ from tests.builder import a, instance, volume, volume_type
class ControllerTest(unittest.TestCase): class ControllerTest(unittest.TestCase):
def setUp(self): def setUp(self):
self.database_adapter = flexmock() self.database_adapter = flexmock()
@ -363,7 +361,8 @@ class ControllerTest(unittest.TestCase):
.once()) .once())
self.controller.create_volume(some_volume.entity_id, some_volume.project_id, '2015-10-21T16:25:00.000000Z', self.controller.create_volume(some_volume.entity_id, some_volume.project_id, '2015-10-21T16:25:00.000000Z',
some_volume.volume_type, some_volume.size, some_volume.name, some_volume.attached_to) some_volume.volume_type, some_volume.size, some_volume.name,
some_volume.attached_to)
def test_volume_updated(self): def test_volume_updated(self):
fake_volume = a(volume()) fake_volume = a(volume())

View File

@ -12,11 +12,11 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from datetime import datetime, timedelta
import dateutil.parser import dateutil.parser
import pytz import pytz
from datetime import datetime, timedelta
DEFAULT_VOLUME_TYPE = "5dadd67f-e21e-4c13-b278-c07b73b21250" DEFAULT_VOLUME_TYPE = "5dadd67f-e21e-4c13-b278-c07b73b21250"
@ -31,7 +31,9 @@ def get_instance_create_end_sample(instance_id=None, tenant_id=None, flavor_name
"os_distro": os_distro or "CentOS", "os_distro": os_distro or "CentOS",
"os_version": os_version or "6.4", "os_version": os_version or "6.4",
"created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 16, 29, 58, tzinfo=pytz.utc), "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 16, 29, 58, tzinfo=pytz.utc),
"launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 16, 30, 02, tzinfo=pytz.utc), "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 16,
30, 02,
tzinfo=pytz.utc),
"terminated_at": None, "terminated_at": None,
"deleted_at": None, "deleted_at": None,
"state": "active", "state": "active",
@ -52,8 +54,11 @@ def get_instance_delete_end_sample(instance_id=None, tenant_id=None, flavor_name
"os_distro": os_distro or "centos", "os_distro": os_distro or "centos",
"os_version": os_version or "6.4", "os_version": os_version or "6.4",
"created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 16, 29, 58, tzinfo=pytz.utc), "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 16, 29, 58, tzinfo=pytz.utc),
"launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 16, 30, 02, tzinfo=pytz.utc), "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 16,
"terminated_at": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 18, 12, 5, 23, tzinfo=pytz.utc), 30, 02,
tzinfo=pytz.utc),
"terminated_at": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 18, 12, 5, 23,
tzinfo=pytz.utc),
"deleted_at": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 18, 12, 5, 23, tzinfo=pytz.utc), "deleted_at": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 18, 12, 5, 23, tzinfo=pytz.utc),
"state": "deleted" "state": "deleted"
} }
@ -70,7 +75,9 @@ def get_volume_create_end_sample(volume_id=None, tenant_id=None, volume_type=Non
"volume_type": volume_type or DEFAULT_VOLUME_TYPE, "volume_type": volume_type or DEFAULT_VOLUME_TYPE,
"volume_size": volume_size or 50, "volume_size": volume_size or 50,
"created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc), "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc),
"launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17,
18, 40,
tzinfo=pytz.utc),
"status": "available" "status": "available"
} }
kwargs["timestamp"] = kwargs["launched_at"] + timedelta(microseconds=200000) kwargs["timestamp"] = kwargs["launched_at"] + timedelta(microseconds=200000)
@ -103,8 +110,11 @@ def get_volume_attach_icehouse_end_sample(volume_id=None, tenant_id=None, volume
"volume_size": volume_size or 50, "volume_size": volume_size or 50,
"attached_to": attached_to or "e7d44dea-21c1-452c-b50c-cbab0d07d7d3", "attached_to": attached_to or "e7d44dea-21c1-452c-b50c-cbab0d07d7d3",
"created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc), "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc),
"launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17,
"timestamp": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), 18, 40,
tzinfo=pytz.utc),
"timestamp": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18,
40, tzinfo=pytz.utc),
} }
return _get_volume_icehouse_payload("volume.attach.end", **kwargs) return _get_volume_icehouse_payload("volume.attach.end", **kwargs)
@ -118,7 +128,8 @@ def get_volume_attach_kilo_end_sample(volume_id=None, tenant_id=None, volume_typ
"volume_type": volume_type or DEFAULT_VOLUME_TYPE, "volume_type": volume_type or DEFAULT_VOLUME_TYPE,
"volume_size": volume_size or 50, "volume_size": volume_size or 50,
"attached_to": attached_to, "attached_to": attached_to,
"timestamp": timestamp + timedelta(seconds=1) if timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), "timestamp": timestamp + timedelta(seconds=1) if timestamp else datetime(2014, 2, 14, 17, 18, 40,
tzinfo=pytz.utc),
} }
return _get_volume_kilo_payload("volume.attach.end", **kwargs) return _get_volume_kilo_payload("volume.attach.end", **kwargs)
@ -132,7 +143,8 @@ def get_volume_detach_kilo_end_sample(volume_id=None, tenant_id=None, volume_typ
"volume_type": volume_type or DEFAULT_VOLUME_TYPE, "volume_type": volume_type or DEFAULT_VOLUME_TYPE,
"volume_size": volume_size or 50, "volume_size": volume_size or 50,
"attached_to": attached_to, "attached_to": attached_to,
"timestamp": timestamp + timedelta(seconds=1) if timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), "timestamp": timestamp + timedelta(seconds=1) if timestamp else datetime(2014, 2, 14, 17, 18, 40,
tzinfo=pytz.utc),
} }
return _get_volume_kilo_payload("volume.detach.end", **kwargs) return _get_volume_kilo_payload("volume.detach.end", **kwargs)
@ -147,7 +159,9 @@ def get_volume_detach_end_sample(volume_id=None, tenant_id=None, volume_type=Non
"volume_size": volume_size or 50, "volume_size": volume_size or 50,
"attached_to": None, "attached_to": None,
"created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc), "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc),
"launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17,
18, 40,
tzinfo=pytz.utc),
"timestamp": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 23, 8, 1, 58, tzinfo=pytz.utc), "timestamp": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 23, 8, 1, 58, tzinfo=pytz.utc),
"status": "detach" "status": "detach"
} }
@ -164,7 +178,9 @@ def get_volume_rename_end_sample(volume_id=None, tenant_id=None, volume_type=Non
"volume_size": volume_size or 50, "volume_size": volume_size or 50,
"attached_to": None, "attached_to": None,
"created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc), "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc),
"launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17,
18, 40,
tzinfo=pytz.utc),
"timestamp": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 23, 8, 1, 58, tzinfo=pytz.utc), "timestamp": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 23, 8, 1, 58, tzinfo=pytz.utc),
"status": "detach" "status": "detach"
} }
@ -181,7 +197,9 @@ def get_volume_exists_sample(volume_id=None, tenant_id=None, volume_type=None, v
"volume_size": volume_size or 50, "volume_size": volume_size or 50,
"attached_to": None, "attached_to": None,
"created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc), "created_at": creation_timestamp if creation_timestamp else datetime(2014, 2, 14, 17, 18, 35, tzinfo=pytz.utc),
"launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17, 18, 40, tzinfo=pytz.utc), "launched_at": creation_timestamp + timedelta(seconds=1) if creation_timestamp else datetime(2014, 2, 14, 17,
18, 40,
tzinfo=pytz.utc),
"timestamp": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 23, 8, 1, 58, tzinfo=pytz.utc), "timestamp": deletion_timestamp if deletion_timestamp else datetime(2014, 2, 23, 8, 1, 58, tzinfo=pytz.utc),
"status": "detach" "status": "detach"
} }
@ -195,7 +213,8 @@ def _format_date(datetime_obj):
def _get_instance_payload(event_type, instance_id=None, tenant_id=None, hostname=None, display_name=None, def _get_instance_payload(event_type, instance_id=None, tenant_id=None, hostname=None, display_name=None,
instance_type=None, instance_type=None,
instance_flavor_id=None, timestamp=None, created_at=None, launched_at=None, instance_flavor_id=None, timestamp=None, created_at=None, launched_at=None,
deleted_at=None, terminated_at=None, state=None, os_type=None, os_distro=None, os_version=None, metadata={}): deleted_at=None, terminated_at=None, state=None, os_type=None, os_distro=None,
os_version=None, metadata={}):
instance_id = instance_id or "e7d44dea-21c1-452c-b50c-cbab0d07d7d3" instance_id = instance_id or "e7d44dea-21c1-452c-b50c-cbab0d07d7d3"
os_type = os_type or "linux" os_type = os_type or "linux"
os_distro = os_distro or "centos" os_distro = os_distro or "centos"
@ -272,7 +291,8 @@ def _get_instance_payload(event_type, instance_id=None, tenant_id=None, hostname
def _get_volume_icehouse_payload(event_type, volume_id=None, tenant_id=None, display_name=None, volume_type=None, def _get_volume_icehouse_payload(event_type, volume_id=None, tenant_id=None, display_name=None, volume_type=None,
volume_size=None, timestamp=None, created_at=None, launched_at=None, status=None, attached_to=None): volume_size=None, timestamp=None, created_at=None, launched_at=None, status=None,
attached_to=None):
volume_id = volume_id or "64a0ca7f-5f5a-4dc5-a1e1-e04e89eb95ed" volume_id = volume_id or "64a0ca7f-5f5a-4dc5-a1e1-e04e89eb95ed"
tenant_id = tenant_id or "46eeb8e44298460899cf4b3554bfe11f" tenant_id = tenant_id or "46eeb8e44298460899cf4b3554bfe11f"
display_name = display_name or "mytenant-0001-myvolume" display_name = display_name or "mytenant-0001-myvolume"

View File

@ -1,7 +1,14 @@
[tox] [tox]
envlist = py27 envlist = py27,flake8
[testenv] [testenv]
deps = -r{toxinidir}/test-requirements.txt deps = -r{toxinidir}/test-requirements.txt
commands = commands =
nosetests --tests tests nosetests --tests tests
[testenv:flake8]
commands = flake8
[flake8]
show-source = True
max-line-length = 120