def create_networks(self):
        """Create and terminate networks and verify in Contrail UI.

        Scenario:
            1. Setup systest_setup.
            2. Add 2 private networks via Horizon.
            3. Verify that networks are present in Contrail UI.
            4. Remove one of the private network via Horizon.
            5. Verify that the network is absent in Contrail UI.
            6. Add a private network via Horizon.
            7. Verify that all networks are present in Contrail UI.

        Duration: 15 min

        """
        # constants
        net_names = ['net_1', 'net_2']

        self.show_step(1)
        self.env.revert_snapshot('systest_setup')
        cluster_id = self.fuel_web.get_last_created_cluster()

        self.show_step(2)
        os_ip = self.fuel_web.get_public_vip(cluster_id)
        contrail_client = ContrailClient(os_ip)
        os_conn = os_actions.OpenStackActions(os_ip, SERVTEST_USERNAME,
                                              SERVTEST_PASSWORD,
                                              SERVTEST_TENANT)
        networks = []
        for net in net_names:
            logger.info('Create network {}'.format(net))
            network = os_conn.create_network(network_name=net)['network']
            networks.append(network)

        self.show_step(3)
        for net in networks:
            net_contrail = contrail_client.get_net_by_id(
                net['id'])['virtual-network']['uuid']
            assert_true(net['id'] == net_contrail)

        self.show_step(4)
        remove_net_id = networks.pop()['id']
        os_conn.neutron.delete_network(remove_net_id)

        self.show_step(5)
        contrail_networks = contrail_client.get_networks()['virtual-networks']
        assert_true(
            remove_net_id not in [net['uuid'] for net in contrail_networks])

        self.show_step(6)
        network = os_conn.create_network(
            network_name=net_names.pop())['network']
        networks.append(network)

        self.show_step(7)
        for net in networks:
            net_contrail = contrail_client.get_net_by_id(
                net['id'])['virtual-network']['uuid']
            assert_true(net['id'] == net_contrail)
    def contrail_login_password(self):
        """Create a new network via Contrail.

        Scenario:
            1. Setup systest_setup.
            2. Login as admin to Openstack Horizon UI.
            3. Create new user.
            4. Login as user to Openstack Horizon UI.
            5. Change login and password for user.
            6. Login to Openstack Horizon UI with new credentials.
            7. Login to Contrail Ui with same credentials.

        Duration: 15 min

        """
        # constants
        max_password_lengh = 64

        self.show_step(1)
        self.env.revert_snapshot('systest_setup')
        cluster_id = self.fuel_web.get_last_created_cluster()

        self.show_step(2)
        os_ip = self.fuel_web.get_public_vip(cluster_id)
        os_conn = os_actions.OpenStackActions(os_ip, SERVTEST_USERNAME,
                                              SERVTEST_PASSWORD,
                                              SERVTEST_TENANT)
        contrail_client = ContrailClient(os_ip)
        projects = contrail_client.get_projects()

        tenant = os_conn.get_tenant(SERVTEST_TENANT)

        self.show_step(3)
        chars = string.ascii_letters + string.digits + string.punctuation
        new_password = ''.join(
            [choice(chars) for i in range(max_password_lengh)])
        new_username = ''.join(
            [choice(chars) for i in range(max_password_lengh)])
        logger.info('New username: {0}, new password: {1}'.format(
            new_username, new_password))
        new_user = os_conn.create_user(new_username, new_password, tenant)
        role = [
            role for role in os_conn.keystone.roles.list()
            if role.name == 'admin'
        ].pop()
        os_conn.keystone.roles.add_user_role(new_user.id, role.id, tenant.id)

        self.show_step(4)
        os_actions.OpenStackActions(os_ip, new_username, new_password,
                                    SERVTEST_TENANT)

        self.show_step(5)
        new_password = ''.join(
            [choice(chars) for i in range(max_password_lengh)])
        new_user.manager.update_password(new_user, new_password)
        logger.info('New username: {0}, new password: {1}'.format(
            new_username, new_password))
        time.sleep(30)  # need to update password for new user

        self.show_step(6)
        os_actions.OpenStackActions(os_ip, new_username, new_password,
                                    SERVTEST_TENANT)
        contrail = ContrailClient(os_ip,
                                  credentials={
                                      'username': new_username,
                                      'tenant_name': SERVTEST_TENANT,
                                      'password': new_password
                                  })

        assert_equal(projects, contrail.get_projects(),
                     "Can not give info by Contrail API.")
    def contrail_vm_connection_in_different_tenants(self):
        """Create a new network via Contrail.

        Scenario:
            1. Setup systest_setup.
            2. Create 1 new tenant(project).
            3. Create networks in the each tenants.
            4. Launch 2 new instance in different tenants(projects).
            5. Check ping connectivity between instances.
            6. Verify on Contrail controller WebUI that networks are there and
               VMs are attached to different networks.

        Duration: 15 min

        """
        # constants
        net_admin = 'net_1'
        net_test = 'net_2'
        cidr = '192.168.115.0'
        self.show_step(1)
        self.env.revert_snapshot('systest_setup')
        cluster_id = self.fuel_web.get_last_created_cluster()

        self.show_step(2)
        os_ip = self.fuel_web.get_public_vip(cluster_id)
        contrail_client = ContrailClient(os_ip)
        os_conn = os_actions.OpenStackActions(os_ip, SERVTEST_USERNAME,
                                              SERVTEST_PASSWORD,
                                              SERVTEST_TENANT)

        test_tenant = 'test'
        os_conn.create_user_and_tenant(test_tenant, test_tenant, test_tenant)
        self.add_role_to_user(os_conn, 'test', 'admin', 'test')

        test = os_actions.OpenStackActions(os_ip, test_tenant, test_tenant,
                                           test_tenant)
        test_contrail = ContrailClient(os_ip,
                                       credentials={
                                           'username': test_tenant,
                                           'tenant_name': test_tenant,
                                           'password': test_tenant
                                       })
        tenant = test.get_tenant(test_tenant)
        for user in os_conn.keystone.users.list():
            if user.name != test_tenant:
                tenant.add_user(user, self.get_role(test, 'admin'))

        self.show_step(3)
        logger.info('Create network {0} in the {1}'.format(
            net_admin, tenant.name))
        network_test = test.create_network(network_name=net_test,
                                           tenant_id=tenant.id)['network']

        subnet_test = test.create_subnet(subnet_name=net_test,
                                         network_id=network_test['id'],
                                         cidr=cidr + '/24')

        router = test.create_router('router_1', tenant=tenant)
        test.add_router_interface(router_id=router["id"],
                                  subnet_id=subnet_test["id"])

        network = contrail_client.create_network(
            ["default-domain", SERVTEST_TENANT, net_admin], [{
                "attr": {
                    "ipam_subnets": [{
                        "subnet": {
                            "ip_prefix": '10.1.1.0',
                            "ip_prefix_len": 24
                        }
                    }]
                },
                "to":
                ["default-domain", "default-project", "default-network-ipam"]
            }])['virtual-network']
        default_router = contrail_client.get_router_by_name(SERVTEST_TENANT)
        contrail_client.add_router_interface(network, default_router)

        self.show_step(4)
        srv_1 = os_conn.create_server_for_migration(neutron=True,
                                                    label=net_admin)
        fip_1 = os_conn.assign_floating_ip(srv_1).ip
        wait(lambda: tcp_ping(fip_1, 22),
             timeout=60,
             interval=5,
             timeout_msg="Node {0} is not accessible by SSH.".format(fip_1))
        ip_1 = os_conn.get_nova_instance_ip(srv_1, net_name=net_admin)
        srv_2 = test.create_server_for_migration(neutron=True, label=net_test)
        srv_3 = test.create_server_for_migration(neutron=True, label=net_test)
        ips = []
        for srv in [srv_2, srv_3]:
            ip = (test.get_nova_instance_ip(srv, net_name=net_test))
            if ip_1 != ip_1:
                ips.append(ip)

        self.show_step(5)
        ip_pair = {}
        ip_pair[fip_1] = ips

        controller = self.fuel_web.get_nailgun_primary_node(
            self.env.d_env.nodes().slaves[1])
        self.ping_instance_from_instance(os_conn,
                                         controller.name,
                                         ip_pair,
                                         ping_result=1)

        self.show_step(6)
        logger.info('{}'.format(network))
        net_info = contrail_client.get_net_by_id(
            network['uuid'])['virtual-network']
        logger.info(''.format(net_info))
        net_interface_ids = []
        for interface in net_info['virtual_machine_interface_back_refs']:
            net_interface_ids.append(interface['uuid'])
        interf_id = contrail_client.get_instance_by_id(
            srv_1.id
        )['virtual-machine']['virtual_machine_interface_back_refs'][0]['uuid']
        assert_true(
            interf_id in net_interface_ids,
            '{0} is not attached to network {1}'.format(srv_1.name, net_admin))
        net_info = test_contrail.get_net_by_id(
            network_test['id'])['virtual-network']
        net_interface_ids = []
        for interface in net_info['virtual_machine_interface_back_refs']:
            net_interface_ids.append(interface['uuid'])
        interf_id = test_contrail.get_instance_by_id(
            srv_2.id
        )['virtual-machine']['virtual_machine_interface_back_refs'][0]['uuid']
        assert_true(
            interf_id in net_interface_ids,
            '{0} is not attached to network {1}'.format(srv_2.name, net_test))
    def create_new_network_via_contrail(self):
        """Create a new network via Contrail.

        Scenario:
            1. Setup systest_setup.
            2. Create a new network via Contrail API.
            3. Launch 2 new instance in the network with default security group
               via Horizon API.
            4. Check ping connectivity between instances.
            5. Verify on Contrail controller WebUI that network is there and
               VMs are attached to it.

        Duration: 15 min

        """
        # constants
        net_name = 'net_1'
        self.show_step(1)
        self.env.revert_snapshot('systest_setup')
        cluster_id = self.fuel_web.get_last_created_cluster()

        self.show_step(2)
        os_ip = self.fuel_web.get_public_vip(cluster_id)
        contrail_client = ContrailClient(os_ip)
        network = contrail_client.create_network(
            ["default-domain", SERVTEST_TENANT, net_name], [{
                "attr": {
                    "ipam_subnets": [{
                        "subnet": {
                            "ip_prefix": '10.1.1.0',
                            "ip_prefix_len": 24
                        }
                    }]
                },
                "to":
                ["default-domain", "default-project", "default-network-ipam"]
            }])['virtual-network']
        default_router = contrail_client.get_router_by_name(SERVTEST_TENANT)
        contrail_client.add_router_interface(network, default_router)

        self.show_step(3)
        os_conn = os_actions.OpenStackActions(os_ip, SERVTEST_USERNAME,
                                              SERVTEST_PASSWORD,
                                              SERVTEST_TENANT)

        hypervisors = os_conn.get_hypervisors()
        instances = []
        fips = []
        for hypervisor in hypervisors:
            instance = os_conn.create_server_for_migration(
                neutron=True,
                availability_zone="nova:{0}".format(
                    hypervisor.hypervisor_hostname),
                label=net_name)
            instances.append(instance)
            ip = os_conn.assign_floating_ip(instance).ip
            wait(lambda: tcp_ping(ip, 22),
                 timeout=60,
                 interval=5,
                 timeout_msg="Node {0} is not accessible by SSH.".format(ip))
            fips.append(ip)

        self.show_step(4)
        ip_pair = dict.fromkeys(fips)
        for key in ip_pair:
            ip_pair[key] = [value for value in fips if key != value]
        controller = self.fuel_web.get_nailgun_primary_node(
            self.env.d_env.nodes().slaves[1])
        self.ping_instance_from_instance(os_conn, controller.name, ip_pair)

        self.show_step(5)
        net_info = contrail_client.get_net_by_id(
            network['uuid'])['virtual-network']
        net_interface_ids = []
        for interface in net_info['virtual_machine_interface_back_refs']:
            net_interface_ids.append(interface['uuid'])
        for instance in instances:
            interf_id = contrail_client.get_instance_by_id(
                instance.id)['virtual-machine'][
                    'virtual_machine_interface_back_refs'][0]['uuid']
            assert_true(
                interf_id in net_interface_ids,
                '{0} is not attached to network {1}'.format(
                    instance.name, net_name))
