<commit_before># Copyright Hybrid Logic Ltd.  See LICENSE file for details.

"""
Tests for communication to applications.
"""
from subprocess import check_output
from yaml import safe_dump

from twisted.python.filepath import FilePath
from twisted.trial.unittest import TestCase

from flocker.node._docker import Unit, PortMap

from .utils import running_units, require_installed, get_nodes, flocker_deploy


class PortsTests(TestCase):
    """
    Tests for communication to applications.

    Similar to http://doc-dev.clusterhq.com/gettingstarted/tutorial/
    exposing-ports.html
    """
    @require_installed
    def setUp(self):
        pass

    def test_deployment_with_ports(self):
        """
        Ports specified are shown by docker inspect.
        """
        node_1, node_2 = get_nodes(num_nodes=2)

        temp = FilePath(self.mktemp())
        temp.makedirs()

        application_config = temp.child(b"application.yml")
        application_config.setContent(safe_dump({
            u"version": 1,
            u"applications": {
                u"mongodb-port-example": {
                    u"image": u"clusterhq/mongodb",
                    u"ports": [{
                        u"internal": 27017,
                        u"external": 27017,
                    }],
                },
            },
        }))

        deployment_config = temp.child(b"deployment.yml")
        deployment_config.setContent(safe_dump({
            u"version": 1,
            u"nodes": {
                node_1: [u"mongodb-port-example"],
                node_2: [],
            },
        }))

        flocker_deploy(deployment_config, application_config)

        running = {
            node_1: running_units(node_1),
            node_2: running_units(node_2),
        }

        expected = set([
            Unit(name=u'/mongodb-port-example',
                 container_name=u'/mongodb-port-example',
                 activation_state=u'active',
                 container_image=u'clusterhq/mongodb:latest',
                 ports=frozenset([
                     PortMap(internal_port=27017, external_port=27017)
                 ]),
                 environment=None, volumes=())
        ])

        self.assertEqual(
            running,
            {
                node_1: expected,
                node_2: set(),
            }
        )

    def test_traffic_routed(self):
        """
        An application can be accessed even from a connection to a node
        which it is not running on.
        """
        pass<commit_msg>Start using pexpect for mongodb interaction<commit_after># Copyright Hybrid Logic Ltd.  See LICENSE file for details.

"""
Tests for communication to applications.
"""
import pexpect

from subprocess import check_output
from yaml import safe_dump

from twisted.python.filepath import FilePath
from twisted.trial.unittest import TestCase

from flocker.node._docker import Unit, PortMap

from .utils import running_units, require_installed, get_nodes, flocker_deploy


class PortsTests(TestCase):
    """
    Tests for communication to applications.

    Similar to http://doc-dev.clusterhq.com/gettingstarted/tutorial/
    exposing-ports.html
    """
    @require_installed
    def setUp(self):
        pass

    def test_deployment_with_ports(self):
        """
        Ports specified are shown by docker inspect.
        """
        node_1, node_2 = get_nodes(num_nodes=2)

        temp = FilePath(self.mktemp())
        temp.makedirs()

        application_config = temp.child(b"application.yml")
        application_config.setContent(safe_dump({
            u"version": 1,
            u"applications": {
                u"mongodb-port-example": {
                    u"image": u"clusterhq/mongodb",
                    u"ports": [{
                        u"internal": 27017,
                        u"external": 27017,
                    }],
                },
            },
        }))

        deployment_config = temp.child(b"deployment.yml")
        deployment_config.setContent(safe_dump({
            u"version": 1,
            u"nodes": {
                node_1: [u"mongodb-port-example"],
                node_2: [],
            },
        }))

        flocker_deploy(deployment_config, application_config)

        running = {
            node_1: running_units(node_1),
            node_2: running_units(node_2),
        }

        expected = set([
            Unit(name=u'/mongodb-port-example',
                 container_name=u'/mongodb-port-example',
                 activation_state=u'active',
                 container_image=u'clusterhq/mongodb:latest',
                 ports=frozenset([
                     PortMap(internal_port=27017, external_port=27017)
                 ]),
                 environment=None, volumes=())
        ])

        self.assertEqual(
            running,
            {
                node_1: expected,
                node_2: set(),
            }
        )

    def test_traffic_routed(self):
        """
        An application can be accessed even from a connection to a node
        which it is not running on.
        """
        # TODO Put this stuff in setUp
        node_1, node_2 = get_nodes(num_nodes=2)

        temp = FilePath(self.mktemp())
        temp.makedirs()

        application_config = temp.child(b"application.yml")
        application_config.setContent(safe_dump({
            u"version": 1,
            u"applications": {
                u"mongodb-port-example": {
                    u"image": u"clusterhq/mongodb",
                    u"ports": [{
                        u"internal": 27017,
                        u"external": 27017,
                    }],
                },
            },
        }))

        deployment_config = temp.child(b"deployment.yml")
        deployment_config.setContent(safe_dump({
            u"version": 1,
            u"nodes": {
                node_1: [u"mongodb-port-example"],
                node_2: [],
            },
        }))

        flocker_deploy(deployment_config, application_config)

        child = pexpect.spawn ('mongo ' + node_1)
        # TODO Improve this expectation
        child.expect('.*')
        child.sendline('use example;')
        child.expect('switched to db example')
        child.sendline('db.records.insert({"flocker": "tested"})')
        child.sendline('db.records.insert({"flocker": "tested"})')
        child.sendline('db.records.find({})')
        child.expect('{ "_id" : ObjectId.*')

{'fraction_non_alphanumeric': 0.1051902048359772, 'fraction_numerical': 0.012937009086708763, 'mean_word_length': 1.7058333333333333, 'pattern_counts': {'":': 29, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4490337', 'n_tokens_mistral': 1902, 'n_tokens_neox': 1735, 'n_words': 347}