Beispiel #1
0
def test_create_extension_invalid_image_hash(fx_copy_fn_main_mock_integration):

    mock_image_hash = "xxx"

    path_fn_main_mock_integration = fx_copy_fn_main_mock_integration[1]

    path_setup_py_file = os.path.join(path_fn_main_mock_integration,
                                      package_helpers.BASE_NAME_SETUP_PY)
    path_apiky_permissions_file = os.path.join(
        path_fn_main_mock_integration,
        package_helpers.BASE_NAME_APIKEY_PERMS_FILE)
    output_dir = os.path.join(path_fn_main_mock_integration,
                              package_helpers.BASE_NAME_DIST_DIR)

    use_setuptools.run_setup(setup_script=path_setup_py_file,
                             args=["sdist", "--formats=gztar"])

    with pytest.raises(
            SDKException,
            match=
            r"image_hash 'xxx' is not a valid SHA256 hash\nIt must be a valid hexadecimal and 64 characters long"
    ):
        package_helpers.create_extension(path_setup_py_file,
                                         path_apiky_permissions_file,
                                         output_dir,
                                         image_hash=mock_image_hash)
Beispiel #2
0
def test_create_extension_image_hash(fx_copy_fn_main_mock_integration):

    mock_image_hash = "dd2a1678b6e0fd1d1a1313f78785fd0c4fad0565ac9008778bdb3b00bdff4420"

    path_fn_main_mock_integration = fx_copy_fn_main_mock_integration[1]

    path_setup_py_file = os.path.join(path_fn_main_mock_integration,
                                      package_helpers.BASE_NAME_SETUP_PY)
    path_apiky_permissions_file = os.path.join(
        path_fn_main_mock_integration,
        package_helpers.BASE_NAME_APIKEY_PERMS_FILE)
    output_dir = os.path.join(path_fn_main_mock_integration,
                              package_helpers.BASE_NAME_DIST_DIR)

    use_setuptools.run_setup(setup_script=path_setup_py_file,
                             args=["sdist", "--formats=gztar"])

    path_app_zip = package_helpers.create_extension(
        path_setup_py_file,
        path_apiky_permissions_file,
        output_dir,
        image_hash=mock_image_hash)
    app_json = json.loads(sdk_helpers.read_zip_file(path_app_zip, "app.json"))

    assert app_json.get("current_installation", {}).get(
        "executables", []
    )[0].get(
        "image", ""
    ) == "ibmresilient/fn_main_mock_integration@sha256:dd2a1678b6e0fd1d1a1313f78785fd0c4fad0565ac9008778bdb3b00bdff4420"
