ariatosca-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [07/16] incubator-ariatosca git commit: Add tests for services delete
Date Sat, 15 Apr 2017 13:07:53 GMT
Add tests for services delete


Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/b332bcb6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/b332bcb6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/b332bcb6

Branch: refs/heads/ARIA-48-aria-cli
Commit: b332bcb6d0d5b4864326e627d8142a021bf74042
Parents: 2bda233
Author: Avia Efrat <avia@gigaspaces.com>
Authored: Thu Apr 13 00:37:08 2017 +0300
Committer: Avia Efrat <avia@gigaspaces.com>
Committed: Sat Apr 15 15:51:54 2017 +0300

----------------------------------------------------------------------
 tests/cli/test_services.py | 50 ++++++++++++++++++++++++++++++----
 tests/cli/utils.py         | 53 +++++++++++++++++++++++++++---------
 tests/mock/models.py       | 60 ++++++++++++++++++++++++++---------------
 3 files changed, 125 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b332bcb6/tests/cli/test_services.py
----------------------------------------------------------------------
diff --git a/tests/cli/test_services.py b/tests/cli/test_services.py
index 0c89b14..af10a45 100644
--- a/tests/cli/test_services.py
+++ b/tests/cli/test_services.py
@@ -3,7 +3,8 @@ from mock import ANY
 from aria.cli.exceptions import AriaCliError
 from aria.cli.env import Environment
 from aria.core import Core
-from aria.exceptions import AriaException
+from aria.exceptions import (AriaException, DependentActiveExecutionsError,
+                             DependentAvailableNodesError)
 from aria.storage import exceptions as storage_exceptions
 from tests.cli.base_test import TestCliBase, mock_storage, raise_exception, assert_exception_raised
 from tests.mock.models import create_service, create_service_template
@@ -17,8 +18,8 @@ class TestServicesList(TestCliBase):
         (' --sort-by name', '', 'name', 'asc'),
         (' --sort-by name', ' --descending', 'name', 'desc')
     ])
-    def test_services_list_specified_service_template(self, monkeypatch, mock_storage, sort_by,
-                                                      order, sort_by_in_output, order_in_output):
+    def test_list_specified_service_template(self, monkeypatch, mock_storage, sort_by, order,
+                                             sort_by_in_output, order_in_output):
 
         monkeypatch.setattr(Environment, 'model_storage', mock_storage)
         self.invoke('services list -t test_st{sort_by}{order}'.format(sort_by=sort_by,
@@ -39,8 +40,8 @@ class TestServicesList(TestCliBase):
         (' --sort-by name', '', 'name', 'asc'),
         (' --sort-by name', ' --descending', 'name', 'desc')
     ])
-    def test_services_list_no_specified_service_template(self, monkeypatch, mock_storage,
sort_by,
-                                                         order, sort_by_in_output, order_in_output):
+    def test_list_no_specified_service_template(self, monkeypatch, mock_storage, sort_by,
order,
+                                                sort_by_in_output, order_in_output):
 
         monkeypatch.setattr(Environment, 'model_storage', mock_storage)
         self.invoke('services list{sort_by}{order}'.format(sort_by=sort_by,
@@ -113,3 +114,42 @@ class TestServicesCreate(TestCliBase):
         assert 'error creating service `test_s`' in self.logger_output_string
         assert 'input1' in self.logger_output_string and 'value1' in self.logger_output_string
         assert "Service created. The service's name is test_s" not in self.logger_output_string
+
+
+class TestServicesDelete(TestCliBase):
+
+    def test_delete_no_exception(self, monkeypatch, mock_object):
+
+        monkeypatch.setattr(Environment, 'model_storage', mock_object)
+        monkeypatch.setattr(Core, 'delete_service', mock_object)
+        self.invoke('services delete test_s')
+        assert 'Deleting service test_s...' in self.logger_output_string
+        assert 'Service test_s deleted' in self.logger_output_string
+
+    def test_delete_active_execution_error(self, monkeypatch, mock_storage):
+        monkeypatch.setattr(Environment, 'model_storage', mock_storage)
+        assert_exception_raised(
+            self.invoke('services delete service_with_active_executions'),
+            expected_exception=DependentActiveExecutionsError,
+            expected_msg="Can't delete service service_with_active_executions - there is
an active "
+                         "execution for this service. Active execution id: 1"
+        )
+        assert 'Deleting service service_with_active_executions...' in self.logger_output_string
+
+    def test_delete_available_nodes_error(self, monkeypatch, mock_storage):
+        monkeypatch.setattr(Environment, 'model_storage', mock_storage)
+        assert_exception_raised(
+            self.invoke('services delete service_with_available_nodes'),
+            expected_exception=DependentAvailableNodesError,
+            expected_msg="Can't delete service service_with_active_executions - "
+                         "there are available nodes for this service. Available node ids:
1"
+        )
+        assert 'Deleting service service_with_available_nodes...' in self.logger_output_string
+
+    def test_delete_available_nodes_error_with_force(self, monkeypatch, mock_storage):
+        monkeypatch.setattr(Environment, 'model_storage', mock_storage)
+        self.invoke('services delete service_with_available_nodes --force')
+
+        assert mock_storage.service.delete.call_count == 1
+        assert 'Deleting service service_with_available_nodes...' in self.logger_output_string
+        assert 'Service service_with_available_nodes deleted' in self.logger_output_string

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b332bcb6/tests/cli/utils.py
----------------------------------------------------------------------
diff --git a/tests/cli/utils.py b/tests/cli/utils.py
index 97f6a67..222a225 100644
--- a/tests/cli/utils.py
+++ b/tests/cli/utils.py
@@ -1,8 +1,8 @@
 import logging
-
+from aria.modeling import models
 from mock import MagicMock
 
-from tests.mock import models
+from tests.mock import models as mock_models
 
 
 def setup_logger(logger_name,
@@ -55,36 +55,36 @@ class MockServiceTemplateStorage(object):
 
     @staticmethod
     def list(**_):
-        return [models.create_service_template('test_st'),
-                models.create_service_template('test_st2')]
+        return [mock_models.create_service_template('test_st'),
+                mock_models.create_service_template('test_st2')]
 
     @staticmethod
     def get(id):
-        st = models.create_service_template('test_st')
+        st = mock_models.create_service_template('test_st')
         if id == '1':  # no services and no description.
             st.services = []
         if id == '2':  # no services, but an description
             st.description = 'test_description'
             st.services = []
         if id == '3':  # one service, and a description
-            service = models.create_service(st, 'test_s')
+            service = mock_models.create_service(st, 'test_s')
             st.description = 'test_description'
             st.services = [service]
         if id == '4':  # one service, and a description
-            service = models.create_service(st, 'test_s')
+            service = mock_models.create_service(st, 'test_s')
             st.services = [service]
         return st
 
     @staticmethod
     def get_by_name(name):
-        st = models.create_service_template('test_st')
+        st = mock_models.create_service_template('test_st')
         if name == 'with_inputs':
-            input = models.create_input(name='input1', value='value1')
+            input = mock_models.create_input(name='input1', value='value1')
             st.inputs = {'input1': input}
         if name == 'without_inputs':
             st.inputs = {}
         if name == 'one_service':
-            service = models.create_service(st, 'test_s')
+            service = mock_models.create_service(st, 'test_s')
             st.services = [service]
         return st
 
@@ -92,5 +92,34 @@ class MockServiceTemplateStorage(object):
 class MockServiceStorage(object):
 
     def __init__(self):
-        st = models.create_service_template('test_st')
-        self.list = MagicMock(return_value=[models.create_service(st, 'test_s')])
+        self.st = mock_models.create_service_template('test_st')
+        self.list = MagicMock(return_value=[mock_models.create_service(self.st, 'test_s')])
+        self.delete = MagicMock()
+
+    @staticmethod
+    def get(id):
+        test_st = mock_models.create_service_template('test_st')
+        test_s = mock_models.create_service(test_st, 'service_with_active_executions')
+        if id == '1':
+            execution = mock_models.create_execution(test_s, status=models.Execution.STARTED)
+            execution.id = '1'
+            test_s.executions = [execution]
+        if id == '2':
+            node_template = mock_models.create_node_template(service_template=test_st)
+            node = mock_models.create_node(name='test_node',
+                                           dependency_node_template=node_template,
+                                           service=test_s,
+                                           state=models.Node.STARTED)
+            node.id = '1'
+        return test_s
+
+    @staticmethod
+    def get_by_name(name):
+        if name == 'service_with_active_executions':
+            m = MagicMock()
+            m.id = '1'
+            return m
+        if name == 'service_with_available_nodes':
+            m = MagicMock()
+            m.id = '2'
+            return m

http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/b332bcb6/tests/mock/models.py
----------------------------------------------------------------------
diff --git a/tests/mock/models.py b/tests/mock/models.py
index 4b935d2..37ecbc0 100644
--- a/tests/mock/models.py
+++ b/tests/mock/models.py
@@ -39,6 +39,7 @@ from aria.orchestrator.workflows.builtin.workflows import (
 
 SERVICE_NAME = 'test_service_name'
 SERVICE_TEMPLATE_NAME = 'test_service_template_name'
+NODE_TEMPLATE_NAME = 'test_node_template'
 WORKFLOW_NAME = 'test_workflow_name'
 TASK_RETRY_INTERVAL = 1
 TASK_MAX_ATTEMPTS = 1
@@ -80,6 +81,33 @@ def create_service(service_template, name=SERVICE_NAME):
     )
 
 
+def create_node_template(service_template,
+                         name=NODE_TEMPLATE_NAME,
+                         type=models.Type(variant='node', name='test_node_type'),
+                         capability_templates=None,
+                         requirement_templates=None,
+                         interface_templates=None,
+                         default_instances=1,
+                         min_instances=1,
+                         max_instances=1):
+    capability_templates = capability_templates or {}
+    requirement_templates = requirement_templates or []
+    interface_templates = interface_templates or {}
+    node_template = models.NodeTemplate(
+        name=name,
+        type=type,
+        capability_templates=capability_templates,
+        requirement_templates=requirement_templates,
+        interface_templates=interface_templates,
+        default_instances=default_instances,
+        min_instances=min_instances,
+        max_instances=max_instances,
+        service_template=service_template)
+
+    service_template.node_templates[node_template.name] = node_template
+    return node_template
+
+
 def create_dependency_node_template(service_template, name=DEPENDENCY_NODE_TEMPLATE_NAME):
     node_type = service_template.node_types.get_descendant('test_node_type')
     capability_type = service_template.capability_types.get_descendant('test_capability_type')
@@ -88,18 +116,12 @@ def create_dependency_node_template(service_template, name=DEPENDENCY_NODE_TEMPL
         name='capability',
         type=capability_type
     )
-
-    node_template = models.NodeTemplate(
+    return create_node_template(
+        service_template=service_template,
         name=name,
         type=node_type,
-        capability_templates=_dictify(capability_template),
-        default_instances=1,
-        min_instances=1,
-        max_instances=1,
-        service_template=service_template
+        capability_templates=_dictify(capability_template)
     )
-    service_template.node_templates[node_template.name] = node_template
-    return node_template
 
 
 def create_dependent_node_template(
@@ -110,29 +132,25 @@ def create_dependent_node_template(
         name='requirement',
         target_node_template=dependency_node_template
     )
-
-    node_template = models.NodeTemplate(
+    return create_node_template(
+        service_template=service_template,
         name=name,
         type=the_type,
-        default_instances=1,
-        min_instances=1,
-        max_instances=1,
         interface_templates=_dictify(get_standard_interface_template(service_template)),
         requirement_templates=[requirement_template],
-        service_template=service_template
     )
-    service_template.node_templates[node_template.name] = node_template
-    return node_template
 
 
-def create_node(name, dependency_node_template, service):
+def create_node(name, dependency_node_template, service,
+                interfaces=None, state=models.Node.INITIAL):
+
     node = models.Node(
         name=name,
         type=dependency_node_template.type,
         runtime_properties={'ip': '1.1.1.1'},
         version=None,
         node_template=dependency_node_template,
-        state=models.Node.INITIAL,
+        state=state,
         scaling_groups=[],
         service=service,
         interfaces=get_standard_interface(service),
@@ -185,10 +203,10 @@ def create_interface(service, interface_name, operation_name, operation_kwargs=N
     )
 
 
-def create_execution(service):
+def create_execution(service, status=models.Execution.PENDING):
     return models.Execution(
         service=service,
-        status=models.Execution.PENDING,
+        status=status,
         workflow_name=WORKFLOW_NAME,
         created_at=datetime.utcnow(),
         started_at=datetime.utcnow(),


Mime
View raw message