Best Python code snippet using autotest_python
AwsResourceStartStopTest.py
Source:AwsResourceStartStopTest.py
...328 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã329 aws_resource_start_stop.start_stop_aws_resources(event, context, script_path=temp_file)330 # => EC2ã¤ã³ã¹ã¿ã³ã¹ãåæ¢ãã¦ãããã¨331 status = ec2_operator.get_status(ec2_instance_01)332 self._baseAssertEqual(status, 'stopped')333 # => RDS DB Clusterãåæ¢ãã¦ãããã¨ã334 status = dbc_operator.get_status(db_cluster_id)335 self._baseAssertEqual(status, 'stopped')336 337 # EC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBCã¯ã©ã¹ã¿ã¼ãåæ¢ä¸ã®ç¶æ
ã§ãã¤ãã³ãã«test01_okã®338 # éå§ãæå®ãã¦ãstart_stop_aws_resourcesé¢æ°ãå®è¡ããã339 event = {"action": "start", "configKey": configkey}340 context = MyContext()341 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã342 aws_resource_start_stop.start_stop_aws_resources(event, context, script_path=temp_file)343 # => EC2ã¤ã³ã¹ã¿ã³ã¹ãéå§ãã¦ãããã¨ã344 status = ec2_operator.get_status(ec2_instance_01)345 self._baseAssertEqual(status, 'running')346 # => RDS DB Clusterãéå§ãã¦ãããã¨ã347 status = dbc_operator.get_status(db_cluster_id)348 self._baseAssertEqual(status, 'available')349 350 logging.info('<<<<< test01_ok_event_start_stop_aws_resources end')351 352 @mock_ec2353 @mock_rds354 def test01_ng_event_start_stop_aws_resources(self):355 '''configKeyã«test01_ngãè¨å®ãã¦ãstart_stop_aws_resourcesé¢æ°ã®ãã¹ããå®è¡ãã¾ãã356 357 ãã¹ãã®å
容ã¯ã以ä¸ã®ã¨ããã§ãã358 359 * è¨å®ãã¡ã¤ã«ã«ãEC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBã¯ã©ã¹ã¿ã¼ãå®è¡ä¸ã®ã¤ã³ã¹ã¿ã³ã¹360 IDã¨åå¨ããªãã¤ã³ã¹ã¿ã³ã¹IDãè¨å®ããã¦ããç¶æ
ã§ãã¤ãã³ãã«361 test01_ngã®åæ¢ãæå®ãã¦ãstart_stop_aws_resourcesé¢æ°ãå®è¡ããã362 * => ä¾å¤ãçºçãããã¨ã363 * => EC2ã¤ã³ã¹ã¿ã³ã¹ãåæ¢ãã¦ãããã¨ã364 * => RDS DB Clusterãåæ¢ãã¦ãããã¨ã365 * è¨å®ãã¡ã¤ã«ã«ãEC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBã¯ã©ã¹ã¿ã¼ãåæ¢ä¸ã®ã¤ã³ã¹ã¿ã³ã¹366 IDã¨åå¨ããªãã¤ã³ã¹ã¿ã³ã¹IDãè¨å®ããã¦ããç¶æ
ã§ãã¤ãã³ãã«367 test01_ngã®éå§ãæå®ãã¦ãstart_stop_aws_resourcesé¢æ°ãå®è¡ããã368 * => ä¾å¤ãçºçãããã¨ã369 * => EC2ã¤ã³ã¹ã¿ã³ã¹ãéå§ãã¦ãããã¨ã370 * => RDS DB Clusterãéå§ãã¦ãããã¨ã371 372 '''373 logging.info('>>>>> test01_ng_event_start_stop_aws_resources start')374 375 ##### ãã¹ãã®æºå #####376 configkey = 'test01_ng'377 # ãã¹ãç¨ã®è¨å®ãã¡ã¤ã«ãèªã¿è¾¼ã378 config_root = aws_test_utils.load_yaml(__file__)379 region_name = config_root['region_name']380 access_key = config_root['access_key_id']381 secret_key = config_root['secret_access_key']382 config = config_root[configkey]383 384 # ã¤ã³ã¹ã¿ã³ã¹ã®ã¹ãã¼ã¿ã¹ç¢ºèªç¨ã«AWSResourceOperatorã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ããã385 ec2_operator = AwsResourceOperatorFactory.create('ec2.instance', region_name)386 dbc_operator = AwsResourceOperatorFactory.create('rds.db_cluster', region_name)387 dbi_operator = AwsResourceOperatorFactory.create('rds.db_instance', region_name)388 389 # ec2ã¤ã³ã¹ã¿ã³ã¹ãï¼ã¤éå§ããã390 ami_id = 'ami-test01ok'391 ec2_instances = aws_test_utils.run_instances(ami_id, region_name, 1)392 ec2_instance_01 = ec2_instances[0]['InstanceId']393 logging.info(f'EC2: {ec2_instance_01}, status: {ec2_operator.get_status(ec2_instance_01)}')394 395 # RDS DBã¯ã©ã¹ã¿ãï¼ã¤éå§ããã396 engine_name = 'postgres'397 username = 'postgres'398 password = 'password@123'399 db_cluster_id = config[1]['ids'][1]400 aws_test_utils.create_db_cluster(db_cluster_id, engine_name, username,401 password, region_name)402 logging.info(f'DB Cluster: {db_cluster_id}, status: {dbc_operator.get_status(db_cluster_id)}')403 404 # ã¤ã³ã¹ã¿ã³ã¹IDãç½®æãã¦ä½æ¥ãã£ã¬ã¯ããªã«ä¿åããã405 mapping_info = dict()406 mapping_info['ec2.instance:i-xxxxxxxxxxxxxxxxx'] = ec2_instance_01407 temp_file = os.path.join(os.path.dirname(__file__), 'work',408 os.path.basename(__file__))409 self._dump_config_workfile(config_root, {configkey: mapping_info}, temp_file)410 411 ##### ãã¹ãéå§ #####412 # è¨å®ãã¡ã¤ã«ã«ãEC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBã¯ã©ã¹ã¿ã¼ãå®è¡ä¸ã®ã¤ã³ã¹ã¿ã³ã¹413 # IDã¨åå¨ããªãã¤ã³ã¹ã¿ã³ã¹IDãè¨å®ããã¦ããç¶æ
ã§ãã¤ãã³ãã«414 # test01_ngã®åæ¢ãæå®ãã¦ãstart_stop_aws_resourcesé¢æ°ãå®è¡ããã415 event = {"action": "stop", "configKey": configkey}416 context = MyContext()417 with self.assertRaises(Exception) as cm:418 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã419 aws_resource_start_stop.start_stop_aws_resources(event, context,420 script_path=temp_file)421 # =>ä¾å¤ãçºçãããã¨422 logging.info(f'Exception: {str(cm.exception)}')423 # => EC2ã¤ã³ã¹ã¿ã³ã¹ãåæ¢ãã¦ãããã¨424 status = ec2_operator.get_status(ec2_instance_01)425 self._baseAssertEqual(status, 'stopped')426 # => RDS DB Clusterãåæ¢ãã¦ãããã¨ã427 status = dbc_operator.get_status(db_cluster_id)428 self._baseAssertEqual(status, 'stopped')429 430 # è¨å®ãã¡ã¤ã«ã«ãEC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBã¯ã©ã¹ã¿ã¼ãåæ¢ä¸ã®ã¤ã³ã¹ã¿ã³ã¹431 # IDã¨åå¨ããªãã¤ã³ã¹ã¿ã³ã¹IDãè¨å®ããã¦ããç¶æ
ã§ãã¤ãã³ãã«432 # test01_ngã®éå§ãæå®ãã¦ãstart_stop_aws_resourcesé¢æ°ãå®è¡ããã433 event = {"action": "start", "configKey": configkey}434 context = MyContext()435 with self.assertRaises(Exception) as e:436 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã437 aws_resource_start_stop.start_stop_aws_resources(event, context,438 script_path=temp_file)439 # => ä¾å¤ãçºçãããã¨440 logging.info(f'Exception: {str(cm.exception)}')441 # => EC2ã¤ã³ã¹ã¿ã³ã¹ãéå§ãã¦ãããã¨ã442 status = ec2_operator.get_status(ec2_instance_01)443 self._baseAssertEqual(status, 'running')444 # => RDS DB Clusterãéå§ãã¦ãããã¨ã445 status = dbc_operator.get_status(db_cluster_id)446 self._baseAssertEqual(status, 'available')447 448 logging.info('<<<<< test01_ng_event_start_stop_aws_resources end')449 450 @mock_ec2451 @mock_rds452 def test02_ok_event_start_stop_aws_resources(self):453 '''configKeyã«test02_okãè¨å®ãã¦ãstart_stop_aws_resourcesé¢æ°ã®ãã¹ããå®è¡ãã¾ãã454 455 ãã¹ãã®å
容ã¯ã以ä¸ã®ã¨ããã§ãã456 457 * EC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBCã¤ã³ã¹ã¿ã³ã¹ãå®è¡ä¸ã®ç¶æ
ã§ãã³ã³ããã¹ãã®458 Lambdaé¢æ°åã«ãec2_stop_test02_okããæå®ãã¦ã459 start_stop_aws_resourcesé¢æ°ãå®è¡ããã460 * => EC2ã¤ã³ã¹ã¿ã³ã¹ãåæ¢ãã¦ãããã¨ã461 * => RDS DB Clusterãåæ¢ãã¦ãããã¨ã462 * EC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBCã¤ã³ã¹ã¿ã³ã¹ãåæ¢ä¸ã®ç¶æ
ã§ãã³ã³ããã¹ãã®463 Lambdaé¢æ°åã«ãec2_stop_test02_okããæå®ãã¦ã464 start_stop_aws_resourcesé¢æ°ãå®è¡ããã465 * => EC2ã¤ã³ã¹ã¿ã³ã¹ãéå§ãã¦ãããã¨ã466 * => RDS DB Clusterãéå§ãã¦ãããã¨ã467 468 '''469 logging.info('>>>>> test02_ok_event_start_stop_aws_resources start')470 471 ##### ãã¹ãã®æºå #####472 configkey = 'test02_ok'473 # ãã¹ãç¨ã®è¨å®ãã¡ã¤ã«ãèªã¿è¾¼ã474 config_root = aws_test_utils.load_yaml(__file__)475 region_name = config_root['region_name']476 access_key = config_root['access_key_id']477 secret_key = config_root['secret_access_key']478 config = config_root[configkey]479 480 # ã¤ã³ã¹ã¿ã³ã¹ã®ã¹ãã¼ã¿ã¹ç¢ºèªç¨ã«AWSResourceOperatorã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ããã481 ec2_operator = AwsResourceOperatorFactory.create('ec2.instance', region_name)482 dbc_operator = AwsResourceOperatorFactory.create('rds.db_cluster', region_name)483 dbi_operator = AwsResourceOperatorFactory.create('rds.db_instance', region_name)484 485 # ec2ã¤ã³ã¹ã¿ã³ã¹ãï¼ã¤éå§ããã486 ami_id = 'ami-test01ok'487 ec2_instances = aws_test_utils.run_instances(ami_id, region_name, 2)488 ec2_instance_01 = ec2_instances[0]['InstanceId']489 logging.info(f'EC2: {ec2_instance_01}, status: {ec2_operator.get_status(ec2_instance_01)}')490 ec2_instance_02 = ec2_instances[1]['InstanceId']491 logging.info(f'EC2: {ec2_instance_02}, status: {ec2_operator.get_status(ec2_instance_02)}')492 493 # RDS DBã¤ã³ã¹ã¿ã³ã¹ãï¼ã¤éå§ããã494 resource_type = 'db.t2.micro'495 engine_name = 'postgres'496 username = 'postgres'497 password = 'password@123'498 db_instance_id = config[1]['ids'][0]499 aws_test_utils.create_db_instance(db_instance_id, resource_type, engine_name,500 username, password, region_name)501 logging.info(f'DB Instance: {db_instance_id}, status: {dbi_operator.get_status(db_instance_id)}')502 503 # ã¤ã³ã¹ã¿ã³ã¹IDãç½®æãã¦ä½æ¥ãã£ã¬ã¯ããªã«ä¿åããã504 mapping_info = dict()505 mapping_info['ec2.instance:i-xxxxxxxxxxxxxxxxx'] = ec2_instance_01506 mapping_info['ec2.instance:i-yyyyyyyyyyyyyyyyy'] = ec2_instance_02507 temp_file = os.path.join(os.path.dirname(__file__), 'work',508 os.path.basename(__file__))509 self._dump_config_workfile(config_root, {configkey: mapping_info}, temp_file)510 511 ##### ãã¹ãéå§ #####512 # EC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBCã¤ã³ã¹ã¿ã³ã¹ãå®è¡ä¸ã®ç¶æ
ã§ãã³ã³ããã¹ãã®513 # Lambdaé¢æ°åã«ãec2_stop_test02_okããæå®ãã¦ãstart_stop_aws_resources514 # é¢æ°ãå®è¡ããã515 event = dict()516 context = MyContext()517 context.function_name = f'ec2_stop_{configkey}'518 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã519 aws_resource_start_stop.start_stop_aws_resources(event, context,520 use_event=False, script_path=temp_file)521 # => EC2ã¤ã³ã¹ã¿ã³ã¹ãåæ¢ãã¦ãããã¨522 status = ec2_operator.get_status(ec2_instance_01)523 self._baseAssertEqual(status, 'stopped')524 status = ec2_operator.get_status(ec2_instance_02)525 self._baseAssertEqual(status, 'stopped')526 # => RDS DB Instanceãåæ¢ãã¦ãããã¨ã527 status = dbi_operator.get_status(db_instance_id)528 self._baseAssertEqual(status, 'stopped')529 530 # EC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBCã¤ã³ã¹ã¿ã³ã¹ãåæ¢ä¸ã®ç¶æ
ã§ãã³ã³ããã¹ãã®531 # Lambdaé¢æ°åã«ãec2_stop_test02_okããæå®ãã¦ã532 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã533 event = dict()534 context = MyContext()535 context.function_name = f'ec2_start_{configkey}'536 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã537 aws_resource_start_stop.start_stop_aws_resources(event, context,538 use_event=False, script_path=temp_file)539 # => EC2ã¤ã³ã¹ã¿ã³ã¹ãéå§ãã¦ãããã¨ã540 status = ec2_operator.get_status(ec2_instance_01)541 self._baseAssertEqual(status, 'running')542 status = ec2_operator.get_status(ec2_instance_02)543 self._baseAssertEqual(status, 'running')544 # => RDS DB Instanceãéå§ãã¦ãããã¨ã545 status = dbi_operator.get_status(db_instance_id)546 self._baseAssertEqual(status, 'available')547 548 logging.info('<<<<< test02_ok_event_start_stop_aws_resources end')549 550 @mock_ec2551 @mock_rds552 def test02_ng_event_start_stop_aws_resources(self):553 '''configKeyã«test02_okãè¨å®ãã¦ãstart_stop_aws_resourcesé¢æ°ã®ãã¹ããå®è¡ãã¾ãã554 555 ãã¹ãã®å
容ã¯ã以ä¸ã®ã¨ããã§ãã556 557 * è¨å®ãã¡ã¤ã«ã«ãEC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBã¤ã³ã¹ã¿ã³ã¹ãå®è¡ä¸ã®ã¤ã³ã¹ã¿ã³ã¹558 IDã¨åå¨ããªãã¤ã³ã¹ã¿ã³ã¹IDãè¨å®ããã¦ããç¶æ
ã§ãã³ã³ããã¹ãã®559 Lambdaé¢æ°åã«ãec2_stop_test02_okããæå®ãã¦ã560 start_stop_aws_resourcesé¢æ°ãå®è¡ããã561 * => ä¾å¤ãçºçãããã¨ã562 * => EC2ã¤ã³ã¹ã¿ã³ã¹ãåæ¢ãã¦ãããã¨ã563 * => RDS DB Clusterãåæ¢ãã¦ãããã¨ã564 * è¨å®ãã¡ã¤ã«ã«ãEC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBã¤ã³ã¹ã¿ã³ã¹ãåæ¢ä¸ã®ã¤ã³ã¹ã¿ã³ã¹565 IDã¨åå¨ããªãã¤ã³ã¹ã¿ã³ã¹IDãè¨å®ããã¦ããç¶æ
ã§ãã³ã³ããã¹ãã®566 Lambdaé¢æ°åã«ãec2_stop_test02_okããæå®ãã¦ã567 start_stop_aws_resourcesé¢æ°ãå®è¡ããã568 * => ä¾å¤ãçºçãããã¨ã569 * => EC2ã¤ã³ã¹ã¿ã³ã¹ãéå§ãã¦ãããã¨ã570 * => RDS DB Clusterãéå§ãã¦ãããã¨ã571 572 '''573 logging.info('>>>>> test02_ng_event_start_stop_aws_resources start')574 575 ##### ãã¹ãã®æºå #####576 configkey = 'test02_ng'577 # ãã¹ãç¨ã®è¨å®ãã¡ã¤ã«ãèªã¿è¾¼ã578 config_root = aws_test_utils.load_yaml(__file__)579 region_name = config_root['region_name']580 access_key = config_root['access_key_id']581 secret_key = config_root['secret_access_key']582 config = config_root[configkey]583 584 # ã¤ã³ã¹ã¿ã³ã¹ã®ã¹ãã¼ã¿ã¹ç¢ºèªç¨ã«AWSResourceOperatorã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ããã585 ec2_operator = AwsResourceOperatorFactory.create('ec2.instance', region_name)586 dbc_operator = AwsResourceOperatorFactory.create('rds.db_cluster', region_name)587 dbi_operator = AwsResourceOperatorFactory.create('rds.db_instance', region_name)588 589 # ec2ã¤ã³ã¹ã¿ã³ã¹ãï¼ã¤éå§ããã590 ami_id = 'ami-test01ok'591 ec2_instances = aws_test_utils.run_instances(ami_id, region_name, 2)592 ec2_instance_01 = ec2_instances[0]['InstanceId']593 logging.info(f'EC2: {ec2_instance_01}, status: {ec2_operator.get_status(ec2_instance_01)}')594 595 # RDS DBã¤ã³ã¹ã¿ã³ã¹ãï¼ã¤éå§ããã596 resource_type = 'db.t2.micro'597 engine_name = 'postgres'598 username = 'postgres'599 password = 'password@123'600 db_instance_id = config[2]['ids'][1]601 aws_test_utils.create_db_instance(db_instance_id, resource_type, engine_name,602 username, password, region_name)603 logging.info(f'DB Instance: {db_instance_id}, status: {dbi_operator.get_status(db_instance_id)}')604 605 # ã¤ã³ã¹ã¿ã³ã¹IDãç½®æãã¦ä½æ¥ãã£ã¬ã¯ããªã«ä¿åããã606 mapping_info = dict()607 mapping_info['ec2.instance:i-xxxxxxxxxxxxxxxxx'] = ec2_instance_01608 temp_file = os.path.join(os.path.dirname(__file__), 'work',609 os.path.basename(__file__))610 self._dump_config_workfile(config_root, {configkey: mapping_info}, temp_file)611 612 ##### ãã¹ãéå§ #####613 # è¨å®ãã¡ã¤ã«ã«ãEC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBã¤ã³ã¹ã¿ã³ã¹ãå®è¡ä¸ã®ã¤ã³ã¹ã¿ã³ã¹614 # IDã¨åå¨ããªãã¤ã³ã¹ã¿ã³ã¹IDãè¨å®ããã¦ããç¶æ
ã§ãã³ã³ããã¹ãã®615 # Lambdaé¢æ°åã«ãec2_stop_test02_ngããæå®ãã¦ã616 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã617 event = dict()618 context = MyContext()619 context.function_name = f'ec2_stop_{configkey}'620 with self.assertRaises(Exception) as cm:621 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã622 aws_resource_start_stop.start_stop_aws_resources(event, context,623 use_event=False, script_path=temp_file)624 # => ä¾å¤ãçºçãããã¨ã625 logging.info(f'Exception: {str(cm.exception)}')626 # => EC2ã¤ã³ã¹ã¿ã³ã¹ãåæ¢ãã¦ãããã¨627 status = ec2_operator.get_status(ec2_instance_01)628 self._baseAssertEqual(status, 'stopped')629 # => RDS DB Instanceãåæ¢ãã¦ãããã¨ã630 status = dbi_operator.get_status(db_instance_id)631 self._baseAssertEqual(status, 'stopped')632 633 # è¨å®ãã¡ã¤ã«ã«ãEC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBã¤ã³ã¹ã¿ã³ã¹ãåæ¢ä¸ã®ã¤ã³ã¹ã¿ã³ã¹634 # IDã¨åå¨ããªãã¤ã³ã¹ã¿ã³ã¹IDãè¨å®ããã¦ããç¶æ
ã§ãã³ã³ããã¹ãã®635 # Lambdaé¢æ°åã«ãec2_start_test02_ngããæå®ãã¦ã636 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã637 event = dict()638 context = MyContext()639 context.function_name = f'ec2_start_{configkey}'640 with self.assertRaises(Exception) as cm:641 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã642 aws_resource_start_stop.start_stop_aws_resources(event, context,643 use_event=False, script_path=temp_file)644 # => ä¾å¤ãçºçãããã¨ã645 logging.info(f'Exception: {str(cm.exception)}')646 # => EC2ã¤ã³ã¹ã¿ã³ã¹ãéå§ãã¦ãããã¨ã647 status = ec2_operator.get_status(ec2_instance_01)648 self._baseAssertEqual(status, 'running')649 # => RDS DB Instanceãéå§ãã¦ãããã¨ã650 status = dbi_operator.get_status(db_instance_id)651 self._baseAssertEqual(status, 'available')652 653 logging.info('<<<<< test02_ng_event_start_stop_aws_resources end')654 655 @mock_ec2656 @mock_rds657 def test_event_list_start_stop_aws_resources(self):658 '''start_stop_aws_resourcesé¢æ°ã«configKeyã®ãªã¹ãã渡ãã¦é¢æ°ãå®è¡ãã¾ãã659 660 start_stop_aws_resourcesé¢æ°ã以ä¸ã®ç¨ã«ãã¦å®è¡ããï¼ã¤ã®configKeyã«661 è¨å®ãããEC2ãDBã¤ã³ã¹ã¿ã³ã¹ãåæ¢ãéå§ã§ãããã¨ã確èªãã¾ãã662 663 * 以ä¸ã®æ§æã®è¨å®ãã¡ã¤ã«ã使ç¨ãã664 * test01_okã®EC2ãtest02_okã®DBã¤ã³ã¹ã¿ã³ã¹ãèµ·åç¶æ
ã«ãã665 * configKeyã®ãªã¹ã [test01_okãtest02_ok] ãstart_stop_aws_resources666 é¢æ°ã«æ¸¡ãã¦é¢æ°ãå®è¡ãã 667 668 test01_ok:669 - type: ec2.instance670 ids:671 - i-xxxxxxxxxxxxxxxxx672 - type: rds.db_cluster673 ids:674 - myrds_cluster01675 test02_ok:676 - type: ec2.instance677 ids:678 - i-xxxxxxxxxxxxxxxxx679 - i-yyyyyyyyyyyyyyyyy680 - type: rds.db_instance681 ids:682 - myrds_instance01683 * => DB Clusterã®åæ¢å¤±æã®ä¾å¤ãçºçãããã¨ã684 * => EC2ã¤ã³ã¹ã¿ã³ã¹ãï¼å°åæ¢ãã¦ãããã¨685 * => RDS DB Instanceãåæ¢ãã¦ãããã¨ã686 * EC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBã¤ã³ã¹ã¿ã³ã¹ãåæ¢ãã¦ããç¶æ
ã§ãconfigKeyã®ãªã¹ã687 [test01_okãtest02_ok] ãstart_stop_aws_resourcesé¢æ°ã«æ¸¡ãã¦688 ã¤ã³ã¹ã¿ã³ã¹ãéå§ããã689 * => EC2ã¤ã³ã¹ã¿ã³ã¹ã®éå§å¤±æã®ä¾å¤ãçºçãããã¨ã690 * => EC2ã¤ã³ã¹ã¿ã³ã¹ã®éå§å¤±æã®ä¾å¤ãçºçãããã¨ã691 * => RDS DB Instanceãéå§ãã¦ãããã¨ã692 693 '''694 logging.info('>>>>> test_event_list_start_stop_aws_resources start')695 696 ##### ãã¹ãã®æºå #####697 configkey01 = 'test01_ok'698 configkey02 = 'test02_ok'699 # ãã¹ãç¨ã®è¨å®ãã¡ã¤ã«ãèªã¿è¾¼ã700 config_root = aws_test_utils.load_yaml(__file__)701 region_name = config_root['region_name']702 access_key = config_root['access_key_id']703 secret_key = config_root['secret_access_key']704 705 # ã¤ã³ã¹ã¿ã³ã¹ã®ã¹ãã¼ã¿ã¹ç¢ºèªç¨ã«AWSResourceOperatorã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ããã706 ec2_operator = AwsResourceOperatorFactory.create('ec2.instance', region_name)707 dbc_operator = AwsResourceOperatorFactory.create('rds.db_cluster', region_name)708 dbi_operator = AwsResourceOperatorFactory.create('rds.db_instance', region_name)709 710 # ec2ã¤ã³ã¹ã¿ã³ã¹ãï¼ã¤éå§ããã711 ami_id = 'ami-test01ok'712 ec2_instances = aws_test_utils.run_instances(ami_id, region_name, 2)713 ec2_instance_01 = ec2_instances[0]['InstanceId']714 logging.info(f'EC2: {ec2_instance_01}, status: {ec2_operator.get_status(ec2_instance_01)}')715 716 # RDS DBã¤ã³ã¹ã¿ã³ã¹ãï¼ã¤éå§ããã717 resource_type = 'db.t2.micro'718 engine_name = 'postgres'719 username = 'postgres'720 password = 'password@123'721 db_instance_id = config_root[configkey02][1]['ids'][0]722 aws_test_utils.create_db_instance(db_instance_id, resource_type, engine_name,723 username, password, region_name)724 logging.info(f'DB Instance: {db_instance_id}, status: {dbi_operator.get_status(db_instance_id)}')725 726 # test01_okã®ã¤ã³ã¹ã¿ã³ã¹IDãç½®æãã¦ä½æ¥ãã£ã¬ã¯ããªã«ä¿åããã727 mapping_info01 = dict()728 mapping_info01['ec2.instance:i-xxxxxxxxxxxxxxxxx'] = ec2_instance_01729 temp_file = os.path.join(os.path.dirname(__file__), 'work',730 os.path.basename(__file__))731 self._dump_config_workfile(config_root, {configkey01: mapping_info01},732 temp_file)733 734 ##### ãã¹ãéå§ #####735 # EC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBã¤ã³ã¹ã¿ã³ã¹ãèµ·åãã¦ããç¶æ
ã§ãconfigKeyã®ãªã¹ã736 # [test01_okãtest02_ok] ãstart_stop_aws_resourcesé¢æ°ã«æ¸¡ãã¦737 # ã¤ã³ã¹ã¿ã³ã¹ãåæ¢ããã738 event = {'action': 'stop', 'configKey': [configkey01, configkey02]}739 context = MyContext()740 with self.assertRaises(Exception) as cm:741 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã742 aws_resource_start_stop.start_stop_aws_resources(event, context,743 script_path=temp_file)744 # => EC2ã®åæ¢å¤±æã®ä¾å¤ãçºçãããã¨ã745 logging.info(f'Exception: {str(cm.exception)}')746 # => EC2ã¤ã³ã¹ã¿ã³ã¹ãï¼å°åæ¢ãã¦ãããã¨747 status = ec2_operator.get_status(ec2_instance_01)748 self._baseAssertEqual(status, 'stopped')749 # => RDS DB Instanceãåæ¢ãã¦ãããã¨ã750 status = dbi_operator.get_status(db_instance_id)751 self._baseAssertEqual(status, 'stopped')752 753 # EC2ã¤ã³ã¹ã¿ã³ã¹ã¨DBã¤ã³ã¹ã¿ã³ã¹ãåæ¢ãã¦ããç¶æ
ã§ãconfigKeyã®ãªã¹ã754 # [test01_okãtest02_ok] ãstart_stop_aws_resourcesé¢æ°ã«æ¸¡ãã¦755 # ã¤ã³ã¹ã¿ã³ã¹ãéå§ããã756 event = {'action': 'start', 'configKey': [configkey01, configkey02]}757 context = MyContext()758 with self.assertRaises(Exception) as cm:759 # start_stop_aws_resourcesé¢æ°ãå®è¡ããã760 aws_resource_start_stop.start_stop_aws_resources(event, context,761 script_path=temp_file)762 # => EC2ã¤ã³ã¹ã¿ã³ã¹ã®éå§å¤±æã®ä¾å¤ãçºçãããã¨ã763 logging.info(f'Exception: {str(cm.exception)}')764 # => EC2ã¤ã³ã¹ã¿ã³ã¹ãï¼å°éå§ãã¦ãããã¨765 status = ec2_operator.get_status(ec2_instance_01)766 self._baseAssertEqual(status, 'running')767 # => RDS DB Instanceãéå§ãã¦ãããã¨ã768 status = dbi_operator.get_status(db_instance_id)769 self._baseAssertEqual(status, 'available')770 771 logging.info('<<<<< test_event_list_start_stop_aws_resources end')772if __name__ == '__main__':773 html_runner = HtmlTestRunner.HTMLTestRunner(774 output=os.path.dirname(__file__) + '/../target/site/test-report',775 add_timestamp=False)...
test_preprocessing.py
Source:test_preprocessing.py
...9 [4, 5, 6, 7]]10 expected1 = [(2, 0, 1, 3),11 (6, 4, 5, 7)]12 acutual1 = data_shuffle(seed1, *input1)13 self._baseAssertEqual(expected1, acutual1)14 acutual1_2 = data_shuffle(seed1, *np.array(input1))15 self._baseAssertEqual(expected1, acutual1_2)16 seed2 = 117 input2 = [[[0, 1], [1, 2, 3], [2, 3], [3, 4], [4, 5, 6]],18 [5, 6, 7, 8, 9],19 [10, 11, 12, 13, 14]]20 expected2 = [([2, 3], [3, 4], [4, 5, 6], [0, 1], [1, 2, 3]),21 (7, 8, 9, 5, 6),22 (12, 13, 14, 10, 11)]23 acutual2 = data_shuffle(seed2, *input2)24 self._baseAssertEqual(expected2, acutual2)25 def test_data_split(self):26 ratio = 0.827 x1 = [0, 1, 2, 3, 4]28 expected1 = "[[array([0, 1, 2, 3]), array([4])]]"29 actual1 = repr(data_split(ratio, x1))30 actual1_2 = repr(data_split(ratio, np.array(x1)))31 self.assertEqual(expected1, actual1)32 self.assertEqual(expected1, actual1_2)33 x2 = [[0, 1],34 [1, 2],35 [2, 3]]36 expected2 = "[[array([[0,1],\n[1,2]]),array([[2,3]])]]"37 actual2 = repr(data_split(ratio, x2)).replace(" ", "")38 actual2_2 = repr(data_split(ratio, np.array(x2))).replace(" ", "")...
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!!