コード例 #1
0
    def test_init(tmpdir, ingestion_strategy, schema_template, plugin_name,
                  format_entry_point_template):
        # Initialize an empty directory.
        init.init(tmpdir.strpath, ingestion_strategy, plugin_name)

        # Validate the config file is as we expect.
        result = plugin_util.read_and_validate_plugin_config_file(
            os.path.join(tmpdir.strpath, init.DEFAULT_PLUGIN_CONFIG_FILE),
            True, False)

        config = result.plugin_config_content

        assert config['pluginType'] == ingestion_strategy
        assert config['name'] == plugin_name
        assert config['entryPoint'] == init.DEFAULT_ENTRY_POINT
        assert config['srcDir'] == init.DEFAULT_SRC_DIRECTORY
        assert config['schemaFile'] == init.DEFAULT_SCHEMA_FILE

        # Validate the schema file is identical to the template.
        schema_file_path = os.path.join(tmpdir.strpath, config['schemaFile'])
        with open(schema_file_path, 'r') as f:
            schema = json.load(f)
            assert schema == schema_template

        # Rebuild the entry file name from the entry point in the config file.
        entry_module, _ = config['entryPoint'].split(':')
        entry_file = entry_module + '.py'

        # Validate the entry file is identical to the template.
        entry_file_path = os.path.join(tmpdir.strpath, config['srcDir'],
                                       entry_file)
        with open(entry_file_path, 'r') as f:
            contents = f.read()
            assert contents == format_entry_point_template(
                config['id'], ingestion_strategy)
コード例 #2
0
def init(root, ingestion_strategy, name, host_type):
    """
    Create a plugin in the root directory. The plugin will be valid
    but have no functionality.
    """
    with command_error_handler():
        init_internal.init(root, ingestion_strategy, name, host_type)
コード例 #3
0
 def test_invalid_with_src_dir(src_dir):
     with pytest.raises(exceptions.PathExistsError):
         init.init(
             os.path.dirname(src_dir),
             util_classes.DIRECT_TYPE,
             None,
             util_classes.UNIX_HOST_TYPE,
         )
コード例 #4
0
 def test_invalid_with_config_file(plugin_config_file):
     with pytest.raises(exceptions.PathExistsError):
         init.init(
             os.path.dirname(plugin_config_file),
             util_classes.DIRECT_TYPE,
             None,
             util_classes.UNIX_HOST_TYPE,
         )
コード例 #5
0
    def test_init_without_plugin_name(tmpdir):
        init.init(tmpdir.strpath, util_classes.DIRECT_TYPE, "")

        result = plugin_util.read_and_validate_plugin_config_file(
            os.path.join(tmpdir.strpath, init.DEFAULT_PLUGIN_CONFIG_FILE),
            True, False)

        config = result.plugin_config_content

        # Validate that the plugin name is equal to plugin id
        assert config['name'] == config['id']
コード例 #6
0
    def test_plugin_from_init_is_valid(tmpdir, ingestion_strategy,
                                       plugin_name):
        init.init(tmpdir.strpath, ingestion_strategy, plugin_name)

        plugin_config_file = os.path.join(tmpdir.strpath,
                                          init.DEFAULT_PLUGIN_CONFIG_FILE)
        schema_file = os.path.join(tmpdir.strpath, init.DEFAULT_SCHEMA_FILE)
        validator = plugin_validator.PluginValidator(plugin_config_file,
                                                     schema_file, True, True)
        validator.validate()

        assert not validator.result.warnings
コード例 #7
0
    def test_init_windows_plugin(tmpdir, plugin_name):
        init.init(tmpdir.strpath, const.DIRECT_TYPE, plugin_name,
                  const.WINDOWS_HOST_TYPE)
        result = plugin_util.validate_plugin_config_file(
            os.path.join(tmpdir.strpath, init.DEFAULT_PLUGIN_CONFIG_FILE),
            True)
        config = result.plugin_config_content

        # Validate that the host type is WINDOWS
        host_types = config['hostTypes']
        assert len(host_types) == 1
        assert host_types[0] == const.WINDOWS_HOST_TYPE
コード例 #8
0
    def test_init_with_relative_path(tmpdir):
        os.chdir(tmpdir.strpath)
        init.init(".", const.DIRECT_TYPE, "", const.UNIX_HOST_TYPE)

        result = plugin_util.validate_plugin_config_file(
            os.path.join(tmpdir.strpath, init.DEFAULT_PLUGIN_CONFIG_FILE),
            True)

        config = result.plugin_config_content

        # Validate that the plugin name is equal to plugin id
        assert config['name'] == config['id']
