How to use verify_ssh method in tempest

Best Python code snippet using tempest_python

test_mdproxy.py

Source:test_mdproxy.py Github

copy

Full Screen

...47 if not CONF.nsxv3.native_dhcp_metadata:48 msg = " native_dhcp_metadata is not enabled under nsxv3 config" \49 ", skipping all the MDProxy tests!!!"50 raise cls.skipException(msg)51 def verify_ssh(self, keypair, instance, port_id=None):52 created_floating_ip = self.create_floating_ip(instance,53 port_id=port_id)54 self.fip = str(created_floating_ip["floating_ip_address"])55 self.assertIsNotNone(self.fip)56 # Check ssh57 self.ssh_client = self.get_remote_client(58 ip_address=self.fip, username=self.ssh_user,59 private_key=keypair["private_key"])60 def _delete_router(self, router):61 body = self.ports_client.list_ports(device_id=router["id"])62 interfaces = body["ports"]63 for interface in interfaces:64 test_utils.call_and_ignore_notfound_exc(65 self.routers_client.remove_router_interface, router["id"],66 subnet_id=interface["fixed_ips"][0]["subnet_id"])67 self.routers_client.delete_router(router["id"])68 def _create_router(self, router_name=None, admin_state_up=True,69 external_network_id=None, enable_snat=None, **kwargs):70 ext_gw_info = {}71 if external_network_id:72 ext_gw_info["network_id"] = external_network_id73 if enable_snat is not None:74 ext_gw_info["enable_snat"] = enable_snat75 body = self.routers_client.create_router(76 name=router_name, external_gateway_info=ext_gw_info,77 admin_state_up=admin_state_up, **kwargs)78 router = body["router"]79 self.addCleanup(self._delete_router, router)80 return router81 def _create_net_subnet_router_interface(self, router=None):82 network = self._create_network(namestart="network-mdproxy")83 subnet = self._create_subnet(network)84 if router:85 self.routers_client.add_router_interface(86 router["id"], subnet_id=subnet["id"])87 self.addCleanup(self.routers_client.remove_router_interface,88 router["id"], subnet_id=subnet["id"])89 return network["id"], subnet["id"]90 def _create_server_on_networks(self, networks):91 security_group = self._create_security_group()92 name = data_utils.rand_name("server-mdproxy")93 keypair = self.create_keypair()94 security_groups = [{"name": security_group["name"]}]95 instance = self.create_server(96 image_id=self.image_ref,97 flavor=self.flavor_ref,98 config_drive=CONF.compute_feature_enabled.config_drive, name=name,99 networks=networks, key_name=keypair["name"],100 security_groups=security_groups, wait_until="ACTIVE")101 self.addCleanup(self.servers_client.delete_server, instance["id"])102 return instance, keypair103 def _list_ports(self, *args, **kwargs):104 """List ports using admin creds """105 ports_list = self.admin_manager.ports_client.list_ports(106 *args, **kwargs)107 return ports_list['ports']108 def _get_port_id(self, network_id, subnet_id, instance):109 instance_addrs = instance["addresses"].items()110 instance_fixed_ips = []111 for addr in instance_addrs:112 instance_fixed_ips.append(addr[1][0]["addr"])113 for port in self._list_ports(device_id=instance['id']):114 port_fixed_ip = port["fixed_ips"][0]["ip_address"]115 if port["network_id"] == network_id and port["fixed_ips"][0][116 "subnet_id"] == subnet_id and "compute:" in port[117 "device_owner"] and port_fixed_ip in instance_fixed_ips:118 port_id = port["id"]119 self.assertIsNotNone(port_id, "Failed to find Instance's port id!!!")120 return port_id121 def _verify_md(self, md_url, expected_value="", check_exist_only=False,122 sub_result=None):123 def exec_cmd_and_verify_output():124 cmd = "curl " + md_url125 exec_cmd_retried = 0126 import time127 while exec_cmd_retried < \128 constants.MAX_NO_OF_TIMES_EXECUTION_OVER_SSH:129 result = self.ssh_client.exec_command(cmd)130 self.assertIsNotNone(result)131 if not result == "":132 break133 exec_cmd_retried += 1134 time.sleep(constants.INTERVAL_BETWEEN_EXEC_RETRY_ON_SSH)135 LOG.info("Tried %s times!!!", exec_cmd_retried)136 if check_exist_only:137 return "Verification is successful!"138 msg = ("Failed while verifying metadata on server. Result "139 "of command %r is NOT %r." % (cmd, expected_value))140 if sub_result:141 msg2 = ("Failed to verify incorrect passowrd on metadata"142 "server. Result %r is NOT in %r." % (143 sub_result, result))144 self.assertIn(sub_result, result, msg2)145 return "Verification is successful!"146 self.assertEqual(expected_value, result, msg)147 return "Verification is successful!"148 if not test_utils.call_until_true(exec_cmd_and_verify_output,149 CONF.compute.build_timeout,150 CONF.compute.build_interval):151 raise exceptions.TimeoutException("Timed out while waiting to "152 "verify metadata on server. "153 "%s is empty." % md_url)154 def verify_metadata_in_detail(self, instance):155 # Check floating IPv4 in Metadata.156 md_url_pubic_ipv4 = constants.MD_BASE_URL + \157 "latest/meta-data/public-ipv4"158 self._verify_md(md_url=md_url_pubic_ipv4, expected_value=self.fip)159 # Check hostname in Metadata.160 md_url_hostname = constants.MD_BASE_URL + "latest/meta-data/hostname"161 self._verify_md(md_url=md_url_hostname,162 expected_value=instance["name"] + ".novalocal")163 # Check local IPv4 in Metadata.164 md_url_local_ipv4 = constants.MD_BASE_URL + \165 "latest/meta-data/local-ipv4"166 self._verify_md(md_url=md_url_local_ipv4, check_exist_only=True)167 # Check hostname in Metadata of 2009-04-04 folder.168 md_url_hostname = constants.MD_BASE_URL + \169 "2009-04-04/meta-data/hostname"170 self._verify_md(md_url=md_url_hostname,171 expected_value=instance["name"] + ".novalocal")172 # Check hostname in Metadata of 1.0 folder.173 md_url_hostname = constants.MD_BASE_URL + "1.0/meta-data/hostname"174 self._verify_md(md_url=md_url_hostname,175 expected_value=instance["name"] + ".novalocal")176 def verify_md_proxy_logical_ports_on_backend(self):177 md_counter = 0178 logical_ports = self.nsx.get_os_logical_ports()179 for port_index in range(len(logical_ports)):180 if logical_ports[port_index]["attachment"][181 "attachment_type"] == "METADATA_PROXY":182 md_counter += 1183 msg = "Admin state of MDProxy logical port is DOWN!!!"184 msg2 = "LS name does not start with mdproxy!!!"185 msg3 = "MDproxy logical port does not have any auto tag!!!"186 self.assertEqual(187 "UP", logical_ports[port_index]["admin_state"], msg)188 self.assertIn("mdproxy-",189 logical_ports[port_index]["display_name"], msg2)190 self.assertNotEqual(0, len(logical_ports[port_index]["tags"]),191 msg3)192 self.assertNotEqual(0, md_counter, "No logical port found for MD "193 "proxy!!!")194 @decorators.idempotent_id("e9a93161-d852-414d-aa55-36d465ea45df")195 @test.services("compute", "network")196 def test_mdproxy_ping(self):197 router = self._create_router(198 router_name=data_utils.rand_name("router-MDProxy"),199 external_network_id=CONF.network.public_network_id)200 (network_id, subnet_id) = self._create_net_subnet_router_interface(201 router)202 networks_ids = {"uuid": network_id}203 instance, keypair = self._create_server_on_networks([networks_ids])204 port_id = self._get_port_id(network_id, subnet_id, instance)205 self.verify_ssh(keypair=keypair, instance=instance, port_id=port_id)206 md_url_pubic_ipv4 = constants.MD_BASE_URL + \207 "latest/meta-data/public-ipv4"208 self._verify_md(md_url=md_url_pubic_ipv4, expected_value=self.fip)209 @decorators.idempotent_id("743f34a6-58b8-4288-a07f-7bee21c55051")210 @test.services("compute", "network")211 def test_mdproxy_verify_backend(self):212 router = self._create_router(213 router_name=data_utils.rand_name("router-MDProxy"),214 external_network_id=CONF.network.public_network_id)215 (network_id, subnet_id) = self._create_net_subnet_router_interface(216 router)217 networks_ids = {"uuid": network_id}218 instance, keypair = self._create_server_on_networks([networks_ids])219 port_id = self._get_port_id(network_id, subnet_id, instance)220 self.verify_ssh(keypair=keypair, instance=instance, port_id=port_id)221 self.verify_metadata_in_detail(instance=instance)222 self.verify_md_proxy_logical_ports_on_backend()223 @decorators.idempotent_id("fce2acc8-b850-40fe-bf02-958dd3cd4343")224 @test.services("compute", "network")225 def test_mdproxy_with_server_on_two_ls(self):226 router = self._create_router(227 router_name=data_utils.rand_name("router-MDProxy"),228 external_network_id=CONF.network.public_network_id)229 (network_id1, subnet_id1) = self._create_net_subnet_router_interface(230 router)231 (network_id2, subnet_id2) = self._create_net_subnet_router_interface(232 router)233 net1 = {"uuid": network_id1}234 net2 = {"uuid": network_id2}235 instance, keypair = self._create_server_on_networks([net1, net2])236 port_id = self._get_port_id(network_id1, subnet_id1, instance)237 self.verify_ssh(keypair=keypair, instance=instance, port_id=port_id)238 self.verify_metadata_in_detail(instance=instance)239 @decorators.idempotent_id("67332752-1295-42cb-a8c3-99210fb6b00b")240 @test.services("compute", "network")241 def test_mdproxy_isolated_network(self):242 (network_id, _) = self._create_net_subnet_router_interface()243 networks_ids = {"uuid": network_id}244 self._create_server_on_networks([networks_ids])245 self.verify_md_proxy_logical_ports_on_backend()246 @decorators.idempotent_id("cc8d2ab8-0bea-4e32-bf80-c9c46a7612b7")247 @test.attr(type=["negative"])248 @test.services("compute", "network")249 def test_mdproxy_delete_when_ls_bounded(self):250 (network_id, _) = self._create_net_subnet_router_interface()251 networks_ids = {"uuid": network_id}252 self._create_server_on_networks([networks_ids])253 md_proxy_uuid = self.nsx.get_md_proxies()[0]["id"]254 result = self.nsx.delete_md_proxy(md_proxy_uuid)255 self.assertEqual(str(result["error_code"]),256 constants.MD_ERROR_CODE_WHEN_LS_BOUNDED)257 @decorators.idempotent_id("501fc3ea-696b-4e9e-b383-293ab94e2545")258 @test.services("compute", "network")259 def test_mdproxy_with_multiple_ports_on_network(self):260 router = self._create_router(261 router_name=data_utils.rand_name("router-MDProxy"),262 external_network_id=CONF.network.public_network_id)263 (network_id, subnet_id) = self._create_net_subnet_router_interface(264 router)265 networks_ids = {"uuid": network_id}266 instance, keypair = self._create_server_on_networks([networks_ids])267 instance2, keypair2 = self._create_server_on_networks([networks_ids])268 port_id = self._get_port_id(network_id, subnet_id, instance)269 # Verify 1st instance.270 self.verify_ssh(keypair=keypair, instance=instance, port_id=port_id)271 self.verify_metadata_in_detail(instance=instance)272 # Verify 2nd instance.273 port_id2 = self._get_port_id(network_id, subnet_id, instance2)274 self.verify_ssh(keypair=keypair2, instance=instance2, port_id=port_id2)275 self.verify_metadata_in_detail(instance=instance2)276 @decorators.idempotent_id("eae21afc-50ea-42e5-9c49-2ee38cee9f06")277 @test.services("compute", "network")278 def test_mdproxy_with_multiple_metadata_ports(self):279 router = self._create_router(280 router_name=data_utils.rand_name("router-MDProxy"),281 external_network_id=CONF.network.public_network_id)282 (network_id1, subnet_id1) = self._create_net_subnet_router_interface(283 router)284 (network_id2, subnet_id2) = self._create_net_subnet_router_interface(285 router)286 net1 = {"uuid": network_id1}287 net2 = {"uuid": network_id2}288 instance, keypair = self._create_server_on_networks([net1])289 instance2, keypair2 = self._create_server_on_networks([net2])290 port_id1 = self._get_port_id(network_id1, subnet_id1, instance)291 port_id2 = self._get_port_id(network_id2, subnet_id2, instance2)292 self.verify_ssh(keypair=keypair, instance=instance, port_id=port_id1)293 self.verify_metadata_in_detail(instance=instance)294 self.verify_ssh(keypair=keypair2, instance=instance2, port_id=port_id2)295 self.verify_metadata_in_detail(instance=instance2)296 @decorators.idempotent_id("29d44d7c-6ea1-4b30-a6c3-a2695c2486fe")297 @test.attr(type=["negative"])298 @test.services("compute", "network")299 def test_mdproxy_with_incorrect_password(self):300 router = self._create_router(301 router_name=data_utils.rand_name("router-MDProxy"),302 external_network_id=CONF.network.public_network_id)303 (network_id, subnet_id) = self._create_net_subnet_router_interface(304 router)305 networks_ids = {"uuid": network_id}306 instance, keypair = self._create_server_on_networks([networks_ids])307 port_id = self._get_port_id(network_id, subnet_id, instance)308 self.verify_ssh(keypair=keypair, instance=instance, port_id=port_id)309 md_url = constants.MD_BASE_URL + "latest/meta-data/public-ipv4"...

Full Screen

Full Screen

cisBonus.py

Source:cisBonus.py Github

copy

Full Screen

...29 "10.{0} Verify network settings - {1}".format(30 counter, config.get('network', setting))31 )32 check_not_empty("grep %s /etc/sysctl.conf" % config.get('network', setting))33def verify_ssh():34 '''35 Verify that all ssh settings has been applied.36 Not a CIS test.37 '''38 #39 print_header("11 BONUS - Verify ssh settings")40 #41 print_header("11.1 BONUS - Verify ssh settings")42 config = ConfigParser.SafeConfigParser()43 config.read('%s/hardening/config.cfg' % app.SYCO_VAR_PATH)44 counter = 045 for setting in config.options('ssh'):46 counter += 147 print_header("11.%s Verify ssh settings - %s" %48 (counter, config.get('ssh', setting)))49 check_not_empty("grep %s /etc/ssh/ssh_config" % config.get('ssh', setting))50 #51 print_header("11.2 BONUS - Verify ssh settings")52 counter = 053 for setting in config.options('sshd'):54 counter += 155 print_header("11.%s Verify sshd settings - %s" %56 (counter, config.get('sshd', setting)))57 check_not_empty("grep %s /etc/ssh/sshd_config" % config.get('sshd', setting))58#59# Tests to execute on import60#61verify_network()...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tempest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful