How to use enable_image_deprecation method in localstack

Best Python code snippet using localstack_python

test_ec2deprecateimg.py

Source:test_ec2deprecateimg.py Github

copy

Full Screen

1#!/usr/bin/python32#3# Copyright (c) 2022 SUSE Linux GmbH. All rights reserved.4#5# This file is part of ec2utils6#7# ec2utils is free software: you can redistribute it8# and/or modify it under the terms of the GNU General Public License9# as published by the Free Software Foundation, either version 3 of10# the License, or (at your option) any later version.11#12# ec2utils is distributed in the hope that it will13# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty14# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU General Public License for more details.16#17# You should have received a copy of the GNU General Public License18# along with ec2utils. If not, see19# <http://www.gnu.org/licenses/>.20#21import logging22import pytest23import os24import argparse25from unittest.mock import patch, MagicMock26# Hack to get the script without the .py imported for testing27from importlib.machinery import SourceFileLoader28ec2deprecateimg = SourceFileLoader(29 'ec2deprecateimg',30 './ec2deprecateimg'31).load_module()32# Global variables33this_path = os.path.dirname(os.path.abspath(__file__))34data_path = this_path + os.sep + 'data'35logger = logging.getLogger('ec2imgutils')36logger.setLevel(logging.INFO)37# --------------------------------------------------------------------38# Tests for valid_YYYYMMDD_date function39test_depr_date_data = [40 ("asdf", True),41 ("20202001", True),42 ("20200230", True),43 ("20200130", False),44 ("20220202", False),45]46@pytest.mark.parametrize(47 "depr_date,expected_exc",48 test_depr_date_data49)50def test_valid_YYYYMMDD_date(depr_date, expected_exc):51 """Test valid_YYYYMMDD_date function"""52 if expected_exc:53 with pytest.raises(argparse.ArgumentTypeError) as depr_exc:54 ec2deprecateimg.valid_YYYYMMDD_date(depr_date)55 assert depr_date in str(depr_exc.value)56 assert 'not a valid date' in str(depr_exc.value)57 else:58 returned_depr_date = ec2deprecateimg.valid_YYYYMMDD_date(depr_date)59 assert depr_date == returned_depr_date60# --------------------------------------------------------------------61# Tests for valid parameters in parsing function62test_cli_args_data = [63 (["--account",64 "testAccName",65 "--access-id",66 "testAccId",67 "--deprecation-date",68 "20220101",69 "--deprecation-period",70 "6",71 "--dry-run",72 "--file",73 "/path/to/configuration/file",74 "--force",75 "--image-name",76 "testImageName",77 "--image-virt-type",78 "hvm",79 "--public-only",80 "--replacement-name",81 "testReplacementName",82 "--regions",83 "region1,region2,region3",84 "--secret-key",85 "testSecretKey",86 "--verbose"]),87]88@pytest.mark.parametrize(89 "cli_args",90 test_cli_args_data91)92def test_args(cli_args):93 parsed_args = ec2deprecateimg.parse_args(cli_args)94 ec2deprecateimg.check_required_arguments(parsed_args, logger)95 assert parsed_args.accountName == "testAccName"96 assert parsed_args.accessKey == "testAccId"97 assert parsed_args.depDate == "20220101"98 assert parsed_args.depTime == 699 assert parsed_args.dryRun is True100 assert parsed_args.configFilePath == "/path/to/configuration/file"101 assert parsed_args.force is True102 assert parsed_args.depImgName == "testImageName"103 assert parsed_args.virtType == "hvm"104 assert parsed_args.publicOnly is True105 assert parsed_args.replImgName == "testReplacementName"106 assert parsed_args.regions == "region1,region2,region3"107 assert parsed_args.secretKey == "testSecretKey"108 assert parsed_args.verbose is True109# --------------------------------------------------------------------110# Tests for required parameters not present111test_cli_args_data = [112 (["--account",113 "testAccName",114 "--access-id",115 "testAccId",116 "-n",117 "--force"], True)118]119@pytest.mark.parametrize(120 "cli_args,expected_exit",121 test_cli_args_data122)123def test_required_image_args_not_present(cli_args, expected_exit):124 if expected_exit:125 with pytest.raises(SystemExit) as excinfo:126 parsed_args = ec2deprecateimg.parse_args(cli_args)127 ec2deprecateimg.check_required_arguments(parsed_args, logger)128 assert excinfo.value.code == 1129# --------------------------------------------------------------------130# Tests for arguments exclusive group131test_cli_args_data = [132 (["--account",133 "testAccName",134 "--access-id",135 "testAccId",136 "--dry-run",137 "--file",138 "/path/to/configuration/file",139 "--force",140 "--image-name",141 "testImageName",142 "--image-id",143 "testImageAMIid",144 "--replacement-name",145 "testReplacementName",146 "--secret-key",147 "testSecretKey",148 "--verbose"]),149]150@pytest.mark.parametrize(151 "cli_args",152 test_cli_args_data153)154def test_image_arguments_exclusive_group(cli_args):155 with pytest.raises(SystemExit) as excinfo:156 parsed_args = ec2deprecateimg.parse_args(cli_args)157 ec2deprecateimg.check_required_arguments(parsed_args, logger)158 assert excinfo.value.code == 2159# --------------------------------------------------------------------160# Tests for wrong format in deprecation-date161test_cli_args_data = [162 (["--account",163 "testAccName",164 "--access-id",165 "testAccId",166 "--deprecation-date",167 "2022/01/01",168 "--image-name",169 "testImageName",170 "--replacement-name",171 "testReplacementName",172 "--secret-key",173 "testSecretKey",174 "--verbose"]),175]176@pytest.mark.parametrize(177 "cli_args",178 test_cli_args_data179)180def test_wrong_format_deprecation_date_parameter(cli_args):181 with pytest.raises(SystemExit) as excinfo:182 ec2deprecateimg.parse_args(cli_args)183 assert excinfo.value.code == 2184# --------------------------------------------------------------------185# Tests for wrong format in deprecation-period186test_cli_args_data = [187 (["--account",188 "testAccName",189 "--access-id",190 "testAccId",191 "--deprecation-period",192 "six",193 "--image-name",194 "testImageName",195 "--replacement-name",196 "testReplacementName",197 "--secret-key",198 "testSecretKey",199 "--verbose"]),200]201@pytest.mark.parametrize(202 "cli_args",203 test_cli_args_data204)205def test_wrong_format_deprecation_period_parameter(cli_args):206 with pytest.raises(SystemExit) as excinfo:207 ec2deprecateimg.parse_args(cli_args)208 assert excinfo.value.code == 2209# --------------------------------------------------------------------210# Tests for wrong virtualization-type211test_cli_args_data = [212 (["--account",213 "testAccName",214 "--access-id",215 "testAccId",216 "--image-virt-type",217 "not_supported_value",218 "--image-name",219 "testImageName",220 "--replacement-name",221 "testReplacementName",222 "--secret-key",223 "testSecretKey",224 "--verbose"]),225]226@pytest.mark.parametrize(227 "cli_args",228 test_cli_args_data229)230def test_wrong_virtualization_type(cli_args):231 with pytest.raises(SystemExit) as excinfo:232 ec2deprecateimg.parse_args(cli_args)233 assert excinfo.value.code == 2234# --------------------------------------------------------------------235# Tests for config file management functions236test_cli_args_data = [237 (["--account",238 "testAccount",239 "--image-name",240 "testImageName",241 "--replacement-name",242 "testReplacementName",243 "-n",244 "-f",245 "/non/existing/file/path"], True),246]247@pytest.mark.parametrize(248 "cli_args,expected_exit",249 test_cli_args_data250)251def test_not_existing_config_file(cli_args, expected_exit):252 parsed_args = ec2deprecateimg.parse_args(cli_args)253 if expected_exit:254 with pytest.raises(SystemExit) as excinfo:255 ec2deprecateimg.get_config(parsed_args, logger)256 assert excinfo.value.code == 1257def test_get_invalid_config():258 class Args:259 configFilePath = ""260 myArgs = Args()261 myArgs.configFilePath = data_path + os.sep + 'invalid.cfg'262 with pytest.raises(SystemExit) as excinfo:263 ec2deprecateimg.get_config(myArgs, logger)264 assert excinfo.value.code == 1265def test_get_valid_config():266 class Args:267 configFilePath = ""268 myArgs = Args()269 myArgs.configFilePath = data_path + os.sep + 'complete.cfg'270 config = ec2deprecateimg.get_config(myArgs, logger)271 assert "AAAAAAAAAAAAAA" == str(272 config._sections["account-tester"]["access_key_id"]273 )274# --------------------------------------------------------------------275# Tests for get_access_key functions276def test_get_access_key():277 class Args:278 configFilePath = ''279 accessKey = ''280 accountName = ''281 myArgs = Args()282 myArgs.configFilePath = data_path + os.sep + 'complete.cfg'283 myArgs.accountName = 'tester'284 config = ec2deprecateimg.get_config(myArgs, logger)285 access_key = ec2deprecateimg.get_access_key(myArgs, config, logger)286 assert "AAAAAAAAAAAAAA" == access_key287def test_get_access_key_exception():288 class Args:289 configFilePath = ''290 accessKey = ''291 accountName = ''292 myArgs = Args()293 myArgs.configFilePath = data_path + os.sep + 'complete.cfg'294 config = ec2deprecateimg.get_config(myArgs, logger)295 with pytest.raises(SystemExit) as excinfo:296 ec2deprecateimg.get_access_key(myArgs, config, logger)297 assert excinfo.value.code == 1298# --------------------------------------------------------------------299# Tests for get_secret_key functions300def test_get_secret_key():301 class Args:302 configFilePath = ''303 secretKey = ''304 accountName = ''305 myArgs = Args()306 myArgs.configFilePath = data_path + os.sep + 'complete.cfg'307 myArgs.accountName = 'tester'308 config = ec2deprecateimg.get_config(myArgs, logger)309 secret_key = ec2deprecateimg.get_secret_key(myArgs, config, logger)310 assert "BBBBBBBBBBBBBBBBBBBBBBBB" == secret_key311def test_get_secret_key_exception():312 class Args:313 configFilePath = ''314 secretKey = ''315 accountName = ''316 myArgs = Args()317 myArgs.configFilePath = data_path + os.sep + 'complete.cfg'318 config = ec2deprecateimg.get_config(myArgs, logger)319 with pytest.raises(SystemExit) as excinfo:320 ec2deprecateimg.get_secret_key(myArgs, config, logger)321 assert excinfo.value.code == 1322# --------------------------------------------------------------------323# Tests for deprecator class initialization324test_cli_args_data = [325 (["--account",326 "testAccName",327 "--access-id",328 "testAccId",329 "--deprecation-date",330 "20220101",331 "--deprecation-period",332 "6",333 "--dry-run",334 "--file",335 "/path/to/configuration/file",336 "--force",337 "--image-name",338 "testImageName",339 "--image-virt-type",340 "hvm",341 "--public-only",342 "--replacement-name",343 "testReplacementName",344 "--regions",345 "region1,region2,region3",346 "--secret-key",347 "testSecretKey",348 "--verbose"]),349]350@pytest.mark.parametrize(351 "cli_args",352 test_cli_args_data353)354def test_deprecator_init(cli_args):355 parsed_args = ec2deprecateimg.parse_args(cli_args)356 deprecator = ec2deprecateimg.get_image_deprecator(357 parsed_args,358 parsed_args.accessKey,359 parsed_args.secretKey,360 logger361 )362 assert deprecator.access_key == "testAccId"363 assert deprecator.secret_key == "testSecretKey"364@pytest.mark.parametrize(365 "cli_args",366 test_cli_args_data367)368def test_deprecator_init_exception(cli_args):369 parsed_args = ec2deprecateimg.parse_args(cli_args)370 parsed_args.depDate = '123'371 with pytest.raises(SystemExit) as excinfo:372 ec2deprecateimg.get_image_deprecator(373 parsed_args,374 parsed_args.accessKey,375 parsed_args.secretKey,376 logger377 )378 assert excinfo.value.code == 1379# --------------------------------------------------------------------380# Tests for main function381@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')382@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')383def test_deprecate_images_filtering_by_name(384 ec2connect_mock,385 get_owned_imgs_mock,386 caplog387):388 def log_args(**kwargs):389 logger.info(str(kwargs))390 ec2 = MagicMock()391 ec2.create_tags.return_value = None392 ec2.create_tags.side_effect = log_args393 ec2.enable_image_deprecation.return_value = None394 ec2connect_mock.return_value = ec2395 get_owned_imgs_mock.return_value = mock_get_owned_images()396 cli_args = [397 "--account",398 "testAccName",399 "--access-id",400 "testAccId",401 "--deprecation-date",402 "20220101",403 "--deprecation-period",404 "7",405 "--file",406 data_path + os.sep + 'complete.cfg',407 "--image-name",408 "testImageName",409 "--image-virt-type",410 "hvm",411 "--regions",412 "region1",413 "--secret-key",414 "testSecretKey"415 ]416 ec2deprecateimg.main(cli_args)417 assert "20220101" in caplog.text418 assert "20220801" in caplog.text419 assert "ami-000cc31892067693a" in caplog.text420@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')421@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')422def test_deprecate_images_filtering_by_id(423 ec2connect_mock,424 get_owned_imgs_mock,425 caplog426):427 def log_args(**kwargs):428 logger.info(str(kwargs))429 ec2 = MagicMock()430 ec2.create_tags.return_value = None431 ec2.create_tags.side_effect = log_args432 ec2.enable_image_deprecation.return_value = None433 ec2connect_mock.return_value = ec2434 get_owned_imgs_mock.return_value = mock_get_owned_images()435 cli_args = [436 "--account",437 "testAccName",438 "--access-id",439 "testAccId",440 "--deprecation-date",441 "20220101",442 "--deprecation-period",443 "7",444 "--file",445 data_path + os.sep + 'complete.cfg',446 "--image-id",447 "ami-000cc31892067693a",448 "--image-virt-type",449 "hvm",450 "--regions",451 "region1",452 "--secret-key",453 "testSecretKey"454 ]455 ec2deprecateimg.main(cli_args)456 assert "20220101" in caplog.text457 assert "20220801" in caplog.text458 assert "ami-000cc31892067693a" in caplog.text459@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')460@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')461def test_deprecate_images_filtering_by_name_match(462 ec2connect_mock,463 get_owned_imgs_mock,464 caplog465):466 def log_args(**kwargs):467 logger.info(str(kwargs))468 ec2 = MagicMock()469 ec2.create_tags.return_value = None470 ec2.create_tags.side_effect = log_args471 ec2.enable_image_deprecation.return_value = None472 ec2connect_mock.return_value = ec2473 get_owned_imgs_mock.return_value = mock_get_owned_images()474 cli_args = [475 "--account",476 "testAccName",477 "--access-id",478 "testAccId",479 "--deprecation-date",480 "20220101",481 "--deprecation-period",482 "7",483 "--file",484 data_path + os.sep + 'complete.cfg',485 "--image-name-match",486 ".*Name$",487 "--image-virt-type",488 "hvm",489 "--regions",490 "region1",491 "--secret-key",492 "testSecretKey"493 ]494 ec2deprecateimg.main(cli_args)495 assert "20220101" in caplog.text496 assert "20220801" in caplog.text497 assert "ami-000cc31892067693a" in caplog.text498@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')499@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')500def test_deprecate_images_filtering_by_name_match_dry_run(501 ec2connect_mock,502 get_owned_imgs_mock,503 caplog504):505 def log_args(**kwargs):506 logger.info(str(kwargs))507 ec2 = MagicMock()508 ec2.create_tags.return_value = None509 ec2.create_tags.side_effect = log_args510 ec2.enable_image_deprecation.return_value = None511 ec2connect_mock.return_value = ec2512 get_owned_imgs_mock.return_value = mock_get_owned_images()513 cli_args = [514 "--account",515 "testAccName",516 "--access-id",517 "testAccId",518 "--deprecation-date",519 "20220101",520 "--deprecation-period",521 "7",522 "--file",523 data_path + os.sep + 'complete.cfg',524 "--image-name-match",525 ".*Name$",526 "--dry-run",527 "--image-virt-type",528 "hvm",529 "--regions",530 "region1",531 "--secret-key",532 "testSecretKey"533 ]534 ec2deprecateimg.main(cli_args)535 assert "20220101" in caplog.text536 assert "20220801" in caplog.text537 assert "ami-000cc31892067693a" in caplog.text538@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')539@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')540def test_deprecate_images_filtering_by_name_frag(541 ec2connect_mock,542 get_owned_imgs_mock,543 caplog544):545 def log_args(**kwargs):546 logger.info(str(kwargs))547 ec2 = MagicMock()548 ec2.create_tags.return_value = None549 ec2.create_tags.side_effect = log_args550 ec2.enable_image_deprecation.return_value = None551 ec2connect_mock.return_value = ec2552 get_owned_imgs_mock.return_value = mock_get_owned_images()553 cli_args = [554 "--account",555 "testAccName",556 "--access-id",557 "testAccId",558 "--deprecation-date",559 "20220101",560 "--deprecation-period",561 "7",562 "--file",563 data_path + os.sep + 'complete.cfg',564 "--image-name-frag",565 "est",566 "--image-virt-type",567 "hvm",568 "--regions",569 "region1",570 "--secret-key",571 "testSecretKey"572 ]573 ec2deprecateimg.main(cli_args)574 assert "20220101" in caplog.text575 assert "20220801" in caplog.text576 assert "ami-000cc31892067693a" in caplog.text577 assert "ami-000cc31892067693b" in caplog.text578@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')579@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')580def test_deprecate_images_filtering_by_name_frag_dry_run(581 ec2connect_mock,582 get_owned_imgs_mock,583 caplog584):585 def log_args(**kwargs):586 logger.info(str(kwargs))587 ec2 = MagicMock()588 ec2.create_tags.return_value = None589 ec2.create_tags.side_effect = log_args590 ec2.enable_image_deprecation.return_value = None591 ec2connect_mock.return_value = ec2592 get_owned_imgs_mock.return_value = mock_get_owned_images()593 cli_args = [594 "--account",595 "testAccName",596 "--access-id",597 "testAccId",598 "--deprecation-date",599 "20220101",600 "--deprecation-period",601 "7",602 "--file",603 data_path + os.sep + 'complete.cfg',604 "--image-name-frag",605 "est",606 "--image-virt-type",607 "hvm",608 "--dry-run",609 "--regions",610 "region1",611 "--secret-key",612 "testSecretKey"613 ]614 ec2deprecateimg.main(cli_args)615 assert "20220101" in caplog.text616 assert "20220801" in caplog.text617 assert "ami-000cc31892067693a" in caplog.text618 assert "ami-000cc31892067693b" in caplog.text619@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')620@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')621def test_deprecate_images_filtering_by_name_frag_replacement_matching_image(622 ec2connect_mock,623 get_owned_imgs_mock,624 caplog625):626 def log_args(**kwargs):627 logger.info(str(kwargs))628 ec2 = MagicMock()629 ec2.create_tags.return_value = None630 # ec2.create_tags.side_effect = log_args631 ec2.enable_image_deprecation.return_value = None632 ec2connect_mock.return_value = ec2633 get_owned_imgs_mock.return_value = mock_get_owned_images()634 cli_args = [635 "--account",636 "testAccName",637 "--access-id",638 "testAccId",639 "--deprecation-date",640 "20220101",641 "--deprecation-period",642 "7",643 "--file",644 data_path + os.sep + 'complete.cfg',645 "--image-name-frag",646 "est",647 "--image-virt-type",648 "hvm",649 "--replacement-id",650 "ami-000cc31892067693a",651 "--regions",652 "region1",653 "--verbose",654 "--secret-key",655 "testSecretKey"656 ]657 ec2deprecateimg.main(cli_args)658 assert "20220101" in caplog.text659 assert "20220801" in caplog.text660 assert "Replacement image ami-000cc31892067693a" in caplog.text661 assert "ami-000cc31892067693b" in caplog.text662 assert "Ignore replacement image as potential target" in caplog.text663@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')664@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')665def test_deprecate_images_filtering_by_name_frag_dry_run_not_match(666 ec2connect_mock,667 get_owned_imgs_mock,668 caplog669):670 def log_args(**kwargs):671 logger.info(str(kwargs))672 ec2 = MagicMock()673 ec2.create_tags.return_value = None674 ec2.create_tags.side_effect = log_args675 ec2.enable_image_deprecation.return_value = None676 ec2connect_mock.return_value = ec2677 get_owned_imgs_mock.return_value = mock_get_owned_images()678 cli_args = [679 "--account",680 "testAccName",681 "--access-id",682 "testAccId",683 "--deprecation-date",684 "20220101",685 "--deprecation-period",686 "7",687 "--file",688 data_path + os.sep + 'complete.cfg',689 "--image-name-frag",690 "NOTMATCHING",691 "--image-virt-type",692 "hvm",693 "--dry-run",694 "--regions",695 "region1",696 "--secret-key",697 "testSecretKey"698 ]699 ec2deprecateimg.main(cli_args)700 assert "No images to deprecate found" in caplog.text701 # assert "ami-000cc31892067693a" in caplog.text702 # assert "ami-000cc31892067693b" in caplog.text703@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')704@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')705def test_deprecate_images_filtering_by_name_frag_no_images(706 ec2connect_mock,707 get_owned_imgs_mock,708 caplog709):710 def log_args(**kwargs):711 logger.info(str(kwargs))712 ec2 = MagicMock()713 ec2.create_tags.return_value = None714 ec2.create_tags.side_effect = log_args715 ec2.enable_image_deprecation.return_value = None716 ec2connect_mock.return_value = ec2717 get_owned_imgs_mock.return_value = mock_get_owned_images_empty()718 cli_args = [719 "--account",720 "testAccName",721 "--access-id",722 "testAccId",723 "--deprecation-date",724 "20220101",725 "--deprecation-period",726 "7",727 "--file",728 data_path + os.sep + 'complete.cfg',729 "--image-name-frag",730 "NOTMATCHING",731 "--image-virt-type",732 "hvm",733 "--dry-run",734 "--regions",735 "region1",736 "--secret-key",737 "testSecretKey"738 ]739 ec2deprecateimg.main(cli_args)740 assert "No images to deprecate found" in caplog.text741 # assert "ami-000cc31892067693a" in caplog.text742 # assert "ami-000cc31892067693b" in caplog.text743@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')744@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')745def test_deprecate_images_filtering_by_id_replacement_by_id(746 ec2connect_mock,747 get_owned_imgs_mock,748 caplog749):750 def log_args(**kwargs):751 logger.info(str(kwargs))752 ec2 = MagicMock()753 ec2.create_tags.return_value = None754 ec2.create_tags.side_effect = log_args755 ec2.enable_image_deprecation.return_value = None756 ec2connect_mock.return_value = ec2757 get_owned_imgs_mock.return_value = mock_get_owned_images()758 cli_args = [759 "--account",760 "testAccName",761 "--access-id",762 "testAccId",763 "--deprecation-date",764 "20220101",765 "--deprecation-period",766 "7",767 "--file",768 data_path + os.sep + 'complete.cfg',769 "--image-id",770 "ami-000cc31892067693a",771 "--replacement-id",772 "ami-000cc31892067693b",773 "--image-virt-type",774 "hvm",775 "--regions",776 "region1",777 "--secret-key",778 "testSecretKey"779 ]780 ec2deprecateimg.main(cli_args)781 assert "20220101" in caplog.text782 assert "20220801" in caplog.text783 assert "ami-000cc31892067693a" in caplog.text784 assert "ami-000cc31892067693b" in caplog.text785@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')786@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')787def test_deprecate_images_filtering_by_id_replacement_by_name(788 ec2connect_mock,789 get_owned_imgs_mock,790 caplog791):792 def log_args(**kwargs):793 logger.info(str(kwargs))794 ec2 = MagicMock()795 ec2.create_tags.return_value = None796 # ec2.create_tags.side_effect = log_args797 ec2.enable_image_deprecation.return_value = None798 ec2connect_mock.return_value = ec2799 get_owned_imgs_mock.return_value = mock_get_owned_images()800 cli_args = [801 "--account",802 "testAccName",803 "--access-id",804 "testAccId",805 "--deprecation-date",806 "20220101",807 "--deprecation-period",808 "7",809 "--file",810 data_path + os.sep + 'complete.cfg',811 "--image-id",812 "ami-000cc31892067693a",813 "--replacement-name",814 "NotTestImage2",815 "--regions",816 "region1",817 "--secret-key",818 "testSecretKey",819 "--verbose"820 ]821 ec2deprecateimg.main(cli_args)822 assert "20220101" in caplog.text823 assert "20220801" in caplog.text824 assert "ami-000cc31892067693a" in caplog.text825 assert "ami-000cc31892067693c" in caplog.text826@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')827@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')828def test_deprecate_images_filtering_by_id_replacement_by_name_match(829 ec2connect_mock,830 get_owned_imgs_mock,831 caplog832):833 def log_args(**kwargs):834 logger.info(str(kwargs))835 ec2 = MagicMock()836 ec2.create_tags.return_value = None837 # ec2.create_tags.side_effect = log_args838 ec2.enable_image_deprecation.return_value = None839 ec2connect_mock.return_value = ec2840 get_owned_imgs_mock.return_value = mock_get_owned_images()841 cli_args = [842 "--account",843 "testAccName",844 "--access-id",845 "testAccId",846 "--deprecation-date",847 "20220101",848 "--deprecation-period",849 "7",850 "--file",851 data_path + os.sep + 'complete.cfg',852 "--image-id",853 "ami-000cc31892067693a",854 "--replacement-name-match",855 "^NotTestImage2$",856 "--regions",857 "region1",858 "--secret-key",859 "testSecretKey",860 "--verbose"861 ]862 ec2deprecateimg.main(cli_args)863 assert "20220101" in caplog.text864 assert "20220801" in caplog.text865 assert "ami-000cc31892067693a" in caplog.text866 assert "ami-000cc31892067693c" in caplog.text867@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')868@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')869def test_deprecate_images_filtering_by_id_replacement_by_name_frag(870 ec2connect_mock,871 get_owned_imgs_mock,872 caplog873):874 def log_args(**kwargs):875 logger.info(str(kwargs))876 ec2 = MagicMock()877 ec2.create_tags.return_value = None878 # ec2.create_tags.side_effect = log_args879 ec2.enable_image_deprecation.return_value = None880 ec2connect_mock.return_value = ec2881 get_owned_imgs_mock.return_value = mock_get_owned_images()882 cli_args = [883 "--account",884 "testAccName",885 "--access-id",886 "testAccId",887 "--deprecation-date",888 "20220101",889 "--deprecation-period",890 "7",891 "--file",892 data_path + os.sep + 'complete.cfg',893 "--image-id",894 "ami-000cc31892067693a",895 "--replacement-name-frag",896 "tTestImage2",897 "--regions",898 "region1",899 "--secret-key",900 "testSecretKey",901 "--verbose"902 ]903 ec2deprecateimg.main(cli_args)904 assert "20220101" in caplog.text905 assert "20220801" in caplog.text906 assert "ami-000cc31892067693a" in caplog.text907 assert "ami-000cc31892067693c" in caplog.text908@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')909@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')910def test_deprecate_images_filtering_by_id_replacement_by_name_frag_ambiguity(911 ec2connect_mock,912 get_owned_imgs_mock,913 caplog914):915 def log_args(**kwargs):916 logger.info(str(kwargs))917 ec2 = MagicMock()918 ec2.create_tags.return_value = None919 # ec2.create_tags.side_effect = log_args920 ec2.enable_image_deprecation.return_value = None921 ec2connect_mock.return_value = ec2922 get_owned_imgs_mock.return_value = mock_get_owned_images()923 cli_args = [924 "--account",925 "testAccName",926 "--access-id",927 "testAccId",928 "--deprecation-date",929 "20220101",930 "--deprecation-period",931 "7",932 "--file",933 data_path + os.sep + 'complete.cfg',934 "--image-id",935 "ami-000cc31892067693a",936 "--replacement-name-frag",937 "tTest",938 "--regions",939 "region1",940 "--secret-key",941 "testSecretKey",942 "--verbose"943 ]944 with pytest.raises(SystemExit) as excinfo:945 ec2deprecateimg.main(cli_args)946 assert "Replacement image ambiguity" in str(caplog.text)947 assert excinfo.value.code == 1948@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')949@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')950def test_deprecate_images_filtering_by_id_replacement_by_name_frag_no_match(951 ec2connect_mock,952 get_owned_imgs_mock,953 caplog954):955 def log_args(**kwargs):956 logger.info(str(kwargs))957 ec2 = MagicMock()958 ec2.create_tags.return_value = None959 # ec2.create_tags.side_effect = log_args960 ec2.enable_image_deprecation.return_value = None961 ec2connect_mock.return_value = ec2962 get_owned_imgs_mock.return_value = mock_get_owned_images()963 cli_args = [964 "--account",965 "testAccName",966 "--access-id",967 "testAccId",968 "--deprecation-date",969 "20220101",970 "--deprecation-period",971 "7",972 "--file",973 data_path + os.sep + 'complete.cfg',974 "--image-id",975 "ami-000cc31892067693a",976 "--replacement-name-frag",977 "NOMATCH",978 "--regions",979 "region1",980 "--secret-key",981 "testSecretKey",982 "--verbose"983 ]984 with pytest.raises(SystemExit) as excinfo:985 ec2deprecateimg.main(cli_args)986 assert "Replacement image not found" in str(caplog.text)987 assert excinfo.value.code == 1988@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')989@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')990def test_deprecate_images_filtering_by_id_tags_already_present(991 ec2connect_mock,992 get_owned_imgs_mock,993 caplog994):995 def log_args(**kwargs):996 logger.info(str(kwargs))997 ec2 = MagicMock()998 ec2.create_tags.return_value = None999 # ec2.create_tags.side_effect = log_args1000 ec2.enable_image_deprecation.return_value = None1001 ec2connect_mock.return_value = ec21002 get_owned_imgs_mock.return_value = mock_get_owned_images()1003 cli_args = [1004 "--account",1005 "testAccName",1006 "--access-id",1007 "testAccId",1008 "--deprecation-date",1009 "20220101",1010 "--deprecation-period",1011 "7",1012 "--file",1013 data_path + os.sep + 'complete.cfg',1014 "--image-id",1015 "ami-000cc31892067693c",1016 "--regions",1017 "region1",1018 "--secret-key",1019 "testSecretKey",1020 "--verbose"1021 ]1022 ec2deprecateimg.main(cli_args)1023 assert "already tagged, skipping" in str(caplog.text)1024 assert "ami-000cc31892067693c" in str(caplog.text)1025@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._get_owned_images')1026@patch('ec2deprecateimg.ec2depimg.EC2DeprecateImg._connect')1027def test_deprecate_images_filtering_by_id_replacement_by_name_frag_dry_run(1028 ec2connect_mock,1029 get_owned_imgs_mock,1030 caplog1031):1032 def log_args(**kwargs):1033 logger.info(str(kwargs))1034 ec2 = MagicMock()1035 ec2.create_tags.return_value = None1036 # ec2.create_tags.side_effect = log_args1037 ec2.enable_image_deprecation.return_value = None1038 ec2connect_mock.return_value = ec21039 get_owned_imgs_mock.return_value = mock_get_owned_images()1040 cli_args = [1041 "--account",1042 "testAccName",1043 "--access-id",1044 "testAccId",1045 "--deprecation-date",1046 "20220101",1047 "--deprecation-period",1048 "7",1049 "--file",1050 data_path + os.sep + 'complete.cfg',1051 "--image-id",1052 "ami-000cc31892067693a",1053 "--replacement-name-frag",1054 "tTestImage2",1055 "--regions",1056 "region1",1057 "--secret-key",1058 "testSecretKey",1059 "--verbose",1060 "--dry-run"1061 ]1062 ec2deprecateimg.main(cli_args)1063 assert "20220101" in caplog.text1064 assert "20220801" in caplog.text1065 assert "ami-000cc31892067693a" in caplog.text1066 assert "Replacement image ami-000cc31892067693c" in caplog.text1067# --------------------------------------------------------------------1068# Aux functions1069def mock_get_owned_images():1070 myImage1 = {}1071 myImage1["Architecture"] = "x86_64"1072 myImage1["CreationDate"] = "2022-04-11T14:01:58.000Z"1073 myImage1["ImageId"] = "ami-000cc31892067693a"1074 myImage1["Name"] = "testImageName"1075 myI1Ebs = {}1076 myI1Ebs["DeleteOnTermination"] = True1077 myI1Ebs["SnapshotId"] = "snap-000f48a8fa4545e1a"1078 myI1Ebs["VolumeSize"] = 101079 myI1Ebs["VolumeType"] = "gp3"1080 myI1Ebs["Encrypted"] = False1081 bdmI1 = {}1082 bdmI1["DeviceName"] = "/dev/sda1"1083 bdmI1["Ebs"] = myI1Ebs1084 myImage1["BlockDeviceMappings"] = []1085 myImage1["BlockDeviceMappings"].append(bdmI1)1086 myImage1["VirtualizationType"] = "hvm"1087 myImage2 = {}1088 myImage2["Architecture"] = "x86_64"1089 myImage2["CreationDate"] = "2022-04-11T14:01:58.000Z"1090 myImage2["ImageId"] = "ami-000cc31892067693b"1091 myImage2["Name"] = "NotTestImage"1092 myI2Ebs = {}1093 myI2Ebs["DeleteOnTermination"] = True1094 myI2Ebs["SnapshotId"] = "snap-000f48a8fa4545e1b"1095 myI2Ebs["VolumeSize"] = 101096 myI2Ebs["VolumeType"] = "gp3"1097 myI2Ebs["Encrypted"] = False1098 bdmI2 = {}1099 bdmI2["DeviceName"] = "/dev/sda1"1100 bdmI2["Ebs"] = myI2Ebs1101 myImage2["BlockDeviceMappings"] = []1102 myImage2["BlockDeviceMappings"].append(bdmI2)1103 myImage2["VirtualizationType"] = "hvm"1104 myImages = []1105 myImage3 = {}1106 myImage3["Architecture"] = "x86_64"1107 myImage3["CreationDate"] = "2022-04-11T14:01:58.000Z"1108 myImage3["ImageId"] = "ami-000cc31892067693c"1109 myImage3["Name"] = "NotTestImage2"1110 myI3Ebs = {}1111 myI3Ebs["DeleteOnTermination"] = True1112 myI3Ebs["SnapshotId"] = "snap-000f48a8fa4545e1c"1113 myI3Ebs["VolumeSize"] = 101114 myI3Ebs["VolumeType"] = "gp3"1115 myI3Ebs["Encrypted"] = False1116 bdmI3 = {}1117 bdmI3["DeviceName"] = "/dev/sda1"1118 bdmI3["Ebs"] = myI3Ebs1119 mytags = []1120 myTag1 = {}1121 myTag1["Key"] = "Deprecated on"1122 myTag1["Value"] = "20220101"1123 mytags.append(myTag1)1124 myImage3["Tags"] = mytags1125 myImage3["BlockDeviceMappings"] = []1126 myImage3["BlockDeviceMappings"].append(bdmI3)1127 myImage3["VirtualizationType"] = "NOThvm"1128 myImages.append(myImage1)1129 myImages.append(myImage2)1130 myImages.append(myImage3)1131 return myImages1132def mock_get_owned_images_empty():1133 myImages = []...