Example #5
0
    def contrail_no_default(self):
        """Check configured no default contrail parameters via Contrail WEB.

        Scenario:
            1. Install contrail plugin.
            2. Create cluster.
            3. Set following no defaults contrail parameters:
               *contrail_api_port
               *contrail_route_target
               *contrail_gateways
               *contrail_external
               *contrail_asnum
            4. Add nodes:
                1 contrail-controller
                1 contrail-analytics + contrail-analytics-db
                1 controller
                1 compute
            5. Deploy cluster.
            6. Verify that all configured contrail parameters present in
               the Contrail WEB.

        Duration: 2.5 hours

        """
        # constants
        contrail_api_port = '18082'
        contrail_route_target = '4294967295'
        contrail_gateways = '10.109.3.250'
        contrail_external = '10.10.1.0/24'
        contrail_asnum = '65534'
        external_net = 'admin_floating_net'

        self.show_step(1)
        self.show_step(2)
        plugin.prepare_contrail_plugin(self, slaves=5)

        # activate vSRX image
        vsrx.activate()

        self.show_step(3)
        plugin.activate_plugin(self,
                               contrail_api_public_port=contrail_api_port,
                               contrail_route_target=contrail_route_target,
                               contrail_gateways=contrail_gateways,
                               contrail_external=contrail_external,
                               contrail_asnum=contrail_asnum)

        cluster_id = self.fuel_web.get_last_created_cluster()

        self.show_step(4)
        self.fuel_web.update_nodes(
            self.cluster_id, {
                'slave-01': ['contrail-controller'],
                'slave-02': ['contrail-analytics', 'contrail-analytics-db'],
                'slave-03': ['controller'],
                'slave-04': ['compute'],
            })

        self.show_step(5)
        openstack.deploy_cluster(self)
        TestContrailCheck(self).cloud_check(['contrail'])

        self.show_step(6)
        os_ip = self.fuel_web.get_public_vip(cluster_id)
        os_conn = os_actions.OpenStackActions(os_ip, SERVTEST_USERNAME,
                                              SERVTEST_PASSWORD,
                                              SERVTEST_TENANT)
        contrail_client = ContrailClient(os_ip,
                                         contrail_port=contrail_api_port)

        logger.info(
            'Check contrail_asnum, contrail_route_target via Contrail.')
        external_net_id = os_conn.get_network(external_net)['id']
        ext_net = contrail_client.get_net_by_id(
            external_net_id)["virtual-network"]
        fuel_router_target = '{0}:{1}'.format(contrail_asnum,
                                              contrail_route_target)
        contrail_router_target = ext_net['route_target_list']['route_target']
        message = 'Parameters {0} from Fuel is not equel {1} from Contrail Web'
        assert_true(
            fuel_router_target in contrail_router_target[0],
            message.format(fuel_router_target, contrail_router_target[0]))

        logger.info('Check contrail_external via Contrail.')
        external_ip = ext_net['network_ipam_refs'][0]['attr']['ipam_subnets'][
            0]['subnet']
        assert_true(
            contrail_external.split('/')[0] == external_ip['ip_prefix'],
            message.format(
                contrail_external.split('/')[0], external_ip['ip_prefix']))
        assert_true(
            contrail_external.split('/')[1] == str(
                external_ip['ip_prefix_len']),
            message.format(
                contrail_external.split('/')[1], external_ip['ip_prefix_len']))

        logger.info('Check contrail_gateways via Contrail.')
        bgp_ids = contrail_client.get_bgp_routers()['bgp-routers']

        bgp_ips = [
            contrail_client.get_bgp_by_id(bgp_id['uuid'])['bgp-router']
            ['bgp_router_parameters']['address'] for bgp_id in bgp_ids
        ]

        assert_true(contrail_gateways in bgp_ips,
                    message.format(contrail_gateways, bgp_ips))