import unittest
from unittest import mock
from pylib import shell
from vm.virtualbox import VirtualBox

class TestVirtualBox(unittest.TestCase):

    @mock.patch.object(shell, "getoutput", autospec=True)
    @mock.patch.object(VirtualBox, "scan_vboxmanage", autospec=True)
    def test_get_vm_list(self, mock_scan_vboxmanage, mock_shell_getoutput):
        output = '''"ubuntu-16.04.1-server" {bcf7ed90-468a-4084-a789-0f5ac61c26d8}
                    "debian-8.6.0" {fac680d9-7220-453c-abbe-03a54859a88e}'''
        mock_shell_getoutput.return_value = output

        vms = VirtualBox().get_vm_list()
        
        self.assertTrue(len(vms) == 2)
        self.assertTrue(vms[0] == "ubuntu-16.04.1-server")
        self.assertTrue(vms[1] == "debian-8.6.0")

    @mock.patch.object(shell, "getoutput", autospec=True)
    @mock.patch.object(VirtualBox, "scan_vboxmanage", autospec=True)
    def test_vmstate(self, mock_scan_vboxmanage, mock_shell_getoutput):
        output = '''VMState="poweroff"'''
        mock_shell_getoutput.return_value = output

        state = VirtualBox().vmstate("any")
        
        self.assertTrue(state == "poweroff")


    @mock.patch.object(shell, "getoutput", autospec=True)
    @mock.patch.object(VirtualBox, "scan_vboxmanage", autospec=True)
    def test_get_nic_list(self, mock_scan_vboxmanage, mock_shell_getoutput):
        output = '''Name:            en0: Wi-Fi (AirPort)
                    Name:            en1: Thunderbolt 1
                    Name:            bridge0'''
        mock_shell_getoutput.return_value = output

        nics = VirtualBox().get_nic_list()
        
        self.assertTrue(len(nics) == 3)
        self.assertTrue(nics[0] == "en0: Wi-Fi (AirPort)")
        self.assertTrue(nics[1] == "en1: Thunderbolt 1")
        self.assertTrue(nics[2] == "bridge0")

if __name__ == '__main__':
    unittest.main()