Exemplo n.º 1
0
 def generate_code(self):
     self._imports.append(
         PyImport("Module", _from="dent_os_testbed.discovery.Module "))
     self._imports.append(
         PyImport(
             camelcase(self._cls.name),
             _from="dent_os_testbed.lib.%s." % (self._cls._mod.name) +
             self._cls.name + " ",
         ))
     methods = []
     methods.extend(self.generate_mbr_set_attr(self._cls))
     args = self._cls.to_dict()
     args['cname_cc'] = camelcase(self._cls.name)
     args['parent'] = self._parent
     discover_body = tokenize(discover_py_code_template % args)
     methods.append(
         PyMethod("discover",
                  "self",
                  discover_body,
                  indent=4,
                  coroutine=True))
     self._classes.append(
         PyClass(camelcase(self._cls.name) + "Mod",
                 parent="Module",
                 methods=methods))
Exemplo n.º 2
0
    def generate_code(self):
        self._imports.append(PyImport("io"))
        self._imports.append(PyImport("json"))
        self._imports.append(PyImport("copy"))
        self._imports.append(
            PyImport("(LeafSchemaDict, SchemaList, SchemaDict, Report)",
                     _from="dent_os_testbed.discovery.Report "))

        # recurse the level from base.

        # discard other accessors
        methods = [PyLines(lines=tokenize(discovery_py_report_body))]
        self._classes.extend(
            self.generate_schema_classes(
                self._pkg.modules['base'].classes_dct['base'], "Report"))

        self._classes.append(
            PyClass("ReportSchema", parent="Report", methods=methods))
Exemplo n.º 3
0
 def generate_code(self):
     args = self._cls.to_dict()
     args["cname_cc"] = camelcase(self._cls.name)
     self._imports.append(PyImport("asyncio"))
     self._imports.append(PyImport("TestDevice", _from=".utils "))
     self._imports.append(
         PyImport(
             "%(cname_cc)s " % args,
             _from="dent_os_testbed.lib.%s." % (self._cls._mod.name) +
             self._cls.name + " ",
         ))
     for api in self._cls.apis:
         args["api"] = api
         test_body_call = ""
         for impl in self._cls.implemented_by:
             for cmd in impl.commands:
                 if api not in cmd.apis: continue
                 args["params1"] = ''
                 args["params2"] = ''
                 for m in cmd.params:
                     if isinstance(m, str):
                         continue
                     # ignore if its readonly
                     if m.readonly:
                         continue
                     args["params1"] += "        '%s':%s,\n" % (
                         m.name, self.get_random_value(m))
                     args["params2"] += "        '%s':%s,\n" % (
                         m.name, self.get_random_value(m))
                 test_body_call += py_test_code_two_cmd_template_call % args
                 test_body_call += py_test_code_two_dev_template_call % args
                 break
         args["platform"] = self._cls.implemented_by[0].platforms[0]
         args["cases"] = test_body_call
         test_body = tokenize(py_test_code_template % args)
         self._methods.append(
             PyMethod("test_that_%(cls_name)s_%(api)s" % args, "capfd",
                      test_body))
Exemplo n.º 4
0
    def generate_code(self):
        args = self._cls.to_dict()
        args["cname_cc"] = camelcase(self._cls.name)
        self._imports.append(
            PyImport(
                "%(cname_cc)s " % args,
                _from="dent_os_testbed.lib.%s.%s." %
                (self._cls._mod.name, self._platform) + self._cls.name + " ",
            ))
        methods = []
        # add format_cmd method
        entries = ""
        for cmd in self._cls.commands:
            # add the methods to handle the generation
            cargs = cmd.to_dict()
            format_cmd_body = tokenize(py_impl_class_common_format_cmd % cargs)
            methods.append(
                PyMethod(
                    "format_%s" % cmd.name,
                    "self, command, *argv, **kwarg",
                    format_cmd_body,
                    indent=4,
                ))
            if self._cls.implements.local:
                run_cmd_body = tokenize(py_impl_class_common_run_cmd % cargs)
                methods.append(
                    PyMethod(
                        "run_%s" % cmd.name,
                        "self, device, command, *argv, **kwarg",
                        run_cmd_body,
                        indent=4,
                    ))

            parse_cmd_body = tokenize(py_impl_class_common_parse_cmd % cargs)
            methods.append(
                PyMethod(
                    "parse_%s" % cmd.name,
                    "self, command, output, *argv, **kwarg",
                    format_cmd_body,
                    indent=4,
                ))
        self._classes.append(
            PyClass(
                camelcase(self._cls.name + "Impl"),
                desc=[PyLines(lines=tokenize(self._cls.desc, indent=8))],
                parent=camelcase(self._cls.name),
                methods=methods,
            ))
