Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
itminedu
synnefo
Commits
ecb7bac8
Commit
ecb7bac8
authored
Apr 07, 2013
by
Christos Stavrakakis
Browse files
Move common test code to snf_django.lib.testing
parent
4e3030f7
Changes
11
Hide whitespace changes
Inline
Side-by-side
snf-astakos-app/astakos/im/tests.py
View file @
ecb7bac8
...
...
@@ -36,7 +36,7 @@ import copy
import
datetime
import
functools
from
s
ynnef
o.util.testing
import
with_settings
,
override_settings
from
s
nf_djang
o.util
s
.testing
import
with_settings
,
override_settings
from
django.test
import
TestCase
,
Client
from
django.core
import
mail
...
...
snf-cyclades-app/synnefo/api/test/flavors.py
View file @
ecb7bac8
...
...
@@ -33,7 +33,7 @@
import
json
from
s
ynnefo.api
.test
s
import
BaseAPITest
from
s
nf_django.utils
.test
ing
import
BaseAPITest
from
synnefo.db.models
import
Flavor
from
synnefo.db.models_factory
import
FlavorFactory
...
...
snf-cyclades-app/synnefo/api/test/images.py
View file @
ecb7bac8
...
...
@@ -34,7 +34,7 @@
import
json
from
snf_django.lib.api
import
faults
from
s
ynnefo.api
.test
s
import
BaseAPITest
from
s
nf_django.utils
.test
ing
import
BaseAPITest
from
mock
import
patch
from
functools
import
wraps
...
...
snf-cyclades-app/synnefo/api/test/networks.py
View file @
ecb7bac8
...
...
@@ -34,7 +34,7 @@
import
json
from
mock
import
patch
from
s
ynnefo.api
.test
s
import
BaseAPITest
from
s
nf_django.utils
.test
ing
import
BaseAPITest
from
synnefo.db.models
import
Network
,
NetworkInterface
from
synnefo.db
import
models_factory
as
mfactory
...
...
snf-cyclades-app/synnefo/api/test/servers.py
View file @
ecb7bac8
...
...
@@ -33,7 +33,7 @@
import
json
from
s
ynnefo.api
.test
s
import
BaseAPITest
from
s
nf_django.utils
.test
ing
import
BaseAPITest
from
synnefo.db.models
import
VirtualMachine
,
VirtualMachineMetadata
from
synnefo.db
import
models_factory
as
mfactory
from
synnefo.logic.utils
import
get_rsapi_state
...
...
snf-cyclades-app/synnefo/api/test/versions.py
0 → 100644
View file @
ecb7bac8
# Copyright 2012-2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from
django.utils
import
simplejson
as
json
from
django.test
import
TestCase
from
snf_django.utils.testing
import
astakos_user
class
APITest
(
TestCase
):
def
test_api_version
(
self
):
"""Check API version."""
with
astakos_user
(
'user'
):
response
=
self
.
client
.
get
(
'/api/v1.1/'
)
self
.
assertEqual
(
response
.
status_code
,
200
)
api_version
=
json
.
loads
(
response
.
content
)[
'version'
]
self
.
assertEqual
(
api_version
[
'id'
],
'v1.1'
)
self
.
assertEqual
(
api_version
[
'status'
],
'CURRENT'
)
snf-cyclades-app/synnefo/api/tests.py
View file @
ecb7bac8
...
...
@@ -31,87 +31,9 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from
__future__
import
with_statement
from
django.utils
import
simplejson
as
json
from
django.test
import
TestCase
from
mock
import
patch
from
contextlib
import
contextmanager
@
contextmanager
def
astakos_user
(
user
):
"""
Context manager to mock astakos response.
usage:
with astakos_user("user@user.com"):
.... make api calls ....
"""
with
patch
(
"snf_django.lib.api.get_token"
)
as
get_token
:
get_token
.
return_value
=
"DummyToken"
with
patch
(
'astakosclient.AstakosClient.get_user_info'
)
as
m
:
m
.
return_value
=
{
"uuid"
:
user
}
yield
class
BaseAPITest
(
TestCase
):
def
get
(
self
,
url
,
user
=
'user'
,
*
args
,
**
kwargs
):
with
astakos_user
(
user
):
response
=
self
.
client
.
get
(
url
,
*
args
,
**
kwargs
)
return
response
def
delete
(
self
,
url
,
user
=
'user'
):
with
astakos_user
(
user
):
response
=
self
.
client
.
delete
(
url
)
return
response
def
post
(
self
,
url
,
user
=
'user'
,
params
=
{},
ctype
=
'json'
,
*
args
,
**
kwargs
):
if
ctype
==
'json'
:
content_type
=
'application/json'
with
astakos_user
(
user
):
response
=
self
.
client
.
post
(
url
,
params
,
content_type
=
content_type
,
*
args
,
**
kwargs
)
return
response
def
put
(
self
,
url
,
user
=
'user'
,
params
=
{},
ctype
=
'json'
,
*
args
,
**
kwargs
):
if
ctype
==
'json'
:
content_type
=
'application/json'
with
astakos_user
(
user
):
response
=
self
.
client
.
put
(
url
,
params
,
content_type
=
content_type
,
*
args
,
**
kwargs
)
return
response
def
assertSuccess
(
self
,
response
):
self
.
assertTrue
(
response
.
status_code
in
[
200
,
203
,
204
])
def
assertFault
(
self
,
response
,
status_code
,
name
):
self
.
assertEqual
(
response
.
status_code
,
status_code
)
fault
=
json
.
loads
(
response
.
content
)
self
.
assertEqual
(
fault
.
keys
(),
[
name
])
def
assertBadRequest
(
self
,
response
):
self
.
assertFault
(
response
,
400
,
'badRequest'
)
def
assertItemNotFound
(
self
,
response
):
self
.
assertFault
(
response
,
404
,
'itemNotFound'
)
class
APITest
(
TestCase
):
def
test_api_version
(
self
):
"""Check API version."""
with
astakos_user
(
'user'
):
response
=
self
.
client
.
get
(
'/api/v1.1/'
)
self
.
assertEqual
(
response
.
status_code
,
200
)
api_version
=
json
.
loads
(
response
.
content
)[
'version'
]
self
.
assertEqual
(
api_version
[
'id'
],
'v1.1'
)
self
.
assertEqual
(
api_version
[
'status'
],
'CURRENT'
)
# Import TestCases
from
synnefo.api.test.servers
import
*
from
synnefo.api.test.networks
import
*
from
synnefo.api.test.flavors
import
*
from
synnefo.api.test.images
import
*
from
synnefo.api.test.versions
import
*
snf-cyclades-app/synnefo/plankton/tests.py
View file @
ecb7bac8
...
...
@@ -39,6 +39,7 @@ from contextlib import contextmanager
from
mock
import
patch
from
functools
import
wraps
from
copy
import
deepcopy
from
snf_django.utils.testing
import
astakos_user
,
BaseAPITest
FILTERS
=
(
'name'
,
'container_format'
,
'disk_format'
,
'status'
,
'size_min'
,
...
...
@@ -58,65 +59,6 @@ UPDATE_FIELDS = ('name', 'disk_format', 'container_format', 'is_public',
'owner'
,
'properties'
,
'status'
)
@
contextmanager
def
astakos_user
(
user
):
"""
Context manager to mock astakos response.
usage:
with astakos_user("user@user.com"):
.... make api calls ....
"""
with
patch
(
"snf_django.lib.api.get_token"
)
as
get_token
:
get_token
.
return_value
=
"DummyToken"
with
patch
(
'astakosclient.AstakosClient.get_user_info'
)
as
m
:
m
.
return_value
=
{
"uuid"
:
user
}
yield
class
BaseAPITest
(
TestCase
):
def
get
(
self
,
url
,
user
=
'user'
,
*
args
,
**
kwargs
):
with
astakos_user
(
user
):
response
=
self
.
client
.
get
(
url
,
*
args
,
**
kwargs
)
return
response
def
delete
(
self
,
url
,
user
=
'user'
):
with
astakos_user
(
user
):
response
=
self
.
client
.
delete
(
url
)
return
response
def
post
(
self
,
url
,
user
=
'user'
,
params
=
{},
ctype
=
'json'
,
*
args
,
**
kwargs
):
if
ctype
==
'json'
:
content_type
=
'application/json'
with
astakos_user
(
user
):
response
=
self
.
client
.
post
(
url
,
params
,
content_type
=
content_type
,
*
args
,
**
kwargs
)
return
response
def
put
(
self
,
url
,
user
=
'user'
,
params
=
{},
ctype
=
'json'
,
*
args
,
**
kwargs
):
if
ctype
==
'json'
:
content_type
=
'application/json'
with
astakos_user
(
user
):
response
=
self
.
client
.
put
(
url
,
params
,
content_type
=
content_type
,
*
args
,
**
kwargs
)
return
response
def
assertSuccess
(
self
,
response
):
self
.
assertTrue
(
response
.
status_code
in
[
200
,
203
,
204
])
def
assertFault
(
self
,
response
,
status_code
,
name
):
self
.
assertEqual
(
response
.
status_code
,
status_code
)
fault
=
response
.
content
self
.
assertEqual
(
fault
,
name
)
def
assertBadRequest
(
self
,
response
):
self
.
assertFault
(
response
,
400
,
'400 Bad Request'
)
def
assertItemNotFound
(
self
,
response
):
self
.
assertFault
(
response
,
404
,
'itemNotFound'
)
DummyImages
=
{
'0786a349-9725-48ec-8b86-8598eefc4043'
:
{
'checksum'
:
u
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
,
...
...
snf-cyclades-app/synnefo/plankton/views.py
View file @
ecb7bac8
...
...
@@ -38,10 +38,10 @@ from string import punctuation
from
urllib
import
unquote
from
django.conf
import
settings
from
django.http
import
(
HttpResponse
,
HttpResponseNotFound
,
HttpResponseBadRequest
)
from
django.http
import
HttpResponse
from
snf_django.lib
import
api
from
snf_django.lib.api
import
faults
from
synnefo.plankton.utils
import
plankton_method
...
...
@@ -233,7 +233,7 @@ def get_image_meta(request, image_id):
image
=
request
.
backend
.
get_image
(
image_id
)
if
not
image
:
r
eturn
HttpResponse
NotFound
()
r
aise
faults
.
Item
NotFound
()
return
_create_image_response
(
image
)
...
...
@@ -285,13 +285,13 @@ def list_images(request, detail=False):
try
:
filters
[
'size_max'
]
=
int
(
filters
[
'size_max'
])
except
ValueError
:
r
eturn
HttpResponse
BadRequest
(
'400 Ba
d
R
equest
'
)
r
aise
faults
.
BadRequest
(
"Malforme
d
r
equest
."
)
if
'size_min'
in
filters
:
try
:
filters
[
'size_min'
]
=
int
(
filters
[
'size_min'
])
except
ValueError
:
r
eturn
HttpResponse
BadRequest
(
'400 Ba
d
R
equest
'
)
r
aise
faults
.
BadRequest
(
"Malforme
d
r
equest
."
)
images
=
request
.
backend
.
list
(
filters
,
params
)
...
...
snf-django-lib/snf_django/utils/__init__.py
0 → 100644
View file @
ecb7bac8
snf-
common/synnef
o/util/testing.py
→
snf-
django-lib/snf_djang
o/util
s
/testing.py
View file @
ecb7bac8
...
...
@@ -33,6 +33,9 @@
from
contextlib
import
contextmanager
from
django.test
import
TestCase
from
django.utils
import
simplejson
as
json
from
mock
import
patch
@
contextmanager
...
...
@@ -112,3 +115,62 @@ def with_settings(settings, prefix='', **override):
return
ret
return
inner
return
wrapper
@
contextmanager
def
astakos_user
(
user
):
"""
Context manager to mock astakos response.
usage:
with astakos_user("user@user.com"):
.... make api calls ....
"""
with
patch
(
"snf_django.lib.api.get_token"
)
as
get_token
:
get_token
.
return_value
=
"DummyToken"
with
patch
(
'astakosclient.AstakosClient.get_user_info'
)
as
m
:
m
.
return_value
=
{
"uuid"
:
user
}
yield
class
BaseAPITest
(
TestCase
):
def
get
(
self
,
url
,
user
=
'user'
,
*
args
,
**
kwargs
):
with
astakos_user
(
user
):
response
=
self
.
client
.
get
(
url
,
*
args
,
**
kwargs
)
return
response
def
delete
(
self
,
url
,
user
=
'user'
):
with
astakos_user
(
user
):
response
=
self
.
client
.
delete
(
url
)
return
response
def
post
(
self
,
url
,
user
=
'user'
,
params
=
{},
ctype
=
'json'
,
*
args
,
**
kwargs
):
if
ctype
==
'json'
:
content_type
=
'application/json'
with
astakos_user
(
user
):
response
=
self
.
client
.
post
(
url
,
params
,
content_type
=
content_type
,
*
args
,
**
kwargs
)
return
response
def
put
(
self
,
url
,
user
=
'user'
,
params
=
{},
ctype
=
'json'
,
*
args
,
**
kwargs
):
if
ctype
==
'json'
:
content_type
=
'application/json'
with
astakos_user
(
user
):
response
=
self
.
client
.
put
(
url
,
params
,
content_type
=
content_type
,
*
args
,
**
kwargs
)
return
response
def
assertSuccess
(
self
,
response
):
self
.
assertTrue
(
response
.
status_code
in
[
200
,
203
,
204
])
def
assertFault
(
self
,
response
,
status_code
,
name
):
self
.
assertEqual
(
response
.
status_code
,
status_code
)
fault
=
json
.
loads
(
response
.
content
)
self
.
assertEqual
(
fault
.
keys
(),
[
name
])
def
assertBadRequest
(
self
,
response
):
self
.
assertFault
(
response
,
400
,
'badRequest'
)
def
assertItemNotFound
(
self
,
response
):
self
.
assertFault
(
response
,
404
,
'itemNotFound'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment