Best Python code snippet using tempest_python
test_servers.py
Source:test_servers.py
...34 """Simple check that listing servers works."""35 servers = self.api.get_servers()36 for server in servers:37 LOG.debug("server: %s" % server)38 def test_create_and_delete_server(self):39 """Creates and deletes a server."""40 self.flags(stub_network=True)41 # Create server42 # Build the server data gradually, checking errors along the way43 server = {}44 good_server = self._build_minimal_create_server_request()45 post = {'server': server}46 # Without an imageRef, this throws 500.47 # TODO(justinsb): Check whatever the spec says should be thrown here48 self.assertRaises(client.OpenStackApiException,49 self.api.post_server, post)50 # With an invalid imageRef, this throws 500.51 server['imageRef'] = self.get_invalid_image()52 # TODO(justinsb): Check whatever the spec says should be thrown here53 self.assertRaises(client.OpenStackApiException,54 self.api.post_server, post)55 # Add a valid imageId/imageRef56 server['imageId'] = good_server.get('imageId')57 server['imageRef'] = good_server.get('imageRef')58 # Without flavorId, this throws 50059 # TODO(justinsb): Check whatever the spec says should be thrown here60 self.assertRaises(client.OpenStackApiException,61 self.api.post_server, post)62 # Set a valid flavorId/flavorRef63 server['flavorRef'] = good_server.get('flavorRef')64 server['flavorId'] = good_server.get('flavorId')65 # Without a name, this throws 50066 # TODO(justinsb): Check whatever the spec says should be thrown here67 self.assertRaises(client.OpenStackApiException,68 self.api.post_server, post)69 # Set a valid server name70 server['name'] = good_server['name']71 created_server = self.api.post_server(post)72 LOG.debug("created_server: %s" % created_server)73 self.assertTrue(created_server['id'])74 created_server_id = created_server['id']75 # Check it's there76 found_server = self.api.get_server(created_server_id)77 self.assertEqual(created_server_id, found_server['id'])78 # It should also be in the all-servers list79 servers = self.api.get_servers()80 server_ids = [server['id'] for server in servers]81 self.assertTrue(created_server_id in server_ids)82 found_server = self._wait_for_creation(found_server)83 # It should be available...84 # TODO(justinsb): Mock doesn't yet do this...85 self.assertEqual('ACTIVE', found_server['status'])86 servers = self.api.get_servers(detail=True)87 for server in servers:88 self.assertTrue("image" in server)89 self.assertTrue("flavor" in server)90 self._delete_server(created_server_id)91 def _delete_server(self, server_id):92 # Delete the server93 self.api.delete_server(server_id)94 # Wait (briefly) for deletion95 for _retries in range(5):96 try:97 found_server = self.api.get_server(server_id)98 except client.OpenStackApiNotFoundException:99 found_server = None100 LOG.debug("Got 404, proceeding")101 break102 LOG.debug("Found_server=%s" % found_server)103 # TODO(justinsb): Mock doesn't yet do accurate state changes104 #if found_server['status'] != 'deleting':105 # break106 time.sleep(1)107 # Should be gone108 self.assertFalse(found_server)109 def test_create_server_with_metadata(self):110 """Creates a server with metadata."""111 # Build the server data gradually, checking errors along the way112 server = self._build_minimal_create_server_request()113 metadata = {}114 for i in range(30):115 metadata['key_%s' % i] = 'value_%s' % i116 server['metadata'] = metadata117 post = {'server': server}118 created_server = self.api.post_server(post)119 LOG.debug("created_server: %s" % created_server)120 self.assertTrue(created_server['id'])121 created_server_id = created_server['id']122 # Reenable when bug fixed123 self.assertEqual(metadata, created_server.get('metadata'))124 # Check it's there125 found_server = self.api.get_server(created_server_id)126 self.assertEqual(created_server_id, found_server['id'])127 self.assertEqual(metadata, found_server.get('metadata'))128 # The server should also be in the all-servers details list129 servers = self.api.get_servers(detail=True)130 server_map = dict((server['id'], server) for server in servers)131 found_server = server_map.get(created_server_id)132 self.assertTrue(found_server)133 # Details do include metadata134 self.assertEqual(metadata, found_server.get('metadata'))135 # The server should also be in the all-servers summary list136 servers = self.api.get_servers(detail=False)137 server_map = dict((server['id'], server) for server in servers)138 found_server = server_map.get(created_server_id)139 self.assertTrue(found_server)140 # Summary should not include metadata141 self.assertFalse(found_server.get('metadata'))142 # Cleanup143 self._delete_server(created_server_id)144 def test_create_and_rebuild_server(self):145 """Rebuild a server."""146 self.flags(stub_network=True)147 # create a server with initially has no metadata148 server = self._build_minimal_create_server_request()149 server_post = {'server': server}150 created_server = self.api.post_server(server_post)151 LOG.debug("created_server: %s" % created_server)152 self.assertTrue(created_server['id'])153 created_server_id = created_server['id']154 created_server = self._wait_for_creation(created_server)155 # rebuild the server with metadata156 post = {}157 post['rebuild'] = {158 "imageRef": "https://localhost/v1.1/32278/images/3",159 "name": "blah",160 }161 self.api.post_server_action(created_server_id, post)162 LOG.debug("rebuilt server: %s" % created_server)163 self.assertTrue(created_server['id'])164 found_server = self.api.get_server(created_server_id)165 self.assertEqual(created_server_id, found_server['id'])166 self.assertEqual({}, found_server.get('metadata'))167 self.assertEqual('blah', found_server.get('name'))168 self.assertEqual('3', found_server.get('image')['id'])169 # Cleanup170 self._delete_server(created_server_id)171 def test_create_and_rebuild_server_with_metadata(self):172 """Rebuild a server with metadata."""173 self.flags(stub_network=True)174 # create a server with initially has no metadata175 server = self._build_minimal_create_server_request()176 server_post = {'server': server}177 created_server = self.api.post_server(server_post)178 LOG.debug("created_server: %s" % created_server)179 self.assertTrue(created_server['id'])180 created_server_id = created_server['id']181 created_server = self._wait_for_creation(created_server)182 # rebuild the server with metadata183 post = {}184 post['rebuild'] = {185 "imageRef": "https://localhost/v1.1/32278/images/2",186 "name": "blah",187 }188 metadata = {}189 for i in range(30):190 metadata['key_%s' % i] = 'value_%s' % i191 post['rebuild']['metadata'] = metadata192 self.api.post_server_action(created_server_id, post)193 LOG.debug("rebuilt server: %s" % created_server)194 self.assertTrue(created_server['id'])195 found_server = self.api.get_server(created_server_id)196 self.assertEqual(created_server_id, found_server['id'])197 self.assertEqual(metadata, found_server.get('metadata'))198 self.assertEqual('blah', found_server.get('name'))199 # Cleanup200 self._delete_server(created_server_id)201 def test_create_and_rebuild_server_with_metadata_removal(self):202 """Rebuild a server with metadata."""203 self.flags(stub_network=True)204 # create a server with initially has no metadata205 server = self._build_minimal_create_server_request()206 server_post = {'server': server}207 metadata = {}208 for i in range(30):209 metadata['key_%s' % i] = 'value_%s' % i210 server_post['server']['metadata'] = metadata211 created_server = self.api.post_server(server_post)212 LOG.debug("created_server: %s" % created_server)213 self.assertTrue(created_server['id'])214 created_server_id = created_server['id']215 created_server = self._wait_for_creation(created_server)216 # rebuild the server with metadata217 post = {}218 post['rebuild'] = {219 "imageRef": "https://localhost/v1.1/32278/images/2",220 "name": "blah",221 }222 metadata = {}223 post['rebuild']['metadata'] = metadata224 self.api.post_server_action(created_server_id, post)225 LOG.debug("rebuilt server: %s" % created_server)226 self.assertTrue(created_server['id'])227 found_server = self.api.get_server(created_server_id)228 self.assertEqual(created_server_id, found_server['id'])229 self.assertEqual(metadata, found_server.get('metadata'))230 self.assertEqual('blah', found_server.get('name'))231 # Cleanup232 self._delete_server(created_server_id)233 def test_rename_server(self):234 """Test building and renaming a server."""235 # Create a server236 server = self._build_minimal_create_server_request()237 created_server = self.api.post_server({'server': server})238 LOG.debug("created_server: %s" % created_server)239 server_id = created_server['id']240 self.assertTrue(server_id)241 # Rename the server to 'new-name'242 self.api.put_server(server_id, {'server': {'name': 'new-name'}})243 # Check the name of the server244 created_server = self.api.get_server(server_id)245 self.assertEqual(created_server['name'], 'new-name')246 # Cleanup247 self._delete_server(server_id)248if __name__ == "__main__":...
nova
Source:nova
...21 return True22 time.sleep(2)23 return False24 25 def _delete_server(self, server):26 """Nassty hack, current stack requires >1 delete"""27 start = time.time()28 while time.time() - start < self.config["timeout"] : 29 try:30 server.delete()31 time.sleep(2)32 except novaclient.exceptions.NotFound:33 # it took34 return 35 raise Exception("unable to delete server") 36 def run(self):37 #38 # clean up last (failed?) test 39 #40 for server in self.client.servers.list():41 if server.name == self.instance_name:42 self._delete_server(server)43 44 45 image_name = self.config.get("image_name", "Cirros 0.3.0 x86_64")46 images = [ i for i in self.client.images.list() if i.name == image_name ] 47 if len(images)!=1:48 raise Exception("Found %d images called %s" %(len(images), image_name))49 flavor_name = self.config.get("flavor", "m1.small")50 flavors = [ f for f in self.client.flavors.list() if f.name == flavor_name] 51 if len(flavors)!=1:52 raise Exception("Found %d flavors called %s" %(len(flavors), flavor_name))53 #54 # Make the server55 #56 server = self.client.servers.create(self.instance_name,57 images[0],58 flavors[0],59 )60 61 if not self._wait_for_statechange(server, "ACTIVE"):62 return False, "unable to create and instance"63 64 time.sleep(120)65 #66 # Kill the server67 #68 self._delete_server(server)69 70 71 return True, ""72if __name__ == '__main__':73 import yaml74 import sys75 config = yaml.load(open(sys.argv[1], "r"))76 rc = {}77 try:78 result, msg = TestNovaCompute(config).run();79 rc["msg"] = msg80 if result:81 rc["result"]= "OKAY"82 else:...
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!!