<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.

# Built-in Imports

# Third Party Imports
import testtools
from docker import Client
from docker import errors

# Cloudify Imports is imported and used in operations
from docker_plugin import tasks
from cloudify.mocks import MockCloudifyContext
from cloudify.exceptions import NonRecoverableError


class TestStop(testtools.TestCase):

    def get_client(self, daemon_client):
        try:
            return Client(**daemon_client)
        except errors.DockerException as e:
            raise NonRecoverableError('Error while getting client: '
                                      '{0}.'.format(str(e)))

    def mock_ctx(self, test_name):
        """ Creates a mock context for the instance
            tests
        """

        test_node_id = test_name
        test_properties = {
            '': 'cloudify-test-container',
            'use_external_resource': False,
            'image': 'docker-test-image',
            'port': None,
            'params': {
                'ports': {
                    80: 80
                },
                'stdin_open': True,
                'tty': True,
                'command': '/bin/sleep 60'
            }
        }

        ctx = MockCloudifyContext(
            node_id=test_node_id,
            properties=test_properties
        )

        return ctx

    def test_stop_clean(self):
        """ This test pulls the docker-dev image from
            the docker hub and deletes it.
        """
        ctx = self.mock_ctx('test_stop_clean')
        daemon_client = {}
        client = self.get_client(daemon_client)

        arguments = {}
        arguments['name'] = ctx.node.properties.get('resource_id')
        arguments['image'] = ctx.node.properties.get('image')
        for key in ctx.node.properties.get('params').keys():
            arguments[key] = ctx.node.properties['params'][key]

        container = client.create_container(**arguments)

        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = container.get('Id')
        ctx.instance.runtime_properties['container_id'] = \
            ctx.node.properties['resource_id']

        d = {}
        d['container'] = container.get('Id')

        client.start(**d)

        tasks.stop(20, ctx=ctx)

        self.assertTrue(
            container.get('Id') in
            [c.get('Id') for c in client.containers(all=True)] and
            container.get('Id') not in
            [c.get('Id') for c in client.containers()])

        client.remove_container(container.get('Id'))<commit_msg>added the interval to the test command<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.

# Built-in Imports

# Third Party Imports
import testtools
from docker import Client
from docker import errors

# Cloudify Imports is imported and used in operations
from docker_plugin import tasks
from cloudify.mocks import MockCloudifyContext
from cloudify.exceptions import NonRecoverableError


class TestStop(testtools.TestCase):

    def get_client(self, daemon_client):
        try:
            return Client(**daemon_client)
        except errors.DockerException as e:
            raise NonRecoverableError('Error while getting client: '
                                      '{0}.'.format(str(e)))

    def mock_ctx(self, test_name):
        """ Creates a mock context for the instance
            tests
        """

        test_node_id = test_name
        test_properties = {
            '': 'cloudify-test-container',
            'use_external_resource': False,
            'image': 'docker-test-image',
            'port': None,
            'params': {
                'ports': {
                    80: 80
                },
                'stdin_open': True,
                'tty': True,
                'command': '/bin/sleep 60'
            }
        }

        ctx = MockCloudifyContext(
            node_id=test_node_id,
            properties=test_properties
        )

        return ctx

    def test_stop_clean(self):
        """ This test pulls the docker-dev image from
            the docker hub and deletes it.
        """
        ctx = self.mock_ctx('test_stop_clean')
        daemon_client = {}
        client = self.get_client(daemon_client)

        arguments = {}
        arguments['name'] = ctx.node.properties.get('resource_id')
        arguments['image'] = ctx.node.properties.get('image')
        for key in ctx.node.properties.get('params').keys():
            arguments[key] = ctx.node.properties['params'][key]

        container = client.create_container(**arguments)

        ctx.node.properties['use_external_resource'] = True
        ctx.node.properties['resource_id'] = container.get('Id')
        ctx.instance.runtime_properties['container_id'] = \
            ctx.node.properties['resource_id']

        d = {}
        d['container'] = container.get('Id')

        client.start(**d)

        tasks.stop(10, 20, ctx=ctx)

        self.assertTrue(
            container.get('Id') in
            [c.get('Id') for c in client.containers(all=True)] and
            container.get('Id') not in
            [c.get('Id') for c in client.containers()])

        client.remove_container(container.get('Id'))
{'fraction_non_alphanumeric': 0.10497751589393704, 'fraction_numerical': 0.00558226081563033, 'mean_word_length': 2.241206030150754, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7536733', 'n_tokens_mistral': 1769, 'n_tokens_neox': 1686, 'n_words': 490}