Best Python code snippet using lisa_python
test_prepare.py
Source:test_prepare.py  
...74        assert self._platform._locations_data_cache75        notreal_key = self._platform._get_location_key("notreal")76        self.assertTrue(notreal_key in self._platform._locations_data_cache)77        assert self._platform._locations_data_cache78        self.verify_eligible_vm_size("westus2", "notreal", False)79        self.assertTrue(notreal_key in self._platform._locations_data_cache)80    def test_predefined_2nd_location(self) -> None:81        # location predefined in eastus2, so all prepared skip westus282        env = self.load_environment(node_req_count=2)83        self.verify_prepared_nodes(84            expected_result=True,85            expected_locations=["westus2", "westus2"],86            expected_vm_sizes=["Standard_DS2_v2", "Standard_DS2_v2"],87            expected_cost=4,88            environment=env,89        )90        env = self.load_environment(node_req_count=2)91        self.set_node_runbook(env, 1, location="eastus2")92        self.verify_prepared_nodes(93            expected_result=True,94            expected_locations=["eastus2", "eastus2"],95            expected_vm_sizes=["Standard_DS2_v2", "Standard_DS2_v2"],96            expected_cost=4,97            environment=env,98        )99    def test_predefined_only_size(self) -> None:100        # predefined an eastus2 vm size, so all are to eastus2101        env = self.load_environment(node_req_count=2)102        self.set_node_runbook(env, 1, location="", vm_size="Standard_B1ls")103        self.verify_prepared_nodes(104            expected_result=True,105            expected_locations=["eastus2", "eastus2"],106            expected_vm_sizes=["Standard_DS2_v2", "Standard_B1ls"],107            expected_cost=3,108            environment=env,109        )110    def test_predefined_with_3_nic(self) -> None:111        # 3 nic cannot be met by Standard_DS2_v2, as it support at most 2 nics112        # the code path of predefined and normal is different, so test it twice113        env = self.load_environment(node_req_count=1)114        assert env.runbook._original_nodes_requirement115        env.runbook._original_nodes_requirement.append(116            schema.NodeSpace(117                network_interface=schema.NetworkInterfaceOptionSettings(nic_count=3)118            )119        )120        self.set_node_runbook(env, 1, location="eastus2")121        self.verify_prepared_nodes(122            expected_result=True,123            expected_locations=["eastus2", "eastus2"],124            expected_vm_sizes=["Standard_DS2_v2", "Standard_DS15_v2"],125            expected_cost=22,126            environment=env,127        )128    def test_predefined_inconsistent_location_failed(self) -> None:129        # two locations westus2, and eastus2 predefined, so failed.130        env = self.load_environment(node_req_count=2)131        self.set_node_runbook(env, 0, location="eastus2")132        self.set_node_runbook(env, 1, location="westus2")133        with self.assertRaises(LisaException) as cm:134            self._platform._prepare_environment(env, self._log)135        message = (136            "predefined node must be in same location, "137            "previous: eastus2, found: westus2"138        )139        self.assertEqual(message, str(cm.exception)[0 : len(message)])140    def test_no_predefined_location_use_default_locations(self) -> None:141        # no predefined location found, use default location list142        env = self.load_environment(node_req_count=1)143        self.verify_prepared_nodes(144            expected_result=True,145            expected_locations=["westus2"],146            expected_vm_sizes=["Standard_DS2_v2"],147            expected_cost=2,148            environment=env,149        )150    def test_use_predefined_vm_size(self) -> None:151        # there is predefined vm size, and out of default scope, but use it152        env = self.load_environment(node_req_count=1)153        self.set_node_runbook(env, 0, location="", vm_size="Standard_NV48s_v3")154        # 448: 48 cores + 100 * 4 gpus155        self.verify_prepared_nodes(156            expected_result=True,157            expected_locations=["eastus2"],158            expected_vm_sizes=["Standard_NV48s_v3"],159            expected_cost=448,160            environment=env,161        )162    def test_predefined_not_found_vm_size(self) -> None:163        # vm size is not found164        env = self.load_environment(node_req_count=1)165        self.set_node_runbook(env, 0, location="", vm_size="not_exist")166        with self.assertRaises(NotMeetRequirementException) as cm:167            self.verify_prepared_nodes(168                expected_result=False,169                expected_locations=[],170                expected_vm_sizes=[],171                expected_cost=0,172                environment=env,173            )174            self.assertIsInstance(cm.exception, NotMeetRequirementException)175            self.assertIn("No capability found for Environment", str(cm.exception))176    def test_predefined_not_found_vm_size_max_enabled(self) -> None:177        # vm size is not found178        env = self.load_environment(node_req_count=1)179        self.set_node_runbook(180            env, 0, location="", vm_size="not_exist", max_capability=True181        )182        # The mock up capability is matched.183        self.verify_prepared_nodes(184            expected_result=True,185            expected_locations=["westus2"],186            expected_vm_sizes=["not_exist"],187            expected_cost=1,188            environment=env,189        )190    def test_predefined_wont_be_override(self) -> None:191        # predefined node won't be overridden in loop192        env = self.load_environment(node_req_count=3)193        self.set_node_runbook(env, 1, location="", vm_size="Standard_A8_v2")194        self.set_node_runbook(env, 2, location="eastus2", vm_size="")195        self.verify_prepared_nodes(196            expected_result=True,197            expected_locations=["eastus2", "eastus2", "eastus2"],198            expected_vm_sizes=["Standard_DS2_v2", "Standard_A8_v2", "Standard_DS2_v2"],199            expected_cost=12,200            environment=env,201        )202    def test_partial_match(self) -> None:203        # the "A2" should match Standard_A2_v2, instead of Standard_A2m_v2. The204        # test data has two Standard_A2m_v2, so the test case can guarantee the205        # problem won't be hidden by different behavior.206        env = self.load_environment(node_req_count=2)207        self.set_node_runbook(env, 0, location="", vm_size="A2m")208        self.set_node_runbook(env, 1, location="", vm_size="A2")209        self.verify_prepared_nodes(210            expected_result=True,211            expected_locations=["westus2", "westus2"],212            expected_vm_sizes=["Standard_A2m_v2", "Standard_A2_v2"],213            expected_cost=4,214            environment=env,215        )216    def test_normal_req_in_same_location(self) -> None:217        # normal requirement will be in same location of predefined218        env = self.load_environment(node_req_count=2)219        self.set_node_runbook(env, 1, location="eastus2", vm_size="")220        self.verify_prepared_nodes(221            expected_result=True,222            expected_locations=["eastus2", "eastus2"],223            expected_vm_sizes=["Standard_DS2_v2", "Standard_DS2_v2"],224            expected_cost=4,225            environment=env,226        )227    def test_count_normal_cost(self) -> None:228        # predefined vm size also count the cost229        env = self.load_environment(node_req_count=2)230        self.verify_prepared_nodes(231            expected_result=True,232            expected_locations=["westus2", "westus2"],233            expected_vm_sizes=["Standard_DS2_v2", "Standard_DS2_v2"],234            expected_cost=4,235            environment=env,236        )237    def test_normal_may_fit_2nd_location(self) -> None:238        # normal req may fit into 2nd location, as 1st location not meet requirement239        env = self.load_environment(node_req_count=1)240        assert env.runbook._original_nodes_requirement241        env.runbook._original_nodes_requirement.append(242            schema.NodeSpace(memory_mb=search_space.IntRange(min=143360))243        )244        self.verify_prepared_nodes(245            expected_result=True,246            expected_locations=["eastus2", "eastus2"],247            expected_vm_sizes=["Standard_DS2_v2", "Standard_DS15_v2"],248            expected_cost=22,249            environment=env,250        )251    def test_normal_may_fit_2nd_batch_vm(self) -> None:252        # fit 2nd batch of candidates253        env = self.load_environment(node_req_count=1)254        assert env.runbook._original_nodes_requirement255        env.runbook._original_nodes_requirement.append(256            schema.NodeSpace(core_count=8, memory_mb=16384)257        )258        self.verify_prepared_nodes(259            expected_result=True,260            expected_locations=["eastus2", "eastus2"],261            expected_vm_sizes=["Standard_DS2_v2", "Standard_A8_v2"],262            expected_cost=10,263            environment=env,264        )265    def test_normal_with_3_nic(self) -> None:266        # 3 nic cannot be met by Standard_DS2_v2, as it support at most 2 nics267        # the code path of predefined and normal is different, so test it twice268        env = self.load_environment(node_req_count=1)269        assert env.runbook._original_nodes_requirement270        env.runbook._original_nodes_requirement.append(271            schema.NodeSpace(272                network_interface=schema.NetworkInterfaceOptionSettings(nic_count=3)273            )274        )275        self.verify_prepared_nodes(276            expected_result=True,277            expected_locations=["eastus2", "eastus2"],278            expected_vm_sizes=["Standard_DS2_v2", "Standard_DS15_v2"],279            expected_cost=22,280            environment=env,281        )282    def test_multiple_locations(self) -> None:283        # vm size is not found284        env = self.load_environment(node_req_count=1)285        self.set_node_runbook(286            env, 0, location="australiaeast,brazilsouth", vm_size="DS1_v2"287        )288        self.verify_prepared_nodes(289            expected_result=True,290            expected_locations=["australiaeast"],291            expected_vm_sizes=["Standard_DS1_v2"],292            expected_cost=1,293            environment=env,294        )295    def test_multiple_vm_sizes(self) -> None:296        # vm size is not found297        env = self.load_environment(node_req_count=1)298        self.set_node_runbook(env, 0, location="", vm_size="A8_v2,NV48s_v3")299        self.verify_prepared_nodes(300            expected_result=True,301            expected_locations=["eastus2"],302            expected_vm_sizes=["Standard_A8_v2"],303            expected_cost=8,304            environment=env,305        )306    def verify_exists_vm_size(307        self, location: str, vm_size: str, expect_exists: bool308    ) -> Optional[platform_.AzureCapability]:309        matched_vm_size = ""310        location_info = self._platform.get_location_info(location, self._log)311        self.assertEqual(312            expect_exists,313            any([x == vm_size for x in location_info.capabilities]),314        )315        if expect_exists:316            matched_vm_size = next(317                x for x in location_info.capabilities if x == vm_size318            )319        return location_info.capabilities.get(matched_vm_size, None)320    def verify_eligible_vm_size(321        self, location: str, vm_size: str, expect_exists: bool322    ) -> Optional[platform_.AzureCapability]:323        result = None324        location_info = self._platform.get_location_info(location, self._log)325        sorted_capabilities = self._platform.get_sorted_vm_sizes(326            [value for _, value in location_info.capabilities.items()], self._log327        )328        self.assertEqual(329            expect_exists,330            any([x.vm_size == vm_size for x in sorted_capabilities]),331        )332        if expect_exists:333            result = next(x for x in sorted_capabilities if x.vm_size == vm_size)334        return result...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!!
