diff --git a/tests/api/README.rst b/tests/api/README.rst index 2ed9bb52..2b9f7008 100644 --- a/tests/api/README.rst +++ b/tests/api/README.rst @@ -34,4 +34,8 @@ To run the tests Tox Support ----------- -Coming Soon to a Repo Near You! +The API tests require cassandra running in your local machine, in order to +run via tox. It is assumed you already have the Cassandra instance up & +running locally. You can make the API tests part of tox, by overriding the +default positional argument in tox.ini:: + example : tox -- --exclude=None diff --git a/tests/api/base.py b/tests/api/base.py index f7979416..a5c24d85 100644 --- a/tests/api/base.py +++ b/tests/api/base.py @@ -56,7 +56,7 @@ class TestBase(fixtures.BaseTestFixture): cls.test_config = config.TestConfig() if cls.test_config.run_server: - conf_file = 'poppy_mockdb.conf' + conf_file = 'poppy_cassandra.conf' conf_path = os.environ["POPPY_TESTS_CONFIGS_DIR"] config_file = os.path.join(conf_path, conf_file) diff --git a/tests/api/providers.py b/tests/api/providers.py index 6a6fe97f..53d02167 100644 --- a/tests/api/providers.py +++ b/tests/api/providers.py @@ -14,6 +14,7 @@ # limitations under the License. from tests.api import base +from tests.api.utils import config from tests.api.utils import fastlyclient as fastly @@ -32,7 +33,7 @@ class TestProviderBase(base.TestBase): def getServiceFromProvider(self, provider, service_name): if provider == 'fastly': - fastly_config = self.config.FastlyConfig() + fastly_config = config.FastlyConfig() fastly_client = fastly.FastlyClient( api_key=fastly_config.api_key, email=fastly_config.email, diff --git a/tests/api/services/test_services.py b/tests/api/services/test_services.py index e214d478..b7b1a47f 100644 --- a/tests/api/services/test_services.py +++ b/tests/api/services/test_services.py @@ -17,8 +17,9 @@ import uuid import ddt +from tests.api import base from tests.api import providers -# from tests.api.utils.schema import response - Uncomment after get_service API +from tests.api.utils.schema import response @ddt.ddt @@ -45,19 +46,17 @@ class TestServices(providers.TestProviderBase): flavorRef=flavor) self.assertEqual(resp.status_code, 202) - # TODO(malini): uncomment after get_service endpoint is complete. - ''' - # Get on Created Service resp = self.client.get_service(service_name=self.service_name) self.assertEqual(resp.status_code, 200) body = resp.json() - self.assertSchema(body, response.create_service) + self.assertSchema(body, response.get_service) self.assertEqual(body['domains'], domain_list) self.assertEqual(body['origins'], origin_list) - self.assertEqual(body['caching_list'], caching_list) - ''' + + # TODO(malini): uncomment below after caching list is implemented. + # self.assertEqual(body['caching_list'], caching_list) # Verify the service is updated at all Providers for the flavor if self.test_config.provider_validation: @@ -86,3 +85,56 @@ class TestServices(providers.TestProviderBase): def tearDown(self): self.client.delete_service(service_name=self.service_name) super(TestServices, self).tearDown() + + +@ddt.ddt +class TestServiceActions(base.TestBase): + + """Tests for PATCH, GET & DELETE Services.""" + + def setUp(self): + super(TestServiceActions, self).setUp() + self.service_name = str(uuid.uuid1()) + self.domain_list = [{"domain": "mywebsite.com"}, + {"domain": "blog.mywebsite.com"}] + + self.origin_list = [{"origin": "mywebsite.com", + "port": 443, "ssl": False}] + + self.caching_list = [{"name": "default", "ttl": 3600}, + {"name": "home", "ttl": 1200, + "rules": [{"name": "index", + "request_url": "/index.htm"}]}] + + self.client.create_service(service_name=self.service_name, + domain_list=self.domain_list, + origin_list=self.origin_list, + caching_list=self.caching_list, + flavorRef='standard') + + def test_get_service(self): + + resp = self.client.get_service(service_name=self.service_name) + self.assertEqual(resp.status_code, 200) + + body = resp.json() + self.assertSchema(body, response.get_service) + self.assertEqual(body['domains'], self.domain_list) + self.assertEqual(body['origins'], self.origin_list) + # TODO(malini): uncomment below after caching list is implemented. + # self.assertEqual(body['caching_list'], self.caching_list) + + def test_get_non_existing_service(self): + + resp = self.client.get_service(service_name='this_cant_be_true') + self.assertEqual(resp.status_code, 404) + + def test_get_failed_service(self): + # TODO(malini): Add test to verify that failed service will return + # status 'failed' on get_service with error message from the provider. + # Placeholder till we figure out how to create provider side failure. + pass + + def tearDown(self): + self.client.delete_service(service_name=self.service_name) + super(TestServiceActions, self).tearDown() diff --git a/tests/api/utils/fastlyclient.py b/tests/api/utils/fastlyclient.py index 250bd79e..e9cf5595 100644 --- a/tests/api/utils/fastlyclient.py +++ b/tests/api/utils/fastlyclient.py @@ -33,7 +33,11 @@ class FastlyClient(client.AutoMarshallingHTTPClient): def get_service(self, service_name): # Get the service - service = self.client.get_service_by_name(service_name) + try: + service = self.client.get_service_by_name(service_name) + except fastly.FastlyError as e: + assert False, e + service_version = self.client.list_versions(service.id) # The create service api_call updates the domain, origin & cache @@ -55,21 +59,17 @@ class FastlyClient(client.AutoMarshallingHTTPClient): for item in cache_setting_list] # Get the Origin List - backends = self.client.list_backends(service.id, version) - origin = backends[0].address - port = backends[0].port - ssl = backends[0].use_ssl - - origin_list = [{'origin': origin, 'port': port, 'ssl': ssl}] + try: + backends = self.client.list_backends(service.id, version) + origin = backends[0].address + port = backends[0].port + ssl = backends[0].use_ssl + origin_list = [{'origin': origin, 'port': port, 'ssl': ssl}] + except IndexError: + assert False, 'Empty Backend in Fastly' + except fastly.FastlyError as e: + assert False, e return {'domain_list': domain_list, 'origin_list': origin_list, 'caching_list': cache_list} - - '''except fastly.FastlyError: - print('1', fastly.FastlyError) - return ("failed to GET service") - except Exception: - print('2', Exception) - return ("failed to GET service") - ''' diff --git a/tests/api/utils/schema/response.py b/tests/api/utils/schema/response.py index 75cd4996..1358df3a 100644 --- a/tests/api/utils/schema/response.py +++ b/tests/api/utils/schema/response.py @@ -16,16 +16,15 @@ domain = { 'type': 'object', 'properties': { - 'domain': {'type': 'string', - 'pattern': '^([a-zA-Z0-9-.]+(.com))$'}}, - 'required': ['domain'] + 'domain': {'type': 'string', 'format': 'uri'}}, + 'required': ['domain'] } origin = { 'type': 'object', 'properties': { 'origin': {'type': 'string', - 'pattern': '^([a-zA-Z0-9-.]{5,1000})$'}, + 'format': 'uri'}, 'port': {'type': 'number', 'minumum': 0, 'maximum': 100000}, @@ -46,36 +45,38 @@ cache = {'type': 'object', links = {'type': 'object', 'properties': { 'href': {'type': 'string', - 'pattern': '^/v1.0/services/[a-zA-Z0-9_-]{1,64}$'}, - 'rel': {'type': 'string'}} + 'anyOf': + [{'format': 'uri'}, + {'pattern': + '^(https?)(:/{1,3})[a-z0-9.\-:]{1,400}' + '(/v1.0/services/)[a-zA-Z0-9_-]{1,256}$'}]}, + 'rel': {'type': 'string', 'enum': ['self', 'access_url']}} } restrictions = {'type': 'array'} # Response Schema Definition for Create Service API -create_service = { +get_service = { 'type': 'object', 'properties': { + 'name': {'type': 'string'}, 'domains': {'type': 'array', 'items': domain, - 'minItems': 1, - 'maxItems': 10 + 'minItems': 1 }, 'origins': {'type': 'array', 'items': origin, - 'minItems': 1, - 'maxItems': 10 + 'minItems': 1 }, 'caching': {'type': 'array', 'items': cache, - 'minItems': 1, - 'maxItems': 10 }, 'links': {'type': 'array', 'items': links, - 'minItems': 1, - 'maxItems': 1}, + 'minItems': 1}, + 'status': {'type': 'string', + 'enum': ['in_progress', 'deployed', 'unknown', 'failed']}, 'restrictions': restrictions, }, - 'required': ['domains', 'origins', 'caching', 'links', 'restrictions'], + 'required': ['domains', 'origins', 'links'], 'additionalProperties': False} diff --git a/tests/etc/poppy_mockdb.conf b/tests/etc/poppy_cassandra.conf similarity index 98% rename from tests/etc/poppy_mockdb.conf rename to tests/etc/poppy_cassandra.conf index 7cebedd9..17393f0f 100644 --- a/tests/etc/poppy_mockdb.conf +++ b/tests/etc/poppy_cassandra.conf @@ -33,7 +33,7 @@ transport = pecan manager = default # Storage driver module (e.g., mongodb, sqlite, cassandra) -storage = mockdb +storage = cassandra [drivers:transport:falcon] diff --git a/tox.ini b/tox.ini index eb828a9a..7279d411 100644 --- a/tox.ini +++ b/tox.ini @@ -18,7 +18,7 @@ deps = -r{toxinidir}/requirements/requirements.txt -r{toxinidir}/tests/test-requirements.txt commands = pip install git+https://github.com/stackforge/opencafe.git#egg=cafe pip install git+https://github.com/tonytan4ever/python-maxcdn.git#egg=maxcdn - nosetests {posargs} + nosetests {posargs:--exclude=api} [tox:jenkins] downloadcache = ~/cache/pip