Best Python code snippet using avocado_python
test_action_vm_bestfit.py
Source:test_action_vm_bestfit.py  
1# Licensed to the StackStorm, Inc ('StackStorm') under one or more2# contributor license agreements.  See the NOTICE file distributed with3# this work for additional information regarding copyright ownership.4# The ASF licenses this file to You under the Apache License, Version 2.05# (the "License"); you may not use this file except in compliance with6# the License.  You may obtain a copy of the License at7#8#     http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14import mock15from vm_bestfit import BestFit16from st2common.runners.base_action import Action17from vsphere_base_action_test_case import VsphereBaseActionTestCase18__all__ = [19    'BestFitTestCase'20]21class BestFitTestCase(VsphereBaseActionTestCase):22    __test__ = True23    action_cls = BestFit24    def setUp(self):25        super(BestFitTestCase, self).setUp()26        self._action = self.get_action_instance(self.new_config)27        self._action.establish_connection = mock.Mock()28        self._action.si_content = mock.Mock()29    def test_init(self):30        action = self.get_action_instance(self.new_config)31        self.assertIsInstance(action, BestFit)32        self.assertIsInstance(action, Action)33    def test_filter_datastores_regex_match(self):34        test_filters = ["(?i)(iso)"]35        test_name = "dev_isos"36        expected_result = False37        result = self._action.filter_datastores(test_name, test_filters)38        self.assertEqual(expected_result, result)39    def test_filter_datastores_regex_no_match(self):40        test_filters = ["(?i)(iso)"]41        test_name = "testdatastore"42        expected_result = True43        result = self._action.filter_datastores(test_name, test_filters)44        self.assertEqual(expected_result, result)45    def test_filter_datastores_name_match(self):46        test_filters = ["dev_isos"]47        test_name = "dev_isos"48        expected_result = False49        result = self._action.filter_datastores(test_name, test_filters)50        self.assertEqual(expected_result, result)51    def test_filter_datastores_name_no_match(self):52        test_filters = ["dev_isos"]53        test_name = "testdatastore"54        expected_result = True55        result = self._action.filter_datastores(test_name, test_filters)56        self.assertEqual(expected_result, result)57    @mock.patch('vmwarelib.inventory.get_managed_entities')58    @mock.patch('vmwarelib.actions.BaseAction.get_vim_type')59    def test_get_host_none_available(self, mock_vim_type, mock_entities):60        test_cluster_name = "cls1"61        test_vim_type = "vimType"62        mock_vim_type.return_value = test_vim_type63        # Mock function results64        mock_host1 = mock.MagicMock()65        mock_host1.parent.name = "cls2"66        mock_host2 = mock.MagicMock()67        mock_host2.parent.name = "cls1"68        mock_host2.runtime.powerState = "poweredOff"69        # Mock a list of 2 hosts that are unavailable70        test_host_list = [mock_host1, mock_host2]71        mock_host_list = mock.MagicMock(view=test_host_list)72        mock_entities.return_value = mock_host_list73        with self.assertRaises(Exception):74            self._action.get_host(test_cluster_name)75            mock_entities.assert_called_with(self._action.si_content, test_vim_type)76    @mock.patch('vmwarelib.inventory.get_managed_entities')77    @mock.patch('vmwarelib.actions.BaseAction.get_vim_type')78    def test_get_host(self, mock_vim_type, mock_entities):79        test_cluster_name = "cls1"80        test_vim_type = "vimType"81        mock_vim_type.return_value = test_vim_type82        # Mock function results83        mock_host1 = mock.MagicMock()84        mock_host1.parent.name = "cls1"85        mock_host1.runtime.powerState = "poweredOn"86        mock_host1.runtime.inMaintenanceMode = False87        mock_host1.vm = ["vm1", "vm2"]88        mock_host2 = mock.MagicMock()89        mock_host2.parent.name = "cls1"90        mock_host2.runtime.powerState = "poweredOn"91        mock_host2.runtime.inMaintenanceMode = False92        # This host will be the expected result since it has the fewest VMs93        mock_host2.vm = ["vm1"]94        # Mock a list of 2 hosts that are unavailable95        test_host_list = [mock_host1, mock_host2]96        mock_host_list = mock.MagicMock(view=test_host_list)97        mock_entities.return_value = mock_host_list98        result = self._action.get_host(test_cluster_name)99        self.assertEqual(result, mock_host2)100        mock_entities.assert_called_with(self._action.si_content, test_vim_type)101    @mock.patch('vmwarelib.inventory.get_managed_entity')102    @mock.patch('vmwarelib.actions.BaseAction.get_vim_type')103    def test_get_storage_not_found(self, mock_vim_type, mock_entity):104        test_vim_type = "vimType"105        mock_vim_type.return_value = test_vim_type106        test_host = mock.MagicMock()107        test_datastore_filter = ["filter"]108        test_disks = [{"datastore": "fail"}]109        # invoke action with invalid names which don't match any objects110        mock_entity.side_effect = Exception("Inventory Error: Unable to Find Object in a test")111        with self.assertRaises(Exception):112            self._action.get_storage(test_host, test_datastore_filter, test_disks)113    @mock.patch('vmwarelib.inventory.get_managed_entity')114    @mock.patch('vmwarelib.actions.BaseAction.get_vim_type')115    def test_get_storage_from_disk(self, mock_vim_type, mock_entity):116        test_vim_type = "vimType"117        mock_vim_type.return_value = test_vim_type118        test_datastore_filter = ["filter"]119        test_disks = [{"datastore": "test-ds-1"}]120        expected_result = "result"121        mock_host = mock.MagicMock()122        mock_entity.return_value = expected_result123        result = self._action.get_storage(mock_host, test_datastore_filter, test_disks)124        self.assertEqual(expected_result, result)125        mock_entity.assert_called_with(self._action.si_content, test_vim_type, name="test-ds-1")126    @mock.patch('vm_bestfit.BestFit.filter_datastores')127    @mock.patch('vmwarelib.actions.BaseAction.get_vim_type')128    def test_get_storage_no_disk(self, mock_vim_type, mock_filter):129        test_vim_type = "vimType"130        mock_vim_type.return_value = test_vim_type131        test_datastore_filter = ["(?i)(filter)"]132        test_disks = None133        # Mock datastore objects134        mock_ds1 = mock.MagicMock()135        type(mock_ds1).name = mock.PropertyMock(return_value="test-ds-1")136        mock_ds1.info.freeSpace = 10137        mock_ds2 = mock.MagicMock()138        type(mock_ds2).name = mock.PropertyMock(return_value="test-ds-2")139        mock_ds2.info.freeSpace = 20140        # This datastre should get filtered out from test_datastore_filter141        mock_ds3 = mock.MagicMock()142        type(mock_ds3).name = mock.PropertyMock(return_value="test-ds-filter")143        mock_ds3.info.freeSpace = 100144        # This is the result from filter_datastores function that filters out mock_ds3145        mock_filter.side_effect = [True, True, False]146        # Mock a list of 2 hosts that are unavailable147        test_ds_list = [mock_ds1, mock_ds2, mock_ds3]148        # Mock host input149        mock_host = mock.MagicMock(datastore=test_ds_list)150        result = self._action.get_storage(mock_host, test_datastore_filter, test_disks)151        self.assertEqual(result, mock_ds2)152        mock_filter.assert_has_calls([mock.call("test-ds-1", test_datastore_filter),153                                      mock.call("test-ds-2", test_datastore_filter),154                                      mock.call("test-ds-filter", test_datastore_filter)])155    @mock.patch('vm_bestfit.BestFit.get_storage')156    @mock.patch('vm_bestfit.BestFit.get_host')157    def test_run(self, mock_get_host, mock_get_storage):158        # Define test variables159        test_ds_filter = ["filter"]160        test_disks = [{"datastore": "test-ds-1"}]161        test_vsphere = "vsphere"162        test_cluster_name = "test-cluster"163        test_host_name = "test-host"164        test_host_id = "host-123"165        test_ds_name = "test-ds"166        test_ds_id = "ds-123"167        self._action.establish_connection = mock.Mock()168        # Mock host and datastore result objects169        mock_host = mock.MagicMock()170        type(mock_host).name = mock.PropertyMock(return_value=test_host_name)171        mock_host.parent.name = test_cluster_name172        mock_host._moId = test_host_id173        mock_get_host.return_value = mock_host174        mock_ds = mock.MagicMock()175        type(mock_ds).name = mock.PropertyMock(return_value=test_ds_name)176        mock_ds._moId = test_ds_id177        mock_get_storage.return_value = mock_ds178        expected_result = {'clusterName': test_cluster_name,179                           'hostName': test_host_name,180                           'hostID': test_host_id,181                           'datastoreName': test_ds_name,182                           'datastoreID': test_ds_id}183        result = self._action.run(test_cluster_name, test_ds_filter, test_disks, test_vsphere)184        self.assertEqual(result, expected_result)185        self._action.establish_connection.assert_called_with(test_vsphere)186        mock_get_host.assert_called_with(test_cluster_name)...test_copy.py
Source:test_copy.py  
1import pytest2import filecmp3from .utils import *4from alphacopy.devicesutil import DevicesUtil5devices = DevicesUtil("test_user")6def test_copy_file(random_file):7    src = random_file8    dst = 'output_file'9    devices.copy_file(src, dst)10    assert os.path.exists(dst)11    assert filecmp.cmp(src, dst)12    os.remove(dst)13def test_compare_hash(random_file):14    src = random_file15    dst = 'output_file'16    devices.copy_file(src, dst)17    # Check if the hash is the same18    assert devices.compare_hash(src, dst) is True19    # Change the dst contents and see if the hash is different20    write_random_data_to_file(dst, 1000)21    assert devices.compare_hash(src, dst) is False22    # Clean up23    os.remove(dst)24def test_list_disks(tmpdir):25    # Make a temporary volumes path26    volumes_path = tmpdir / "test_user"27    devices.volumes_path = volumes_path28    os.mkdir(volumes_path)29    # Make sure it exists30    assert os.path.exists(volumes_path)31    test_disks = ["test_disk_1", "test_disk_2"]32    for test_disk in test_disks:33        test_disk_path = volumes_path / test_disk34        os.mkdir(test_disk_path)35    disks = devices.list_disks()36    assert len(disks) == len(test_disks)37    for disk in disks:38        assert disk in test_disks...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!!
