From 0b70384bc8b8095cffb5f3d411dc7d379af3ffb7 Mon Sep 17 00:00:00 2001 From: Przemyslaw Kaminski Date: Tue, 30 Jun 2015 15:28:40 +0200 Subject: [PATCH] Testing: autonomous resource testing draft --- example.py | 64 +++++++++++++++------------- resources/glance_api_service/test.py | 28 ++++++++++++ resources/haproxy_service/test.py | 8 ++++ resources/keystone_service/test.py | 8 ++++ resources/rabbitmq_service/test.py | 7 +++ solar/solar/cli.py | 5 +++ solar/solar/core/testing.py | 31 ++++++++++++++ solar/solar/operations.py | 4 -- 8 files changed, 121 insertions(+), 34 deletions(-) create mode 100644 resources/glance_api_service/test.py create mode 100644 resources/haproxy_service/test.py create mode 100644 resources/keystone_service/test.py create mode 100644 resources/rabbitmq_service/test.py create mode 100644 solar/solar/core/testing.py diff --git a/example.py b/example.py index 2c944a81..d498e21b 100644 --- a/example.py +++ b/example.py @@ -8,6 +8,7 @@ from solar.core import actions from solar.core import virtual_resource as vr from solar.core import resource from solar.core import signals +from solar.core import testing from solar.interfaces.db import get_db from solar.core.resource_provider import GitProvider, RemoteZipProvider @@ -224,39 +225,42 @@ def deploy(): time.sleep(10) # test working configuration - requests.get('http://%s:%s' % (keystone_service1.args['ip'].value, keystone_service1.args['port'].value)) - requests.get('http://%s:%s' % (keystone_service2.args['ip'].value, keystone_service2.args['port'].value)) - requests.get('http://%s:%s' % (haproxy_service.args['ip'].value, haproxy_service.args['ports'].value[0]['value'][0]['value'])) + testing.test_all() + # requests.get('http://%s:%s' % (keystone_service1.args['ip'].value, keystone_service1.args['port'].value)) + # requests.get('http://%s:%s' % (keystone_service2.args['ip'].value, keystone_service2.args['port'].value)) + # requests.get('http://%s:%s' % (haproxy_service.args['ip'].value, haproxy_service.args['ports'].value[0]['value'][0]['value'])) + # + # token_data = requests.post( + # 'http://%s:%s/v2.0/tokens' % (haproxy_service.args['ip'].value, haproxy_keystone_config.args['listen_port'].value), + # json.dumps({ + # 'auth': { + # 'tenantName': glance_keystone_user.args['tenant_name'].value, + # 'passwordCredentials': { + # 'username': glance_keystone_user.args['user_name'].value, + # 'password': glance_keystone_user.args['user_password'].value, + # } + # } + # }), + # headers={'Content-Type': 'application/json'} + # ) + # + # token = token_data.json()['access']['token']['id'] + # print 'TOKEN: {}'.format(token) - token_data = requests.post( - 'http://%s:%s/v2.0/tokens' % (haproxy_service.args['ip'].value, haproxy_keystone_config.args['listen_port'].value), - json.dumps({ - 'auth': { - 'tenantName': glance_keystone_user.args['tenant_name'].value, - 'passwordCredentials': { - 'username': glance_keystone_user.args['user_name'].value, - 'password': glance_keystone_user.args['user_password'].value, - } - } - }), - headers={'Content-Type': 'application/json'} - ) + # requests.get('http://%s:%s' % (rabbitmq_service1.args['ip'].value, rabbitmq_service1.args['management_port'].value)) - token = token_data.json()['access']['token']['id'] - print 'TOKEN: {}'.format(token) + # images = requests.get( + # 'http://%s:%s/v1/images' % (glance_api_container.args['ip'].value, haproxy_glance_api_config.args['listen_port'].value), + # headers={'X-Auth-Token': token} + # ) + # assert images.json() == {'images': []} - requests.get('http://%s:%s' % (rabbitmq_service1.args['ip'].value, rabbitmq_service1.args['management_port'].value)) - - images = requests.get( - 'http://%s:%s/v1/images' % (glance_api_container.args['ip'].value, haproxy_glance_api_config.args['listen_port'].value), - headers={'X-Auth-Token': token} - ) - assert images.json() == {'images': []} - images = requests.get( - 'http://%s:%s' % (glance_registry_container.args['ip'].value, glance_registry_container.args['ports'].value[0]['value'][0]['value']), - headers={'X-Auth-Token': token} - ) - assert images.json() == {'images': []} + # TODO + # images = requests.get( + # 'http://%s:%s' % (glance_registry_container.args['ip'].value, glance_registry_container.args['ports'].value[0]['value'][0]['value']), + # headers={'X-Auth-Token': token} + # ) + # assert images.json() == {'images': []} @click.command() diff --git a/resources/glance_api_service/test.py b/resources/glance_api_service/test.py new file mode 100644 index 00000000..d56e7f26 --- /dev/null +++ b/resources/glance_api_service/test.py @@ -0,0 +1,28 @@ +import json +import requests + + +def test(resource): + print 'Testing glance_service' + token_data = requests.post( + 'http://%s:%s/v2.0/tokens' % (resource.args['ip'].value, resource.args['listen_port'].value), + json.dumps({ + 'auth': { + 'tenantName': resource.args['tenant_name'].value, + 'passwordCredentials': { + 'username': resource.args['user_name'].value, + 'password': resource.args['user_password'].value, + } + } + }), + headers={'Content-Type': 'application/json'} + ) + + token = token_data.json()['access']['token']['id'] + print 'GLANCE TOKEN: {}'.format(token) + + images = requests.get( + 'http://%s:%s/v1/images' % (resource.args['ip'].value, resource.args['listen_port'].value), + headers={'X-Auth-Token': token} + ) + assert images.json() == {'images': []} diff --git a/resources/haproxy_service/test.py b/resources/haproxy_service/test.py new file mode 100644 index 00000000..f5941d0d --- /dev/null +++ b/resources/haproxy_service/test.py @@ -0,0 +1,8 @@ +import requests + + +def test(resource): + print 'Testing haproxy_service' + requests.get( + 'http://%s:%s' % (resource.args['ip'].value, resource.args['ports'].value[0]['value'][0]['value']) + ) diff --git a/resources/keystone_service/test.py b/resources/keystone_service/test.py new file mode 100644 index 00000000..b397bd2e --- /dev/null +++ b/resources/keystone_service/test.py @@ -0,0 +1,8 @@ +import requests + + +def test(resource): + print 'Testing keystone_service' + requests.get( + 'http://%s:%s' % (resource.args['ip'].value, resource.args['port'].value) + ) diff --git a/resources/rabbitmq_service/test.py b/resources/rabbitmq_service/test.py new file mode 100644 index 00000000..da81cb17 --- /dev/null +++ b/resources/rabbitmq_service/test.py @@ -0,0 +1,7 @@ +import requests + + +def test(resource): + print 'Testing rabbitmq_service' + + requests.get('http://%s:%s' % (resource.args['ip'].value, resource.args['management_port'].value)) diff --git a/solar/solar/cli.py b/solar/solar/cli.py index 8b97cb94..8b58bcb5 100644 --- a/solar/solar/cli.py +++ b/solar/solar/cli.py @@ -34,6 +34,7 @@ from solar.core.resource import assign_resources_to_nodes from solar.core import signals from solar.core.tags_set_parser import Expression from solar.core import virtual_resource as vr +from solar.core import testing from solar.interfaces.db import get_db # NOTE: these are extensions, they shouldn't be imported here @@ -158,6 +159,10 @@ def init_changes(): elif uid: click.echo(operations.rollback_uid(uid)) + @changes.command() + def test(): + testing.test_all() + def init_cli_connect(): @main.command() diff --git a/solar/solar/core/testing.py b/solar/solar/core/testing.py new file mode 100644 index 00000000..46146424 --- /dev/null +++ b/solar/solar/core/testing.py @@ -0,0 +1,31 @@ +import imp +import networkx as nx +import os + +from solar.core import resource +from solar.core import signals + + +def test_all(): + conn_graph = signals.detailed_connection_graph() + srt = nx.topological_sort(conn_graph) + + for name in srt: + print 'Trying {}'.format(name) + r = resource.load(name) + + script_path = os.path.join(r.metadata['base_path'], 'test.py') + if not os.path.exists(script_path): + continue + + print 'File {} found'.format(script_path) + + with open(script_path) as f: + module = imp.load_module( + '{}_test'.format(name), + f, + script_path, + ('', 'r', imp.PY_SOURCE) + ) + + module.test(r) diff --git a/solar/solar/operations.py b/solar/solar/operations.py index 5a94e42e..87483430 100644 --- a/solar/solar/operations.py +++ b/solar/solar/operations.py @@ -62,8 +62,6 @@ def create_diff(staged, commited): def _stage_changes(staged_resources, conn_graph, commited_resources, staged_log): - action = None - try: srt = nx.topological_sort(conn_graph) except: @@ -195,5 +193,3 @@ def rollback_all(): while cl: rollback(cl.pop()) - -