示例#1
0
    def test_invalid_data(self):
        """
        Test creating a vm with invalid data
        """
        with pytest.raises(ValueError, message='template should fail to instantiate if data dict is missing the nodeId'):
            vm = Vm(name='vm', data={})
            vm.validate()

        with pytest.raises(ValueError, message='template should fail to instantiate if image is not valid'):
            vm = Vm(name='vm', data={'nodeId': 'main', 'image': 'test'})
            vm.validate()

        with pytest.raises(ValueError, message='template should fail to instantiate if zerotier dict is missing id'):
            vm = Vm(name='vm', data={'nodeId': 'main', 'image': 'ubuntu', 'zerotier': {'id': ''}})
            vm.validate()

        with pytest.raises(ValueError, message='template should fail to instantiate if zerotier dict is missing ztClient'):
            vm = Vm(name='vm', data={'nodeId': 'main', 'image': 'ubuntu', 'zerotier': {'id': 'id'}})
            vm.validate()

        with pytest.raises(ValueError, message='template should fail to instantiate if there are no nodes'):

            capacity = MagicMock()
            capacity.api.ListCapacity.return_value = ([],)
            patch('js9.j.clients.grid_capacity.get.return_value', capacity).start()
            self.vm.validate()
示例#2
0
 def test_enable_vnc(self):
     vm = Vm('vm', data=self.valid_data)
     vm.state.set('actions', 'install', 'ok')
     vm_sal = MagicMock(info={'vnc': 90})
     vm._node_sal.primitives.from_dict.return_value = vm_sal
     vm.enable_vnc()
     vm._vm_sal.enable_vnc.assert_called_with()
示例#3
0
    def main():
        if len(sys.argv) > 2:
            if (sys.argv[2] == '-i'):
                Lox.runFileInteractive(sys.argv[1], Vm())
            else:
                print('''usage: 
                    plox files [-i]
                    -i: run files in interactive mode''')
            exit(64) #see open_bsd exit code (just for standardization)
        elif len(sys.argv) == 2:
            # Lox.runFile(sys.argv[1], Vm())
            Lox.runDebugFile(sys.argv[1], Vm())

        else:
            # Lox.runPrompt(Vm())
            Lox.runDebugPrompt(Vm())
示例#4
0
    def vm_list(self):
        logger.debug("Geting vm_list")
        if not self._vm_list:
            for site_name, testbed in self.testbed.iteritems():
                self._vm_list[site_name] = []

                tb_json = testbed.tb_json
                for vm in testbed.vm_list:
                    vm_json, cloud_json = get_vm_and_cloud_json(
                        vm.get('name'), tb_json)
                    vm_cloud_sdk_conn = get_vm_cloud_sdk(cloud_json=cloud_json,
                                                         vm_json=vm_json)
                    if vm['type'].lower() == 'client':
                        vm_ins = Client(vm_json = vm, networks_detail = testbed.networks, \
                                        vm_cloud_sdk_conn=vm_cloud_sdk_conn)
                    elif vm['type'].lower() == 'server':
                        vm_ins = Server(vm_json = vm, networks_detail = testbed.networks, \
                                        vm_cloud_sdk_conn=vm_cloud_sdk_conn)
                    elif vm['type'].lower() == 'controller':
                        vm_ins = Controller(
                            vm_json=vm, vm_cloud_sdk_conn=vm_cloud_sdk_conn)
                    elif vm['type'].lower() == 'se':
                        continue
                    else:
                        vm_ins = Vm(vm_json=vm,
                                    vm_cloud_sdk_conn=vm_cloud_sdk_conn)
                    self._vm_list[site_name].append(vm_ins)
        return self._vm_list
示例#5
0
 def test_valid_data(self):
     """
     Test creating a vm service with valid data
     """
     vm = Vm('vm', data=self.valid_data)
     vm.validate()
     assert vm.data == self.valid_data
示例#6
0
文件: main.py 项目: S-YOU/tinyVM
def main():
    parser = argparse.ArgumentParser(description="A Tiny VM written in Python")
    parser.add_argument('--file', dest="filepath", help='the source file')
    args = parser.parse_args()
    with open(args.filepath) as srcfile:
        instance = Vm(srcfile)
        instance.run()
示例#7
0
 def test_reset_vm(self):
     """
     Test successfully reset the vm
     """
     vm = Vm('vm', data=self.valid_data)
     vm.state.set('actions', 'install', 'ok')
     vm.reset()
     vm._vm_sal.reset.assert_called_with()
