Esempio n. 1
0
    def test_generate_package(self, m_zipfile, m_join):
        """
        Ensures that a package file is created with correct name and location
        """
        # First, create a workspace to give to Packager
        workspace = Workspace("ws/root", ws_name="ws_test", log_level='debug')

        # Create project
        project = Project(workspace, 'prj/path')

        # Instantiate a Packager instance
        packager = Packager(workspace=workspace,
                            project=project,
                            generate_pd=False,
                            dst_path="dst/path")

        packager._package_descriptor = True

        # Prepare mocks
        context_manager_mock = Mock()
        m_zipfile.ZipFile.return_value = context_manager_mock
        enter_mock = Mock()
        exit_mock = Mock()
        setattr(context_manager_mock, '__enter__', enter_mock)
        setattr(context_manager_mock, '__exit__', exit_mock)

        # execute
        packager.generate_package("package_name")

        # make assertions
        self.assertEqual(m_join.call_args_list[-1],
                         mock.call('dst/path', 'package_name.son'))
Esempio n. 2
0
    def test_validate_project_invalid(self):
        """
        Tests the validation of an invalid SONATA project.
        """
        prj_path = os.path.join(SAMPLES_DIR, 'sample_project_invalid')
        project = Project(self._workspace, prj_path)
        validator = Validator(workspace=self._workspace)
        validator.validate_project(project)

        self.assertGreater(validator.error_count, 0)
Esempio n. 3
0
    def test_validate_project_warning(self):
        """
        Tests the validation of a SONATA project with warnings.
        """
        prj_path = os.path.join(SAMPLES_DIR, 'sample_project_warning')
        project = Project(self._workspace, prj_path)
        validator = Validator(workspace=self._workspace)
        validator.validate_project(project)

        self.assertEqual(validator.error_count, 0)
        self.assertGreater(validator.warning_count, 0)
Esempio n. 4
0
    def test_validate_project_valid(self):
        """
        Tests the validation of a valid SONATA project.
        """
        self.reset_counters()
        prj_path = os.path.join(SAMPLES_DIR, 'sample_project_valid')
        project = Project(self._workspace, prj_path)
        validator = Validator(workspace=self._workspace)
        validator.validate_project(project)

        self.assertEqual(val.log.error.counter, 0)
        self.assertEqual(val.log.warning.counter, 0)
Esempio n. 5
0
    def test_package_gds(self):
        """
        Test the validation of the project general description section
        """
        # First, create a workspace to give to Packager
        workspace = Workspace("ws/root", ws_name="ws_test", log_level='debug')

        # Create project
        project = Project(workspace, 'prj/path')

        # Instantiate a Packager instance
        packager = Packager(workspace=workspace,
                            project=project,
                            generate_pd=False,
                            dst_path="dst/path")

        packager._package_descriptor = True

        # Create fake project configuration
        prj_config = {
            'version': '0.5',
            'package': {
                'version': '0.1',
                'name': 'sonata-project-sample',
                'vendor': 'com.sonata.project',
                'maintainer': 'Name, Company, Contact',
                'description': 'Project description',
            },
            'descriptor_extension': 'yml'
        }

        # Remove package keys, one by one...
        for key in prj_config['package']:
            value = prj_config['package'].pop(key)
            self.assertIsNone(packager.package_gds(prj_config))
            prj_config['package'][key] = value

        # Make prj_config complete...
        prj_config['name'] = 'sonata - project - sample'

        self.assertTrue(packager.package_gds(prj_config))
Esempio n. 6
0
 def __init__(self, *args, **kwargs):
     super(IntPDTester, self).__init__(*args, **kwargs)
     ws = Workspace("")
     prj = Project(ws, '/')
     self.pck = Packager(workspace=ws, project=prj, generate_pd=False)
