Best Python code snippet using tempest_python
test_minimum_basic.py
Source:test_minimum_basic.py  
...57        return self.volumes_client.list_volumes()['volumes']58    def cinder_show(self, volume):59        got_volume = self.volumes_client.show_volume(volume['id'])['volume']60        self.assertEqual(volume, got_volume)61    def nova_reboot(self, server):62        self.servers_client.reboot_server(server['id'], type='SOFT')63        waiters.wait_for_server_status(self.servers_client,64                                       server['id'], 'ACTIVE')65    def check_partitions(self):66        # NOTE(andreaf) The device name may be different on different guest OS67        partitions = self.linux_client.get_partitions()68        self.assertEqual(1, partitions.count(CONF.compute.volume_device_name))69    def create_and_add_security_group_to_server(self, server):70        secgroup = self._create_security_group()71        self.servers_client.add_security_group(server['id'],72                                               name=secgroup['name'])73        self.addCleanup(self.servers_client.remove_security_group,74                        server['id'], name=secgroup['name'])75        def wait_for_secgroup_add():76            body = (self.servers_client.show_server(server['id'])77                    ['server'])78            return {'name': secgroup['name']} in body['security_groups']79        if not test.call_until_true(wait_for_secgroup_add,80                                    CONF.compute.build_timeout,81                                    CONF.compute.build_interval):82            msg = ('Timed out waiting for adding security group %s to server '83                   '%s' % (secgroup['id'], server['id']))84            raise exceptions.TimeoutException(msg)85    @test.idempotent_id('bdbb5441-9204-419d-a225-b4fdbfb1a1a8')86    @test.services('compute', 'volume', 'image', 'network')87    def test_minimum_basic_scenario(self):88        image = self.glance_image_create()89        keypair = self.create_keypair()90        server = self.create_server(image_id=image,91                                    key_name=keypair['name'],92                                    wait_until='ACTIVE')93        servers = self.nova_list()94        self.assertIn(server['id'], [x['id'] for x in servers])95        self.nova_show(server)96        volume = self.cinder_create()97        volumes = self.cinder_list()98        self.assertIn(volume['id'], [x['id'] for x in volumes])99        self.cinder_show(volume)100        volume = self.nova_volume_attach(server, volume)101        self.addCleanup(self.nova_volume_detach, server, volume)102        self.cinder_show(volume)103        floating_ip = self.create_floating_ip(server)104        self.create_and_add_security_group_to_server(server)105        # check that we can SSH to the server before reboot106        self.linux_client = self.get_remote_client(107            floating_ip['ip'], private_key=keypair['private_key'])108        self.nova_reboot(server)109        # check that we can SSH to the server after reboot110        # (both connections are part of the scenario)111        self.linux_client = self.get_remote_client(112            floating_ip['ip'], private_key=keypair['private_key'])...test1.py
Source:test1.py  
...21            server, custom_matchers.MatchesDictExceptForKeys(22                got_server, excluded_keys=excluded_keys))232425    def nova_reboot(self, server):26        self.servers_client.reboot_server(server['id'], type='SOFT')27        waiters.wait_for_server_status(self.servers_client,28                                       server['id'], 'ACTIVE')2930    def check_partitions(self):31        # NOTE(andreaf) The device name may be different on different guest OS32        partitions = self.linux_client.get_partitions()33        self.assertEqual(1, partitions.count(CONF.compute.volume_device_name))3435    def create_and_add_security_group_to_server(self, server):36        secgroup = self._create_security_group()37        self.servers_client.add_security_group(server['id'],38                                               name=secgroup['name'])39        self.addCleanup(self.servers_client.remove_security_group,40                        server['id'], name=secgroup['name'])4142        def wait_for_secgroup_add():43            body = (self.servers_client.show_server(server['id'])44                    ['server'])45            return {'name': secgroup['name']} in body['security_groups']4647        if not test_utils.call_until_true(wait_for_secgroup_add,48                                          CONF.compute.build_timeout,49                                          CONF.compute.build_interval):50            msg = ('Timed out waiting for adding security group %s to server '51                   '%s' % (secgroup['id'], server['id']))52            raise exceptions.TimeoutException(msg)5354    def _get_floating_ip_in_server_addresses(self, floating_ip, server):55        for network_name, addresses in server['addresses'].items():56            for address in addresses:57                if (address['OS-EXT-IPS:type'] == 'floating' and58                        address['addr'] == floating_ip['ip']):59                    return address6061    @test.idempotent_id('bdbb5441-9204-419d-a225-b4fdbfb1a1a8')62    @test.services('compute', 'volume', 'image', 'network')63    def test_minimum_basic_scenario(self):64        image = self.glance_image_create()65        keypair = self.create_keypair()6667        server = self.create_server(image_id=image,68                                    key_name=keypair['name'],69                                    wait_until='ACTIVE')70        servers = self.servers_client.list_servers()['servers']71        self.assertIn(server['id'], [x['id'] for x in servers])7273		print("creating Instance")74        self.nova_show(server)7576        floating_ip = self.create_floating_ip(server)77        # fetch the server again to make sure the addresses were refreshed78        # after associating the floating IP79        server = self.servers_client.show_server(server['id'])['server']80        address = self._get_floating_ip_in_server_addresses(81            floating_ip, server)82        self.assertIsNotNone(83            address,84            "Failed to find floating IP '%s' in server addresses: %s" %85            (floating_ip['ip'], server['addresses']))8687        self.create_and_add_security_group_to_server(server)8889        # check that we can SSH to the server before reboot90        self.linux_client = self.get_remote_client(91            floating_ip['ip'], private_key=keypair['private_key'])9293        self.nova_reboot(server)9495        # check that we can SSH to the server after reboot96        # (both connections are part of the scenario)97        self.linux_client = self.get_remote_client(98            floating_ip['ip'], private_key=keypair['private_key'])99100        self.check_partitions()101102        # delete the floating IP, this should refresh the server addresses103        self.compute_floating_ips_client.delete_floating_ip(floating_ip['id'])104105        def is_floating_ip_detached_from_server():106            server_info = self.servers_client.show_server(107                server['id'])['server']
...test_reboot.py
Source:test_reboot.py  
1# Copyright (c) 2020 Red Hat, Inc.2#3# All Rights Reserved.4#5#    Licensed under the Apache License, Version 2.0 (the "License"); you may6#    not use this file except in compliance with the License. You may obtain7#    a copy of the License at8#9#         http://www.apache.org/licenses/LICENSE-2.010#11#    Unless required by applicable law or agreed to in writing, software12#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT13#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the14#    License for the specific language governing permissions and limitations15#    under the License.16from __future__ import absolute_import17import time18from oslo_log import log19import testtools20import tobiko21from tobiko.shell import ping22from tobiko.shell import sh23from tobiko.openstack import nova24from tobiko.openstack import stacks25LOG = log.getLogger(__name__)26class RebootHostStack(stacks.CirrosServerStackFixture):27    """Server to be rebooted"""28class RebootHostTest(testtools.TestCase):29    stack = tobiko.required_fixture(RebootHostStack)30    def test_reboot_host(self, nova_reboot=False, **params):31        server = self.stack.ensure_server_status('ACTIVE')32        self.assertEqual('ACTIVE', server.status)33        ssh_client = self.stack.ssh_client34        uptime_0 = sh.get_uptime(ssh_client=ssh_client)35        LOG.debug("Testing reboot command on remote host: "36                  "uptime=%r", uptime_0)37        boottime_0 = time.time() - uptime_038        # Wait for CirrOS init script to terminate before rebooting the VM39        sh.wait_for_processes(command='^{.*}',40                              ssh_client=ssh_client,41                              timeout=90.)42        reboot = sh.reboot_host(ssh_client=ssh_client, **params)43        self.assertIs(ssh_client, reboot.ssh_client)44        self.assertEqual(ssh_client.hostname, reboot.hostname)45        method = params.get('method') or sh.soft_reset_method46        self.assertIs(method, reboot.method)47        if not reboot.is_rebooted:48            self.assert_is_not_connected(ssh_client)49            if nova_reboot:50                ping.ping_until_unreceived(self.stack.ip_address)51                nova.reboot_server(server)52            reboot.wait_for_operation()53        self.assertTrue(reboot.is_rebooted)54        self.assert_is_connected(ssh_client)55        server = nova.wait_for_server_status(self.stack.server_id, 'ACTIVE')56        self.assertEqual('ACTIVE', server.status)57        uptime_1 = sh.get_uptime(ssh_client=ssh_client)58        boottime_1 = time.time() - uptime_159        LOG.debug("Reboot operation executed on remote host: "60                  "uptime=%r", uptime_1)61        self.assertGreater(boottime_1, boottime_0)62    def test_reboot_host_with_chash_method(self):63        self.test_reboot_host(method=sh.crash_method,64                              wait=False,65                              nova_reboot=True)66    def test_reboot_host_with_hard_method(self):67        self.test_reboot_host(method=sh.hard_reset_method)68    def test_reboot_host_with_soft_method(self):69        self.test_reboot_host(method=sh.soft_reset_method)70    def test_reboot_host_with_invalid_method(self):71        self.assertRaises(TypeError,72                          sh.reboot_host,73                          ssh_client=self.stack.ssh_client,74                          method='<invalid-method>')75    def test_reboot_host_with_wait(self):76        self.test_reboot_host(wait=True)77    def test_reboot_host_with_no_wait(self):78        self.test_reboot_host(wait=False)79    def test_reboot_server_when_shutoff(self):80        server = self.stack.ensure_server_status('SHUTOFF')81        self.assertEqual('SHUTOFF', server.status)82        ssh_client = self.stack.ssh_client83        self.assert_is_not_connected(ssh_client)84        self.assertRaises(sh.RebootHostTimeoutError,85                          sh.reboot_host,86                          ssh_client=ssh_client,87                          timeout=5.0)88        self.assert_is_not_connected(ssh_client)89        server = nova.get_server(self.stack.server_id)90        self.assertEqual('SHUTOFF', server.status)91    def assert_is_connected(self, ssh_client):92        self.assertIsNotNone(ssh_client.client)93    def assert_is_not_connected(self, ssh_client):...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!!