Exemplo n.º 5
0
 def generate_code(self):
     args = self._test.to_dict()
     self._imports.append(PyImport("pytest"))
     self._imports.append(PyImport("time"))
     self._imports.append(PyImport("re"))
     self._imports.append(PyImport("TestCaseSetup", _from="dent_os_testbed.utils.decorators "))
     imported = {}
     classes = ''
     for tc in self._test.test_cases:
         targs = {**args, **tc.to_dict()}
         if tc.cls:
             targs = {**targs, **tc.cls.to_dict()}
             # if there is an class then do an import if not done.
             if tc.cls.implemented_by and tc.cls.name not in imported:
                 self._imports.append(
                     PyImport(
                         "%(cls_cc_name)s " % targs,
                         _from="dent_os_testbed.lib.%s."
                         % (tc.cls._mod.name)
                         + tc.cls.name
                         + " ",
                     )
                 )
                 imported[tc.cls.name] = True
         if tc.template not in imported:
             self._imports.append(
                 PyImport(
                     "%sBase, %sMeta "%(tc.template, tc.template),
                     _from="dent_os_testbed.test.lib.%s " %(tc.template)
                 )
             )
             imported[tc.template] = True
         if tc.args:
             tc_args = json.loads(tc.args)
             methods = []
             for k in tc_args.keys():
                 methods.append(
                     PyMethod(k, "obj=None", ["return "+tc_args[k]], indent=4)
                 )
             cls = camelcase(tc.cls.name)+tc.template+"Meta"
             self._classes.append(
                 PyClass(cls,
                         desc=[PyLines(lines=[])],
                         parent=("%sMeta"%tc.template),
                         methods=methods)
             )
             self._classes.append(
                 PyClass(camelcase(tc.cls.name)+tc.template,
                         desc=[PyLines(lines=[])],
                         parent=("%sBase"%tc.template),
                         methods=[PyLines(
                             lines=["    meta="+
                                    camelcase(tc.cls.name)+
                                    tc.template+
                                    "Meta"])]
                 )
             )
             classes += camelcase(tc.cls.name)+tc.template + ','
         #self._methods.append(PyLines(lines=tokenize(self._templates[tc.template]._data%targs)))
     self._methods.append(PyLines(lines=["@pytest.fixture(params=[%s])"%classes]))
     self._methods.append(PyMethod(self._test.name+'_class', "request",  ["return request.param"]))
     self._methods.append(PyLines(lines=["@pytest.mark.asyncio"]))
     self._methods.append(PyMethod('test_'+self._test.name, "testbed, "+self._test.name+'_class', ['await ' + self._test.name+'_class'+'().run_test(testbed)'], coroutine=True))
     self._imports.append(PyLines(lines=['pytestmark = pytest.mark.suite_basic_trigger']))
