Best Python code snippet using tempest_python
test_list_server_filters.py
Source:test_list_server_filters.py
...76 @utils.skip_unless_attr('multiple_images', 'Only one image found')77 def test_list_servers_filter_by_image(self):78 # Filter the list of servers by image79 params = {'image': self.image_ref}80 body = self.client.list_servers(**params)81 servers = body['servers']82 self.assertIn(self.s1['id'], map(lambda x: x['id'], servers))83 self.assertNotIn(self.s2['id'], map(lambda x: x['id'], servers))84 self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))85 @test.idempotent_id('573637f5-7325-47bb-9144-3476d0416908')86 def test_list_servers_filter_by_flavor(self):87 # Filter the list of servers by flavor88 params = {'flavor': self.flavor_ref_alt}89 body = self.client.list_servers(**params)90 servers = body['servers']91 self.assertNotIn(self.s1['id'], map(lambda x: x['id'], servers))92 self.assertNotIn(self.s2['id'], map(lambda x: x['id'], servers))93 self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))94 @test.idempotent_id('9b067a7b-7fee-4f6a-b29c-be43fe18fc5a')95 def test_list_servers_filter_by_server_name(self):96 # Filter the list of servers by server name97 params = {'name': self.s1_name}98 body = self.client.list_servers(**params)99 servers = body['servers']100 self.assertIn(self.s1_name, map(lambda x: x['name'], servers))101 self.assertNotIn(self.s2_name, map(lambda x: x['name'], servers))102 self.assertNotIn(self.s3_name, map(lambda x: x['name'], servers))103 @test.idempotent_id('ca78e20e-fddb-4ce6-b7f7-bcbf8605e66e')104 def test_list_servers_filter_by_server_status(self):105 # Filter the list of servers by server status106 params = {'status': 'active'}107 body = self.client.list_servers(**params)108 servers = body['servers']109 self.assertIn(self.s1['id'], map(lambda x: x['id'], servers))110 self.assertIn(self.s2['id'], map(lambda x: x['id'], servers))111 self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))112 @test.idempotent_id('451dbbb2-f330-4a9f-b0e1-5f5d2cb0f34c')113 def test_list_servers_filter_by_shutoff_status(self):114 # Filter the list of servers by server shutoff status115 params = {'status': 'shutoff'}116 self.client.stop_server(self.s1['id'])117 waiters.wait_for_server_status(self.client, self.s1['id'],118 'SHUTOFF')119 body = self.client.list_servers(**params)120 self.client.start_server(self.s1['id'])121 waiters.wait_for_server_status(self.client, self.s1['id'],122 'ACTIVE')123 servers = body['servers']124 self.assertIn(self.s1['id'], map(lambda x: x['id'], servers))125 self.assertNotIn(self.s2['id'], map(lambda x: x['id'], servers))126 self.assertNotIn(self.s3['id'], map(lambda x: x['id'], servers))127 @test.idempotent_id('614cdfc1-d557-4bac-915b-3e67b48eee76')128 def test_list_servers_filter_by_limit(self):129 # Verify only the expected number of servers are returned130 params = {'limit': 1}131 servers = self.client.list_servers(**params)132 self.assertEqual(1, len([x for x in servers['servers'] if 'id' in x]))133 @test.idempotent_id('b1495414-2d93-414c-8019-849afe8d319e')134 def test_list_servers_filter_by_zero_limit(self):135 # Verify only the expected number of servers are returned136 params = {'limit': 0}137 servers = self.client.list_servers(**params)138 self.assertEqual(0, len(servers['servers']))139 @test.idempotent_id('37791bbd-90c0-4de0-831e-5f38cba9c6b3')140 def test_list_servers_filter_by_exceed_limit(self):141 # Verify only the expected number of servers are returned142 params = {'limit': 100000}143 servers = self.client.list_servers(**params)144 all_servers = self.client.list_servers()145 self.assertEqual(len([x for x in all_servers['servers'] if 'id' in x]),146 len([x for x in servers['servers'] if 'id' in x]))147 @test.idempotent_id('b3304c3b-97df-46d2-8cd3-e2b6659724e7')148 @utils.skip_unless_attr('multiple_images', 'Only one image found')149 def test_list_servers_detailed_filter_by_image(self):150 # Filter the detailed list of servers by image151 params = {'image': self.image_ref}152 body = self.client.list_servers(detail=True, **params)153 servers = body['servers']154 self.assertIn(self.s1['id'], map(lambda x: x['id'], servers))155 self.assertNotIn(self.s2['id'], map(lambda x: x['id'], servers))156 self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))157 @test.idempotent_id('80c574cc-0925-44ba-8602-299028357dd9')158 def test_list_servers_detailed_filter_by_flavor(self):159 # Filter the detailed list of servers by flavor160 params = {'flavor': self.flavor_ref_alt}161 body = self.client.list_servers(detail=True, **params)162 servers = body['servers']163 self.assertNotIn(self.s1['id'], map(lambda x: x['id'], servers))164 self.assertNotIn(self.s2['id'], map(lambda x: x['id'], servers))165 self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))166 @test.idempotent_id('f9eb2b70-735f-416c-b260-9914ac6181e4')167 def test_list_servers_detailed_filter_by_server_name(self):168 # Filter the detailed list of servers by server name169 params = {'name': self.s1_name}170 body = self.client.list_servers(detail=True, **params)171 servers = body['servers']172 self.assertIn(self.s1_name, map(lambda x: x['name'], servers))173 self.assertNotIn(self.s2_name, map(lambda x: x['name'], servers))174 self.assertNotIn(self.s3_name, map(lambda x: x['name'], servers))175 @test.idempotent_id('de2612ab-b7dd-4044-b0b1-d2539601911f')176 def test_list_servers_detailed_filter_by_server_status(self):177 # Filter the detailed list of servers by server status178 params = {'status': 'active'}179 body = self.client.list_servers(detail=True, **params)180 servers = body['servers']181 test_ids = [s['id'] for s in (self.s1, self.s2, self.s3)]182 self.assertIn(self.s1['id'], map(lambda x: x['id'], servers))183 self.assertIn(self.s2['id'], map(lambda x: x['id'], servers))184 self.assertIn(self.s3['id'], map(lambda x: x['id'], servers))185 self.assertEqual(['ACTIVE'] * 3, [x['status'] for x in servers186 if x['id'] in test_ids])187 @test.idempotent_id('e9f624ee-92af-4562-8bec-437945a18dcb')188 def test_list_servers_filtered_by_name_wildcard(self):189 # List all servers that contains '-instance' in name190 params = {'name': '-instance'}191 body = self.client.list_servers(**params)192 servers = body['servers']193 self.assertIn(self.s1_name, map(lambda x: x['name'], servers))194 self.assertIn(self.s2_name, map(lambda x: x['name'], servers))195 self.assertIn(self.s3_name, map(lambda x: x['name'], servers))196 # Let's take random part of name and try to search it197 part_name = self.s1_name[6:-1]198 params = {'name': part_name}199 body = self.client.list_servers(**params)200 servers = body['servers']201 self.assertIn(self.s1_name, map(lambda x: x['name'], servers))202 self.assertNotIn(self.s2_name, map(lambda x: x['name'], servers))203 self.assertNotIn(self.s3_name, map(lambda x: x['name'], servers))204 @test.idempotent_id('24a89b0c-0d55-4a28-847f-45075f19b27b')205 def test_list_servers_filtered_by_name_regex(self):206 # list of regex that should match s1, s2 and s3207 regexes = ['^.*\-instance\-[0-9]+$', '^.*\-instance\-.*$']208 for regex in regexes:209 params = {'name': regex}210 body = self.client.list_servers(**params)211 servers = body['servers']212 self.assertIn(self.s1_name, map(lambda x: x['name'], servers))213 self.assertIn(self.s2_name, map(lambda x: x['name'], servers))214 self.assertIn(self.s3_name, map(lambda x: x['name'], servers))215 # Let's take random part of name and try to search it216 part_name = self.s1_name[-10:]217 params = {'name': part_name}218 body = self.client.list_servers(**params)219 servers = body['servers']220 self.assertIn(self.s1_name, map(lambda x: x['name'], servers))221 self.assertNotIn(self.s2_name, map(lambda x: x['name'], servers))222 self.assertNotIn(self.s3_name, map(lambda x: x['name'], servers))223 @test.idempotent_id('43a1242e-7b31-48d1-88f2-3f72aa9f2077')224 def test_list_servers_filtered_by_ip(self):225 # Filter servers by ip226 # Here should be listed 1 server227 if not self.fixed_network_name:228 msg = 'fixed_network_name needs to be configured to run this test'229 raise self.skipException(msg)230 self.s1 = self.client.show_server(self.s1['id'])['server']231 for addr_spec in self.s1['addresses'][self.fixed_network_name]:232 ip = addr_spec['addr']233 if addr_spec['version'] == 4:234 params = {'ip': ip}235 break236 else:237 msg = "Skipped until bug 1450859 is resolved"238 raise self.skipException(msg)239 body = self.client.list_servers(**params)240 servers = body['servers']241 self.assertIn(self.s1_name, map(lambda x: x['name'], servers))242 self.assertNotIn(self.s2_name, map(lambda x: x['name'], servers))243 self.assertNotIn(self.s3_name, map(lambda x: x['name'], servers))244 @test.idempotent_id('a905e287-c35e-42f2-b132-d02b09f3654a')245 def test_list_servers_filtered_by_ip_regex(self):246 # Filter servers by regex ip247 # List all servers filtered by part of ip address.248 # Here should be listed all servers249 if not self.fixed_network_name:250 msg = 'fixed_network_name needs to be configured to run this test'251 raise self.skipException(msg)252 self.s1 = self.client.show_server(self.s1['id'])['server']253 addr_spec = self.s1['addresses'][self.fixed_network_name][0]254 ip = addr_spec['addr'][0:-3]255 if addr_spec['version'] == 4:256 params = {'ip': ip}257 else:258 params = {'ip6': ip}259 # capture all servers in case something goes wrong260 all_servers = self.client.list_servers(detail=True)261 body = self.client.list_servers(**params)262 servers = body['servers']263 self.assertIn(self.s1_name, map(lambda x: x['name'], servers),264 "%s not found in %s, all servers %s" %265 (self.s1_name, servers, all_servers))266 self.assertIn(self.s2_name, map(lambda x: x['name'], servers),267 "%s not found in %s, all servers %s" %268 (self.s2_name, servers, all_servers))269 self.assertIn(self.s3_name, map(lambda x: x['name'], servers),270 "%s not found in %s, all servers %s" %271 (self.s3_name, servers, all_servers))272 @test.idempotent_id('67aec2d0-35fe-4503-9f92-f13272b867ed')273 def test_list_servers_detailed_limit_results(self):274 # Verify only the expected number of detailed results are returned275 params = {'limit': 1}276 servers = self.client.list_servers(detail=True, **params)...
test_list_servers_negative.py
Source:test_list_servers_negative.py
...47 @test.idempotent_id('24a26f1a-1ddc-4eea-b0d7-a90cc874ad8f')48 def test_list_servers_with_a_deleted_server(self):49 # Verify deleted servers do not show by default in list servers50 # List servers and verify server not returned51 body = self.client.list_servers()52 servers = body['servers']53 deleted_ids = [s['id'] for s in self.deleted_fixtures]54 actual = [srv for srv in servers55 if srv['id'] in deleted_ids]56 self.assertEqual([], actual)57 @test.attr(type=['negative'])58 @test.idempotent_id('ff01387d-c7ad-47b4-ae9e-64fa214638fe')59 def test_list_servers_by_non_existing_image(self):60 # Listing servers for a non existing image returns empty list61 non_existing_image = '1234abcd-zzz0-aaa9-ppp3-0987654abcde'62 body = self.client.list_servers(image=non_existing_image)63 servers = body['servers']64 self.assertEqual([], servers)65 @test.attr(type=['negative'])66 @test.idempotent_id('5913660b-223b-44d4-a651-a0fbfd44ca75')67 def test_list_servers_by_non_existing_flavor(self):68 # Listing servers by non existing flavor returns empty list69 non_existing_flavor = 123470 body = self.client.list_servers(flavor=non_existing_flavor)71 servers = body['servers']72 self.assertEqual([], servers)73 @test.attr(type=['negative'])74 @test.idempotent_id('e2c77c4a-000a-4af3-a0bd-629a328bde7c')75 def test_list_servers_by_non_existing_server_name(self):76 # Listing servers for a non existent server name returns empty list77 non_existing_name = 'junk_server_1234'78 body = self.client.list_servers(name=non_existing_name)79 servers = body['servers']80 self.assertEqual([], servers)81 @test.attr(type=['negative'])82 @test.idempotent_id('fcdf192d-0f74-4d89-911f-1ec002b822c4')83 def test_list_servers_status_non_existing(self):84 # Return an empty list when invalid status is specified85 non_existing_status = 'BALONEY'86 body = self.client.list_servers(status=non_existing_status)87 servers = body['servers']88 self.assertEqual([], servers)89 @test.idempotent_id('12c80a9f-2dec-480e-882b-98ba15757659')90 def test_list_servers_by_limits(self):91 # List servers by specifying limits92 body = self.client.list_servers(limit=1)93 self.assertEqual(1, len([x for x in body['servers'] if 'id' in x]))94 @test.attr(type=['negative'])95 @test.idempotent_id('d47c17fb-eebd-4287-8e95-f20a7e627b18')96 def test_list_servers_by_limits_greater_than_actual_count(self):97 # List servers by specifying a greater value for limit98 body = self.client.list_servers(limit=100)99 self.assertEqual(len(self.existing_fixtures), len(body['servers']))100 @test.attr(type=['negative'])101 @test.idempotent_id('679bc053-5e70-4514-9800-3dfab1a380a6')102 def test_list_servers_by_limits_pass_string(self):103 # Return an error if a string value is passed for limit104 self.assertRaises(lib_exc.BadRequest, self.client.list_servers,105 limit='testing')106 @test.attr(type=['negative'])107 @test.idempotent_id('62610dd9-4713-4ee0-8beb-fd2c1aa7f950')108 def test_list_servers_by_limits_pass_negative_value(self):109 # Return an error if a negative value for limit is passed110 self.assertRaises(lib_exc.BadRequest, self.client.list_servers,111 limit=-1)112 @test.attr(type=['negative'])113 @test.idempotent_id('87d12517-e20a-4c9c-97b6-dd1628d6d6c9')114 def test_list_servers_by_changes_since_invalid_date(self):115 # Return an error when invalid date format is passed116 params = {'changes-since': '2011/01/01'}117 self.assertRaises(lib_exc.BadRequest, self.client.list_servers,118 **params)119 @test.attr(type=['negative'])120 @test.idempotent_id('74745ad8-b346-45b5-b9b8-509d7447fc1f')121 def test_list_servers_by_changes_since_future_date(self):122 # Return an empty list when a date in the future is passed123 changes_since = {'changes-since': '2051-01-01T12:34:00Z'}124 body = self.client.list_servers(**changes_since)125 self.assertEqual(0, len(body['servers']))126 @test.attr(type=['negative'])127 @test.idempotent_id('93055106-2d34-46fe-af68-d9ddbf7ee570')128 def test_list_servers_detail_server_is_deleted(self):129 # Server details are not listed for a deleted server130 deleted_ids = [s['id'] for s in self.deleted_fixtures]131 body = self.client.list_servers(detail=True)132 servers = body['servers']133 actual = [srv for srv in servers134 if srv['id'] in deleted_ids]...
apps.py
Source:apps.py
1from django.apps import AppConfig2from django.db.models import Q3from django.http import Http4044from django.utils import timezone, dateformat5from django.utils.timezone import localdate6from monitoring import constance7from monitoring.constance import NUM_SERVERS_ON_PAGE8from monitoring.models import Server, Order, Place9class MainConfig(AppConfig):10 name = 'monitoring'11def get_premium_servers():12 server_null = Server()13 server_null.name = "---"14 server_null.ip = "-"15 server_null.port = "-"16 server_null.id = "0"17 server_null.map_name = '-'18 server_null.max_players = '-'19 server_null.player_count = '-'20 NUM_PREMIUM_PLACES = Place.objects.filter(status=1).count()21 premium_servers = [server_null] * NUM_PREMIUM_PLACES22 orders = Order.objects.filter(active=True, place__status=1, date_from__lte=localdate(), date_to__gte=localdate())23 for order in orders:24 order.server.update_info()25 if len(order.server.name) > 30:26 order.server.name = order.server.name[:25] + "..."27 if len(order.server.map_name) > 30:28 order.server.map_name = order.server.map_name[:25] + "..."29 order.server.save()30 premium_servers[order.place.place_number - 1] = order.server31 return premium_servers32def get_disabled_dates(id):33 orders = Order.objects.filter(place_id=id)34 disabled = []35 for order in orders:36 current = order.date_from37 while current <= order.date_to:38 formatted_date = dateformat.format(current, constance.DATE_FORMAT_PYTHON_TO)39 disabled.append(str(formatted_date))40 current = current + timezone.timedelta(days=1)41 return disabled42def get_servers_by_page(page):43 list_servers = Server.objects.all().filter().order_by('status')44 orders = Order.objects.all().order_by('place__place_number').filter(active=True, place__status=0,45 date_from__lte=timezone.localdate(),46 date_to__gte=timezone.localdate())47 pages = range(1, len(list_servers) // NUM_SERVERS_ON_PAGE + min(1, len(list_servers) % NUM_SERVERS_ON_PAGE) + 1)48# if len(list_servers) == 0 and page != 1:49# raise Http404()50# servers = []51# pos = max(0, NUM_SERVERS_ON_PAGE * (page - 1) - len(orders)) + 152# updated = 053# if page == 1:54# for order in orders:55# server = order.server56# server.position_in_table = pos57# server.update_info()58# servers.append(server)59# list_servers = list_servers.filter(~Q(ip=server.ip, port=server.port))60# pos += 161# list_servers = list_servers[62# max(0, NUM_SERVERS_ON_PAGE * (page - 1) - len(orders)):(NUM_SERVERS_ON_PAGE * page - len(orders))]63# for server in list_servers:64# server.position_in_table = pos65# if updated < 0 and server.update_info():66# updated += 167# if server.status == 'connected':68# servers.append(server)69# pos += 170 71# print('Total:', len(servers))72 73# return pages, servers74 for server in list_servers[:100]:75 server.update_info()76 return pages, list_servers[:100]77def clear_orders():78 Order.objects.filter(active=False, date_of_create__lt=timezone.localtime() - timezone.timedelta(minutes=3)).delete()79 Order.objects.filter(date_to__lt=timezone.localtime()).delete()80def add_places_():81 for i in range(1, 11):82 Place.objects.create(place_number=i, status=1, game='1.0.0.34', price=25, name='меÑÑо в Ñопе #%d' % i)83 for i in range(1, 101):...
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!!