<commit_before>########
# Copyright (c) 2013 GigaSpaces Technologies Ltd. All rights reserved
#
# 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.

__author__ = 'idanmo'

import uuid
from testenv import undeploy_application as undeploy
from workflow_tests.testenv import TestCase
from workflow_tests.testenv import get_resource as resource
from workflow_tests.testenv import deploy_application as deploy
from workflow_tests.testenv import timeout
from workflow_tests.testenv import run_search as search
from workflow_tests.testenv import get_blueprint
from workflow_tests.testenv import delete_blueprint
from workflow_tests.testenv import publish_blueprint
from testenv import get_node_instance
from testenv import get_deployment_nodes
from cosmo_manager_rest_client.cosmo_manager_rest_client \
    import CosmoManagerRestClient, CosmoManagerRestCallError


class BasicWorkflowsTest(TestCase):

    def test_execute_operation(self):
        dsl_path = resource("dsl/basic.yaml")
        blueprint_id = 'my_new_blueprint'
        deployment, _ = deploy(dsl_path, blueprint_id=blueprint_id)

        self.assertEqual(blueprint_id, deployment.blueprintId)

        from plugins.cloudmock.tasks import get_machines
        result = self.send_task(get_machines)
        machines = result.get(timeout=10)

        self.assertEquals(1, len(machines))

    def test_dependencies_order_with_two_nodes(self):
        dsl_path = resource("dsl/dependencies-order-with-two-nodes.yaml")
        deployment, _ = deploy(dsl_path)

        self.assertEquals('mock_app', deployment.blueprintId)

        from plugins.testmockoperations.tasks import get_state as \
            testmock_get_state
        states = self.send_task(testmock_get_state)\
            .get(timeout=10)
        self.assertEquals(2, len(states))
        self.assertTrue('host_node' in states[0]['id'])
        self.assertTrue('db_node' in states[1]['id'])

    @timeout(seconds=60)
    def test_execute_operation_failure(self):
        from plugins.cloudmock.tasks import set_raise_exception_on_start
        self.send_task(set_raise_exception_on_start).get(timeout=10)
        dsl_path = resource("dsl/basic.yaml")
        try:
            deploy(dsl_path)
            self.fail('expected exception')
        except Exception:
            pass

    def test_cloudify_runtime_properties_injection(self):
        dsl_path = resource("dsl/dependencies-order-with-two-nodes.yaml")
        deploy(dsl_path)

        from plugins.testmockoperations.tasks import get_state as \
            testmock_get_state
        states = self.send_task(testmock_get_state).get(timeout=10)
        node_runtime_props = None
        for k, v in states[1]['capabilities'].iteritems():
            if 'host_node' in k:
                node_runtime_props = v
                break
        self.assertEquals('value1', node_runtime_props['property1'])
        # length should be 2 because of auto injected ip property
        self.assertEquals(1,
                          len(node_runtime_props),
                          msg='Expected 2 but contains: {0}'.format(
                              node_runtime_props))

    def test_dsl_with_agent_plugin(self):
        dsl_path = resource("dsl/with_plugin.yaml")
        deploy(dsl_path)

        from plugins.plugin_installer.tasks import get_installed_plugins as \
            test_get_installed_plugins
        result = self.send_task(test_get_installed_plugins)
        installed_plugins = result.get(timeout=10)
        self.assertTrue("test_plugin" in installed_plugins)

    def test_dsl_with_manager_plugin(self):
        dsl_path = resource("dsl/with_manager_plugin.yaml")
        deployment, _ = deploy(dsl_path)
        deployment_id = deployment.id
<commit_msg>CFY-287 fixed tests<commit_after>########
# Copyright (c) 2013 GigaSpaces Technologies Ltd. All rights reserved
#
# 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.

__author__ = 'idanmo'

import uuid
from testenv import undeploy_application as undeploy
from workflow_tests.testenv import TestCase
from workflow_tests.testenv import get_resource as resource
from workflow_tests.testenv import deploy_application as deploy
from workflow_tests.testenv import timeout
from workflow_tests.testenv import run_search as search
from workflow_tests.testenv import get_blueprint
from workflow_tests.testenv import delete_blueprint
from workflow_tests.testenv import publish_blueprint
from testenv import get_node_instance
from testenv import get_deployment_nodes
from cosmo_manager_rest_client.cosmo_manager_rest_client \
    import CosmoManagerRestClient, CosmoManagerRestCallError


class BasicWorkflowsTest(TestCase):

    def test_execute_operation(self):
        dsl_path = resource("dsl/basic.yaml")
        blueprint_id = 'my_new_blueprint'
        deployment, _ = deploy(dsl_path, blueprint_id=blueprint_id)

        self.assertEqual(blueprint_id, deployment.blueprintId)

        from plugins.cloudmock.tasks import get_machines
        result = self.send_task(get_machines)
        machines = result.get(timeout=10)

        self.assertEquals(1, len(machines))

    def test_dependencies_order_with_two_nodes(self):
        dsl_path = resource("dsl/dependencies-order-with-two-nodes.yaml")
        deployment, _ = deploy(dsl_path)

        self.assertEquals('mock_app', deployment.blueprintId)

        from plugins.testmockoperations.tasks import get_state as \
            testmock_get_state
        states = self.send_task(testmock_get_state)\
            .get(timeout=10)
        self.assertEquals(2, len(states))
        self.assertTrue('host_node' in states[0]['id'])
        self.assertTrue('db_node' in states[1]['id'])

    @timeout(seconds=60)
    def test_execute_operation_failure(self):
        from plugins.cloudmock.tasks import set_raise_exception_on_start
        self.send_task(set_raise_exception_on_start).get(timeout=10)
        dsl_path = resource("dsl/basic.yaml")
        try:
            deploy(dsl_path)
            self.fail('expected exception')
        except Exception:
            pass

    def test_cloudify_runtime_properties_injection(self):
        dsl_path = resource("dsl/dependencies-order-with-two-nodes.yaml")
        deploy(dsl_path)

        from plugins.testmockoperations.tasks import get_state as \
            testmock_get_state
        states = self.send_task(testmock_get_state).get(timeout=10)
        node_runtime_props = None
        for k, v in states[1]['capabilities'].iteritems():
            if 'host_node' in k:
                node_runtime_props = v
                break
        self.assertEquals('value1', node_runtime_props['property1'])
        # length should be 2 because of auto injected ip property
        self.assertEquals(1,
                          len(node_runtime_props),
                          msg='Expected 2 but contains: {0}'.format(
                              node_runtime_props))

    def test_dsl_with_agent_plugin(self):
        dsl_path = resource("dsl/with_plugin.yaml")
        # since this test involves vm with an agent, deployment_id must
        # be named after the management's worker queue name.
        deploy(dsl_path, deployment_id='cloudify.management')

        from plugins.plugin_installer.tasks import get_installed_plugins as \
            test_get_installed_plugins
        result = self.send_task(test_get_installed_plugins)
        installed_plugins = result.get(timeout=10)
        self.assertTrue("test_plugin" in installed_plugins)

    def test_dsl_with_manager_plugin(self):
        dsl_path = resource("dsl/with_manager_plugin.yaml")
        deployment, _ = deploy(dsl_path, deployment_id='cloudify.management')
        deployment_id = deployment.id

{'fraction_non_alphanumeric': 0.07596384852991649, 'fraction_numerical': 0.007436220112115318, 'mean_word_length': 3.2748166259168703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5201123', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2406, 'n_words': 596}