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()
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()
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
def __init__(self, vzId='', vzName='', vzType='VZsolo', vzSize='5g', vzIp='', vzGw='', vzLimits='enovance-nolimit.tpl', vzTemplate='enovance-debian-6.0-amd64', workdir = '.', fix_network = True, vm = None, **kwds): self.boxname = vzName self.basedir = os.path.join('/home/vz/up/', self.boxname, 'DATA/root') self.vz = str(vzId) Vm.__init__(self, root_vm_dir = self.basedir, vm = vm, **kwds) self.vzName = vzName self.vzType = vzType self.vzSize = vzSize self.vzIp = vzIp self.vzGw = vzGw self.vzLimits = vzLimits self.vzTemplate = vzTemplate self.can_snap = False self.fix_network = fix_network self.name = self.boxname # self.name = 'VZ ' + self.boxname self.property_run = self.boxname + '_RUN' self.property_exists = 'VZ_INSTALLED' self.add_to_post_start_hook(self._fix_net)
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
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()
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()
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()
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()
class VmTests(unittest.TestCase): def setUp(self) -> None: self.vm = Vm() @parameterized.expand([ ['+'], ['negate'], ['` negate 1'], ['` ` + 1 2'], ]) def test_parse_exp(self, exp): self.assertEqual(exp, str(self.vm.parse_exp(exp))) @parameterized.expand([ ['+', '+'], ['1', '1'], ['` negate 1', '-1'], ['` ` + 1 2', '3'], ['` ` pair 1 2', '"1 2"'], ['` ` pair 1 emptyList', '[1]'], ['` ` pair 2 ` ` pair 1 emptyList', '[2, 1]'], ['` isEmptyList ` ` pair 1 emptyList', 'F'], ['` isEmptyList emptyList', 'K'], ['` ` K a b', 'a'], ['` ` K a ` 1 2', 'a'], ['` ` F a b', 'b'], ['` ` ` S K b c', 'c'], ['` ` ` B ` + 1 negate 20', '-19'], ['` I 20', '20'], ['` ` ` C + 1 2', '3'], ['` ` + x x', '20', 'x = 10'], ['` head ` ` pair 10 20', '10'], ['` tail ` ` pair 10 20', '20'], [ '` n1035 ` ` pair 10 emptyList', '1', 'n1035 = ` ` S ` ` C isEmptyList 0 ` ` B ` + 1 ` ` B n1035 tail' ], [ '` ` drawClickedPixelProgram emptyList ` ` pair 0 0', '[0, [], [["0 0"]]]', 'drawClickedPixelProgram = ` ` C ` ` B B ` ` B ` B ` pair 0 ` ` C ` ` B B pair ` ` C pair emptyList ` ` C ` ` B pair ` ` C pair emptyList emptyList' ], [ '` ` paintProgram emptyList ` ` pair 0 0', '[0, ["0 0"], [["0 0"]]]', 'paintProgram = ` ` B ` B ` ` S ` ` B ` B ` pair 0 ` ` C ` ` B B pair ` ` C pair emptyList ` ` C pair emptyList ` C pair' ], [ '` ` paintProgram ` ` pair ` ` pair 0 0 emptyList ` ` pair 1 1', '[0, ["1 1", "0 0"], [["1 1", "0 0"]]]', 'paintProgram = ` ` B ` B ` ` S ` ` B ` B ` pair 0 ` ` C ` ` B B pair ` ` C pair emptyList ` ` C pair emptyList ` C pair' ], ]) def test_eval(self, exp, expected, *statements): for statement in statements: self.vm.execute_statement(statement) self.assertEqual(expected, Vm.format(self.vm.eval(self.vm.parse_exp(exp))))
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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
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()
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')
def __init__(self, boxname='', vm=None, **kwargs): self.boxname = boxname self.vm = vm self.is_vm = False Vm.__init__(self, vm = vm, **kwargs) self.can_snap = True self.property_exists = self.name + '_EXI' self.property_run = self.name + '_RUN' # The root VM should snap if self.run_on_vm: self.can_snap = False
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')
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()
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()
def test_monitor_vm_running(self): """ Test monitor vm running """ vm = Vm('vm', data=self.valid_data) vm.state.set('status', 'rebooting', 'ok') vm.state.set('actions', 'install', 'ok') vm.state.set('actions', 'start', 'ok') vm._vm_sal.is_running.return_value = True vm._monitor() vm.state.check('status', 'running', 'ok') with pytest.raises(StateCheckError): vm.state.check('status', 'rebooting', 'ok')
def test_shutdown_vm(self): """ Test successfully shutting down the vm """ vm = Vm('vm', data=self.valid_data) vm.state.set('status', 'running', 'ok') vm.shutdown() vm._vm_sal.shutdown.assert_called_with() with pytest.raises(StateCheckError): vm.state.check("status", 'running', 'ok') with pytest.raises(StateCheckError): vm.state.check("actions", 'start', 'ok')
def test_install_vm(self): """ Test successfully creating a vm """ vm = Vm('vm', data=self.valid_data) data = self.valid_data.copy() data['name'] = vm.name vm_sal = MagicMock(uuid='uuid') vm._node_sal.primitives.from_dict.return_value = vm_sal vm.install() assert 'uuid' == 'uuid' vm._node_sal.primitives.from_dict.called_once_with(data) vm.state.check('actions', 'install', 'ok') vm.state.check('status', 'running', 'ok')
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())
def __init__(self, #: where the vm directory is basedir = '', #: the name used by vagrant init, when the box is on #: the slave. boxname = None, #: the url where the vagrant box is. boxurl = None, #: the url where the vagrant definition is. Only support Git. vagrant_src = None, #: in multivm, we need the machine's name machine = None, #: The name of the vagrantfile vagrantfile = 'Vagrantfile', #: running on a vm? vm = None, **kwargs): self.boxname = boxname self.basedir = os.path.expanduser(basedir) self.vagrantfile = path.join(self.basedir, vagrantfile) self.machine = machine if self.boxname == None and self.machine: self.boxname = self.machine # TODO: improve the logic. box_m = re.match("^[-\w_\d.]+$", self.boxname) if not box_m: raise VmError("The name of the box (%s) can only contain [-\w_\d.]+" % self.boxname) self.vagrant_src = vagrant_src # MRO should kick in both Vm, and Base, and I should use new style class: # http://www.python.org/doc/newstyle/ Vm.__init__(self, root_vm_dir = self.basedir, vm = vm, **kwargs) self.boxurl = boxurl self.can_snap = True #: determine if the "package" steps are executed. self.property_exists = self.name + '_EXI' #: determine if the "install" and "run" steps are executed. self.property_run = self.name + '_RUN' # The root VM should snap if self.run_on_vm: self.can_snap = False