def execute_with_machine_output(self, cmd_args): """ Execute the given cmd_args command and adds the machine readable flag. Returns the parsed json output. """ LinstorTestCase.add_controller_arg(cmd_args) linstor_cli = linstor_client_main.LinStorCLI() backupstd = sys.stdout jout = None try: sys.stdout = StringIO() retcode = linstor_cli.parse_and_execute(["-m"] + cmd_args) self.assertEqual(0, retcode) finally: stdval = sys.stdout.getvalue() sys.stdout.close() sys.stdout = backupstd if stdval: try: jout = json.loads(stdval) except ValueError as ve: sys.stderr.write("Could not parse: {j}\n".format(j=stdval)) raise ve self.assertIsInstance(jout, list) else: sys.stderr.write(str(cmd_args) + " Result empty") return jout
def execute(cls, cmd_args): LinstorTestCase.add_controller_arg(cmd_args) linstor_cli = linstor_client_main.LinStorCLI() try: return linstor_cli.parse_and_execute(cmd_args) except SystemExit as e: return e.code
def test_resource_completer(self): jout = self.execute_with_machine_output( ['resource-definition', 'list']) resources = self.get_list('rsc_dfns', jout) linstor_cli = linstor_client_main.LinStorCLI() linstor_cli._node_commands.get_linstorapi( parsed_args=_FakeArgs(self.host() + ':' + str(self.port()))) cmpl_resources = linstor_cli._node_commands.resource_completer("") self.assertEqual(len(resources), len(cmpl_resources))
def test_storpool_completer(self): jout = self.execute_with_machine_output( ['storage-pool-definition', 'list']) stor_pools = self.get_list('stor_pool_dfns', jout) linstor_cli = linstor_client_main.LinStorCLI() linstor_cli._node_commands.get_linstorapi( parsed_args=_FakeArgs(self.host() + ':' + str(self.port()))) cmpl_stor_pools = linstor_cli._node_commands.storage_pool_completer("") self.assertEqual(len(stor_pools), len(cmpl_stor_pools)) cmpl_stor_pools = linstor_cli._node_commands.storage_pool_completer( "zfs") self.assertEqual(1, len(cmpl_stor_pools))
def test_node_completer(self): jout = self.execute_with_machine_output(['node', 'list']) nodes = self.get_list('nodes', jout) linstor_cli = linstor_client_main.LinStorCLI() linstor_cli._node_commands.get_linstorapi( parsed_args=_FakeArgs(self.host() + ':' + str(self.port()))) cmpl_nodes = linstor_cli._node_commands.node_completer("") self.assertEqual(len(nodes), len(cmpl_nodes)) cmpl_nodes = linstor_cli._node_commands.node_completer("fakem") self.assertEqual(1, len(cmpl_nodes)) cmpl_nodes = linstor_cli._node_commands.node_completer("fakeh") self.assertEqual(3, len(cmpl_nodes))
def execute_with_text_output(self, cmd_args): """ Execute the given cmd_args command and adds the machine readable flag. Returns the parsed json output. """ LinstorTestCase.add_controller_arg(cmd_args) linstor_cli = linstor_client_main.LinStorCLI() backupstd = sys.stdout try: sys.stdout = StringIO() retcode = linstor_cli.parse_and_execute(["--no-utf8", "--no-color"] + cmd_args) self.assertEqual(0, retcode) finally: stdval = sys.stdout.getvalue() sys.stdout.close() sys.stdout = backupstd return stdval
def test_netifs_completer(self): jout = self.execute_with_machine_output( ['node', 'interface', 'list', 'fakehost1']) nodes = self.get_list('nodes', jout) netifs = [ x for n in nodes if n['name'] == 'fakehost1' for x in n['net_interfaces'] ] linstor_cli = linstor_client_main.LinStorCLI() args = _FakeArgs(self.host() + ':' + str(self.port())) args.node_name = 'fakehost1' linstor_cli._node_commands.get_linstorapi(parsed_args=args) cmpl_netifs = linstor_cli._node_commands.netif_completer( "", parsed_args=args) self.assertEqual(len(netifs), len(cmpl_netifs)) cmpl_netifs = linstor_cli._node_commands.netif_completer( "def", parsed_args=args) self.assertEqual(1, len(cmpl_netifs))
def parse_args(cls, cmd_args): LinstorTestCase.add_controller_arg(cmd_args) linstor_cli = linstor_client_main.LinStorCLI() return linstor_cli.parse(cmd_args)
def test_main_commands(self): cli = linstor_client_main.LinStorCLI() cli.check_parser_commands()