<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 fabric.api
import fabric.contrib.files

from cosmo_tester.framework.testenv import TestCase
from cosmo_tester.framework.util import (
    YamlPatcher,
    get_actual_keypath
)

PRIVATE_KEY_PATH = '/tmp/home/neutron-test.pem'


class NeutronGaloreTest(TestCase):

    def test_neutron_galore(self):

        blueprint_path = self.copy_blueprint('neutron-galore')
        self.blueprint_yaml = blueprint_path / 'blueprint.yaml'

        inputs = {
            'server_name': 'novaservertest',
            'image_name': self.env.ubuntu_image_name,
            'flavor_name': self.env.flavor_name,
            'private_key_path': PRIVATE_KEY_PATH,
        }

        before, after = self.upload_deploy_and_execute_install(inputs=inputs)

        node_states = self.get_delta_node_states(before, after)
        self.post_install_assertions(node_states)

        self._test_use_external_resource(inputs=inputs)

        self.execute_uninstall()

        self.post_uninstall_assertions()

    def post_install_assertions(self, node_states):

        def p(name):
            return '{}{}'.format(self.env.resources_prefix, name)

        management_network_name = self.env.management_network_name
        agents_security_group = self.env.agents_security_group
        if self.env.is_provider_bootstrap:
            # using existing resources - manager blueprints currently don't
            # use the prefix feature for the resources brought up during
            # bootstrap, and thus this section is only relevant if providers
            # have been used
            management_network_name = p(management_network_name)
            agents_security_group = p(agents_security_group)

        openstack = self.get_openstack_components(node_states)

        self.assertTrue(self._check_if_private_key_is_on_manager())

        port_assigned_addr = openstack['server']['addresses'][
            p('neutron_network_test')][0]['addr']
        # the port will get connected to the dst security group automatically
        #  when the server connects to the security group
        port_security_groups = openstack['port']['security_groups']
        port_fixed_ip = openstack['port']['fixed_ips'][0]['ip_address']
        port_network_id = openstack['port']['network_id']
        port_subnet_id = openstack['port']['fixed_ips'][0]['subnet_id']
        router_network_id = openstack['router']['external_gateway_info'][
            'network_id']
        sg_src_id = openstack['sg_src']['id']
        sg_dst_id = openstack['sg_dst']['id']
        sg_3 = openstack['sg_3']['id']
        sg_4 = openstack['sg_4']['id']
        network_subnet_id = openstack['network']['subnets'][0]

        self.assertEqual(openstack['server']['addresses']
                         [management_network_name][0]
                         ['OS-EXT-IPS:type'], 'fixed')
        self.assertEqual(openstack['server']['addresses']
                         [p('neutron_network_test')][0]
                         ['OS-EXT-IPS:type'], 'fixed')
        self.assertEqual(openstack['server']['addresses']
                         [p('neutron_network_test')][0]
                         ['version'], 4)
        self.assertTrue(port_assigned_addr.startswith('10.10.10.'))

        nova, _, _ = self.env.handler.openstack_clients()
        self.assertEquals(5, len(openstack['server']['security_groups']),
                          msg='Expected {0} security groups on the server '
                              'but there were {1}; security groups on the '
                              'server are: {2}; but then also {3}'.format(
                              5, len(openstack['server']['security_groups']),
                              openstack['server']['security_groups'],
                              nova.servers.get(openstack['server'][
                                  'id']).list_security_groups()))

        self.assert_obj_list_contains_subset(
            openstack['server']['security_groups'],
            {'name': p('neutron_test_security_group_dst')})
        self.assert_obj_list_contains_subset(
            openstack['server']['security_groups'],
            {'name': agents_security_group})
        self.assert_obj_list_contains_subset(
            openstack['server']['security_groups'],
            {'name': p('neutron_test_security_group_src')})
        self.assert_obj_list_contains_subset(
            openstack['server']['security_groups'],
            {'name': p('neutron_test_security_group_3')})
        self.assert_obj_list_contains_subset(
            openstack['server']['security_groups'],
            {'name': p('neutron_test_security_group_4')})
        self.assertEqual(openstack['server']['name'], p('novaservertest'))
        self.assertEqual(openstack['port']['name'], p('neutron_test_port'))
        self.assertEqual(port_fixed_ip, port_assigned_addr)
        self.assertEqual(openstack['keypair']['name'],
                         openstack['server']['key_name'])
        self.assertEqual(openstack['floatingip']['floating_network_id'],
                         router_network_id)<commit_msg>CFY-2132 added assertion to neutron test<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 fabric.api
import fabric.contrib.files

