diff --git a/senlin/tests/functional/api.py b/senlin/tests/functional/api.py index 531766b69..89e399b91 100644 --- a/senlin/tests/functional/api.py +++ b/senlin/tests/functional/api.py @@ -11,6 +11,7 @@ # under the License. from oslo_serialization import jsonutils +import requests def create_cluster(client, name, profile_id, desired_capacity, @@ -128,6 +129,55 @@ def delete_policy(client, policy_id): return +def create_webhook(client, name, obj_type, obj_id, action, + credential=None, params=None): + rel_url = 'webhooks' + status = [200] + data = { + 'webhook': { + 'name': name, + 'obj_type': obj_type, + 'obj_id': obj_id, + 'action': action, + 'credential': credential, + 'params': params + } + } + body = jsonutils.dumps(data) + resp = client.api_request('POST', rel_url, body=body, + resp_status=status) + + webhook = resp.body['webhook'] + return webhook + + +def get_webhook(client, webhook_id, ignore_missing=False): + rel_url = 'webhooks/%(id)s' % {'id': webhook_id} + status = [200, 404] if ignore_missing else [200] + resp = client.api_request('GET', rel_url, resp_status=status) + return resp if ignore_missing else resp.body['webhook'] + + +def trigger_webhook(webhook_url, params=None): + body = None + if params is not None: + body = jsonutils.dumps(params) + resp = requests.request('POST', webhook_url, data=body) + if resp.content: + resp_body = jsonutils.loads(resp.content) + if 'action' in resp_body: + return resp_body['action'] + + raise Exception('Webhook %s triggering failed.' % webhook_url) + + +def delete_webhook(client, webhook_id): + rel_url = 'webhooks/%(id)s' % {'id': webhook_id} + status = [204] + client.api_request('DELETE', rel_url, resp_status=status) + return + + def get_action(client, action_id, ignore_missing=False): rel_url = 'actions/%(id)s' % {'id': action_id} status = [200, 404] if ignore_missing else [200] diff --git a/senlin/tests/functional/test_webhook.py b/senlin/tests/functional/test_webhook.py new file mode 100644 index 000000000..cae261171 --- /dev/null +++ b/senlin/tests/functional/test_webhook.py @@ -0,0 +1,93 @@ +# 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. + +from oslo_log import log as logging + +from senlin.tests.functional import api as test_api +from senlin.tests.functional import base +from senlin.tests.functional.utils import test_utils + +LOG = logging.getLogger(__name__) + + +class TestWebhook(base.SenlinFunctionalTest): + def setUp(self): + super(TestWebhook, self).setUp() + # Create profile + self.profile = test_api.create_profile(self.client, 'test-profile', + test_utils.spec_nova_server) + + def tearDown(self): + # Delete profile + test_api.delete_profile(self.client, self.profile['id']) + super(TestWebhook, self).tearDown() + + def test_webhook(self): + # Create cluster + desired_capacity = 2 + min_size = 1 + max_size = 5 + cluster = test_api.create_cluster(self.client, 'test-cluster', + self.profile['id'], desired_capacity, + min_size, max_size) + + # Wait and verify cluster creation result + cluster = test_utils.wait_for_status(test_api.get_cluster, self.client, + cluster['id'], 'ACTIVE') + self.assertEqual('test-cluster', cluster['name']) + self.assertEqual(desired_capacity, cluster['desired_capacity']) + self.assertEqual(min_size, cluster['min_size']) + self.assertEqual(max_size, cluster['max_size']) + self.assertEqual(desired_capacity, len(cluster['nodes'])) + + # Create a webhook targets on cluster resize action with params + params = { + 'adjustment_type': 'EXACT_CAPACITY', + 'number': 2, + } + webhook = test_api.create_webhook(self.client, 'webhook-c-resize', + 'cluster', cluster['id'], + 'CLUSTER_RESIZE', + params=params) + webhook_url = webhook['url'] + + # Verify webhook params + self.assertEqual('webhook-c-resize', webhook['name']) + self.assertEqual('cluster', webhook['obj_type']) + self.assertEqual(cluster['id'], webhook['obj_id']) + self.assertEqual('CLUSTER_RESIZE', webhook['action']) + self.assertEqual(params, webhook['params']) + + # Trigger webhook and wait for action complete + action_id = test_api.trigger_webhook(webhook_url) + test_utils.wait_for_status(test_api.get_action, self.client, + action_id, 'SUCCEEDED') + + # Verify action is as expected + action = test_api.get_action(self.client, action_id) + self.assertEqual('CLUSTER_RESIZE', action['action']) + self.assertEqual(cluster['id'], action['target']) + self.assertEqual(params, action['inputs']) + + # Verify cluster resize result + cluster = test_api.get_cluster(self.client, cluster['id']) + self.assertEqual('ACTIVE', cluster['status']) + self.assertEqual(2, len(cluster['nodes'])) + + # Delete webhook + test_api.delete_webhook(self.client, webhook['id']) + + # Delete cluster + test_api.delete_cluster(self.client, cluster['id']) + test_utils.wait_for_status(test_api.get_cluster, self.client, + cluster['id'], 'ACTIVE', + ignore_missing=True)