Allow to run tests in offline mode

Currently for several unit and functional tests it is
required to download data from external source, which
prevents their execution in offline mode.

This commit eliminates the drawback.

Change-Id: I35bfe8fba0a4d332c132a718b1845b3a09d6f68b
Closes-bug: #1721290
This commit is contained in:
Mike Fedosin 2017-10-05 16:57:46 +03:00
parent d2ce390ca8
commit 80be8dfbc2
4 changed files with 100 additions and 29 deletions

View File

@ -827,10 +827,17 @@ class TestBlobs(base.TestArtifact):
'string_required': '123'})
self.assertIsNotNone(art['id'])
# Create auxiliary artifact and upload data there
aux = self.create_artifact({'name': 'auxiliary'})
url = '/sample_artifact/%s/blob' % aux['id']
data = b'a' * 1000
self.put(url=url, data=data)
data_url = self._url(url)
# Set custom location
url = '/sample_artifact/%s' % art['id']
body = jsonutils.dumps(
{'url': 'https://www.apache.org/licenses/LICENSE-2.0.txt',
{'url': data_url,
'md5': "fake", 'sha1': "fake_sha", "sha256": "fake_sha256"})
headers = {'Content-Type':
'application/vnd+openstack.glare-custom-location+json'}
@ -851,8 +858,7 @@ class TestBlobs(base.TestArtifact):
self.assertEqual('fake_sha256', art['blob']['sha256'])
self.assertIsNone(art['blob']['size'])
self.assertIsNone(art['blob']['content_type'])
self.assertEqual('https://www.apache.org/licenses/LICENSE-2.0.txt',
art['blob']['url'])
self.assertEqual(data_url, art['blob']['url'])
self.assertNotIn('id', art['blob'])
# Set custom location
@ -866,8 +872,7 @@ class TestBlobs(base.TestArtifact):
self.assertIsNotNone(art['dict_of_blobs']['blob']['md5'])
self.assertIsNone(art['dict_of_blobs']['blob']['size'])
self.assertIsNone(art['dict_of_blobs']['blob']['content_type'])
self.assertEqual('https://www.apache.org/licenses/LICENSE-2.0.txt',
art['dict_of_blobs']['blob']['url'])
self.assertEqual(data_url, art['dict_of_blobs']['blob']['url'])
self.assertNotIn('id', art['dict_of_blobs']['blob'])
# test re-add failed
self.put(url=url + '/dict_of_blobs/blob', data=body, status=409,
@ -892,10 +897,17 @@ class TestBlobs(base.TestArtifact):
'string_required': '123'})
self.assertIsNotNone(art['id'])
# Create auxiliary artifact and upload data there
aux = self.create_artifact({'name': 'auxiliary'})
url = '/sample_artifact/%s/blob' % aux['id']
data = b'a' * 1000
self.put(url=url, data=data)
data_url = self._url(url)
# Set custom location
url = '/sample_artifact/%s' % art['id']
body = jsonutils.dumps(
{'url': 'https://www.apache.org/licenses/LICENSE-2.0.txt',
{'url': data_url,
'md5': "fake", 'sha1': "fake_sha", "sha256": "fake_sha256"})
headers = {'Content-Type':
'application/vnd+openstack.glare-custom-location+json'}
@ -907,8 +919,7 @@ class TestBlobs(base.TestArtifact):
self.assertEqual('fake_sha256', art['blob']['sha256'])
self.assertIsNone(art['blob']['size'])
self.assertIsNone(art['blob']['content_type'])
self.assertEqual('https://www.apache.org/licenses/LICENSE-2.0.txt',
art['blob']['url'])
self.assertEqual(data_url, art['blob']['url'])
self.assertNotIn('id', art['blob'])
# Delete should work
@ -952,10 +963,17 @@ class TestBlobs(base.TestArtifact):
'string_required': '123'})
self.assertIsNotNone(art['id'])
# Create auxiliary artifact and upload data there
aux = self.create_artifact({'name': 'auxiliary'})
url = '/sample_artifact/%s/blob' % aux['id']
data = b'a' * 1000
self.put(url=url, data=data)
data_url = self._url(url)
# Set custom location
url = '/sample_artifact/%s' % art['id']
body = jsonutils.dumps(
{'url': 'https://www.apache.org/licenses/LICENSE-2.0.txt',
{'url': data_url,
'md5': "fake", 'sha1': "fake_sha", "sha256": "fake_sha256"})
headers = {'Content-Type':
'application/vnd+openstack.glare-custom-location+json'}
@ -967,8 +985,7 @@ class TestBlobs(base.TestArtifact):
self.assertEqual('fake_sha256', art['dict_of_blobs']['blob']['sha256'])
self.assertIsNone(art['dict_of_blobs']['blob']['size'])
self.assertIsNone(art['dict_of_blobs']['blob']['content_type'])
self.assertEqual('https://www.apache.org/licenses/LICENSE-2.0.txt',
art['dict_of_blobs']['blob']['url'])
self.assertEqual(data_url, art['dict_of_blobs']['blob']['url'])
self.assertNotIn('id', art['dict_of_blobs']['blob'])
# Delete should work

View File

@ -58,7 +58,7 @@ class TestScrubber(base.TestArtifact):
# add external location
body = jsonutils.dumps(
{'url': 'https://www.apache.org/licenses/LICENSE-2.0.txt',
{'url': self._url(url + '/small_blob'),
'md5': "fake", 'sha1': "fake_sha", "sha256": "fake_sha256"})
headers = {'Content-Type':
'application/vnd+openstack.glare-custom-location+json'}

View File

@ -15,19 +15,19 @@
import os
import tempfile
import mock
from six import BytesIO
from glare.common import exception as exc
from glare.common import store_api
from glare.tests.unit import base
from glare.tests import utils
class TestStoreAPI(base.BaseTestArtifactAPI):
def test_read_data(self):
"""Read data from file, http and database."""
# test local temp file
def test_read_data_filesystem(self):
# test local read from temp file
tfd, path = tempfile.mkstemp()
try:
os.write(tfd, b'a' * 1000)
@ -45,7 +45,8 @@ class TestStoreAPI(base.BaseTestArtifactAPI):
finally:
os.remove(path)
# test sql object
def test_read_data_database(self):
# test read from sql object
values = {'name': 'ttt', 'version': '1.0'}
self.sample_artifact = self.controller.create(
self.req, 'sample_artifact', values)
@ -60,15 +61,32 @@ class TestStoreAPI(base.BaseTestArtifactAPI):
self.assertRaises(exc.RequestEntityTooLarge,
store_api.read_data, flobj['data'], limit=99)
# test external http
flobj = store_api.load_from_store(
'https://www.apache.org/licenses/LICENSE-2.0.txt',
self.req.context
)
self.assertEqual(3967, len(store_api.read_data(flobj)))
flobj = store_api.load_from_store(
'https://www.apache.org/licenses/LICENSE-2.0.txt',
self.req.context
)
self.assertRaises(exc.RequestEntityTooLarge,
store_api.read_data, flobj, limit=3966)
def test_read_data_http(self):
request = mock.patch('requests.Session.request')
try:
self.request = request.start()
self.request.return_value = utils.fake_response(
content=b'a' * 1000)
# test read from external http
flobj = store_api.load_from_store(
'http://localhost/test_file.txt',
self.req.context
)
self.assertEqual(1000, len(store_api.read_data(flobj)))
finally:
request.stop()
def test_read_data_http_too_large_data(self):
request = mock.patch('requests.Session.request')
try:
self.request = request.start()
self.request.return_value = utils.fake_response(
content=b'a' * 1000)
flobj = store_api.load_from_store(
'http://localhost/test_file.txt',
self.req.context
)
self.assertRaises(exc.RequestEntityTooLarge,
store_api.read_data, flobj, limit=999)
finally:
request.stop()

View File

@ -19,6 +19,7 @@ import functools
import os
import shlex
import shutil
import six
import socket
import subprocess
@ -26,6 +27,7 @@ import fixtures
from oslo_config import cfg
from oslo_config import fixture as cfg_fixture
from oslo_log import log
import requests
import testtools
from glare.common import config
@ -374,3 +376,37 @@ def safe_mkdirs(path):
except OSError as e:
if e.errno != errno.EEXIST:
raise
class FakeHTTPResponse(object):
def __init__(self, status=200, headers=None, data=None, *args, **kwargs):
data = data or b'some_data'
self.data = six.BytesIO(data)
self.read = self.data.read
self.status = status
self.headers = headers or {'content-length': len(data)}
if not kwargs.get('no_response_body', False):
self.body = None
def getheader(self, name, default=None):
return self.headers.get(name.lower(), default)
def getheaders(self):
return self.headers or {}
def read(self, amt):
self.data.read(amt)
def release_conn(self):
pass
def close(self):
self.data.close()
def fake_response(status_code=200, headers=None, content=None, **kwargs):
r = requests.models.Response()
r.status_code = status_code
r.headers = headers or {}
r.raw = FakeHTTPResponse(status_code, headers, content, kwargs)
return r