from cosmo_tester.framework.testenv import TestCase
from cosmo_tester.framework.util import (
    YamlPatcher,
    get_actual_keypath
)

PRIVATE_KEY_PATH = '/tmp/home/neutron-test.pem'


class NeutronGaloreTest(TestCase):

    def test_neutron_galore(self):

        blueprint_path = self.copy_blueprint('neutron-galore')
        self.blueprint_yaml = blueprint_path / 'blueprint.yaml'

        inputs = {
            'server_name': 'novaservertest',
            'image_name': self.env.ubuntu_image_name,
            'flavor_name': self.env.flavor_name,
            'private_key_path': PRIVATE_KEY_PATH,
        }

        before, after = self.upload_deploy_and_execute_install(inputs=inputs)

        node_states = self.get_delta_node_states(before, after)
        self.post_install_assertions(node_states)

        self._test_use_external_resource(inputs=inputs)

        self.execute_uninstall()

        self.post_uninstall_assertions()

    def post_install_assertions(self, node_states):

        def p(name):
            return '{}{}'.format(self.env.resources_prefix, name)

        management_network_name = self.env.management_network_name
        agents_security_group = self.env.agents_security_group
        if self.env.is_provider_bootstrap:
            # using existing resources - manager blueprints currently don't
            # use the prefix feature for the resources brought up during
            # bootstrap, and thus this section is only relevant if providers
            # have been used
            management_network_name = p(management_network_name)
            agents_security_group = p(agents_security_group)

        openstack = self.get_openstack_components(node_states)

        self.assertTrue(self._check_if_private_key_is_on_manager())

        port_assigned_addr = openstack['server']['addresses'][
            p('neutron_network_test')][0]['addr']
        # the port will get connected to the dst security group automatically
        #  when the server connects to the security group
        port_security_groups = openstack['port']['security_groups']
        port_fixed_ip = openstack['port']['fixed_ips'][0]['ip_address']
        port_network_id = openstack['port']['network_id']
        port_subnet_id = openstack['port']['fixed_ips'][0]['subnet_id']
        router_network_id = openstack['router']['external_gateway_info'][
            'network_id']
        sg_src_id = openstack['sg_src']['id']
        sg_dst_id = openstack['sg_dst']['id']
        sg_3 = openstack['sg_3']['id']
        sg_4 = openstack['sg_4']['id']
        network_subnet_id = openstack['network']['subnets'][0]

        self.assertEqual(openstack['server']['addresses']
                         [management_network_name][0]
                         ['OS-EXT-IPS:type'], 'fixed')
        self.assertEqual(openstack['server']['addresses']
                         [p('neutron_network_test')][0]
                         ['OS-EXT-IPS:type'], 'fixed')
        self.assertEqual(openstack['server']['addresses']
                         [p('neutron_network_test')][0]
                         ['version'], 4)
        self.assertTrue(port_assigned_addr.startswith('10.10.10.'))

        nova, _, _ = self.env.handler.openstack_clients()
        self.assertEquals(5, len(openstack['server']['security_groups']),
                          msg='Expected {0} security groups on the server '
                              'but there were {1}; security groups on the '
                              'server are: {2}; but then also {3}'.format(
                              5, len(openstack['server']['security_groups']),
                              openstack['server']['security_groups'],
                              nova.servers.get(openstack['server'][
                                  'id']).list_security_group()))

        self.assert_obj_list_contains_subset(
            openstack['server']['security_groups'],
            {'name': p('neutron_test_security_group_dst')})
        self.assert_obj_list_contains_subset(
            openstack['server']['security_groups'],
            {'name': agents_security_group})
        self.assert_obj_list_contains_subset(
            openstack['server']['security_groups'],
            {'name': p('neutron_test_security_group_src')})
        self.assert_obj_list_contains_subset(
            openstack['server']['security_groups'],
            {'name': p('neutron_test_security_group_3')})
        self.assert_obj_list_contains_subset(
            openstack['server']['security_groups'],
            {'name': p('neutron_test_security_group_4')})
        self.assertEqual(openstack['server']['name'], p('novaservertest'))
        self.assertEqual(openstack['port']['name'], p('neutron_test_port'))
        self.assertEqual(port_fixed_ip, port_assigned_addr)
        self.assertEqual(openstack['keypair']['name'],
                         openstack['server']['key_name'])
        self.assertEqual(openstack['floatingip']['floating_network_id'],
                         router_network_id)
{'fraction_non_alphanumeric': 0.11449124631651933, 'fraction_numerical': 0.0062402496099844, 'mean_word_length': 2.7427830035679532, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2495798', 'n_tokens_mistral': 3220, 'n_tokens_neox': 3116, 'n_words': 602}