Skip to content

Commit

Permalink
Reorganize and refactor tests and setup files
Browse files Browse the repository at this point in the history
Allows us to run e.g. tox cactus/tests/... to run specific tests.
  • Loading branch information
krallin committed Jun 3, 2015
1 parent 398ed4a commit 1f2a703
Show file tree
Hide file tree
Showing 21 changed files with 170 additions and 190 deletions.
6 changes: 1 addition & 5 deletions cactus/tests/__init__.py
Expand Up @@ -2,11 +2,7 @@
import tempfile
import shutil
import os

try:
import unittest2 as unittest
except ImportError:
import unittest
import unittest2 as unittest

import django.conf

Expand Down
8 changes: 2 additions & 6 deletions cactus/tests/deployment/__init__.py
@@ -1,14 +1,10 @@
#coding:utf-8
import shutil
import tempfile
import unittest2 as unittest

from cactus.plugin.manager import PluginManager
from cactus.utils.parallel import PARALLEL_DISABLED

try:
import unittest2 as unittest
except ImportError:
import unittest

from cactus.config.fallback import ConfigFallback
from cactus.deployment.engine import BaseDeploymentEngine
from cactus.deployment.file import BaseFile
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
136 changes: 133 additions & 3 deletions cactus/tests/integration/__init__.py
@@ -1,13 +1,15 @@
#coding:utf-8
import httplib
import os
import shutil
from cactus.plugin.manager import PluginManager
import urlparse

from cactus.site import Site
from cactus.plugin.manager import PluginManager
from cactus.utils.helpers import CaseInsensitiveDict
from cactus.utils.parallel import PARALLEL_DISABLED

from cactus.tests import BaseTestCase
from cactus.tests.integration.http import DebugHTTPSConnectionFactory
from cactus.utils.parallel import PARALLEL_DISABLED


class DummyPluginManager(PluginManager):
Expand Down Expand Up @@ -41,3 +43,131 @@ def get_deployment_engine_class(self):
Should return a deployment engine in tests.
"""
pass


class BaseTestHTTPConnection(object):
last_request = None

def __init__(self, host, *args, **kwargs):
self.host = host
self.requests = []

def connect(self):
pass

def close(self):
pass

def request(self, method, url, body='', headers=None):
"""
Send a full request at once
"""
if headers is None:
headers = {}
self.last_request = TestHTTPRequest(self, method, url, body, headers)

def putrequest(self, method, url, *args, **kwargs):
"""
Create a new request, but add more things to it later
"""
self.current_request = TestHTTPRequest(self, method, url, '', {})
self.current_request.state = "headers"

def putheader(self, header, value):
"""
Add an header to a request that's in progress
"""
self.current_request.headers[header] = value

def endheaders(self, data=None):
"""
End the headers of a request that's in progress
"""
self.current_request.state = "body"
self.last_request = self.current_request

if data is not None:
self.send(data)

def send(self, data):
"""
Add data to a request that's in progress
"""
self.current_request.body += data

def getresponse(self):
request = self.last_request
self.requests.append(request)
return self.handle_request(request)

def handle_request(self, request):
"""
:param request: The request to handle
"""
raise NotImplementedError("handle_request should be implemented by subclasses")


def set_debuglevel(self, level):
pass


class DebugHTTPSConnectionFactory(object):
def __init__(self, conn_cls):
self.conn_cls = conn_cls
self.connections = []

@property
def requests(self):
"""
:returns: A dictionary of the calls made through this connection factory (method -> list of calls)
"""
out = []
for connection in self.connections:
out.extend(connection.requests)
return out

def __call__(self, *args, **kwargs):
"""
Create a new connection from our connection class
"""
connection = self.conn_cls(*args, **kwargs)
self.connections.append(connection)
return connection


class TestHTTPRequest(object):
state = None

def __init__(self, connection, method, url, body, headers):
self.connection = connection

self.method = method
self.url = url
self.body = body
self.headers = CaseInsensitiveDict(headers)

u = urlparse.urlparse(url)
self.path = u.path
self.params = urlparse.parse_qs(u.query, keep_blank_values=True)


class TestHTTPResponse(object):
def __init__(self, status, reason=None, headers=None, body=''):
if reason is None:
reason = httplib.responses[status]
if headers is None:
headers = {}

self.status = status
self.reason = reason
self.headers = CaseInsensitiveDict(headers)
self.body = body

def getheader(self, header, default=None):
return self.headers.get(header, default)

def getheaders(self):
return self.headers

def read(self):
return self.body
133 changes: 0 additions & 133 deletions cactus/tests/integration/http.py

This file was deleted.

7 changes: 4 additions & 3 deletions cactus/tests/integration/s3/__init__.py
@@ -1,11 +1,12 @@
#coding:utf-8
import os
from cactus.deployment.s3.engine import S3DeploymentEngine

from cactus.tests.integration import IntegrationTestCase, DebugHTTPSConnectionFactory
from cactus.tests.integration.http import BaseTestHTTPConnection, TestHTTPResponse
from cactus.deployment.s3.engine import S3DeploymentEngine
from cactus.utils.helpers import checksum

from cactus.tests.integration import IntegrationTestCase, DebugHTTPSConnectionFactory, BaseTestHTTPConnection, \
TestHTTPResponse


class DummyAWSCredentialsManager(object):
def __init__(self, site):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 2 additions & 5 deletions cactus/tests/test_config.py
Expand Up @@ -2,10 +2,7 @@
import os
import shutil
import tempfile
try:
import unittest2 as unittest
except ImportError:
import unittest
import unittest2 as unittest

from cactus.config.file import ConfigFile
from cactus.config.router import ConfigRouter
Expand Down Expand Up @@ -125,4 +122,4 @@ def test_dirty(self):
self.conf1.write()

with open(self.path1) as f:
self.assertEqual("canary", f.read())
self.assertEqual("canary", f.read())
8 changes: 2 additions & 6 deletions cactus/tests/test_credentials.py
Expand Up @@ -4,13 +4,9 @@
import tempfile
import keyring
import keyring.backend
from cactus.deployment.s3.auth import AWSCredentialsManager

try:
import unittest2 as unittest
except ImportError:
import unittest
import unittest2 as unittest

from cactus.deployment.s3.auth import AWSCredentialsManager
from cactus.config.file import ConfigFile


Expand Down
11 changes: 3 additions & 8 deletions cactus/tests/test_deploy.py
Expand Up @@ -3,15 +3,10 @@
import shutil
import tempfile
import hashlib

import unittest2 as unittest
import mock
from cactus.deployment.engine import BaseDeploymentEngine

try:
import unittest2 as unittest
except ImportError:
import unittest

from cactus.deployment.engine import BaseDeploymentEngine
from cactus.config.router import ConfigRouter
from cactus.deployment.file import BaseFile
from cactus.plugin.builtin.cache import CacheDurationPlugin
Expand Down Expand Up @@ -108,4 +103,4 @@ def test_cache_control(self):
self.site.plugin_manager.preDeploy(self.site)

f.upload()
self.assertEqual(p.file.cache_control, 123)
self.assertEqual(p.file.cache_control, 123)

0 comments on commit 1f2a703

Please sign in to comment.