コード例 #9
0
    def test_init_calls_cleanup_on_failure(mock_cleanup, mock_yaml_dump,
                                           tmpdir, plugin_name):
        mock_yaml_dump.side_effect = RuntimeError()
        with pytest.raises(exceptions.UserError):
            init.init(tmpdir.strpath, util_classes.STAGED_TYPE, plugin_name)

        src_dir_path = os.path.join(tmpdir.strpath, init.DEFAULT_SRC_DIRECTORY)
        config_file_path = os.path.join(tmpdir.strpath,
                                        init.DEFAULT_PLUGIN_CONFIG_FILE)
        schema_file_path = os.path.join(tmpdir.strpath,
                                        init.DEFAULT_SCHEMA_FILE)

        mock_cleanup.assert_called_once_with(config_file_path,
                                             schema_file_path, src_dir_path)
コード例 #10
0
    def test_init_windows_plugin(tmpdir, plugin_name):
        init.init(
            tmpdir.strpath,
            util_classes.DIRECT_TYPE,
            plugin_name,
            util_classes.WINDOWS_HOST_TYPE,
        )
        result = plugin_util.read_and_validate_plugin_config_file(
            os.path.join(tmpdir.strpath, init.DEFAULT_PLUGIN_CONFIG_FILE),
            True, False)
        config = result.plugin_config_content

        # Validate that the host type is WINDOWS
        host_types = config["hostTypes"]
        assert len(host_types) == 1
        assert host_types[0] == util_classes.WINDOWS_HOST_TYPE
コード例 #11
0
    def test_build_success_from_init(mock_relative_path, mock_install_deps,
                                     mock_patch_dependencies, tmpdir,
                                     ingestion_strategy, host_type,
                                     plugin_name, artifact_file):
        # Initialize an empty directory.
        init.init(tmpdir.strpath, ingestion_strategy, plugin_name, host_type)
        plugin_config_file = os.path.join(tmpdir.strpath,
                                          init.DEFAULT_PLUGIN_CONFIG_FILE)
        # Before running build assert that the artifact file does not exist.
        assert not os.path.exists(artifact_file)

        build.build(plugin_config_file, artifact_file, False, False)

        mock_relative_path.assert_called()
        mock_install_deps.assert_called()

        assert os.path.exists(artifact_file)
コード例 #12
0
    def test_plugin_from_init_is_valid(tmpdir, ingestion_strategy,
                                       plugin_name):
        init.init(tmpdir.strpath, ingestion_strategy, plugin_name,
                  const.UNIX_HOST_TYPE)

        plugin_config_file = os.path.join(tmpdir.strpath,
                                          init.DEFAULT_PLUGIN_CONFIG_FILE)
        schema_file = os.path.join(tmpdir.strpath, init.DEFAULT_SCHEMA_FILE)
        validator = plugin_validator.PluginValidator(plugin_config_file,
                                                     schema_file)

        # Assert config file validation is not done.
        assert not validator.result.plugin_config_content

        validator.validate_plugin_config()

        # Assert config file is validated.
        assert validator.result.plugin_config_content
コード例 #13
0
    def test_init(
        tmpdir,
        ingestion_strategy,
        host_type,
        schema_template,
        plugin_name,
        format_entry_point_template,
    ):
        # Initialize an empty directory.
        init.init(tmpdir.strpath, ingestion_strategy, plugin_name, host_type)

        # Validate the config file is as we expect.
        result = plugin_util.read_and_validate_plugin_config_file(
            os.path.join(tmpdir.strpath, init.DEFAULT_PLUGIN_CONFIG_FILE),
            True, False)

        config = result.plugin_config_content

        assert config["hostTypes"] == [host_type]
        assert config["pluginType"] == ingestion_strategy
        assert config["name"] == plugin_name
        assert config["entryPoint"] == init.DEFAULT_ENTRY_POINT
        assert config["srcDir"] == init.DEFAULT_SRC_DIRECTORY
        assert config["schemaFile"] == init.DEFAULT_SCHEMA_FILE

        # Validate the schema file is identical to the template.
        schema_file_path = os.path.join(tmpdir.strpath, config["schemaFile"])
        with open(schema_file_path, "r") as f:
            schema = json.load(f)
            assert schema == schema_template

        # Rebuild the entry file name from the entry point in the config file.
        entry_module, _ = config["entryPoint"].split(":")
        entry_file = entry_module + ".py"

        # Validate the entry file is identical to the template.
        entry_file_path = os.path.join(tmpdir.strpath, config["srcDir"],
                                       entry_file)
        with open(entry_file_path, "r") as f:
            contents = f.read()
            assert contents == format_entry_point_template(
                config["id"], ingestion_strategy, host_type)
コード例 #14
0
 def test_invalid_with_schema_file(schema_file):
     with pytest.raises(exceptions.PathExistsError):
         init.init(os.path.dirname(schema_file), util_classes.DIRECT_TYPE,
                   None)
コード例 #15
0
 def test_invalid_with_schema_file(schema_file):
     with pytest.raises(exceptions.PathExistsError):
         init.init(os.path.dirname(schema_file), const.DIRECT_TYPE, None,
                   const.UNIX_HOST_TYPE)