<commit_before>#########
# Copyright (c) 2014 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.


import shutil
import tempfile
import unittest
from os import path

import mock

from cloudify.workflows import local
from nova_plugin import server

import nova_plugin


IGNORED_LOCAL_WORKFLOW_MODULES = (
    'worker_installer.tasks',
    'plugin_installer.tasks'
)


class TestServer(unittest.TestCase):
    def setUp(self):
        self.counter = 0
        self.server = mock.MagicMock()
        blueprint_filename = 'test-start-operation-retry-blueprint.yaml'
        blueprint_path = path.join(path.dirname(__file__),
                                   'resources',
                                   blueprint_filename)
        plugin_yaml_filename = 'plugin.yaml'

        plugin_yaml_path = path.realpath(
            path.join(path.dirname(nova_plugin.__file__),
                      '../{0}'.format(plugin_yaml_filename)))

        self.tempdir = tempfile.mkdtemp(prefix='openstack-plugin-unit-tests-')

        temp_blueprint_path = path.join(self.tempdir, blueprint_filename)
        temp_plugin_yaml_path = path.join(self.tempdir, plugin_yaml_filename)

        shutil.copyfile(blueprint_path, temp_blueprint_path)
        shutil.copyfile(plugin_yaml_path, temp_plugin_yaml_path)

        # setup local workflow execution environment
        self.env = local.init_env(
            temp_blueprint_path,
            name=self._testMethodName,
            ignored_modules=IGNORED_LOCAL_WORKFLOW_MODULES)

    def tearDown(self):
        if path.exists(self.tempdir):
            shutil.rmtree(self.tempdir)

    @mock.patch('nova_plugin.server.create')
    @mock.patch('nova_plugin.server._set_network_and_ip_runtime_properties')
    def test_nova_server_lifecycle_start(self, *_):
        def mock_get_server_by_context(_):
            s = self.server
            if self.counter == 0:
                s.status = nova_plugin.server.SERVER_STATUS_BUILD
            else:
                s.status = nova_plugin.server.SERVER_STATUS_ACTIVE
            self.counter += 1
            return s

        with mock.patch('nova_plugin.server.get_server_by_context',
                        new=mock_get_server_by_context):
            self.env.execute('install', task_retries=3)

        self.assertEqual(2, self.counter)
        self.assertEqual(0, self.server.start.call_count)

    @mock.patch('nova_plugin.server.create')
    @mock.patch('nova_plugin.server._set_network_and_ip_runtime_properties')
    def test_nova_server_lifecycle_start_after_stop(self, *_):
        def mock_get_server_by_context(_):
            s = self.server
            if self.counter == 0:
                s.status = nova_plugin.server.SERVER_STATUS_SHUTOFF
            elif self.counter == 1:
                setattr(s,
                        nova_plugin.server.OS_EXT_STS_TASK_STATE,
                        nova_plugin.server.SERVER_TASK_STATE_POWERING_ON)
            else:
                s.status = nova_plugin.server.SERVER_STATUS_ACTIVE
            self.counter += 1
            self.server = s
            return s

        with mock.patch('nova_plugin.server.get_server_by_context',
                        new=mock_get_server_by_context):
            self.env.execute('install', task_retries=3)

        self.assertEqual(1, self.server.start.call_count)
        self.assertEqual(3, self.counter)

    @mock.patch('nova_plugin.server.create')
    @mock.patch('nova_plugin.server._set_network_and_ip_runtime_properties')
    def test_nova_server_lifecycle_start_unknown_status(self, *_):
        def mock_get_server_by_context(_):
            s = self.server
            if self.counter == 0:
                s.status = '### unknown-status ###'
            self.counter += 1
            self.server = s
            return s

        with mock.patch('nova_plugin.server.get_server_by_context',
                        new=mock_get_server_by_context):
            self.assertRaisesRegexp(RuntimeError,
                                    'Unexpected server state',
                                    self.env.execute,
                                    'install')

        self.assertEqual(0, self.server.start.call_count)
        self.assertEqual(1, self.counter)

    @mock.patch('os.path.isfile', lambda: True)
    @mock.patch(
        'openstack_plugin_common.get_single_connected_node_by_openstack_type',
        lambda x, y, z: None)
    def test_s(self, *_):
        ctx_mock = mock.MagicMock()
        ctx_mock.boostrap_context.cloudify_agent.agent_key_path = \
            'mockKeyPath'
        with mock.patch('cloudify.ctx', ctx_mock):
            self.assertEqual(server._get_private_key(True), 'mockKeyPath')<commit_msg>Test fixes.<commit_after>#########
# Copyright (c) 2014 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.


import shutil
import tempfile
import unittest
from os import path

import mock

