forked from jacobian/openstack.compute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
58 lines (47 loc) · 1.8 KB
/
test_client.py
File metadata and controls
58 lines (47 loc) · 1.8 KB
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
import mock
import httplib2
from openstack.compute.client import ComputeClient
from nose.tools import assert_equal
from fakeserver import FakeConfig
fake_response = httplib2.Response({"status": 200})
fake_body = '{"hi": "there"}'
mock_request = mock.Mock(return_value=(fake_response, fake_body))
def client():
cl = ComputeClient(FakeConfig())
cl.management_url = "http://example.com"
cl.auth_token = "token"
return cl
def test_get():
cl = client()
@mock.patch.object(httplib2.Http, "request", mock_request)
@mock.patch('time.time', mock.Mock(return_value=1234))
def test_get_call():
resp, body = cl.get("/hi")
mock_request.assert_called_with("http://example.com/hi?fresh=1234", "GET",
headers={"X-Auth-Token": "token", "User-Agent": cl.config.user_agent})
# Automatic JSON parsing
assert_equal(body, {"hi":"there"})
test_get_call()
def test_get_allow_cache():
cl = client()
cl.config.allow_cache = True
@mock.patch.object(httplib2.Http, "request", mock_request)
def test_get_call():
resp, body = cl.get("/hi")
# No ?fresh because we're allowing caching.
mock_request.assert_called_with("http://example.com/hi", "GET",
headers={"X-Auth-Token": "token", "User-Agent": cl.config.user_agent})
test_get_call()
def test_post():
cl = client()
@mock.patch.object(httplib2.Http, "request", mock_request)
def test_post_call():
cl.post("/hi", body=[1, 2, 3])
mock_request.assert_called_with("http://example.com/hi", "POST",
headers = {
"X-Auth-Token": "token",
"Content-Type": "application/json",
"User-Agent": cl.config.user_agent},
body = '[1, 2, 3]'
)
test_post_call()