Beispiel #3
0
def test_create_extension_display_name(fx_copy_fn_main_mock_integration):

    path_fn_main_mock_integration = fx_copy_fn_main_mock_integration[1]

    path_setup_py_file = os.path.join(path_fn_main_mock_integration, package_helpers.PATH_SETUP_PY)
    path_apiky_permissions_file = os.path.join(path_fn_main_mock_integration, package_helpers.BASE_NAME_APIKEY_PERMS_FILE)
    output_dir = os.path.join(path_fn_main_mock_integration, package_helpers.BASE_NAME_DIST_DIR)

    use_setuptools.run_setup(setup_script=path_setup_py_file, args=["sdist", "--formats=gztar"])

    path_app_zip = package_helpers.create_extension(path_setup_py_file, path_apiky_permissions_file, output_dir)
    app_json = json.loads(sdk_helpers.read_zip_file(path_app_zip, "app.json"))

    assert app_json.get("display_name") == "Main Mock Integration"
    def execute_command(self, args):
        """
        Function that creates The App.zip file from the give source path and returns
        the path to the new App.zip

        :param args: Arguments from command line:

            -  **args.package**: path to directory that must include a setup.py, customize.py and config.py file.
            -  **args.cmd**: `package` in this case
            -  **args.display_name**: will give the App that display name. Default: name from setup.py file
            -  **args.repository_name**: if defined, it will replace the default image repository name in app.json for
                                         container access.
            -  **args.keep_build_dir**: if defined, dist/build/ will not be removed.
        :type args: argparse Namespace

        :return: Path to new app.zip
        :rtype: str
        """
        # Set name for SDKException
        SDKException.command_ran = self.CMD_NAME

        # Get absolute path_to_src
        path_to_src = os.path.abspath(args.package)

        # Get basename of path_to_src (version information is stripped from the basename).
        path_to_src_basename = re.split(VERSION_REGEX,
                                        os.path.basename(path_to_src), 1)[0]

        LOG.debug("Path to project: %s", path_to_src)
        LOG.debug("Project basename: %s", path_to_src_basename)

        # Ensure the src directory exists and we have WRITE access
        sdk_helpers.validate_dir_paths(os.W_OK, path_to_src)

        # Generate paths to files required to create app
        path_setup_py_file = os.path.join(path_to_src, BASE_NAME_SETUP_PY)
        path_docker_file = os.path.join(path_to_src, BASE_NAME_DOCKER_FILE)
        path_entry_point = os.path.join(path_to_src, BASE_NAME_ENTRY_POINT)
        path_apikey_permissions_file = os.path.join(
            path_to_src, BASE_NAME_APIKEY_PERMS_FILE)
        path_output_dir = os.path.join(path_to_src, BASE_NAME_DIST_DIR)
        path_extension_logo = os.path.join(path_to_src,
                                           PATH_ICON_EXTENSION_LOGO)
        path_company_logo = os.path.join(path_to_src, PATH_ICON_COMPANY_LOGO)

        LOG.info("Built Distribution starting\n")

        # Create the built distribution
        use_setuptools.run_setup(setup_script=path_setup_py_file,
                                 args=["sdist", "--formats=gztar"])

        LOG.info("\nBuilt Distribution finished. See: %s", path_output_dir)

        # Check that files 'Dockerfile' and 'entrypoint.sh' files exist in the integration package
        # before attempting to create the app.
        sdk_helpers.validate_file_paths(os.R_OK, path_docker_file,
                                        path_entry_point)

        # Create the app
        path_the_extension_zip = create_extension(
            path_setup_py_file=path_setup_py_file,
            path_apikey_permissions_file=path_apikey_permissions_file,
            output_dir=path_output_dir,
            custom_display_name=args.display_name,
            repository_name=args.repository_name,
            keep_build_dir=args.keep_build_dir,
            path_extension_logo=path_extension_logo,
            path_company_logo=path_company_logo)

        LOG.info("App created at: %s", path_the_extension_zip)

        return path_the_extension_zip
    def execute_command(self, args):
        """
        Function that creates The App.zip file from the give source path and returns
        the path to the new App.zip

        :param args: Arguments from command line:

            -  **args.package**: path to directory that must include a setup.py, customize.py and config.py file.
            -  **args.cmd**: `package` in this case
            -  **args.display_name**: will give the App that display name. Default: name from setup.py file
            -  **args.repository_name**: if defined, it will replace the default image repository name in app.json for
                                         container access.
            -  **args.keep_build_dir**: if defined, dist/build/ will not be removed.
            -  **args.no_samples**: if defined, set path_payload_samples to None.
            -  **args.validate**: if defined, run ``validate`` and save report in packaged app.
        :type args: argparse Namespace

        :return: Path to new app.zip
        :rtype: str
        """
        # Set name for SDKException
        SDKException.command_ran = self.CMD_NAME

        # Get absolute path_to_src
        path_to_src = os.path.abspath(args.package)

        LOG.debug("\nPath to project: %s", path_to_src)

        # Ensure the src directory exists and we have WRITE access
        sdk_helpers.validate_dir_paths(os.W_OK, path_to_src)

        # Generate path to setup.py file
        path_setup_py_file = os.path.join(path_to_src,
                                          package_helpers.BASE_NAME_SETUP_PY)

        # Ensure we have read permissions for setup.py
        sdk_helpers.validate_file_paths(os.R_OK, path_setup_py_file)

        # Parse the setup.py file
        setup_py_attributes = package_helpers.parse_setup_py(
            path_setup_py_file,
            package_helpers.SUPPORTED_SETUP_PY_ATTRIBUTE_NAMES)

        LOG.debug("\nProject name: %s",
                  setup_py_attributes.get("name", "unknown"))

        # Generate paths to files required to create app
        path_docker_file = os.path.join(path_to_src,
                                        package_helpers.BASE_NAME_DOCKER_FILE)
        path_entry_point = os.path.join(path_to_src,
                                        package_helpers.BASE_NAME_ENTRY_POINT)
        path_apikey_permissions_file = os.path.join(
            path_to_src, package_helpers.BASE_NAME_APIKEY_PERMS_FILE)
        path_output_dir = os.path.join(path_to_src,
                                       package_helpers.BASE_NAME_DIST_DIR)
        path_extension_logo = os.path.join(
            path_to_src, package_helpers.PATH_ICON_EXTENSION_LOGO)
        path_company_logo = os.path.join(
            path_to_src, package_helpers.PATH_ICON_COMPANY_LOGO)
        path_payload_samples = os.path.join(
            path_to_src, package_helpers.BASE_NAME_PAYLOAD_SAMPLES_DIR)

        # if --no-samples flag, set path_payload_samples to None
        if args.no_samples:
            path_payload_samples = None

        # if --validate flag is set, run validate command
        # else set the path to the file if it exists or None if doesn't exist
        if args.validate:
            LOG.info(
                "Validation on {0} is starting. \nTo skip, run the 'package' command without the '--validate' flag.\nValidations can be executated separately by running: \n  'resilient-sdk validate -p {0}' \nto see more in-depth results.\n"
                .format(args.package))

            validate_args = self.cmd_validate.parser.parse_known_args()[0]

            path_validate_report = self.cmd_validate.execute_command(
                validate_args, output_suppressed=True, run_from_package=True)
        else:
            path_validate_report = package_helpers.check_validate_report_exists(
            )

        # Ensure the 'Dockerfile' and 'entrypoint.sh' files exist and we have READ access
        sdk_helpers.validate_file_paths(os.R_OK, path_docker_file,
                                        path_entry_point)

        LOG.info("\nBuild Distribution starting\n")

        # Create the build distribution
        use_setuptools.run_setup(setup_script=path_setup_py_file,
                                 args=["sdist", "--formats=gztar"])

        LOG.info("\nBuild Distribution finished. See: %s", path_output_dir)

        # Create the app
        path_the_extension_zip = package_helpers.create_extension(
            path_setup_py_file=path_setup_py_file,
            path_apikey_permissions_file=path_apikey_permissions_file,
            output_dir=path_output_dir,
            custom_display_name=args.display_name,
            repository_name=args.repository_name,
            keep_build_dir=args.keep_build_dir,
            path_extension_logo=path_extension_logo,
            path_company_logo=path_company_logo,
            path_payload_samples=path_payload_samples,
            path_validate_report=path_validate_report,
            image_hash=args.image_hash)

        LOG.info("App created at: %s", path_the_extension_zip)

        return path_the_extension_zip