def test_543349_HeatCreateStackNeutronResources(self):
        """ This test creates stack with Neutron resources

        Steps:
        1. Download Cirros image
        2. Create image with Glance and check that it is 'Active'
        3. Create new key-pair with Nova
        4. Find ID of internal network with Neutron
        5. Find ID of internal sub network with Neutron
        6. Find ID of public network with Neutron
        7. Create stack with Neutron resources and check that it was \
        created successfully
        8. CleanUp

        https://mirantis.testrail.com/index.php?/cases/view/543349
        """
        file_name = "cirros-0.3.4-x86_64-disk.img.txt"
        image_name = "543349_Cirros-image" + "_" + str(randint(100, 10000))

        # Prepare full path to image file. Return e.g.:
        # Like: /root/mos_tests/heat/images/cirros-0.3.4-x86_64-disk.img.txt
        image_link_location = os.path.join(self.images_dir, file_name)

        # Download image on node. Like: /tmp/cirros-0.3.4-x86_64-disk.img
        image_path = common_functions.download_image(image_link_location)

        # Create image in Glance
        image = self.glance.images.create(
            name=image_name, os_distro="Cirros", disk_format="qcow2", visibility="public", container_format="bare"
        )
        # Check that status is 'queued'
        if image.status != "queued":
            raise AssertionError(
                "ERROR: Image status after creation is:" "[{0}]. " "Expected [queued]".format(image.status)
            )

        # Put image-file in created Image
        with open(image_path, "rb") as image_content:
            self.glance.images.upload(image.id, image_content)

        # Check that status of image is 'active'
        self.assertEqual(
            self.glance.images.get(image.id)["status"],
            "active",
            "After creation in Glance image status is [{0}]. "
            "Expected is [active]".format(self.glance.images.get(image.id)["status"]),
        )

        # Create new keypair
        keypair = self.nova.keypairs.create(name=image_name)

        # Get list of networks
        networks = self.neutron.list_networks()

        # Check if Neutron has more then 1 network. We need intern and extern.
        if len(networks["networks"]) < 2:
            raise AssertionError("ERROR: Need to have at least 2 networks")

        # Find internal network ID if network name contains 'inter'
        int_network_id = [x["id"] for x in networks["networks"] if "intern" in x["name"] and x["status"] == "ACTIVE"]
        # If can't find 'inter' in networks -> get ID of last network
        if not int_network_id:
            int_network_id = networks["networks"][-1]["id"]
        else:
            int_network_id = int_network_id[0]

        # Find private subnet ID
        int_sub_network_id = [
            x["subnets"][0] for x in networks["networks"] if int_network_id in x["id"] and x["status"] == "ACTIVE"
        ]
        int_sub_network_id = int_sub_network_id[0]

        # Find public network ID
        pub_network_id = [x["id"] for x in networks["networks"] if "float" in x["name"] and x["status"] == "ACTIVE"]
        # If can't find 'float' in networks -> get ID of 0 network
        if not int_network_id:
            pub_network_id = networks["networks"][0]["id"]
        else:
            pub_network_id = pub_network_id[0]

        # Create stack with Heat
        template = common_functions.read_template(self.templates_dir, "Heat_Neutron_resources_543349.yaml")

        uid = common_functions.create_stack(
            self.heat,
            "Heat_Neutron_resources_543349",
            template,
            {
                "key_name": image_name,
                "image": image_name,
                "flavor": "m1.small",
                "public_net_id": pub_network_id,
                "private_net_id": int_network_id,
                "private_subnet_id": int_sub_network_id,
            },
            15,
        )
        # CLEANUP
        # Delete stack with tearDown:
        self.uid_list.append(uid)
        # Delete image:
        self.glance.images.delete(image.id)
        # Delete keypair:
        keypair.delete()
    def test_543350_HeatCreateStackNovaResources(self):
        """ This test creates stack with Nova resources

        Steps:
        1. Download Cirros image
        2. Create image with Glance and check that it is 'Active'
        3. Create new key-pair with Nova
        4. Find network ID
        5. Prepare template and reference template
        6. Create stack
        7. CleanUp

        https://mirantis.testrail.com/index.php?/cases/view/543350
        """
        file_name = "cirros-0.3.4-x86_64-disk.img.txt"
        image_name = "543350_Cirros-image" + "_" + str(randint(100, 10000))

        # Prepare full path to image file. Return e.g.:
        # Like: /root/mos_tests/heat/images/cirros-0.3.4-x86_64-disk.img.txt
        image_link_location = os.path.join(self.images_dir, file_name)

        # Download image on node. Like: /tmp/cirros-0.3.4-x86_64-disk.img
        image_path = common_functions.download_image(image_link_location)

        # Create image in Glance
        image = self.glance.images.create(
            name=image_name, os_distro="Cirros", disk_format="qcow2", visibility="public", container_format="bare"
        )
        # Check that status is 'queued'
        if image.status != "queued":
            raise AssertionError(
                "ERROR: Image status after creation is:" "[{0}]. " "Expected [queued]".format(image.status)
            )

        # Put image-file in created Image
        with open(image_path, "rb") as image_content:
            self.glance.images.upload(image.id, image_content)

        # Check that status of image is 'active'
        self.assertEqual(
            self.glance.images.get(image.id)["status"],
            "active",
            "After creation in Glance image status is [{0}]. "
            "Expected is [active]".format(self.glance.images.get(image.id)["status"]),
        )

        # Create new keypair
        keypair = self.nova.keypairs.create(name=image_name)

        # Get list of networks
        networks = self.neutron.list_networks()

        # Find internal network ID if network name contains 'inter'
        int_network_id = [x["id"] for x in networks["networks"] if "intern" in x["name"] and x["status"] == "ACTIVE"]
        # If can't find 'inter' in networks -> get ID of last network
        if not int_network_id:
            int_network_id = networks["networks"][-1]["id"]
        else:
            int_network_id = int_network_id[0]

        # Read main template for creation
        template = common_functions.read_template(self.templates_dir, "Heat_Nova_resources_543350.yaml")

        # Read additional reference template for creation
        template_additional = common_functions.read_template(
            self.templates_dir, "Heat_Nova_resources_543350_volume_with_attachment.yaml"
        )

        # Create stack
        uid = common_functions.create_stack(
            self.heat,
            "Heat_Nova_resources_543350",
            template,
            {
                "key_name": image_name,
                "image_id": image_name,
                "volume_size": 1,
                "num_volumes": 1,
                "flavor": "m1.tiny",
                "network_id": int_network_id,
            },
            15,
            {"Heat_Nova_resources_543350" "_volume_with_attachment.yaml": template_additional},
        )
        # CLEANUP
        # Delete stack with tearDown:
        self.uid_list.append(uid)
        # Delete image:
        self.glance.images.delete(image.id)
        # Delete keypair:
        keypair.delete()
    def test_543346_HeatCreateStackDockerResources(self):
        """ This test creates stack with Docker resource

        Steps:
        1. Download custom Fedora image
        2. Create image in Glance and check status
        3. With Nova create new key-pair
        4. Find internal network ID
        5. Find name of public network
        6. Create stack with Docker host
        7. Get public floating IP of Docker host instance
        8. Prepare docker endpoint URL
        9. Create Docker stack
        10. CleanUp

        https://mirantis.testrail.com/index.php?/cases/view/543346
        """
        # At first we need to check that heat has Docker resources
        # but it was already verified in "test_543328_HeatResourceTypeList".
        # So nothing to do here.

        file_name = "fedora_22-docker-image.qcow2.txt"
        image_name = "543346_Fedora-docker" + "_" + str(randint(100, 10000))

        # Prepare full path to image file. Return e.g.
        # like: /root/mos_tests/heat/images/fedora_22-docker-image.qcow2.txt
        image_link_location = os.path.join(self.images_dir, file_name)

        # Download image on node. Like: /tmp/fedora-software-config.qcow2
        image_path = common_functions.download_image(image_link_location)

        # Create image in Glance
        image = self.glance.images.create(
            name=image_name, os_distro="Fedora", disk_format="qcow2", visibility="public", container_format="bare"
        )
        # Check that status is 'queued'
        if image.status != "queued":
            raise AssertionError(
                "ERROR: Image status after creation is:" "[{0}]. " "Expected [queued]".format(image.status)
            )

        # Put image-file in created Image
        with open(image_path, "rb") as image_content:
            self.glance.images.upload(image.id, image_content)

        # Check that status of image is 'active'
        self.assertEqual(
            self.glance.images.get(image.id)["status"],
            "active",
            "After creation in Glance image status is [{0}]. "
            "Expected is [active]".format(self.glance.images.get(image.id)["status"]),
        )

        # Create new keypair
        keypair = self.nova.keypairs.create(name=image_name)

        # Get list of networks
        networks = self.neutron.list_networks()

        # Find internal network ID if network name contains 'inter'
        int_network_id = [x["id"] for x in networks["networks"] if "intern" in x["name"] and x["status"] == "ACTIVE"]
        # If can't find 'inter' in networks -> get ID of last network
        if not int_network_id:
            int_network_id = networks["networks"][-1]["id"]
        else:
            int_network_id = int_network_id[0]

        # Find name of public network
        pub_network_name = [
            x["name"]
            for x in networks["networks"]
            if "float" in x["name"] or "public" in x["name"] and x["status"] == "ACTIVE"
        ]
        # If can't find 'float/public' in networks -> get ID of first network
        if not pub_network_name:
            pub_network_name = pub_network_name["networks"][0]["name"]
        else:
            pub_network_name = pub_network_name[0]

        # Read template for Docker host creation
        template = common_functions.read_template(self.templates_dir, "Heat_Docker_Resources_543346_Host.yaml")

        # Create stack for Docker host
        uid = common_functions.create_stack(
            self.heat,
            "Heat_Docker_543346_Host",
            template,
            {
                "key": image_name,
                "flavor": "m1.small",
                "image": image_name,
                "public_net": pub_network_name,
                "int_network_id": int_network_id,
                "timeout": 600,
            },
            15,
        )

        # Get resource ID of 'docker_server'. We know this name from template
        instance_id = self.heat.resources.get(uid, "docker_server").to_dict()
        instance_id = instance_id["physical_resource_id"]

        # Get public floating IP of created server instance in stack
        floating_ip_list = self.nova.floating_ips.list()
        floating_ip = [x.ip for x in floating_ip_list if x.instance_id == instance_id]
        floating_ip = floating_ip[0]

        # Check that floating IP is not empty
        self.assertIsNotNone(floating_ip, "ERROR: Floating IP of Docker host instance is empty")

        # Read template for Docker stack creation
        template = common_functions.read_template(self.templates_dir, "Heat_Docker_Resources_543346_docker.yaml")

        # Before creating new docker stack give a few second too start
        # Docker bind in first stack. We have a WaitCondition in it, but
        # anyway there may be a need to wait several seconds.
        time.sleep(5)

        # Prepare docker endpoint. Like: 'tcp://172.16.0.161:2376'
        # Where IP is a floating IP of host instance. And port# is in template.
        docker_endpoint = "tcp://{0}:2376".format(floating_ip)

        # Create Docker stack
        docker_uid = common_functions.create_stack(
            self.heat, "Heat_Docker_543346_docker", template, {"docker_endpoint": docker_endpoint}, 15
        )
        # CLEANUP
        # First we need to delete second docker stack
        common_functions.delete_stack(self.heat, docker_uid)
        # Delete host stack with tearDown:
        self.uid_list.append(uid)
        # Delete image:
        self.glance.images.delete(image.id)
        # Delete keypair:
        keypair.delete()