This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconftest.py
181 lines (140 loc) · 4.68 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""Test configuration for the ObjectRocket Python Client."""
import datetime
import uuid
import mock
import pytest
import requests
from objectrocket.client import Client
from objectrocket import acls
from objectrocket import instances
from objectrocket import constants
from .mock_stats import (SHARD_STATS_THIS_SECOND, INSTANCE_STATS_THIS_SECOND,
INSTANCE_STATS_NEXT_SECOND, NEW_RELIC_STATS)
#####################
# Generic fixtures. #
#####################
@pytest.fixture
def client(patched_requests_map):
"""Build a client for use in testing."""
return Client(base_url='http://localhost:5050/v2/')
@pytest.fixture
def default_create_instance_kwargs():
"""Return a dict having default data for instance creation."""
data = {
'name': 'instance0',
'size': 5,
'zone': 'US-West',
'service_type': 'mongodb',
'version': '2.4.6',
}
return data
@pytest.fixture
def obj():
"""A generic object for testing purposes."""
class Obj(object):
pass
return Obj()
##########################
# ACLs related fixtures. #
##########################
@pytest.fixture
def acl_doc():
now = datetime.datetime.utcnow()
doc = {
'_id': uuid.uuid4().hex,
'cidr_mask': '0.0.0.0/1',
'description': 'testing',
'instance': 'testinstance',
'login': 'testuser',
'port': 27017,
'date_created': datetime.datetime.strftime(now, constants.TIME_FORMAT),
'instance_id': uuid.uuid4().hex,
'instance_type': 'mongodb_sharded',
'metadata': {},
'service_type': 'mongodb'
}
return doc
@pytest.fixture
def acl(acl_doc, client):
return acls.Acl(document=acl_doc, acls=client.acls)
##############################
# Instance related fixtures. #
##############################
@pytest.fixture
def mongodb_replica_doc():
now = datetime.datetime.utcnow()
doc = {
'id': uuid.uuid4().hex,
'api_endpoint': 'not_a_real_endpoint',
'connect_string': 'REPLSET_60000/localhost:60000,localhost:60001,localhost:60002',
'created': datetime.datetime.strftime(now, constants.TIME_FORMAT),
'name': 'testinstance',
'plan': 1,
'service': 'mongodb',
'type': 'mongodb_replica_set',
'version': '2.4.6',
}
return doc
@pytest.fixture
def mongodb_sharded_doc():
now = datetime.datetime.utcnow()
doc = {
'id': uuid.uuid4().hex,
'api_endpoint': 'not_a_real_endpoint',
'connect_string': 'localhost:50002',
'created': datetime.datetime.strftime(now, constants.TIME_FORMAT),
'name': 'testinstance',
'plan': 5,
'service': 'mongodb',
'ssl_connect_string': 'localhost:60002',
'type': 'mongodb_sharded',
'version': '2.4.6',
}
return doc
@pytest.fixture
def mongodb_replicaset_instance(client, mongodb_replica_doc):
return instances.MongodbInstance(instance_document=mongodb_replica_doc,
instances=client.instances)
@pytest.fixture
def mongodb_sharded_instance(client, mongodb_sharded_doc):
return instances.MongodbInstance(instance_document=mongodb_sharded_doc,
instances=client.instances)
######################
# Patches and mocks. #
######################
@pytest.fixture
def mocked_response(request):
"""Mock a request's response object."""
return mock.create_autospec(requests.Response)
@pytest.fixture
def patched_requests_map(request):
"""Return a dict of ``MagicMock``s which patch the requests library in various places.
.. deprecated:: 0.4.0
Let's go ahead and kill this as we go, and favor the `responses` lib, as it is way better.
:returns: A dict where each key is the name of a module, and its value is the ``MagicMock``
which is patching the requests library in its respective module.
"""
patches = {}
mocked = mock.patch('objectrocket.instances.requests', autospec=True)
request.addfinalizer(mocked.stop)
patches['instances'] = mocked.start()
mocked = mock.patch('objectrocket.instances.mongodb.requests', autospec=True)
request.addfinalizer(mocked.stop)
patches['instances.mongodb'] = mocked.start()
return patches
@pytest.fixture
def mock_shard_stats():
"""Mock shard stats."""
return SHARD_STATS_THIS_SECOND
@pytest.fixture
def mock_instance_stats_this_second():
"""Mock shard stats."""
return INSTANCE_STATS_THIS_SECOND
@pytest.fixture
def mock_instance_stats_next_second():
"""Mock shard stats."""
return INSTANCE_STATS_NEXT_SECOND
@pytest.fixture
def mock_new_relic_stats():
"""Mock shard stats."""
return NEW_RELIC_STATS