Esempio n. 7
0
def main():
    coloredlogs.install(level='info')

    import argparse

    # specify arguments
    parser = argparse.ArgumentParser(
        description="Validate a SONATA Service. By default it performs a "
        "validation to the syntax, integrity and network "
        "topology.\n",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""Example usage:
        son-validate --project /home/sonata/projects/project_X
                     --workspace /home/sonata/.son-workspace
        son-validate --service ./nsd_file.yml --path ./vnfds/ --dext yml
        son-validate --function ./vnfd_file.yml
        son-validate --function ./vnfds/ --dext yml
        """)

    exclusive_parser = parser.add_mutually_exclusive_group(required=True)

    parser.add_argument(
        "-w",
        "--workspace",
        dest="workspace_path",
        help="Specify the directory of the SDK workspace for validating the "
        "SDK project. If not specified will assume the directory: '{}'".format(
            Workspace.DEFAULT_WORKSPACE_DIR),
        required=False)

    exclusive_parser.add_argument(
        "--project",
        dest="project_path",
        help="Validate the service of the specified SDK project. If "
        "not specified will assume the current directory: '{}'\n".format(
            os.getcwd()),
        required=False)
    exclusive_parser.add_argument(
        "--package",
        dest="package_file",
        help="Validate the specified package descriptor. ")
    exclusive_parser.add_argument(
        "--service",
        dest="nsd",
        help="Validate the specified service descriptor. "
        "The directory of descriptors referenced in the service "
        "descriptor should be specified using the argument '--path'.",
        required=False)
    exclusive_parser.add_argument(
        "--function",
        dest="vnfd",
        help="Validate the specified function descriptor. If a directory is "
        "specified, it will search for descriptor files with extension "
        "defined in '--dext'",
        required=False)
    parser.add_argument(
        "--dpath",
        help="Specify a directory to search for descriptors. Particularly "
        "useful when using the '--service' argument.",
        required=False)
    parser.add_argument(
        "--dext",
        help="Specify the extension of descriptor files. Particularly "
        "useful when using the '--function' argument",
        required=False)
    parser.add_argument("--syntax",
                        "-s",
                        help="Perform a syntax validation.",
                        required=False,
                        action="store_true",
                        default=False)
    parser.add_argument("--integrity",
                        "-i",
                        help="Perform an integrity validation.",
                        required=False,
                        action="store_true",
                        default=False)
    parser.add_argument("--topology",
                        "-t",
                        help="Perform a network topology validation.",
                        required=False,
                        action="store_true",
                        default=False)
    parser.add_argument("--debug",
                        help="sets verbosity level to debug",
                        required=False,
                        action="store_true")

    # parse arguments
    args = parser.parse_args()

    # by default, perform all validations
    if not args.syntax and not args.integrity and not args.topology:
        args.syntax = args.integrity = args.topology = True

    if args.package_file:
        if not os.path.isfile(args.package_file):
            log.error("Provided package is not a valid file")
            exit(1)

        if args.workspace_path:
            ws_root = args.workspace_path
        else:
            ws_root = Workspace.DEFAULT_WORKSPACE_DIR

        # Obtain Workspace object
        workspace = Workspace.__create_from_descriptor__(ws_root)
        if not workspace:
            log.error("Invalid workspace path: '%s'\n" % ws_root)
            exit(1)

        validator = Validator(workspace=workspace)
        validator.configure(syntax=args.syntax,
                            integrity=args.integrity,
                            topology=args.topology,
                            debug=args.debug)

        result = validator.validate_package(args.package_file)
        print_result(validator, result)

    elif args.project_path:

        if args.workspace_path:
            ws_root = args.workspace_path
        else:
            ws_root = Workspace.DEFAULT_WORKSPACE_DIR

        prj_root = args.project_path if args.project_path else os.getcwd()

        # Obtain Workspace object
        workspace = Workspace.__create_from_descriptor__(ws_root)
        if not workspace:
            log.error("Invalid workspace path: '%s'\n" % ws_root)
            exit(1)

        project = Project.__create_from_descriptor__(workspace, prj_root)
        if not project:
            log.error("Invalid project path: '%s'\n  " % prj_root)
            exit(1)

        validator = Validator(workspace=workspace)
        validator.configure(syntax=args.syntax,
                            integrity=args.integrity,
                            topology=args.topology,
                            debug=args.debug)

        result = validator.validate_project(project)
        print_result(validator, result)

    elif args.nsd:
        validator = Validator()
        validator.configure(dpath=args.dpath,
                            dext=args.dext,
                            syntax=args.syntax,
                            integrity=args.integrity,
                            topology=args.topology,
                            debug=args.debug)

        result = validator.validate_service(args.nsd)
        print_result(validator, result)

    elif args.vnfd:
        validator = Validator()
        validator.configure(dext=args.dext,
                            syntax=args.syntax,
                            integrity=args.integrity,
                            topology=args.topology,
                            debug=args.debug)

        result = validator.validate_function(args.vnfd)
        print_result(validator, result)

    else:
        log.error("Invalid arguments.")
        exit(1)

    exit(0)
Esempio n. 8
0
def main():
    coloredlogs.install(level='info')

    import argparse

    # specify arguments
    parser = argparse.ArgumentParser(
        description="Validate a SONATA Service. By default it performs a "
                    "validation to the syntax, integrity and network "
                    "topology.\n",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""Example usage:
        son-validate --project /home/sonata/projects/project_X
                     --workspace /home/sonata/.son-workspace
        son-validate --service ./nsd_file.yml --path ./vnfds/ --dext yml
        son-validate --function ./vnfd_file.yml
        son-validate --function ./vnfds/ --dext yml
        """
    )

    exclusive_parser = parser.add_mutually_exclusive_group(
        required=True
    )

    parser.add_argument(
        "-w", "--workspace",
        dest="workspace_path",
        help="Specify the directory of the SDK workspace for validating the "
             "SDK project. If not specified will assume the directory: '{}'"
             .format(Workspace.DEFAULT_WORKSPACE_DIR),
        required=False
    )

    exclusive_parser.add_argument(
        "--project",
        dest="project_path",
        help="Validate the service of the specified SDK project. If "
             "not specified will assume the current directory: '{}'\n"
             .format(os.getcwd()),
        required=False
    )
    exclusive_parser.add_argument(
        "--package",
        dest="package_file",
        help="Validate the specified package descriptor. "
    )
    exclusive_parser.add_argument(
        "--service",
        dest="nsd",
        help="Validate the specified service descriptor. "
             "The directory of descriptors referenced in the service "
             "descriptor should be specified using the argument '--path'.",
        required=False
    )
    exclusive_parser.add_argument(
        "--function",
        dest="vnfd",
        help="Validate the specified function descriptor. If a directory is "
             "specified, it will search for descriptor files with extension "
             "defined in '--dext'",
        required=False
    )
    parser.add_argument(
        "--dpath",
        help="Specify a directory to search for descriptors. Particularly "
             "useful when using the '--service' argument.",
        required=False
    )
    parser.add_argument(
        "--dext",
        help="Specify the extension of descriptor files. Particularly "
             "useful when using the '--function' argument",
        required=False
    )
    parser.add_argument(
        "--syntax", "-s",
        help="Perform a syntax validation.",
        required=False,
        action="store_true",
        default=False
    )
    parser.add_argument(
        "--integrity", "-i",
        help="Perform an integrity validation.",
        required=False,
        action="store_true",
        default=False
    )
    parser.add_argument(
        "--topology", "-t",
        help="Perform a network topology validation.",
        required=False,
        action="store_true",
        default=False
    )
    parser.add_argument(
        "--debug",
        help="sets verbosity level to debug",
        required=False,
        action="store_true")

    # parse arguments
    args = parser.parse_args()

    # by default, perform all validations
    if not args.syntax and not args.integrity and not args.topology:
        args.syntax = args.integrity = args.topology = True

    if args.package_file:
        if not os.path.isfile(args.package_file):
            log.error("Provided package is not a valid file")
            exit(1)

        if args.workspace_path:
            ws_root = args.workspace_path
        else:
            ws_root = Workspace.DEFAULT_WORKSPACE_DIR

        # Obtain Workspace object
        workspace = Workspace.__create_from_descriptor__(ws_root)
        if not workspace:
            log.error("Invalid workspace path: '%s'\n" % ws_root)
            exit(1)

        validator = Validator(workspace=workspace)
        validator.configure(syntax=args.syntax,
                            integrity=args.integrity,
                            topology=args.topology,
                            debug=args.debug)

        if not validator.validate_package(args.package_file):
            log.critical("Package validation has failed.")
            exit(1)
        if validator.warning_count == 0:
            log.info("Validation of package '{0}' has succeeded."
                     .format(args.package_file))
        else:
            log.warning("Validation of package '{0}' returned {1} warning(s)"
                        .format(args.package_file,
                                validator.warning_count))

        print(validator.error_count)

    elif args.project_path:

        if args.workspace_path:
            ws_root = args.workspace_path
        else:
            ws_root = Workspace.DEFAULT_WORKSPACE_DIR

        prj_root = args.project_path if args.project_path else os.getcwd()

        # Obtain Workspace object
        workspace = Workspace.__create_from_descriptor__(ws_root)
        if not workspace:
            log.error("Invalid workspace path: '%s'\n" % ws_root)
            exit(1)

        project = Project.__create_from_descriptor__(workspace, prj_root)
        if not project:
            log.error("Invalid project path: '%s'\n  " % prj_root)
            exit(1)

        validator = Validator(workspace=workspace)
        validator.configure(syntax=args.syntax,
                            integrity=args.integrity,
                            topology=args.topology,
                            debug=args.debug)

        if not validator.validate_project(project):
            log.critical("Project validation has failed.")
            exit(1)
        if validator.warning_count == 0:
            log.info("Validation of project '{0}' has succeeded."
                     .format(project.project_root))
        else:
            log.warning("Validation of project '{0}' returned {1} warning(s)"
                        .format(project.project_root,
                                validator.warning_count))

    elif args.nsd:
        validator = Validator()
        validator.configure(dpath=args.dpath, dext=args.dext,
                            syntax=args.syntax,
                            integrity=args.integrity,
                            topology=args.topology,
                            debug=args.debug)

        if not validator.validate_service(args.nsd):
            log.critical("Project validation has failed.")
            exit(1)
        if validator.warning_count == 0:
            log.info("Validation of service '{0}' has succeeded."
                     .format(args.nsd))
        else:
            log.warning("Validation of service '{0}' returned {1} warning(s)"
                        .format(args.nsd, validator.warning_count))

    elif args.vnfd:
        validator = Validator()
        validator.configure(dext=args.dext,
                            syntax=args.syntax,
                            integrity=args.integrity,
                            topology=args.topology,
                            debug=args.debug)

        if not validator.validate_function(args.vnfd):
            log.critical("Function validation has failed.")
            exit(1)
        if validator.warning_count == 0:
            log.info("Validation of function '{0}' has succeeded."
                     .format(args.vnfd))
        else:
            log.warning("Validation of function '{0}' returned {1} warning(s)"
                        .format(args.vnfd, validator.warning_count))
    else:
        log.error("Provided arguments are invalid.")
        exit(1)

    log.info("Done.")

    exit(0)