Best Python code snippet using tempest_python
test_network_advanced_server_ops.py
Source:test_network_advanced_server_ops.py
...56 networks=[{'uuid': network['id']}],57 key_name=keypair['name'],58 security_groups=security_groups)59 return server60 def _setup_network(self, server, keypair):61 public_network_id = CONF.network.public_network_id62 floating_ip = self.create_floating_ip(server, public_network_id)63 # Verify that we can indeed connect to the server before we mess with64 # it's state65 self._wait_server_status_and_check_network_connectivity(66 server, keypair, floating_ip)67 return floating_ip68 def _check_network_connectivity(self, server, keypair, floating_ip,69 should_connect=True):70 username = CONF.validation.image_ssh_user71 private_key = keypair['private_key']72 self._check_tenant_network_connectivity(73 server, username, private_key,74 should_connect=should_connect,75 servers_for_debug=[server])76 floating_ip_addr = floating_ip['floating_ip_address']77 # Check FloatingIP status before checking the connectivity78 self.check_floating_ip_status(floating_ip, 'ACTIVE')79 self.check_public_network_connectivity(floating_ip_addr, username,80 private_key, should_connect,81 servers=[server])82 def _wait_server_status_and_check_network_connectivity(self, server,83 keypair,84 floating_ip):85 waiters.wait_for_server_status(self.servers_client, server['id'],86 'ACTIVE')87 self._check_network_connectivity(server, keypair, floating_ip)88 def _get_host_for_server(self, server_id):89 body = self.admin_servers_client.show_server(server_id)['server']90 return body['OS-EXT-SRV-ATTR:host']91 @decorators.idempotent_id('61f1aa9a-1573-410e-9054-afa557cab021')92 @test.services('compute', 'network')93 def test_server_connectivity_stop_start(self):94 keypair = self.create_keypair()95 server = self._setup_server(keypair)96 floating_ip = self._setup_network(server, keypair)97 self.servers_client.stop_server(server['id'])98 waiters.wait_for_server_status(self.servers_client, server['id'],99 'SHUTOFF')100 self._check_network_connectivity(server, keypair, floating_ip,101 should_connect=False)102 self.servers_client.start_server(server['id'])103 self._wait_server_status_and_check_network_connectivity(104 server, keypair, floating_ip)105 @decorators.idempotent_id('7b6860c2-afa3-4846-9522-adeb38dfbe08')106 @test.services('compute', 'network')107 def test_server_connectivity_reboot(self):108 keypair = self.create_keypair()109 server = self._setup_server(keypair)110 floating_ip = self._setup_network(server, keypair)111 self.servers_client.reboot_server(server['id'], type='SOFT')112 self._wait_server_status_and_check_network_connectivity(113 server, keypair, floating_ip)114 @decorators.idempotent_id('88a529c2-1daa-4c85-9aec-d541ba3eb699')115 @test.services('compute', 'network')116 def test_server_connectivity_rebuild(self):117 keypair = self.create_keypair()118 server = self._setup_server(keypair)119 floating_ip = self._setup_network(server, keypair)120 image_ref_alt = CONF.compute.image_ref_alt121 self.servers_client.rebuild_server(server['id'],122 image_ref=image_ref_alt)123 self._wait_server_status_and_check_network_connectivity(124 server, keypair, floating_ip)125 @decorators.idempotent_id('2b2642db-6568-4b35-b812-eceed3fa20ce')126 @testtools.skipUnless(CONF.compute_feature_enabled.pause,127 'Pause is not available.')128 @test.services('compute', 'network')129 def test_server_connectivity_pause_unpause(self):130 keypair = self.create_keypair()131 server = self._setup_server(keypair)132 floating_ip = self._setup_network(server, keypair)133 self.servers_client.pause_server(server['id'])134 waiters.wait_for_server_status(self.servers_client, server['id'],135 'PAUSED')136 self._check_network_connectivity(server, keypair, floating_ip,137 should_connect=False)138 self.servers_client.unpause_server(server['id'])139 self._wait_server_status_and_check_network_connectivity(140 server, keypair, floating_ip)141 @decorators.idempotent_id('5cdf9499-541d-4923-804e-b9a60620a7f0')142 @testtools.skipUnless(CONF.compute_feature_enabled.suspend,143 'Suspend is not available.')144 @test.services('compute', 'network')145 def test_server_connectivity_suspend_resume(self):146 keypair = self.create_keypair()147 server = self._setup_server(keypair)148 floating_ip = self._setup_network(server, keypair)149 self.servers_client.suspend_server(server['id'])150 waiters.wait_for_server_status(self.servers_client, server['id'],151 'SUSPENDED')152 self._check_network_connectivity(server, keypair, floating_ip,153 should_connect=False)154 self.servers_client.resume_server(server['id'])155 self._wait_server_status_and_check_network_connectivity(156 server, keypair, floating_ip)157 @decorators.idempotent_id('719eb59d-2f42-4b66-b8b1-bb1254473967')158 @testtools.skipUnless(CONF.compute_feature_enabled.resize,159 'Resize is not available.')160 @test.services('compute', 'network')161 def test_server_connectivity_resize(self):162 resize_flavor = CONF.compute.flavor_ref_alt163 if resize_flavor == CONF.compute.flavor_ref:164 msg = "Skipping test - flavor_ref and flavor_ref_alt are identical"165 raise self.skipException(msg)166 keypair = self.create_keypair()167 server = self._setup_server(keypair)168 floating_ip = self._setup_network(server, keypair)169 self.servers_client.resize_server(server['id'],170 flavor_ref=resize_flavor)171 waiters.wait_for_server_status(self.servers_client, server['id'],172 'VERIFY_RESIZE')173 self.servers_client.confirm_resize_server(server['id'])174 self._wait_server_status_and_check_network_connectivity(175 server, keypair, floating_ip)176 @decorators.idempotent_id('a4858f6c-401e-4155-9a49-d5cd053d1a2f')177 @testtools.skipUnless(CONF.compute_feature_enabled.cold_migration,178 'Cold migration is not available.')179 @testtools.skipUnless(CONF.compute.min_compute_nodes > 1,180 'Less than 2 compute nodes, skipping multinode '181 'tests.')182 @test.services('compute', 'network')183 def test_server_connectivity_cold_migration(self):184 keypair = self.create_keypair()185 server = self._setup_server(keypair)186 floating_ip = self._setup_network(server, keypair)187 src_host = self._get_host_for_server(server['id'])188 self._wait_server_status_and_check_network_connectivity(189 server, keypair, floating_ip)190 self.admin_servers_client.migrate_server(server['id'])191 waiters.wait_for_server_status(self.servers_client, server['id'],192 'VERIFY_RESIZE')193 self.servers_client.confirm_resize_server(server['id'])194 self._wait_server_status_and_check_network_connectivity(195 server, keypair, floating_ip)196 dst_host = self._get_host_for_server(server['id'])197 self.assertNotEqual(src_host, dst_host)198 @decorators.idempotent_id('25b188d7-0183-4b1e-a11d-15840c8e2fd6')199 @testtools.skipUnless(CONF.compute_feature_enabled.cold_migration,200 'Cold migration is not available.')201 @testtools.skipUnless(CONF.compute.min_compute_nodes > 1,202 'Less than 2 compute nodes, skipping multinode '203 'tests.')204 @test.services('compute', 'network')205 def test_server_connectivity_cold_migration_revert(self):206 keypair = self.create_keypair()207 server = self._setup_server(keypair)208 floating_ip = self._setup_network(server, keypair)209 src_host = self._get_host_for_server(server['id'])210 self._wait_server_status_and_check_network_connectivity(211 server, keypair, floating_ip)212 self.admin_servers_client.migrate_server(server['id'])213 waiters.wait_for_server_status(self.servers_client, server['id'],214 'VERIFY_RESIZE')215 self.servers_client.revert_resize_server(server['id'])216 self._wait_server_status_and_check_network_connectivity(217 server, keypair, floating_ip)218 dst_host = self._get_host_for_server(server['id'])...
net_real.py
Source:net_real.py
...8class MnistReal0(SeqRealModelHelper, nn.Module, ModelHelper):9 """real-valued mnist for reference"""10 def __init__(self, quant_step: float):11 super().__init__()12 self._setup_network(float(quant_step))13 def _setup_network(self, quant_step):14 self.make_small_network(self, quant_step, 1, 7)15 @classmethod16 def make_small_network(cls, self, quant_step, input_chl, pre_fc_spatial):17 self.features = nn.Sequential(18 InputQuantizer(quant_step),19 nn.Conv2d(input_chl, 16, 4, stride=2, padding=1),20 nn.ReLU(),21 nn.Conv2d(16, 32, 4, stride=2, padding=1),22 nn.ReLU(),23 Flatten(),24 nn.Linear(32 * pre_fc_spatial**2, 100),25 nn.ReLU(),26 nn.Linear(100, 10),27 )28 def forward(self, x):29 return self.features(x)30 def make_dataset_loader(self, args, train: bool):31 return Mnist0.make_dataset_loader(args, train)32class MnistReal1(MnistReal0):33 def _setup_network(self, quant_step):34 self.make_large_network(self, quant_step, 1, 7)35 @classmethod36 def make_large_network(37 cls, self, quant_step, input_chl, pre_fc_spatial,38 channel_sizes=[32, 64, 512, 512], kernel_sizes=[3, 4]):39 layers = [40 InputQuantizer(quant_step),41 ]42 prev_chl = input_chl43 # conv layers44 for out_chl in channel_sizes[:2]:45 for ksize in kernel_sizes:46 layers.extend([47 nn.Conv2d(prev_chl, out_chl, ksize,48 stride=ksize-2, padding=1),49 nn.ReLU(),50 ])51 prev_chl = out_chl52 # fc layers53 layers.append(Flatten())54 prev_chl *= pre_fc_spatial**255 for out_chl in channel_sizes[2:] + [10]:56 layers.extend([57 nn.Linear(prev_chl, out_chl),58 nn.ReLU(),59 ])60 prev_chl = out_chl61 layers.pop() # remove last relu62 self.features = nn.Sequential(*layers)63class Cifar10Real0(SeqRealModelHelper, nn.Module, ModelHelper):64 def __init__(self, quant_step: float):65 super().__init__()66 self._setup_network(float(quant_step))67 def _setup_network(self, quant_step):68 MnistReal0.make_small_network(self, quant_step, 3, 8)69 @classmethod70 def make_dataset_loader(self, args, train: bool):71 return Cifar10_0.make_dataset_loader(args, train)72class Cifar10Real1(Cifar10Real0):73 def _setup_network(self, quant_step):74 MnistReal1.make_large_network(self, quant_step, 3, 8)...
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!!