Best Python code snippet using localstack_python
test_openstack.py
Source:test_openstack.py
...59 return_value=None):60 with pytest.raises(SystemExit):61 conn = get_connection()62@pytest.fixture63def get_os(scope="function"):64 conn = MagicMock()65 lb = LoadBalancer(CONFIG, conn)66 lb._data = default_data()67 lb._id = lb._data.id68 return conn, lb69def test_create_loadbalancer():70 conn = get_connection()71 # All good72 lb = LoadBalancer(CONFIG, conn)73 assert lb74 assert isinstance(lb, LoadBalancer)75def test_master_listener_unitialized_lb(get_os):76 conn, lb = get_os77 lb._data = None...
accountmanagement.py
Source:accountmanagement.py
...5import os6try:7 import win32net8except ModuleNotFoundError:9 if "Windows" in plugin.get_os():10 common.warn("The 'win32net' package is required for Windows systems!")11 sys.exit(1)12class AccountManagement(plugin.Plugin):13 """A universal plugin to configure users.14 Add and remove users and promote to/demote from admin.15 """16 name = "Account Management"17 os = ["ALL"]18 os_version = ["ALL"]19 def execute(self):20 """Execute plugin."""21 if "Linux" in plugin.get_os():22 common.backup("/etc/passwd")23 common.backup("/etc/group")24 common.backup("/etc/shadow")25 current_user = common.input_text("What is the current username")26 admins = self._get_users("Admin")27 # ensures the current user isn't in the admin list28 if current_user in admins:29 admins.remove(current_user)30 standard = self._get_users("Standard")31 # ensures the current user isn't in the standard list32 if current_user in standard:33 standard.remove(current_user)34 current_users = self._get_current_users()35 common.debug("Found users: {}".format(", ".join(current_users)))36 # first we need to get rid of the bad users37 bad_users = []38 for user in current_users:39 if user not in [current_user] + admins + standard:40 bad_users.append(user)41 self._delete_users(bad_users)42 current_users = list(set(current_users) - set(bad_users))43 # find new users44 new_users = []45 for user in admins + standard:46 if user not in current_users:47 new_users.append(user)48 self._create_users(new_users)49 # set all users to a standard user50 self._set_standard_users(standard)51 # set admin users to admin52 self._set_admin_users(admins)53 # change password to a secure one54 common.info("Changing passwords")55 for index, user in enumerate([current_user] + admins + standard):56 if user != current_user:57 # Not sure if we want to do this on the main user58 password = "CyberCenturion{}!".format(index)59 self._change_password(user, password)60 self._set_password_no_expire(user)61 self._change_password_on_login(user)62 def _get_users(self, rank="standard"):63 return common.input_list("Enter a list of {} users".format(rank.lower()))64 def _get_current_users(self):65 if "Linux" in plugin.get_os():66 return common.get_current_users()67 elif "Windows" in plugin.get_os():68 all_users = []69 data = list(win32net.NetUserEnum(None, 0))[0]70 for piece in data:71 all_users.append(piece["name"])72 return all_users73 def _delete_users(self, users):74 for user in users:75 common.info("Deleting {}...".format(user))76 if "Linux" in plugin.get_os():77 # TODO backup user directory78 # TODO find any other files elsewhere in the system that user owns79 common.run("crontab -r -u {}".format(user))80 common.run("userdel -r {}".format(user))81 common.info("Deleted user {}".format(user))82 elif "Windows" in plugin.get_os():83 # TODO remove this84 if user in "GuestAdministrator,DefaultAccount,defaultuser0":85 continue86 try:87 win32net.NetUserDel(None, user)88 except Exception as ex:89 common.error("Error while deleting user {}".format(user), ex)90 def _create_users(self, users):91 for user in users:92 common.info("Adding {}...".format(user))93 if "Linux" in plugin.get_os():94 common.run("useradd -s /bin/bash -m {}".format(user))95 common.info("Added user {}".format(user))96 elif "Windows" in plugin.get_os():97 os.system("net user \"{}\" /add".format(user))98 def _set_standard_users(self, users):99 common.info("Setting standard users...")100 for user in users:101 if "Linux" in plugin.get_os():102 # set only group to be the user's primary group103 common.run("usermod -G {0} {0}".format(user))104 common.run("usermod -aG users {}".format(user))105 common.info("Removed all groups from user {}".format(user))106 elif "Windows" in plugin.get_os():107 groups = win32net.NetUserGetLocalGroups(None, user)108 for group in groups:109 if group != "Users":110 os.system("net localgroup \"{}\" \"{}\" /delete".format(group, user))111 def _set_admin_users(self, users):112 common.info("Setting admin users...")113 for user in users:114 if "Linux" in plugin.get_os():115 # list of groups we want to add the user to116 admin_roles = ["sudo", "adm"]117 # add the admin roles118 common.run("usermod -aG {0} {1}".format(",".join(admin_roles), user))119 elif "Windows" in plugin.get_os():120 groups = win32net.NetUserGetLocalGroups(None, user)121 if "Administrators" not in groups:122 os.system("net localgroup Administrators \"{}\" /add".format(user))123 def _change_password(self, user, password):124 common.info("Changing password of {0} to {1}".format(user, password))125 if "Linux" in plugin.get_os():126 common.run_full("echo '{0}:{1}' | chpasswd".format(user, password))127 elif "Windows" in plugin.get_os():128 os.system("net user \"{}\" \"{}\"".format(user, password))129 def _change_password_on_login(self, user):130 if "Linux" in plugin.get_os():131 # TODO see if this can be implemented132 pass133 elif "Windows" in plugin.get_os():134 os.system("net user \"{}\" /logonpasswordchg:yes".format(user))135 def _set_password_no_expire(self, user):136 if "Windows" in plugin.get_os():137 # Password has to be set to expire in order to enforce change password on login...
test_os_factory.py
Source:test_os_factory.py
...11class TestOsFactory(TestCase):12 def setUp(self):13 self.mock_disp_obj = MockDispatcher.build_mock_dispatcher()14 def test_get_factory_ubuntu(self):15 assert type(SotaOsFactory(self.mock_disp_obj).get_os('Ubuntu')) is DebianBasedSotaOs16 def test_get_factory_yocto(self):17 assert type(SotaOsFactory(self.mock_disp_obj).get_os('YoctoX86_64')) is YoctoX86_6418 def test_get_factory_windows(self):19 assert type(SotaOsFactory(self.mock_disp_obj).get_os('Windows')) is Windows20 def test_raise_error_unsupported_OsFactory(self):21 factory = SotaOsFactory(self.mock_disp_obj)22 self.assertRaises(ValueError, factory.get_os, "MacOS")23 def test_create_ubuntu_upgrader_checker(self):24 assert type(SotaOsFactory(self.mock_disp_obj).get_os('Ubuntu').create_os_upgrader()) \25 is UbuntuUpgrader26 def test_create_yocto_upgrader_checker(self):27 assert type(SotaOsFactory(self.mock_disp_obj).get_os('YoctoX86_64').create_os_upgrader()) \28 is YoctoUpgrader29 def test_create_windows_upgrader_checker(self):30 assert type(SotaOsFactory(self.mock_disp_obj).get_os('Windows').create_os_upgrader()) \31 is WindowsUpgrader32 def test_create_ubuntu_snapshot_checker(self):33 assert type(SotaOsFactory(self.mock_disp_obj).get_os('Ubuntu').create_snapshotter('update', '1', False)) \34 is DebianBasedSnapshot35 def test_create_yocto_snapshot_checker(self):36 assert type(SotaOsFactory(self.mock_disp_obj).get_os('YoctoX86_64').create_snapshotter('update', '1', False)) \37 is YoctoSnapshot38 def test_create_windows_snapshot_checker(self):39 assert type(SotaOsFactory(self.mock_disp_obj).get_os('Windows').create_snapshotter('update', '1', False)) \40 is WindowsSnapshot41 def test_create_ubuntu_updater_checker(self):42 assert type(SotaOsFactory(self.mock_disp_obj).get_os('Ubuntu').create_os_updater()) \43 is DebianBasedUpdater44 def test_create_yocto_updater_checker(self):45 assert type(SotaOsFactory(self.mock_disp_obj).get_os('YoctoX86_64').create_os_updater()) \46 is YoctoX86_64Updater47 def test_create_ubuntu_setup_helper_checker(self):48 assert type(SotaOsFactory(self.mock_disp_obj).get_os('Ubuntu').create_setup_helper()) \49 is DebianBasedSetupHelper50 def test_create_yocto_setup_helper_checker(self):51 assert type(SotaOsFactory(self.mock_disp_obj).get_os('YoctoX86_64').create_setup_helper()) \52 is YoctoSetupHelper53 def test_create_windows_setup_helper_checker(self):54 assert type(SotaOsFactory(self.mock_disp_obj).get_os('Windows').create_setup_helper()) \55 is WindowsSetupHelper56 def test_create_ubuntu_downloader(self):57 assert type(SotaOsFactory(self.mock_disp_obj).get_os('Ubuntu').create_downloader()) \58 is DebianBasedDownloader59 def test_create_yocto_downloader(self):60 assert type(SotaOsFactory(self.mock_disp_obj).get_os('YoctoX86_64').create_downloader()) \61 is YoctoDownloader62 def test_create_windows_downloader(self):63 assert type(SotaOsFactory(self.mock_disp_obj).get_os('Windows').create_downloader()) \64 is WindowsDownloader65 @patch('platform.system')66 def test_verify_os_supported_success(self, mock_func):67 mock_func.return_value = 'Linux'68 ret = SotaOsFactory.verify_os_supported()69 self.assertEquals(ret, 'Linux')70 @patch('platform.system')71 def test_verify_os_supported_fail(self, mock_func):72 mock_func.return_value = 'MacOs'...
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!!