<commit_before>from unittest import TestCase

from mock import Mock
from pyVmomi import vim

from common.logger.service import LoggingService
from vCenterShell.network.vnic.vnic_common import *
from vCenterShell.commands.disconnect_dvswitch import VirtualSwitchToMachineDisconnectCommand


class TestVirtualSwitchToMachineDisconnectCommand(TestCase):
    LoggingService("CRITICAL", "DEBUG", None)

    def test_delete_all(self):
        # arrange
        uuid = 'uuid'
        vcenter_name = 'vcenter_name'
        si = Mock()
        vm = Mock()

        connection_detail = Mock()
        connection_detail.host = Mock()
        connection_detail.username = Mock()
        connection_detail.password = Mock()
        connection_detail.port = Mock()

        connection_retriever = Mock()
        connection_retriever.connection_details = Mock(return_value=connection_detail)

        pv_service = Mock()
        pv_service.connect = Mock(return_value=si)
        pv_service.find_by_uuid = Mock(return_value=vm)

        connector = VirtualSwitchToMachineDisconnectCommand(pv_service, connection_retriever, Mock())
        #virtual_switch_to_machine_connector.remove_interfaces_from_vm = Mock(return_value=True)
        connector.get_network_by_name = lambda x, y: Mock()

        # act
        res = connector.disconnect(vcenter_name, uuid)
        # assert
        self.assertTrue(connection_retriever.connection_details.called_with(vcenter_name))
        self.assertTrue(pv_service.connect.called_with(connection_detail.host,
                                                       connection_detail.username,
                                                       connection_detail.password,
                                                       connection_detail.port))
        self.assertTrue(pv_service.find_by_uuid.called_with(si, uuid))
        #self.assertTrue(virtual_switch_to_machine_connector.remove_interfaces_from_vm.called_with(vm))
        self.assertTrue(res)

    def test_delete(self):
        # arrange
        uuid = 'uuid'
        vcenter_name = 'vcenter_name'
        network_name = 'vcenter_name'

        si = Mock()
        vm = Mock()

        connection_detail = Mock()
        connection_detail.host = Mock()
        connection_detail.username = Mock()
        connection_detail.password = Mock()
        connection_detail.port = Mock()

        connection_retriever = Mock()
        connection_retriever.connection_details = Mock(return_value=connection_detail)

        pv_service = Mock()
        pv_service.connect = Mock(return_value=si)
        pv_service.find_by_uuid = Mock(return_value=vm)

        connector = VirtualSwitchToMachineDisconnectCommand(pv_service, connection_retriever, Mock())
        connector.get_network_by_name = Mock(return_value=Mock())

        # act
        res = connector.disconnect(vcenter_name, uuid, network_name)
        # assert
        self.assertTrue(connection_retriever.connection_details.called_with(vcenter_name))
        self.assertTrue(pv_service.connect.called_with(connection_detail.host,
                                                       connection_detail.username,
                                                       connection_detail.password,
                                                       connection_detail.port))
        self.assertTrue(pv_service.find_by_uuid.called_with(si, uuid))
        #self.assertTrue(virtual_switch_to_machine_connector.remove_interfaces_from_vm_task.called)
        self.assertTrue(res)

    def test_is_device_match_network_port_type(self):
        # arrange
        backing = Mock(spec=[])
        device = Mock()
        port = Mock()

        device.backing = backing
        backing.port = port
        port.portgroupKey = 'port key'

        virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), Mock())

        # act
        #res = virtual_switch_to_machine_connector.is_device_match_network(device, port.portgroupKey)
        res = device_is_attached_to_network(device, port.portgroupKey)

        # assert
        self.assertTrue(res)

    def test_is_device_match_network_other_type(self):
        # arrange
        backing = Mock(spec=[])
        device = Mock()
        nerwork = Mock()

        device.backing = backing
        backing.network = nerwork
        nerwork.name = 'vln name or network name'

        virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), Mock())

        # act
        #res = virtual_switch_to_machine_connector.is_device_match_network(device, nerwork.name)
        res = device_is_attached_to_network(device, nerwork.name)

        # assert
        self.assertTrue(res)

    def test_is_device_match_network_not_found(self):
        # arrange
        device = Mock()
        device.backing = Mock(spec=[])

        virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), Mock())

        # act
        #res = virtual_switch_to_machine_connector.is_device_match_network(device, 'Fake name')
        res = device_is_attached_to_network(device, 'Fake name')

        # assert
        self.assertFalse(res)

    def test_is_device_match_network_not_found(self):
        # arrange
        device = Mock()
        device.backing = Mock(spec=[])

        virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), Mock())

        # act
        #res = virtual_switch_to_machine_connector.is_device_match_network(device, 'Fake name')
        res = device_is_attached_to_network(device, 'Fake name')

        # assert
        self.assertFalse(res)

    def test_remove_interfaces_from_vm_no_nic_found(self):
        # arrange
        vm = Mock()
        vm.config = Mock()
        vm.config.hardware()
        vm.config.hardware.device = []

        virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), Mock())

        # act
        res = virtual_switch_to_machine_connector.remove_interfaces_from_vm_task(vm)

        # assert
        self.assertIsNone(res)<commit_msg>extra test removed<commit_after>from unittest import TestCase