from cloudify.workflows import local
from nova_plugin import server

import nova_plugin


IGNORED_LOCAL_WORKFLOW_MODULES = (
    'worker_installer.tasks',
    'plugin_installer.tasks'
)


class TestServer(unittest.TestCase):
    def setUp(self):
        self.counter = 0
        self.server = mock.MagicMock()
        blueprint_filename = 'test-start-operation-retry-blueprint.yaml'
        blueprint_path = path.join(path.dirname(__file__),
                                   'resources',
                                   blueprint_filename)
        plugin_yaml_filename = 'plugin.yaml'

        plugin_yaml_path = path.realpath(
            path.join(path.dirname(nova_plugin.__file__),
                      '../{0}'.format(plugin_yaml_filename)))

        self.tempdir = tempfile.mkdtemp(prefix='openstack-plugin-unit-tests-')

        temp_blueprint_path = path.join(self.tempdir, blueprint_filename)
        temp_plugin_yaml_path = path.join(self.tempdir, plugin_yaml_filename)

        shutil.copyfile(blueprint_path, temp_blueprint_path)
        shutil.copyfile(plugin_yaml_path, temp_plugin_yaml_path)

        # setup local workflow execution environment
        self.env = local.init_env(
            temp_blueprint_path,
            name=self._testMethodName,
            ignored_modules=IGNORED_LOCAL_WORKFLOW_MODULES)

    def tearDown(self):
        if path.exists(self.tempdir):
            shutil.rmtree(self.tempdir)

    @mock.patch('nova_plugin.server.create')
    @mock.patch('nova_plugin.server._set_network_and_ip_runtime_properties')
    def test_nova_server_lifecycle_start(self, *_):
        def mock_get_server_by_context(_):
            s = self.server
            if self.counter == 0:
                s.status = nova_plugin.server.SERVER_STATUS_BUILD
            else:
                s.status = nova_plugin.server.SERVER_STATUS_ACTIVE
            self.counter += 1
            return s

        with mock.patch('nova_plugin.server.get_server_by_context',
                        new=mock_get_server_by_context):
            self.env.execute('install', task_retries=3)

        self.assertEqual(2, self.counter)
        self.assertEqual(0, self.server.start.call_count)

    @mock.patch('nova_plugin.server.create')
    @mock.patch('nova_plugin.server._set_network_and_ip_runtime_properties')
    def test_nova_server_lifecycle_start_after_stop(self, *_):
        def mock_get_server_by_context(_):
            s = self.server
            if self.counter == 0:
                s.status = nova_plugin.server.SERVER_STATUS_SHUTOFF
            elif self.counter == 1:
                setattr(s,
                        nova_plugin.server.OS_EXT_STS_TASK_STATE,
                        nova_plugin.server.SERVER_TASK_STATE_POWERING_ON)
            else:
                s.status = nova_plugin.server.SERVER_STATUS_ACTIVE
            self.counter += 1
            self.server = s
            return s

        with mock.patch('nova_plugin.server.get_server_by_context',
                        new=mock_get_server_by_context):
            self.env.execute('install', task_retries=3)

        self.assertEqual(1, self.server.start.call_count)
        self.assertEqual(3, self.counter)

    @mock.patch('nova_plugin.server.create')
    @mock.patch('nova_plugin.server._set_network_and_ip_runtime_properties')
    def test_nova_server_lifecycle_start_unknown_status(self, *_):
        def mock_get_server_by_context(_):
            s = self.server
            if self.counter == 0:
                s.status = '### unknown-status ###'
            self.counter += 1
            self.server = s
            return s

        with mock.patch('nova_plugin.server.get_server_by_context',
                        new=mock_get_server_by_context):
            self.assertRaisesRegexp(RuntimeError,
                                    'Unexpected server state',
                                    self.env.execute,
                                    'install')

        self.assertEqual(0, self.server.start.call_count)
        self.assertEqual(1, self.counter)

    @mock.patch(
        'openstack_plugin_common.get_single_connected_node_by_openstack_type',
        lambda x, y, z: None)
    @mock.patch('cloudify.ctx', mock.MagicMock())
    @mock.patch('os.path.isfile', lambda: True)
    def test_s(self, *_):
        mock_key = mock.Mock(return_value='mockKeyPath')
        with mock.patch('ctx.bootstrap_context.cloudify_agent.agent_key_path',
                        mock_key):
            self.assertEqual(server._get_private_key(True), 'mockKeyPath')
{'fraction_non_alphanumeric': 0.09017013232514177, 'fraction_numerical': 0.004725897920604915, 'mean_word_length': 2.376196553924697, '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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7036707', 'n_tokens_mistral': 3128, 'n_tokens_neox': 3027, 'n_words': 558}