Add general functional tests for python written storlets
This patch added the following functional test items in python. - crash test - hold(sleep) test - parallel execution test - ACL test we use java's functional tests as a reference. Change-Id: I48f842b8cae5cf06dc4e5fb2d6b895df442cb767
This commit is contained in:
48
StorletSamples/python/storlet_samples/test/test.py
Normal file
48
StorletSamples/python/storlet_samples/test/test.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
# Copyright (c) 2010-2016 OpenStack Foundation
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
# implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
class TestStorlet(object):
|
||||||
|
def __init__(self, logger):
|
||||||
|
self.logger = logger
|
||||||
|
|
||||||
|
def __call__(self, in_files, out_files, params):
|
||||||
|
"""
|
||||||
|
The function called for storlet invocation
|
||||||
|
|
||||||
|
:param in_files: a list of StorletInputFile
|
||||||
|
:param out_files: a list of StorletOutputFile
|
||||||
|
:param params: a dict of request parameters
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.logger.debug('execute test.py')
|
||||||
|
op = params.get('op')
|
||||||
|
self.logger.debug('op = %s' % op)
|
||||||
|
|
||||||
|
if op == 'crash':
|
||||||
|
raise Exception('Crashed')
|
||||||
|
metadata = in_files[0].get_metadata()
|
||||||
|
out_files[0].set_metadata(metadata)
|
||||||
|
|
||||||
|
if op == 'print':
|
||||||
|
for key, value in params.items():
|
||||||
|
out_files[0].write('%s %s\n' % (key, value))
|
||||||
|
|
||||||
|
if op == 'hold':
|
||||||
|
time.sleep(100000)
|
||||||
|
in_files[0].close()
|
||||||
|
out_files[0].close()
|
161
tests/functional/python/test_test_storlet.py
Normal file
161
tests/functional/python/test_test_storlet.py
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
# Copyright (c) 2010-2016 OpenStack Foundation
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
# implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import threading
|
||||||
|
from swiftclient import client as swift_client
|
||||||
|
from swiftclient import ClientException
|
||||||
|
from nose.plugins.attrib import attr
|
||||||
|
from storlets.tools.utils import get_member_auth
|
||||||
|
from tests.functional.python import StorletPythonFunctionalTest
|
||||||
|
|
||||||
|
|
||||||
|
class myTestThread(threading.Thread):
|
||||||
|
def __init__(self, url, token, test_class):
|
||||||
|
super(myTestThread, self).__init__()
|
||||||
|
self.token = token
|
||||||
|
self.url = url
|
||||||
|
self.test_class = test_class
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.test_class.invoke_storlet("print", False)
|
||||||
|
|
||||||
|
|
||||||
|
class TestTestStorlet(StorletPythonFunctionalTest):
|
||||||
|
def setUp(self):
|
||||||
|
self.additional_headers = {}
|
||||||
|
super(TestTestStorlet, self).setUp(
|
||||||
|
storlet_dir='test',
|
||||||
|
storlet_name='test.py',
|
||||||
|
storlet_main='test.TestStorlet',
|
||||||
|
container='myobjects',
|
||||||
|
storlet_file=None,
|
||||||
|
headers={})
|
||||||
|
|
||||||
|
self.member_url, self.member_token = get_member_auth(self.conf)
|
||||||
|
|
||||||
|
swift_client.put_object(self.url,
|
||||||
|
self.token,
|
||||||
|
self.container,
|
||||||
|
'test_object',
|
||||||
|
'some content')
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
headers = {'X-Container-Read': ''}
|
||||||
|
swift_client.post_container(self.url,
|
||||||
|
self.token,
|
||||||
|
'myobjects',
|
||||||
|
headers)
|
||||||
|
|
||||||
|
def invoke_storlet(self, op, withlog=False):
|
||||||
|
headers = {'X-Run-Storlet': self.storlet_name}
|
||||||
|
headers.update(self.additional_headers)
|
||||||
|
if withlog is True:
|
||||||
|
headers['X-Storlet-Generate-Log'] = 'True'
|
||||||
|
|
||||||
|
params = 'op={0}¶m2=val2'.format(op)
|
||||||
|
resp_dict = dict()
|
||||||
|
try:
|
||||||
|
resp_headers, get_text = swift_client.get_object(
|
||||||
|
self.url, self.token, 'myobjects', 'test_object',
|
||||||
|
None, None, params, resp_dict, headers)
|
||||||
|
get_response_status = resp_dict.get('status')
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
get_response_status = resp_dict.get('status')
|
||||||
|
if op == 'crash':
|
||||||
|
self.assertTrue(get_response_status >= 500 or
|
||||||
|
get_response_status == 404)
|
||||||
|
|
||||||
|
if withlog:
|
||||||
|
resp_headers, get_text = swift_client.get_object(
|
||||||
|
self.url, self.token, 'storletlog', 'test.log',
|
||||||
|
None, None, None, None, headers)
|
||||||
|
self.assertEqual(200, resp_headers.get('status'))
|
||||||
|
self.assertEqual('aaa', get_text.read())
|
||||||
|
|
||||||
|
if op == 'print':
|
||||||
|
self.assertEqual(200, get_response_status)
|
||||||
|
self.assertIn('op print', get_text)
|
||||||
|
self.assertIn('param2 val2', get_text)
|
||||||
|
|
||||||
|
def test_print(self):
|
||||||
|
self.invoke_storlet("print", False)
|
||||||
|
|
||||||
|
def test_crash(self):
|
||||||
|
self.invoke_storlet("crash")
|
||||||
|
|
||||||
|
@attr('slow')
|
||||||
|
def test_hold(self):
|
||||||
|
self.invoke_storlet("hold")
|
||||||
|
|
||||||
|
def invoke_storlet_in_parallel(self):
|
||||||
|
mythreads = []
|
||||||
|
|
||||||
|
for i in range(10):
|
||||||
|
new_thread = myTestThread(self.url, self.token, self)
|
||||||
|
mythreads.append(new_thread)
|
||||||
|
|
||||||
|
for t in mythreads:
|
||||||
|
t.start()
|
||||||
|
|
||||||
|
for t in mythreads:
|
||||||
|
t.join()
|
||||||
|
|
||||||
|
@attr('slow')
|
||||||
|
def test_parallel_print(self):
|
||||||
|
self.invoke_storlet_in_parallel()
|
||||||
|
|
||||||
|
def test_storlet_acl_get_fail(self):
|
||||||
|
headers = {'X-Run-Storlet': self.storlet_name}
|
||||||
|
headers.update(self.additional_headers)
|
||||||
|
exc_pattern = '^.*403 Forbidden.*$'
|
||||||
|
with self.assertRaisesRegexp(ClientException, exc_pattern):
|
||||||
|
swift_client.get_object(self.member_url, self.member_token,
|
||||||
|
'myobjects', 'test_object',
|
||||||
|
headers=headers)
|
||||||
|
|
||||||
|
def test_storlet_acl_get_success(self):
|
||||||
|
headers = {'X-Run-Storlet': self.storlet_name}
|
||||||
|
headers.update(self.additional_headers)
|
||||||
|
exc_pattern = '^.*403 Forbidden.*$'
|
||||||
|
with self.assertRaisesRegexp(ClientException, exc_pattern):
|
||||||
|
swift_client.get_object(self.member_url, self.member_token,
|
||||||
|
'myobjects', 'test_object',
|
||||||
|
headers=headers)
|
||||||
|
|
||||||
|
headers = {'X-Storlet-Container-Read': self.conf.member_user,
|
||||||
|
'X-Storlet-Name': self.storlet_name}
|
||||||
|
swift_client.post_container(self.url,
|
||||||
|
self.token,
|
||||||
|
'myobjects',
|
||||||
|
headers)
|
||||||
|
swift_client.head_container(self.url,
|
||||||
|
self.token,
|
||||||
|
'myobjects')
|
||||||
|
headers = {'X-Run-Storlet': self.storlet_name}
|
||||||
|
headers.update(self.additional_headers)
|
||||||
|
resp_dict = dict()
|
||||||
|
swift_client.get_object(self.member_url,
|
||||||
|
self.member_token,
|
||||||
|
'myobjects', 'test_object',
|
||||||
|
response_dict=resp_dict,
|
||||||
|
headers=headers)
|
||||||
|
self.assertEqual(200, resp_dict['status'])
|
||||||
|
|
||||||
|
|
||||||
|
class TestTestStorletOnProxy(TestTestStorlet):
|
||||||
|
def setUp(self):
|
||||||
|
super(TestTestStorletOnProxy, self).setUp()
|
||||||
|
self.additional_headers = {'X-Storlet-Run-On-Proxy': ''}
|
Reference in New Issue
Block a user