1
from cStringIO import StringIO
3
from twisted.internet.defer import fail, succeed
4
from twisted.web.error import Error
6
from juju.errors import FileNotFound, ProviderError, ProviderInteractionError
7
from juju.lib.testing import TestCase
8
from juju.providers.orchestra import MachineProvider
10
from .test_digestauth import GetPageAuthTestCase
13
class SomeError(Exception):
17
def get_file_storage(custom_config=None):
18
config = {"orchestra-server": "somewhereel.se",
19
"orchestra-user": "fallback-user",
20
"orchestra-pass": "fallback-pass",
21
"acquired-mgmt-class": "acquired",
22
"available-mgmt-class": "available"}
23
if custom_config is None:
24
config["storage-url"] = "http://somewhe.re"
25
config["storage-user"] = "user"
26
config["storage-pass"] = "pass"
28
config.update(custom_config)
29
provider = MachineProvider("blah", config)
30
return provider.get_file_storage()
33
class FileStorageGetTest(TestCase):
36
self.uuid4_m = self.mocker.replace("uuid.uuid4")
37
self.getPage = self.mocker.replace("twisted.web.client.getPage")
39
def test_get_url(self):
41
fs = get_file_storage()
42
self.assertEquals(fs.get_url("angry/birds"),
43
"http://somewhe.re/angry/birds")
45
def test_get_url_fallback(self):
47
fs = get_file_storage({})
48
self.assertEquals(fs.get_url("angry/birds"),
49
"http://somewhereel.se/webdav/angry/birds")
52
self.getPage("http://somewhe.re/rubber/chicken")
53
self.mocker.result(succeed("pulley"))
56
fs = get_file_storage()
57
d = fs.get("rubber/chicken")
60
self.assertEquals(result.read(), "pulley")
64
def check_get_error(self, result, err_type, err_message):
65
self.getPage("http://somewhe.re/rubber/chicken")
66
self.mocker.result(result)
69
fs = get_file_storage()
70
d = fs.get("rubber/chicken")
71
self.assertFailure(d, err_type)
74
self.assertEquals(str(error), err_message)
78
def test_get_error(self):
79
return self.check_get_error(
80
fail(SomeError("pow!")),
81
ProviderInteractionError,
82
"Unexpected SomeError interacting with provider: pow!")
84
def test_get_404(self):
85
return self.check_get_error(
88
"File was not found: 'http://somewhe.re/rubber/chicken'")
90
def test_get_bad_code(self):
91
return self.check_get_error(
94
"Unexpected HTTP 999 trying to GET "
95
"http://somewhe.re/rubber/chicken")
98
class FileStoragePutTest(GetPageAuthTestCase):
100
def setup_mock(self):
101
self.uuid4_m = self.mocker.replace("uuid.uuid4")
103
def get_file_storage(self, with_user=True):
104
storage_url = self.get_base_url()
105
custom_config = {"storage-url": storage_url}
107
custom_config["storage-user"] = "user"
108
custom_config["storage-pass"] = "pass"
109
return get_file_storage(custom_config)
111
def test_no_auth_error(self):
112
self.add_plain("peregrine", "PUT", "", "croissant", 999)
113
fs = self.get_file_storage()
114
d = fs.put("peregrine", StringIO("croissant"))
115
self.assertFailure(d, ProviderError)
118
self.assertIn("Unexpected HTTP 999 trying to PUT ", str(error))
119
d.addCallback(verify)
122
def test_no_auth_201(self):
123
self.add_plain("peregrine", "PUT", "", "croissant", 201)
124
fs = self.get_file_storage()
125
d = fs.put("peregrine", StringIO("croissant"))
126
d.addCallback(self.assertEquals, True)
129
def test_no_auth_204(self):
130
self.add_plain("peregrine", "PUT", "", "croissant", 204)
131
fs = self.get_file_storage()
132
d = fs.put("peregrine", StringIO("croissant"))
133
d.addCallback(self.assertEquals, True)
136
def auth_common(self, username, status, with_user=True):
139
self.mocker.result("dinner")
142
url = self.get_url("possum")
145
self.assertTrue(response.startswith(
146
'Digest username="%s", realm="sparta", nonce="meh", uri="%s"'
149
'qop="auth", nc="00000001", cnonce="dinner"', response)
151
"possum", "PUT", "", "Digest realm=sparta, nonce=meh, qop=auth",
152
check, expect_content="canabalt", status=status)
154
fs = self.get_file_storage(with_user)
155
return fs.put("possum", StringIO("canabalt"))
157
def test_auth_error(self):
158
d = self.auth_common("user", 808)
159
self.assertFailure(d, ProviderError)
162
self.assertIn("Unexpected HTTP 808 trying to PUT", str(error))
163
d.addCallback(verify)
166
def test_auth_bad_credentials(self):
167
d = self.auth_common("user", 401)
168
self.assertFailure(d, ProviderError)
173
"The supplied storage credentials were not accepted by the "
175
d.addCallback(verify)
178
def test_auth_201(self):
179
d = self.auth_common("user", 201)
180
d.addCallback(self.assertEquals, True)
183
def test_auth_204(self):
184
d = self.auth_common("user", 204)
185
d.addCallback(self.assertEquals, True)
188
def test_auth_fallback_error(self):
189
d = self.auth_common("fallback-user", 747, False)
190
self.assertFailure(d, ProviderError)
193
self.assertIn("Unexpected HTTP 747 trying to PUT", str(error))
194
d.addCallback(verify)
197
def test_auth_fallback_201(self):
198
d = self.auth_common("fallback-user", 201, False)
199
d.addCallback(self.assertEquals, True)
202
def test_auth_fallback_204(self):
203
d = self.auth_common("fallback-user", 204, False)
204
d.addCallback(self.assertEquals, True)