Full Screen

Full Screen

test_libec2deprecateimg.py

Source:test_libec2deprecateimg.py Github

copy

Full Screen

1#!/usr/bin/python32#3# Copyright (c) 2015 SUSE Linux GmbH. All rights reserved.4#5# This file is part of ec2utils6#7# ec2utils is free software: you can redistribute it8# and/or modify it under the terms of the GNU General Public License9# as published by the Free Software Foundation, either version 3 of10# the License, or (at your option) any later version.11#12# ec2utils is distributed in the hope that it will13# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty14# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15# GNU General Public License for more details.16#17# You should have received a copy of the GNU General Public License18# along with ec2utils. If not, see19# <http://www.gnu.org/licenses/>.20#21import logging22import pytest23import datetime24import dateutil.relativedelta25from unittest.mock import patch, MagicMock26import ec2imgutils.ec2deprecateimg as ec2depimg27from ec2imgutils.ec2imgutilsExceptions import (28 EC2DeprecateImgException29)30logger = logging.getLogger('ec2imgutils')31logger.setLevel(logging.INFO)32# --------------------------------------------------------------------33# Test data for deprecation date parameter34today = datetime.datetime.now()35today_tag = today.strftime('%Y%m%d')36one_month_delta = dateutil.relativedelta.relativedelta(months=+1)37six_month_delta = dateutil.relativedelta.relativedelta(months=+6)38test_depr_date_data = [39 ("", 1, False, today_tag, (today + one_month_delta).strftime('%Y%m%d')),40 ("", 6, False, today_tag, (today + six_month_delta).strftime('%Y%m%d')),41 ("20220707", 6, False, '20220707', '20230107'),42 ("20221231", 1, False, '20221231', '20230131'),43 ("20220331", 1, False, '20220331', '20220430'),44 ("asdf", 6, True, '', ''),45 ("20220230", 6, True, '', ''),46 ("2022", 6, True, '', ''),47]48@pytest.mark.parametrize(49 "depr_date,depr_period,expected_exc,expected_depr_date,expected_del_date",50 test_depr_date_data51)52def test_deprecation_date_parameter(53 depr_date,54 depr_period,55 expected_exc,56 expected_depr_date,57 expected_del_date58):59 """Test deprecation_date parameter"""60 if expected_exc:61 with pytest.raises(EC2DeprecateImgException) as depr_exc:62 deprecator = ec2depimg.EC2DeprecateImg(63 access_key='',64 deprecation_date=depr_date,65 deprecation_period=depr_period,66 deprecation_image_id='',67 deprecation_image_name='',68 deprecation_image_name_fragment='',69 deprecation_image_name_match='',70 force=False,71 image_virt_type='',72 public_only=False,73 replacement_image_id='',74 replacement_image_name='',75 replacement_image_name_fragment='',76 replacement_image_name_match='',77 secret_key='',78 log_callback=logger79 )80 assert "deprecation date" in str(depr_exc.value)81 assert depr_date in str(depr_exc.value)82 else:83 deprecator = ec2depimg.EC2DeprecateImg(84 access_key='',85 deprecation_date=depr_date,86 deprecation_period=depr_period,87 deprecation_image_id='',88 deprecation_image_name='',89 deprecation_image_name_fragment='',90 deprecation_image_name_match='',91 force=False,92 image_virt_type='',93 public_only=False,94 replacement_image_id='',95 replacement_image_name='',96 replacement_image_name_fragment='',97 replacement_image_name_match='',98 secret_key='',99 log_callback=logger100 )101 assert expected_depr_date == deprecator.deprecation_date102 assert expected_del_date == deprecator.deletion_date103# -----------------------------------------------------------------------------104@patch('ec2imgutils.ec2deprecateimg.EC2DeprecateImg._get_owned_images')105@patch('ec2imgutils.ec2deprecateimg.EC2DeprecateImg._connect')106def test_deprecate_class_filtering_by_name_match_no_virt_type_no_pub_img(107 ec2connect_mock,108 get_owned_imgs_mock,109 caplog110):111 def log_args(**kwargs):112 logger.info(str(kwargs))113 ec2 = MagicMock()114 ec2.create_tags.return_value = None115 ec2.create_tags.side_effect = log_args116 ec2.enable_image_deprecation.return_value = None117 ec2connect_mock.return_value = ec2118 get_owned_imgs_mock.return_value = mock_get_owned_images()119 deprecator = ec2depimg.EC2DeprecateImg(120 access_key='',121 deprecation_date='20220101',122 deprecation_period=6,123 deprecation_image_id='',124 deprecation_image_name='',125 deprecation_image_name_fragment='est',126 deprecation_image_name_match='',127 force=False,128 image_virt_type='',129 public_only=False,130 replacement_image_id='',131 replacement_image_name='',132 replacement_image_name_fragment='',133 replacement_image_name_match='',134 secret_key='',135 log_callback=logger136 )137 images = deprecator._get_images_to_deprecate()138 assert 3 == len(images)139@patch('ec2imgutils.ec2deprecateimg.EC2DeprecateImg._get_owned_images')140@patch('ec2imgutils.ec2deprecateimg.EC2DeprecateImg._connect')141def test_deprecate_class_filtering_by_name_match_virt_type_no_pub_img(142 ec2connect_mock,143 get_owned_imgs_mock,144 caplog145):146 def log_args(**kwargs):147 logger.info(str(kwargs))148 ec2 = MagicMock()149 ec2.create_tags.return_value = None150 ec2.create_tags.side_effect = log_args151 ec2.enable_image_deprecation.return_value = None152 ec2connect_mock.return_value = ec2153 get_owned_imgs_mock.return_value = mock_get_owned_images()154 deprecator = ec2depimg.EC2DeprecateImg(155 access_key='',156 deprecation_date='20220101',157 deprecation_period=6,158 deprecation_image_id='',159 deprecation_image_name='',160 deprecation_image_name_fragment='est',161 deprecation_image_name_match='',162 force=False,163 image_virt_type='hvm',164 public_only=False,165 replacement_image_id='',166 replacement_image_name='',167 replacement_image_name_fragment='',168 replacement_image_name_match='',169 secret_key='',170 log_callback=logger171 )172 images = deprecator._get_images_to_deprecate()173 assert 1 == len(images)174 assert "ami-000cc31892067693a" == images[0]['ImageId']175@patch('ec2imgutils.ec2deprecateimg.EC2DeprecateImg._get_owned_images')176@patch('ec2imgutils.ec2deprecateimg.EC2DeprecateImg._connect')177def test_deprecate_class_filtering_by_name_match_no_virt_type_pub_img(178 ec2connect_mock,179 get_owned_imgs_mock,180 caplog181):182 def log_args(**kwargs):183 logger.info(str(kwargs))184 la1 = {}185 launchAtt1 = MagicMock()186 launchAtt1.get.return_value = 'all'187 la1['LaunchPermissions'] = [launchAtt1]188 la2 = {}189 launchAtt2 = MagicMock()190 launchAtt2.get.return_value = 'all'191 la2['LaunchPermissions'] = [launchAtt1]192 ec2 = MagicMock()193 ec2.describe_image_attribute.side_effect = [la1, la2, la2]194 ec2connect_mock.return_value = ec2195 get_owned_imgs_mock.return_value = mock_get_owned_images()196 deprecator = ec2depimg.EC2DeprecateImg(197 access_key='',198 deprecation_date='20220101',199 deprecation_period=6,200 deprecation_image_id='',201 deprecation_image_name='',202 deprecation_image_name_fragment='est',203 deprecation_image_name_match='',204 force=False,205 image_virt_type='',206 public_only=True,207 replacement_image_id='',208 replacement_image_name='',209 replacement_image_name_fragment='',210 replacement_image_name_match='',211 secret_key='',212 log_callback=logger213 )214 images = deprecator._get_images_to_deprecate()215 assert 3 == len(images)216@patch('ec2imgutils.ec2deprecateimg.EC2DeprecateImg._get_owned_images')217@patch('ec2imgutils.ec2deprecateimg.EC2DeprecateImg._connect')218def test_deprecate_class_filtering_by_name_match_no_virt_type_pub_img2(219 ec2connect_mock,220 get_owned_imgs_mock,221 caplog222):223 def log_args(**kwargs):224 logger.info(str(kwargs))225 la1 = {}226 launchAtt1 = MagicMock()227 launchAtt1.get.return_value = 'all'228 la1['LaunchPermissions'] = [launchAtt1]229 la2 = {}230 launchAtt2 = MagicMock()231 launchAtt2.get.return_value = 'NOTall'232 la2['LaunchPermissions'] = [launchAtt2]233 ec2 = MagicMock()234 ec2.describe_image_attribute.side_effect = [la1, la2, la2]235 ec2connect_mock.return_value = ec2236 get_owned_imgs_mock.return_value = mock_get_owned_images()237 deprecator = ec2depimg.EC2DeprecateImg(238 access_key='',239 deprecation_date='20220101',240 deprecation_period=6,241 deprecation_image_id='',242 deprecation_image_name='',243 deprecation_image_name_fragment='est',244 deprecation_image_name_match='',245 force=False,246 image_virt_type='',247 public_only=True,248 replacement_image_id='',249 replacement_image_name='',250 replacement_image_name_fragment='',251 replacement_image_name_match='',252 secret_key='',253 log_callback=logger254 )255 images = deprecator._get_images_to_deprecate()256 assert 1 == len(images)257 assert "ami-000cc31892067693a" == images[0]['ImageId']258@patch('ec2imgutils.ec2deprecateimg.EC2DeprecateImg._get_owned_images')259@patch('ec2imgutils.ec2deprecateimg.EC2DeprecateImg._connect')260def test_deprecate_class_filtering_by_name_match_virt_type_pub_img(261 ec2connect_mock,262 get_owned_imgs_mock,263 caplog264):265 def log_args(**kwargs):266 logger.info(str(kwargs))267 la1 = {}268 launchAtt1 = MagicMock()269 launchAtt1.get.return_value = 'all'270 la1['LaunchPermissions'] = [launchAtt1]271 la2 = {}272 launchAtt2 = MagicMock()273 launchAtt2.get.return_value = 'NOTall'274 la2['LaunchPermissions'] = [launchAtt2]275 ec2 = MagicMock()276 ec2.describe_image_attribute.side_effect = [la2, la1]277 ec2connect_mock.return_value = ec2278 get_owned_imgs_mock.return_value = mock_get_owned_images()279 deprecator = ec2depimg.EC2DeprecateImg(280 access_key='',281 deprecation_date='20220101',282 deprecation_period=6,283 deprecation_image_id='',284 deprecation_image_name='',285 deprecation_image_name_fragment='est',286 deprecation_image_name_match='',287 force=False,288 image_virt_type='para',289 public_only=True,290 replacement_image_id='',291 replacement_image_name='',292 replacement_image_name_fragment='',293 replacement_image_name_match='',294 secret_key='',295 log_callback=logger296 )297 images = deprecator._get_images_to_deprecate()298 assert 1 == len(images)299 assert "ami-000cc31892067693c" == images[0]['ImageId']300# --------------------------------------------------------------------301# Aux functions302def mock_get_owned_images():303 myImage1 = {}304 myImage1["Architecture"] = "x86_64"305 myImage1["CreationDate"] = "2022-04-11T14:01:58.000Z"306 myImage1["ImageId"] = "ami-000cc31892067693a"307 myImage1["Name"] = "testImageName"308 myI1Ebs = {}309 myI1Ebs["DeleteOnTermination"] = True310 myI1Ebs["SnapshotId"] = "snap-000f48a8fa4545e1a"311 myI1Ebs["VolumeSize"] = 10312 myI1Ebs["VolumeType"] = "gp3"313 myI1Ebs["Encrypted"] = False314 bdmI1 = {}315 bdmI1["DeviceName"] = "/dev/sda1"316 bdmI1["Ebs"] = myI1Ebs317 myImage1["BlockDeviceMappings"] = []318 myImage1["BlockDeviceMappings"].append(bdmI1)319 myImage1["VirtualizationType"] = "hvm"320 myImage2 = {}321 myImage2["Architecture"] = "x86_64"322 myImage2["CreationDate"] = "2022-04-11T14:01:58.000Z"323 myImage2["ImageId"] = "ami-000cc31892067693b"324 myImage2["Name"] = "NotTestImage"325 myI2Ebs = {}326 myI2Ebs["DeleteOnTermination"] = True327 myI2Ebs["SnapshotId"] = "snap-000f48a8fa4545e1b"328 myI2Ebs["VolumeSize"] = 10329 myI2Ebs["VolumeType"] = "gp3"330 myI2Ebs["Encrypted"] = False331 bdmI2 = {}332 bdmI2["DeviceName"] = "/dev/sda1"333 bdmI2["Ebs"] = myI2Ebs334 myImage2["BlockDeviceMappings"] = []335 myImage2["BlockDeviceMappings"].append(bdmI2)336 myImage2["VirtualizationType"] = "para"337 myImage3 = {}338 myImage3["Architecture"] = "x86_64"339 myImage3["CreationDate"] = "2022-04-11T14:01:58.000Z"340 myImage3["ImageId"] = "ami-000cc31892067693c"341 myImage3["Name"] = "testImageName2"342 myI3Ebs = {}343 myI3Ebs["DeleteOnTermination"] = True344 myI3Ebs["SnapshotId"] = "snap-000f48a8fa4545e1a"345 myI3Ebs["VolumeSize"] = 10346 myI3Ebs["VolumeType"] = "gp3"347 myI3Ebs["Encrypted"] = False348 bdmI3 = {}349 bdmI3["DeviceName"] = "/dev/sda1"350 bdmI3["Ebs"] = myI1Ebs351 myImage3["BlockDeviceMappings"] = []352 myImage3["BlockDeviceMappings"].append(bdmI1)353 myImage3["VirtualizationType"] = "para"354 myImages = []355 myImages.append(myImage1)356 myImages.append(myImage2)357 myImages.append(myImage3)...

Full Screen

Full Screen

__main__.py

Source:__main__.py Github

copy

Full Screen

...62 :param image:63 :param max_age:64 """65 client = boto3.client("ec2")66 client.enable_image_deprecation(67 ImageId=image.id, DeprecateAt=image.deprecation_date(max_age)68 )69def remove_image(image: Image):70 """71 Remove a AMI based on image.id72 - https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.deregister_image73 :param image:74 """75 client = boto3.client("ec2")76 client.deregister_image(ImageId=image.id)77def get_image_list(name_filter: str, owner: str) -> list[dict]:78 """79 Returns a list of AMIs based on a given name filter expression and ownership80 :param name_filter: an expression to match while searching AMIs, such as "*image*-dev"...

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 localstack 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