Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
lekhajee committed May 6, 2014
1 parent a69fbb7 commit 30736f8
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 17 deletions.
13 changes: 10 additions & 3 deletions .travis.yml
Expand Up @@ -2,9 +2,16 @@ language: python
python:
- "2.7"
# command to install dependencies
install: "pip install -r requirements.txt"
# command to run tests
script: python -m unittest discover
install:
- "pip install -r requirements.txt"
- "pip install flake8"
- "pip install pep257==0.3.2"
script:
# Lint
- flake8 --max-line-length 105 mimic
- "pep257 --ignore=D400,D401,D200,D203,D204,D205 mimic"
# Run tests
- python -m unittest discover
notifications:
irc:
channels: "chat.freenode.net##mimic"
Expand Down
3 changes: 3 additions & 0 deletions mimic/canned_responses/__init__.py
@@ -0,0 +1,3 @@
"""
Canned responses
"""
16 changes: 16 additions & 0 deletions mimic/canned_responses/auth.py
Expand Up @@ -8,6 +8,10 @@


def get_token(tenant_id):
"""
Canned response for authentication, with service catalog containing endpoints only
for services implemented by Mimic.
"""
return {
"access": {
"token": {
Expand Down Expand Up @@ -36,12 +40,20 @@ def get_token(tenant_id):


def get_user(tenant_id):
"""
Canned response for get user. This adds the tenant_id to the auth_cache and
returns unique username for the tenant id.
"""
username = 'mockuser{0}'.format(str(randrange(999999)))
auth_cache[username] = {'tenant_id': tenant_id}
return {'user': {'id': username}}


def get_user_token(expires_in, username):
"""
Canned response for get user token. Also, creates a unique token for a given username,
and associated that token to the username in auth_cache.
"""
token = 'mocked-token{0}'.format(str(randrange(9999999)))
if username in auth_cache:
if not auth_cache.get('username.token'):
Expand All @@ -62,6 +74,10 @@ def get_user_token(expires_in, username):


def get_endpoints(token_id):
"""
Canned response for Identity's get enpoints call. This returns endpoints only
for the services implemented by Mimic.
"""
if token_id in token_cache:
tenant_id = token_cache[token_id]
else:
Expand Down
18 changes: 9 additions & 9 deletions mimic/canned_responses/loadbalancer.py
Expand Up @@ -36,9 +36,7 @@ def load_balancer_example(lb_info, lb_id, status):
"timeout": lb_info.get("tiemout", 30),
"created": {"time": current_time_in_utc()},
"virtualIps": [{"address": "127.0.0.1",
"id": 1111,
"type": "PUBLIC",
"ipVersion": "IPV4"},
"id": 1111, "type": "PUBLIC", "ipVersion": "IPV4"},
{"address": "0000:0000:0000:0000:1111:111b:0000:0000",
"id": 1111,
"type": "PUBLIC",
Expand Down Expand Up @@ -103,10 +101,12 @@ def add_node(node_list, lb_id):
if lb_cache[lb_id].get("nodes"):
for existing_node in lb_cache[lb_id]["nodes"]:
for new_node in node_list:
if (existing_node["address"] == new_node["address"] and
existing_node["port"] == new_node["port"]):
return invalid_resource("Duplicate nodes detected. One or more nodes "
"already configured on load balancer.", 413), 413
if (
existing_node["address"] == new_node["address"] and
existing_node["port"] == new_node["port"]
):
return invalid_resource("Duplicate nodes detected. One or more nodes "
"already configured on load balancer.", 413), 413
lb_cache[lb_id]["nodes"] = lb_cache[lb_id]["nodes"] + nodes
else:
lb_cache[lb_id]["nodes"] = nodes
Expand All @@ -122,7 +122,8 @@ def delete_node(lb_id, node_id):
Note : Currently even if node does not exist, return 202 on delete.
"""
if lb_id in lb_cache:
lb_cache[lb_id]["nodes"] = [x for x in lb_cache[lb_id]["nodes"] if not (node_id == x.get("id"))]
lb_cache[lb_id]["nodes"] = [x for x in lb_cache[
lb_id]["nodes"] if not (node_id == x.get("id"))]
if not lb_cache[lb_id]["nodes"]:
del lb_cache[lb_id]["nodes"]
return None, 202
Expand All @@ -134,7 +135,6 @@ def list_nodes(lb_id):
"""
Returns the list of nodes remaining on the load balancer
"""

if lb_id in lb_cache:
node_list = []
if lb_cache[lb_id].get("nodes"):
Expand Down
5 changes: 2 additions & 3 deletions mimic/canned_responses/mimic_presets.py
Expand Up @@ -7,9 +7,8 @@
"for 10 seconds. Used only on create load balancer",
"lb_name-PENDING_UPDATE": "Changes the load balancer to PENDING-UPDATE"
"state",
"time": 30,
#"Time in seconds, determines the given lb status (other than"
#"the ACTIVE state) lasts. Defaults to 30 seconds",
"time": 30, # "Time in seconds, determines the given lb status (other
# "than the ACTIVE state) lasts. Defaults to 30 seconds",
"failing_lb_id": "175647",
"invalid_lb": "3909",
"return_422_on_add_node_count": 3},
Expand Down
3 changes: 3 additions & 0 deletions mimic/canned_responses/nova.py
@@ -1,3 +1,6 @@
"""
Canned response for Nova
"""
from random import randrange
from mimic.canned_responses.mimic_presets import get_presets
from mimic.util.helper import (not_found_response, invalid_resource,
Expand Down
3 changes: 3 additions & 0 deletions mimic/rest/__init__.py
@@ -0,0 +1,3 @@
"""
Rest API
"""
4 changes: 4 additions & 0 deletions mimic/rest/loadbalancer_api.py
Expand Up @@ -23,6 +23,10 @@ class LoadBalancerApi(object):
app = MimicApp()

def __init__(self):
"""
Fetches the load balancer id for a failure, invalid scenarios and
the count on the number of time 422 should be returned on add node.
"""
self.failing_lb_id = get_presets['loadbalancers']['failing_lb_id']
self.invalid_lb = get_presets['loadbalancers']['invalid_lb']
self.count = get_presets['loadbalancers'][
Expand Down
6 changes: 6 additions & 0 deletions mimic/tap.py
@@ -1,3 +1,6 @@
"""
Twisted Application plugin for Mimic
"""
from twisted.application.strports import service
from twisted.application.service import MultiService
from twisted.web.server import Site
Expand All @@ -6,6 +9,9 @@


class Options(usage.Options):
"""
Options for Mimic
"""
pass


Expand Down
3 changes: 3 additions & 0 deletions mimic/util/__init__.py
@@ -0,0 +1,3 @@
"""
Utilities
"""
5 changes: 4 additions & 1 deletion mimic/util/helper.py
@@ -1,3 +1,7 @@

"""
Helper methods
"""
from datetime import datetime, timedelta


Expand Down Expand Up @@ -32,7 +36,6 @@ def invalid_resource(message, response_code=400):
Returns the given message within in bad request body, and sets the response
code to given response code. Defaults response code to 404, if not provided.
"""

return {"message": message, "code": response_code}


Expand Down
5 changes: 4 additions & 1 deletion twisted/plugins/mimic.py
@@ -1,8 +1,11 @@
"""
Mimic twisted application plugins.
"""
from twisted.application.service import ServiceMaker


mimicService = ServiceMaker(
"mimic Service/",
"mimic.tap",
"Mocks for Autoscale.",
"Mocks for Rackspace APIs",
"mimic")

0 comments on commit 30736f8

Please sign in to comment.