from mock import Mock
from pyVmomi import vim

from common.logger.service import LoggingService
from vCenterShell.network.vnic.vnic_common import *
from vCenterShell.commands.disconnect_dvswitch import VirtualSwitchToMachineDisconnectCommand


class TestVirtualSwitchToMachineDisconnectCommand(TestCase):
    LoggingService("CRITICAL", "DEBUG", None)

    def test_delete_all(self):
        # arrange
        uuid = 'uuid'
        vcenter_name = 'vcenter_name'
        si = Mock()
        vm = Mock()

        connection_detail = Mock()
        connection_detail.host = Mock()
        connection_detail.username = Mock()
        connection_detail.password = Mock()
        connection_detail.port = Mock()

        connection_retriever = Mock()
        connection_retriever.connection_details = Mock(return_value=connection_detail)

        pv_service = Mock()
        pv_service.connect = Mock(return_value=si)
        pv_service.find_by_uuid = Mock(return_value=vm)

        connector = VirtualSwitchToMachineDisconnectCommand(pv_service, connection_retriever, Mock())
        #virtual_switch_to_machine_connector.remove_interfaces_from_vm = Mock(return_value=True)
        connector.get_network_by_name = lambda x, y: Mock()

        # act
        res = connector.disconnect(vcenter_name, uuid)
        # assert
        self.assertTrue(connection_retriever.connection_details.called_with(vcenter_name))
        self.assertTrue(pv_service.connect.called_with(connection_detail.host,
                                                       connection_detail.username,
                                                       connection_detail.password,
                                                       connection_detail.port))
        self.assertTrue(pv_service.find_by_uuid.called_with(si, uuid))
        #self.assertTrue(virtual_switch_to_machine_connector.remove_interfaces_from_vm.called_with(vm))
        self.assertTrue(res)

    def test_delete(self):
        # arrange
        uuid = 'uuid'
        vcenter_name = 'vcenter_name'
        network_name = 'vcenter_name'

        si = Mock()
        vm = Mock()

        connection_detail = Mock()
        connection_detail.host = Mock()
        connection_detail.username = Mock()
        connection_detail.password = Mock()
        connection_detail.port = Mock()

        connection_retriever = Mock()
        connection_retriever.connection_details = Mock(return_value=connection_detail)

        pv_service = Mock()
        pv_service.connect = Mock(return_value=si)
        pv_service.find_by_uuid = Mock(return_value=vm)

        connector = VirtualSwitchToMachineDisconnectCommand(pv_service, connection_retriever, Mock())
        connector.get_network_by_name = Mock(return_value=Mock())

        # act
        res = connector.disconnect(vcenter_name, uuid, network_name)
        # assert
        self.assertTrue(connection_retriever.connection_details.called_with(vcenter_name))
        self.assertTrue(pv_service.connect.called_with(connection_detail.host,
                                                       connection_detail.username,
                                                       connection_detail.password,
                                                       connection_detail.port))
        self.assertTrue(pv_service.find_by_uuid.called_with(si, uuid))
        #self.assertTrue(virtual_switch_to_machine_connector.remove_interfaces_from_vm_task.called)
        self.assertTrue(res)

    def test_is_device_match_network_port_type(self):
        # arrange
        backing = Mock(spec=[])
        device = Mock()
        port = Mock()

        device.backing = backing
        backing.port = port
        port.portgroupKey = 'port key'

        virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), Mock())

        # act
        #res = virtual_switch_to_machine_connector.is_device_match_network(device, port.portgroupKey)
        res = device_is_attached_to_network(device, port.portgroupKey)

        # assert
        self.assertTrue(res)

    def test_is_device_match_network_other_type(self):
        # arrange
        backing = Mock(spec=[])
        device = Mock()
        nerwork = Mock()

        device.backing = backing
        backing.network = nerwork
        nerwork.name = 'vln name or network name'

        virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), Mock())

        # act
        #res = virtual_switch_to_machine_connector.is_device_match_network(device, nerwork.name)
        res = device_is_attached_to_network(device, nerwork.name)

        # assert
        self.assertTrue(res)

    def test_is_device_match_network_not_found(self):
        # arrange
        device = Mock()
        device.backing = Mock(spec=[])

        virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), Mock())

        # act
        #res = virtual_switch_to_machine_connector.is_device_match_network(device, 'Fake name')
        res = device_is_attached_to_network(device, 'Fake name')

        # assert
        self.assertFalse(res)


    def test_remove_interfaces_from_vm_no_nic_found(self):
        # arrange
        vm = Mock()
        vm.config = Mock()
        vm.config.hardware()
        vm.config.hardware.device = []

        virtual_switch_to_machine_connector = VirtualSwitchToMachineDisconnectCommand(Mock(), Mock(), Mock())

        # act
        res = virtual_switch_to_machine_connector.remove_interfaces_from_vm_task(vm)

        # assert
        self.assertIsNone(res)
{'fraction_non_alphanumeric': 0.08037303942348453, 'fraction_numerical': 0.0, 'mean_word_length': 2.608442948914041, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1356974', 'n_tokens_mistral': 3218, 'n_tokens_neox': 3163, 'n_words': 501}