Exemplo n.º 6
0
 def generate_code(self):
     args = self._cls.to_dict()
     args["cname_cc"] = camelcase(self._cls.name)
     self._imports.append(PyImport("pytest"))
     self._imports.append(
         PyImport("TestLibObject",
                  _from="dent_os_testbed.lib.test_lib_object "))
     for impl in self._cls.implemented_by:
         pd_impl = ""
         if "dentos" in impl.platforms:
             args['pd_impl_cc'] = '%sImpl' % camelcase(impl.name)
             args['pd_impl'] = 'linux'
         elif "ixnetwork" in impl.platforms:
             args['pd_impl_cc'] = '%sImpl' % camelcase(impl.name)
             args['pd_impl'] = 'ixnetwork'
         else:
             continue
         self._imports.append(
             PyImport(
                 "%(pd_impl_cc)s " % args,
                 _from="dent_os_testbed.lib.%(cls_mod_name)s.%(pd_impl)s." %
                 (args) + impl.name + "_impl ",
             ))
     # generate the init routine
     methods = []
     impl_form_cmd = ''
     for obj in self._cls.implemented_by:
         iargs = {
             'cname_cc': camelcase(obj.name),
             'platforms': obj.platforms,
         }
         impl_form_cmd += py_class_common_impl_form_command % iargs
     args['impl_form_cmd'] = impl_form_cmd
     if self._cls.local:
         args[
             'invoke_command'] = 'rc, output = impl_obj.run_command(device_obj, command=api, params=device[device_name])'
     else:
         args[
             'invoke_command'] = 'rc, output = await device_obj.run_cmd(("sudo " if device_obj.ssh_conn_params.pssh else "") + commands)'
     args['local'] = 'impl' if self._cls.local else 'device'
     run_body = tokenize(py_class_common_run % args),
     methods.append(
         PyMethod("_run_command",
                  "api, *argv, **kwarg",
                  run_body[0],
                  indent=4,
                  coroutine=True))
     for api in self._cls.apis:
         args['cls_api'] = api
         params = ''
         platforms = ''
         cmd_desc = ''
         for impl in self._cls.implemented_by:
             platforms = impl.platforms
             for cmd in impl.commands:
                 if api in cmd.apis:
                     cmd_desc = cmd.desc
                     for p in cmd.params:
                         if not isinstance(p, str):
                             params += "                '%s':'%s',\n" % (
                                 p.name, p.type)
                         else:
                             params += "                '%s':'undefined',\n" % (
                                 p)
                     break
         args['params'] = params[:-1]
         args['platforms'] = platforms
         args['cmd_desc'] = cmd_desc
         run_body = tokenize(py_class_common_run_api % args),
         methods.append(
             PyMethod(api.lower(),
                      "*argv, **kwarg",
                      run_body[0],
                      indent=4,
                      coroutine=True))
     # discard other accessors
     self._classes = [
         PyClass(camelcase(self._cls.name),
                 desc=[PyLines(lines=tokenize(self._cls.desc, indent=8))],
                 parent="TestLibObject",
                 methods=methods)
     ]
Exemplo n.º 7
0
    def generate_code(self):
        self._imports.append(
            PyImport("TestLibObject",
                     _from="dent_os_testbed.lib.test_lib_object "))
        args = self._cls.to_dict()
        args["cname_cc"] = camelcase(self._cls.name)
        methods = []
        # add format_cmd method
        format_entries = ""
        run_entries = ""
        parse_entries = ""
        need_run = False
        for cmd in self._cls.commands:
            # add the methods to handle the generation
            cargs = cmd.to_dict()
            format_cmd_body = tokenize(py_class_common_format_cmd % cargs)
            methods.append(
                PyMethod(
                    "format_%s" % cmd.name,
                    "self, command, *argv, **kwarg",
                    format_cmd_body,
                    indent=4,
                ))
            if self._cls.implements.local:
                need_run = True
                run_cmd_body = tokenize(py_class_common_run_cmd % cargs)
                methods.append(
                    PyMethod(
                        "run_%s" % cmd.name,
                        "self, device, command, *argv, **kwarg",
                        run_cmd_body,
                        indent=4,
                    ))
            parse_cmd_body = tokenize(py_class_common_parse_cmd % cargs)
            methods.append(
                PyMethod(
                    "parse_%s" % cmd.name,
                    "self, command, output, *argv, **kwarg",
                    parse_cmd_body,
                    indent=4,
                ))
            args['cmds'] = cmd.apis
            args['cmd'] = cmd.name
            format_entries += py_class_common_format_cmd_case % (args)
            run_entries += py_class_common_run_cmd_case % (args)
            parse_entries += py_class_common_parse_cmd_case % (args)
        args["format_entries"] = format_entries
        args["run_entries"] = run_entries if need_run else ""
        args["parse_entries"] = parse_entries
        format_cmd_body = tokenize(py_class_common_format_cmds % args)
        methods.append(
            PyMethod("format_command",
                     "self, command, *argv, **kwarg",
                     format_cmd_body,
                     indent=4))
        if need_run:
            run_cmd_body = tokenize(py_class_common_run_cmds % args)
            methods.append(
                PyMethod("run_command",
                         "self, device_obj, command, *argv, **kwarg",
                         run_cmd_body,
                         indent=4))

        parse_cmd_body = tokenize(py_class_common_parse_cmds % args)
        methods.append(
            PyMethod("parse_output",
                     "self, command, output, *argv, **kwarg",
                     parse_cmd_body,
                     indent=4))
        self._classes.append(
            PyClass(camelcase(self._cls.name),
                    desc=[PyLines(lines=tokenize(self._cls.desc, indent=8))],
                    parent="TestLibObject",
                    methods=methods))