def test_543352_HeatStackUpdateInPlace(self):
        """ This test case checks stack id doesn't change after stack update.

        Steps:
        1. Create stack using template nova_server.yaml.
        2. Check id of created image.
        3. Update stack template: flavor = 'm1.small'
        4. Update stack.
        5. Check id of updated image.
        """
        stack_name = "vm_stack"
        template_name = "nova_server.yaml"
        template_path = os.path.join(self.templates_dir, template_name)
        try:
            networks = self.neutron.list_networks()
            if len(networks["networks"]) < 2:
                raise AssertionError("ERROR: Need to have at least 2 networks")
            internal_network_id = networks["networks"][1]["id"]
            create_template = common_functions.read_template(self.templates_dir, template_name)
            parameters = {"network": internal_network_id}
            sid = common_functions.create_stack(self.heat, stack_name, create_template, parameters)
            first_resource_id = common_functions.get_specific_resource_id(self.heat, sid, "vm")
            flavor_change = {"flavor": "m1.small"}
            common_functions.update_template_file(template_path, "flavor", **flavor_change)
            update_template = common_functions.read_template(self.templates_dir, template_name)
            common_functions.update_stack(self.heat, sid, update_template, parameters)
            second_resource_id = common_functions.get_specific_resource_id(self.heat, sid, "vm")
            self.assertEqual(
                first_resource_id, second_resource_id, msg="Resource id should not be changed" " after modifying stack"
            )
        finally:
            common_functions.delete_stack(self.heat, sid)
            back_flavor_change = {"flavor": "m1.tiny"}
            common_functions.update_template_file(template_path, "flavor", **back_flavor_change)
    def test_543342_ShowInfoOfSpecifiedStackEvent(self):
        """ This test checks info about stack event

        Steps:
        1. Create new stack
        2. Launch heat event-list stack_name
        3. Launch heat event-show <NAME or ID> <RESOURCE> <EVENT> \
        for specified event and check result
        """
        stack_name = "stack_to_show_event_info_543342"
        template_content = common_functions.read_template(self.templates_dir, "empty_heat_templ.yaml")
        stack_id = common_functions.create_stack(self.heat, stack_name, template_content, {"param": 123})
        self.uid_list.append(stack_id)
        stack_status = self.heat.stacks.get(stack_id).to_dict()["stack_status"]
        even_list = self.heat.events.list(stack_id)
        self.assertTrue(even_list, "NOK, event list is empty")
        event_to_show = even_list[-1]
        resource_name, event_id = event_to_show.resource_name, event_to_show.id
        event_info = self.heat.events.get(stack_id, resource_name, event_id)
        self.assertEqual(
            event_info.resource_name,
            stack_name,
            "Expected resource name: {0}, actual: {1}".format(event_info.resource_name, stack_id),
        )
        self.assertEqual(
            event_info.resource_status,
            stack_status,
            "Expected resource status: {0}, actual: {1}".format(event_info.resource_status, stack_status),
        )
        common_functions.delete_stack(self.heat, stack_id)
    def test_543334_HeatStackCreateWithURL(self):
        """ This test case checks creation of stack using template URL.

        Steps:
        1. Create stack using template URL.
        2. Check that the stack is in the list of stacks
        3. Check that stack status is 'CREATE_COMPLETE'
        4. Delete stack
        """
        stack_name = "empty__543334"
        template_url = (
            "https://raw.githubusercontent.com/tkuterina/"
            "mos-integration-tests/master/mos_tests/heat/"
            "templates/empty_heat_templ.yaml"
        )
        timeout = 20
        if common_functions.is_stack_exists(stack_name, self.heat):
            uid = common_functions.get_stack_id(self.heat, stack_name)
            common_functions.delete_stack(self.heat, uid)
        stack_data = {
            "stack_name": stack_name,
            "template_url": template_url,
            "parameters": {"param": "some_param_string"},
            "timeout_mins": timeout,
        }
        output = self.heat.stacks.create(**stack_data)
        stack_id = output["stack"]["id"]
        self.uid_list.append(stack_id)
        stacks_id = [s.id for s in self.heat.stacks.list()]
        self.assertIn(stack_id, stacks_id)
        self.assertTrue(common_functions.check_stack_status(stack_name, self.heat, "CREATE_COMPLETE", timeout))
    def test_543336_HeatStackShow(self):
        """ This test case checks detailed stack's information.

        Steps:
        1. Create stack using template file empty_heat_templ.yaml
        2. Check that the stack is in the list of stacks
        3. Check that stack status is 'CREATE_COMPLETE'
        4. Check stack's information
        5. Delete stack
        """
        stack_name = "empty__543336"
        parameter = "some_param_string"
        timeout = 20
        if common_functions.is_stack_exists(stack_name, self.heat):
            uid = common_functions.get_stack_id(self.heat, stack_name)
            common_functions.delete_stack(self.heat, uid)
        template = common_functions.read_template(self.templates_dir, "empty_heat_templ.yaml")
        uid = common_functions.create_stack(
            self.heat, stack_name, template, timeout=timeout, parameters={"param": parameter}
        )
        self.uid_list.append(uid)
        parameters = {
            "OS::project_id": self.keystone.auth_tenant_id,
            "OS::stack_id": uid,
            "OS::stack_name": stack_name,
            "param": parameter,
        }
        correct_data = {
            "description": "Sample template",
            "stack_name": stack_name,
            "disable_rollback": True,
            "template_description": "Sample template",
            "timeout_mins": timeout,
            "stack_status": "CREATE_COMPLETE",
            "id": uid,
            "stack_status_reason": "Stack CREATE completed " "successfully",
            "parameters": parameters,
        }
        output = self.heat.stacks.get(uid)
        show_data = {
            "description": output.description,
            "stack_name": output.stack_name,
            "disable_rollback": output.disable_rollback,
            "template_description": output.template_description,
            "timeout_mins": output.timeout_mins,
            "stack_status": output.stack_status,
            "id": output.id,
            "stack_status_reason": output.stack_status_reason,
            "parameters": output.parameters,
        }
        self.assertDictEqual(show_data, correct_data)
        self.assertEqual(len(output.links), 1)
        self.assertEqual(len(output.links[0]), 2)
        self.assertNotEqual(output.links[0]["href"].find(stack_name), -1)
        self.assertEqual(output.links[0]["rel"], "self")
    def test_543332_HeatStackPreview(self):
        """ This test case previews a stack.

        Steps:
        1. Execute stack preview.
        2. Check output result.
        """
        stack_name = "empty__543332"
        parameter = "some_param_string"
        parameters = {
            "OS::project_id": self.keystone.auth_tenant_id,
            "OS::stack_id": "None",
            "OS::stack_name": stack_name,
            "param": parameter,
        }
        correct_data = {
            "description": "Sample template",
            "stack_name": stack_name,
            "disable_rollback": True,
            "template_description": "Sample template",
            "parameters": parameters,
        }
        if common_functions.is_stack_exists(stack_name, self.heat):
            uid = common_functions.get_stack_id(self.heat, stack_name)
            common_functions.delete_stack(self.heat, uid)
        template = common_functions.read_template(self.templates_dir, "empty_heat_templ.yaml")
        stack_data = {"stack_name": stack_name, "template": template, "parameters": {"param": parameter}}
        output = self.heat.stacks.preview(**stack_data)
        preview_data = {
            "description": output.description,
            "stack_name": output.stack_name,
            "disable_rollback": output.disable_rollback,
            "template_description": output.template_description,
            "parameters": output.parameters,
        }
        self.assertDictEqual(preview_data, correct_data)
        self.assertEqual(len(output.links), 1)
        self.assertEqual(len(output.links[0]), 2)
        self.assertNotEqual(output.links[0]["href"].find(stack_name), -1)
        self.assertEqual(output.links[0]["rel"], "self")
    def test_543333_HeatStackCreateWithTemplate(self):
        """ This test case checks creation of stack.

        Steps:
        1. Create stack using template file empty_heat_templ.yaml.
        2. Check that the stack is in the list of stacks
        3. Check that stack status is 'CREATE_COMPLETE'
        4. Delete stack
        """
        stack_name = "empty__543333"
        parameter = "some_param_string"
        timeout = 20
        if common_functions.is_stack_exists(stack_name, self.heat):
            uid = common_functions.get_stack_id(self.heat, stack_name)
            common_functions.delete_stack(self.heat, uid)
        template = common_functions.read_template(self.templates_dir, "empty_heat_templ.yaml")
        uid = common_functions.create_stack(
            self.heat, stack_name, template, timeout=timeout, parameters={"param": parameter}
        )
        self.uid_list.append(uid)
        stacks_id = [s.id for s in self.heat.stacks.list()]
        self.assertIn(uid, stacks_id)
        self.assertTrue(common_functions.check_stack_status(stack_name, self.heat, "CREATE_COMPLETE", timeout))
    def test_543344_HeatStackTemplateShow(self):
        """ This test case checks representation of template of created stack.

        Steps:
        1. Create stack using template file empty_heat_templ.yaml.
        2. Check that template of created stack has correct \
        representation.
        """
        stack_name = "empty_stack"
        timeout = 60
        parameter = "some_string"
        if common_functions.is_stack_exists(stack_name, self.heat):
            uid = common_functions.get_stack_id(self.heat, stack_name)
            common_functions.delete_stack(self.heat, uid)
        template = common_functions.read_template(self.templates_dir, "empty_heat_templ.yaml")
        uid = common_functions.create_stack(
            self.heat, stack_name, template, timeout=timeout, parameters={"param": parameter}
        )
        self.uid_list.append(uid)
        self.assertTrue(common_functions.check_stack_status(stack_name, self.heat, "CREATE_COMPLETE"))
        stack_dict = {s.stack_name: s.id for s in self.heat.stacks.list()}
        stack_id = stack_dict[stack_name]
        stack_template = self.heat.stacks.template(stack_id)
        self.assertIsInstance(stack_template, dict)
    def test_543335_HeatStackDelete(self):
        """ This test case checks deletion of stack.

        Steps:
        1. Create stack using template file empty_heat_templ.yaml.
        2. Check that the stack is in the list of stacks
        3. Delete the stack.
        4. Check that the stack is absent in the list of stacks

        """
        stack_name = "empty_543335"
        timeout = 20
        parameter = "some_param_string"
        if common_functions.is_stack_exists(stack_name, self.heat):
            uid = common_functions.get_stack_id(self.heat, stack_name)
            common_functions.delete_stack(self.heat, uid)
        template = common_functions.read_template(self.templates_dir, "empty_heat_templ.yaml")
        uid = common_functions.create_stack(
            self.heat, stack_name, template, timeout=timeout, parameters={"param": parameter}
        )
        self.assertTrue(common_functions.check_stack_status(stack_name, self.heat, "CREATE_COMPLETE", timeout))
        common_functions.delete_stack(self.heat, uid)
        stacks = [s.stack_name for s in self.heat.stacks.list()]
        self.assertNotIn(stack_name, stacks)
 def delete_stacks(self, environment_id):
     stack = self._get_stack(environment_id)
     if not stack:
         return
     else:
         delete_stack(self.heat, stack.id)
 def tearDown(self):
     for stack_uid in self.uid_list:
         common_functions.delete_stack(self.heat, stack_uid)
     self.uid_list = []
    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()