Best Python code snippet using tempest_python
test_attach_volume.py
Source:test_attach_volume.py  
...288        kwargs = {}289        if bootable:290            kwargs['image_ref'] = CONF.compute.image_ref291        return self.create_volume(multiattach=True, **kwargs)292    def _create_and_multiattach(self):293        """Creates two server instances and a volume and attaches to both.294        :returns: A three-item tuple of the list of created servers,295                  the created volume, and dict of server ID to volumeAttachment296                  dict entries297        """298        servers = []299        for x in range(2):300            name = 'multiattach-server-%i' % x301            servers.append(self.create_test_server(name=name))302        # Now wait for the servers to be ACTIVE.303        for server in servers:304            waiters.wait_for_server_status(self.servers_client, server['id'],305                                           'ACTIVE')306        volume = self._create_multiattach_volume()307        # Attach the volume to the servers308        attachments = self._attach_volume_to_servers(volume, servers)309        return servers, volume, attachments310    @decorators.idempotent_id('8d5853f7-56e7-4988-9b0c-48cea3c7049a')311    def test_list_get_volume_attachments_multiattach(self):312        # Attach a single volume to two servers.313        servers, volume, attachments = self._create_and_multiattach()314        # List attachments from the volume and make sure the server uuids315        # are in that list.316        vol_attachments = self.volumes_client.show_volume(317            volume['id'])['volume']['attachments']318        attached_server_ids = [attachment['server_id']319                               for attachment in vol_attachments]320        self.assertEqual(2, len(attached_server_ids))321        # List Volume attachment of the servers322        for server in servers:323            self.assertIn(server['id'], attached_server_ids)324            vol_attachments = self.servers_client.list_volume_attachments(325                server['id'])['volumeAttachments']326            self.assertEqual(1, len(vol_attachments))327            attachment = attachments[server['id']]328            self.assertDictEqual(attachment, vol_attachments[0])329            # Detach the volume from this server.330            self._detach_multiattach_volume(volume['id'], server['id'])331    def _boot_from_multiattach_volume(self):332        """Boots a server from a multiattach volume.333        The volume will not be deleted when the server is deleted.334        :returns: 2-item tuple of (server, volume)335        """336        volume = self._create_multiattach_volume(bootable=True)337        # Now create a server from the bootable volume.338        bdm = [{339            'uuid': volume['id'],340            'source_type': 'volume',341            'destination_type': 'volume',342            'boot_index': 0,343            'delete_on_termination': False}]344        server = self.create_test_server(345            image_id='', block_device_mapping_v2=bdm, wait_until='ACTIVE')346        # Assert the volume is attached to the server.347        attachments = self.servers_client.list_volume_attachments(348            server['id'])['volumeAttachments']349        self.assertEqual(1, len(attachments))350        self.assertEqual(volume['id'], attachments[0]['volumeId'])351        return server, volume352    @decorators.idempotent_id('65e33aa2-185b-44c8-b22e-e524973ed625')353    def test_boot_from_multiattach_volume(self):354        """Simple test to boot an instance from a multiattach volume."""355        self._boot_from_multiattach_volume()356    @utils.services('image')357    @decorators.idempotent_id('885ac48a-2d7a-40c5-ae8b-1993882d724c')358    def test_snapshot_volume_backed_multiattach(self):359        """Boots a server from a multiattach volume and snapshots the server.360        Creating the snapshot of the server will also create a snapshot of361        the volume.362        """363        server, volume = self._boot_from_multiattach_volume()364        # Create a snapshot of the server (and volume implicitly).365        self.create_image_from_server(366            server['id'], name='multiattach-snapshot',367            wait_until='active', wait_for_server=True)368        # TODO(mriedem): Make sure the volume snapshot exists. This requires369        # adding the volume snapshots client to BaseV2ComputeTest.370        # Delete the server, wait for it to be gone, and make sure the volume371        # still exists.372        self.servers_client.delete_server(server['id'])373        waiters.wait_for_server_termination(self.servers_client, server['id'])374        # Delete the volume and cascade the delete of the volume snapshot.375        self.volumes_client.delete_volume(volume['id'], cascade=True)376        # Now we have to wait for the volume to be gone otherwise the normal377        # teardown will fail since it will race with our call and the snapshot378        # might still exist.379        self.volumes_client.wait_for_resource_deletion(volume['id'])380    @decorators.idempotent_id('f01c7169-a124-4fc7-ae60-5e380e247c9c')381    @testtools.skipUnless(CONF.compute_feature_enabled.resize,382                          'Resize not available.')383    def test_resize_server_with_multiattached_volume(self):384        # Attach a single volume to multiple servers, then resize the servers385        servers, volume, _ = self._create_and_multiattach()386        for server in servers:387            self.resize_server(server['id'], self.flavor_ref_alt)388        for server in servers:389            self._detach_multiattach_volume(volume['id'], server['id'])390    # TODO(mriedem): Might be interesting to create a bootable multiattach391    # volume with delete_on_termination=True, create server1 from the392    # volume, then attach it to server2, and then delete server1 in which393    # case the volume won't be deleted because it's still attached to...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