示例#8
0
 def test_vm_sal(self):
     """
     Test the _vm_sal property
     """
     vm = Vm('vm', data=self.valid_data)
     vm_sal = 'vm_sal'
     vm._node_sal.primitives.from_dict.return_value = vm_sal
     assert vm._vm_sal == vm_sal
示例#9
0
 def test_pause_vm_not_running(self):
     """
     Test pausing the vm without creation
     """
     with pytest.raises(StateCheckError,
                        message='Pausing vm that is not running'):
         vm = Vm('vm', data=self.valid_data)
         vm.pause()
示例#10
0
def six():
    txt = open("./data/payload_6.txt", "r")
    txt_string = txt.read()
    txt_decode = base64.a85decode(txt_string, adobe=True)

    # vm = Vm(bytes.fromhex('50 48 c2 02 a8 4d 00 00 00 4f 02 50 09 c4 02 02 e1 01 4f 02 c1 22 1d 00 00 00 48 30 02 58 03 4f 02 b0 29 00 00 00 48 31 02 50 0c c3 02 aa 57 48 02 c1 21 3a 00 00 00 48 32 02 48 77 02 48 6f 02 48 72 02 48 6c 02 48 64 02 48 21 02 01 65 6f 33 34 2c'))
    vm = Vm(bytearray(txt_decode))
    vm.run()
示例#11
0
 def test_install_vm(self):
     """
     Test successfully creating a vm
     """
     vm = Vm('vm', data=self.valid_data)
     vm.api.services.get = MagicMock()
     vm.install()
     assert 'uuid' in vm.data
示例#12
0
 def test_disable_vnc(self):
     """
     Test disable_vnc when there is a vnc port
     """
     vm = Vm('vm', data=self.valid_data)
     vm.state = MagicMock()
     vm.disable_vnc()
     self.node.hypervisor.get.return_value.disable_vnc.assert_called_with()
示例#13
0
 def test_enable_vnc_vm_not_installed(self):
     """
     Test enable_vnc vm not installed
     """
     with pytest.raises(
             StateCheckError,
             message='enable vnc before install should raise an error'):
         vm = Vm('vm', data=self.valid_data)
         vm.enable_vnc()
示例#14
0
 def test_reset_vm_not_installed(self):
     """
     Test reset the vm without creation
     """
     with pytest.raises(
             StateCheckError,
             message='Resetting vm before install should raise an error'):
         vm = Vm('vm', data=self.valid_data)
         vm.reset()
示例#15
0
 def test_resume_vm_not_pause(self):
     """
     Test resume the vm without creation
     """
     with pytest.raises(
             StateCheckError,
             message='Resuming vm before pause should raise an error'):
         vm = Vm('vm', data=self.valid_data)
         vm.resume()
示例#16
0
    def test_uninstall_vm(self):
        """
        Test successfully destroying the vm
        """
        vm = Vm('vm', data=self.valid_data)
        vm.api.services.get = MagicMock()
        vm.uninstall()

        self.node.hypervisor.get.return_value.destroy.assert_called_with()
示例#17
0
 def test_shutdown_vm(self):
     """
     Test successfully shutting down the vm
     """
     vm = Vm('vm', data=self.valid_data)
     vm.api.services.get = MagicMock()
     vm.state = MagicMock()
     vm.shutdown()
     self.node.hypervisor.get.return_value.shutdown.assert_called_with()
示例#18
0
 def test_reset_vm(self):
     """
     Test successfully reset the vm
     """
     vm = Vm('vm', data=self.valid_data)
     vm.api.services.get = MagicMock()
     vm.state = MagicMock()
     vm.reset()
     self.node.hypervisor.get.return_value.reset.assert_called_with()
示例#19
0
 def test_shutdown_vm_not_running(self):
     """
     Test shutting down the vm without creation
     """
     with pytest.raises(
             StateCheckError,
             message=
             'Shuting down vm that is not running should raise an error'):
         vm = Vm('vm', data=self.valid_data)
         vm.shutdown()
示例#20
0
 def test_monitor_before_install(self):
     """
     Test monitor before install
     :return:
     """
     with pytest.raises(
             StateCheckError,
             message='monitor vm before install should raise an error'):
         vm = Vm('vm', data=self.valid_data)
         vm._monitor()
