<commit_before># Copyright 2015 - Mirantis, Inc.
#
#    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 json
import os
from os import path
import time

from oslo_log import log as logging
from paramiko import ssh_exception
from tempest import config
from tempest import test

from mistral.tests.functional import base
from mistral import utils
from mistral.utils import ssh_utils


LOG = logging.getLogger(__name__)
CONF = config.CONF
SSH_KEYS_DIRECTORY = path.expanduser("~/.ssh/")


class SSHActionsTestsV2(base.TestCaseAdvanced):

    _service = 'workflowv2'

    @classmethod
    def _create_security_group_rule_ssh(cls):
        sec_groups = cls.mgr.security_groups_client.list_security_groups()
        sec_groups = sec_groups['security_groups']

        default_group = next(
            g for g in sec_groups if g['name'] == 'default'
        )

        rule = cls.mgr.security_group_rules_client.create_security_group_rule(
            parent_group_id=default_group['id'],
            ip_protocol="tcp",
            from_port=22,
            to_port=22,
            cidr="0.0.0.0/0"
        )

        cls.ssh_rule_id = rule['security_group_rule']['id']

    @classmethod
    def _create_server(cls, server_name, **kwargs):
        return cls.server_client.create_server(
            name=server_name,
            imageRef=CONF.compute.image_ref,
            flavorRef=CONF.compute.flavor_ref,
            **kwargs
        ).get('server')

    @classmethod
    def _associate_floating_ip_to_server(cls, server_id):
        fl_ip_client = cls.mgr.floating_ips_client

        all_ips = fl_ip_client.list_floating_ips().get(
            'floating_ips'
        )
        free_ips = list(
            filter(lambda fl_ip: fl_ip['instance_id'] is None, all_ips)
        )

        if free_ips:
            ip = free_ips[0]['ip']
        else:
            # Allocate new floating ip.
            ip = fl_ip_client.create_floating_ip()['floating_ip']['ip']

        # Associate IP.<commit_msg>Fixing reference of floating_ips_client in tests<commit_after># Copyright 2015 - Mirantis, Inc.
#
#    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 json
import os
from os import path
import time

from oslo_log import log as logging
from paramiko import ssh_exception
from tempest import config
from tempest import test

from mistral.tests.functional import base
from mistral import utils
from mistral.utils import ssh_utils


LOG = logging.getLogger(__name__)
CONF = config.CONF
SSH_KEYS_DIRECTORY = path.expanduser("~/.ssh/")


class SSHActionsTestsV2(base.TestCaseAdvanced):

    _service = 'workflowv2'

    @classmethod
    def _create_security_group_rule_ssh(cls):
        sec_groups = cls.mgr.security_groups_client.list_security_groups()
        sec_groups = sec_groups['security_groups']

        default_group = next(
            g for g in sec_groups if g['name'] == 'default'
        )

        rule = cls.mgr.security_group_rules_client.create_security_group_rule(
            parent_group_id=default_group['id'],
            ip_protocol="tcp",
            from_port=22,
            to_port=22,
            cidr="0.0.0.0/0"
        )

        cls.ssh_rule_id = rule['security_group_rule']['id']

    @classmethod
    def _create_server(cls, server_name, **kwargs):
        return cls.server_client.create_server(
            name=server_name,
            imageRef=CONF.compute.image_ref,
            flavorRef=CONF.compute.flavor_ref,
            **kwargs
        ).get('server')

    @classmethod
    def _associate_floating_ip_to_server(cls, server_id):
        fl_ip_client = cls.mgr.compute_floating_ips_client

        all_ips = fl_ip_client.list_floating_ips().get(
            'floating_ips'
        )
        free_ips = list(
            filter(lambda fl_ip: fl_ip['instance_id'] is None, all_ips)
        )

        if free_ips:
            ip = free_ips[0]['ip']
        else:
            # Allocate new floating ip.
            ip = fl_ip_client.create_floating_ip()['floating_ip']['ip']

        # Associate IP.
{'fraction_non_alphanumeric': 0.08416912487708948, 'fraction_numerical': 0.007866273352999017, 'mean_word_length': 2.7507374631268435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '1692461', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1505, 'n_words': 406}