示例#21
0
 def test_disable_vnc_before_enable(self):
     """
     Test disable vnc before enable
     :return:
     """
     with pytest.raises(
             StateCheckError,
             message='disable vnc before enable should raise an error'):
         vm = Vm('vm', data=self.valid_data)
         vm.disable_vnc()
示例#22
0
 def test_valid_data(self):
     """
     Test creating a vm service with valid data
     """
     capacity = MagicMock()
     capacity.api.GetCapacity.return_value = (MagicMock(robot_address='url'), None)
     patch('js9.j.clients.grid_capacity.get.return_value', capacity).start()
     vm = Vm('vm', data=self.valid_data)
     vm.validate()
     j.clients.zrobot.get.assert_called_with(self.valid_data['nodeId'], data={'url': 'url'})
     assert vm.data == self.valid_data
示例#23
0
    def test_node_sal(self):
        """
        Test the _node_sal property
        """
        vm = Vm('vm', data=self.valid_data)
        node_sal_return = 'node_sal'
        patch('js9.j.clients.zos.sal.get_node',
              MagicMock(return_value=node_sal_return)).start()

        assert vm._node_sal == node_sal_return
        j.clients.zos.sal.get_node.assert_called_with(NODE_CLIENT)
示例#24
0
 def test_reset_vm_hypervisor_not_created(self):
     """
     Test reset the vm without creation
     """
     with pytest.raises(
             StateCheckError,
             message='Resetting vm before install should raise an error'):
         vm = Vm('vm', data=self.valid_data)
         vm.api.services.get = MagicMock(
             side_effect=scol.ServiceNotFoundError())
         vm.reset()
示例#25
0
 def test_invalid_data(self):
     """
     Test creating a vm with invalid data
     """
     with pytest.raises(
             ValueError,
             message=
             'template should fail to instantiate if data dict is missing the node'
     ):
         vm = Vm(name='vm', data={})
         vm.validate()
示例#26
0
 def test_disable_vnc(self):
     """
     Test disable_vnc when there is a vnc port
     """
     vm = Vm('vm', data=self.valid_data)
     vm.state.set('vnc', 90, 'ok')
     vm.state.set('actions', 'install', 'ok')
     vm.state.delete = MagicMock()
     vm_sal = MagicMock(info={'vnc': 90})
     vm._node_sal.primitives.from_dict.return_value = vm_sal
     vm.disable_vnc()
     vm._vm_sal.disable_vnc.assert_called_with()
示例#27
0
    def test_resume_vm(self):
        """
        Test successfully resume the vm
        """
        vm = Vm('vm', data=self.valid_data)
        vm.state.set('actions', 'pause', 'ok')
        vm.state.delete = MagicMock()
        vm.resume()

        vm._vm_sal.resume.assert_called_with()
        vm.state.check('status', 'running', 'ok')
        vm.state.delete.assert_called_once_with('actions', 'pause')
示例#28
0
    def test_uninstall_vm(self):
        """
        Test successfully destroying the vm
        """
        vm = Vm('vm', data=self.valid_data)
        vm.state.set('actions', 'install', 'ok')
        vm.uninstall()

        vm._vm_sal.destroy.assert_called_with()
        with pytest.raises(StateCheckError):
            vm.state.check('actions', 'install', 'ok')
        with pytest.raises(StateCheckError):
            vm.state.check('status', 'running', 'ok')
示例#29
0
    def test_monitor_vm_not_running_deploy_success(self):
        """
        Test monitor vm not running and deploy fails
        """
        vm = Vm('vm', data=self.valid_data)
        vm._vm_sal.is_running.side_effect = [False, True]
        vm._vm_sal.deploy = MagicMock()
        vm.state.delete = MagicMock()
        vm.state.set('actions', 'install', 'ok')
        vm.state.set('actions', 'start', 'ok')

        vm._monitor()
        vm.state.check("status", "running", "ok")
        vm._vm_sal.deploy.assert_called_once_with()
示例#30
0
    def test_monitor_vm_not_running_deploy_fails(self):
        """
        Test monitor vm not running and deploy fails
        """
        vm = Vm('vm', data=self.valid_data)
        vm._vm_sal.is_running.return_value = False
        vm._vm_sal.deploy = MagicMock()
        vm.state.delete = MagicMock()
        vm.state.set('actions', 'install', 'ok')
        vm.state.set('actions', 'start', 'ok')

        vm._monitor()
        vm.state.delete.assert_called_once_with('status', 'running')
        vm._vm_sal.deploy.assert_called_once_with()