def test_build_success(mock_relative_path, mock_install_deps, mock_generate_python, mock_plugin_manifest, mock_patch_dependencies, plugin_config_file, artifact_file, artifact_content, codegen_gen_py_inputs): gen_py = codegen_gen_py_inputs # 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_generate_python.assert_called_once_with(gen_py.name, gen_py.source_dir, gen_py.plugin_content_dir, gen_py.schema_dict) mock_plugin_manifest.assert_called() mock_install_deps.assert_called() mock_relative_path.assert_called() mock_patch_dependencies.assert_called() # After running build this file should now exist. assert os.path.exists(artifact_file) with open(artifact_file, 'rb') as f: content = json.load(f) assert content == artifact_content
def test_build_generate_artifact_fail( mock_generate_python, mock_plugin_manifest, mock_gen_artifact, plugin_config_file, artifact_file, codegen_gen_py_inputs, ): gen_py = codegen_gen_py_inputs # Before running build assert that the artifact file does not exist. assert not os.path.exists(artifact_file) # Raise UserError when codegen is called mock_gen_artifact.side_effect = exceptions.UserError("generate_artifact_error") with pytest.raises(exceptions.BuildFailedError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message mock_generate_python.assert_called_once_with( gen_py.name, gen_py.source_dir, gen_py.plugin_content_dir, gen_py.schema_dict, ) mock_plugin_manifest.assert_called() mock_gen_artifact.assert_called() # After running build this file should not exist. assert not os.path.exists(artifact_file) assert "generate_artifact_error" in message assert "BUILD FAILED" in message
def test_build_success_non_default_output_file( mock_generate_python, mock_import_plugin, plugin_config_file, artifact_file, artifact_content, codegen_gen_py_inputs, ): gen_py = codegen_gen_py_inputs # 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_generate_python.assert_called_once_with( gen_py.name, gen_py.source_dir, gen_py.plugin_content_dir, gen_py.schema_dict, ) mock_import_plugin.assert_called() # After running build this file should now exist. assert os.path.exists(artifact_file) with open(artifact_file, "rb") as f: content = json.load(f) assert content == artifact_content
def build(plugin_config, upload_artifact, generate_only, skip_id_validation, dev): """ Build the plugin code and generate upload artifact file using the configuration provided in the plugin config file. """ # Set upload artifact to None if -g is true. if generate_only: upload_artifact = None local_vsdk_root = None with command_error_handler(): if dev: if not DVP_CONFIG_MAP.get('dev') or not DVP_CONFIG_MAP.get( 'dev').get('vsdk_root'): raise RuntimeError("The dev flag was specified but there is " "not a vsdk_root entry in the dvp config " "file. Please look in the SDK's README for " "details on configuring the vsdk_root " "property.") local_vsdk_root = DVP_CONFIG_MAP.get('dev').get('vsdk_root') build_internal.build(plugin_config, upload_artifact, generate_only, skip_id_validation, local_vsdk_root=local_vsdk_root)
def test_empty_plugin_name(mock_generate_python, plugin_config_file, artifact_file): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert "Validation failed" in message assert not mock_generate_python.called
def test_plugin_no_src_dir(mock_generate_python, plugin_config_file, artifact_file): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert message == "The path '/not/a/real/dir/src' does not exist." assert not mock_generate_python.called
def test_no_schema_file(mock_generate_python, plugin_config_file, artifact_file): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert message == ("The path '/not/a/real/file/schema.json'" " does not exist.") assert not mock_generate_python.called
def test_plugin_bad_language(mock_generate_python, plugin_config_file, artifact_file): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert "'BAD_LANGUAGE' is not one of ['PYTHON27']" in message assert not mock_generate_python.called
def test_plugin_missing_fields(mock_generate_python, plugin_config_file, artifact_file): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert "'srcDir' is a required property" in message assert not mock_generate_python.called
def test_plugin_missing_schema_def(mock_generate_python, plugin_config_file, artifact_file): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert "'virtualSourceDefinition' is a required property" in message assert not mock_generate_python.called
def test_plugin_no_src_dir(mock_generate_python, mock_path_is_relative, plugin_config_file, artifact_file, tmpdir): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert message == "The path '{}' does not exist.".format( tmpdir.join(os.path.join('fake', 'dir')).strpath) assert not mock_generate_python.called
def test_plugin_extra_schema_def(mock_generate_python, plugin_config_file, artifact_file): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert "Additional properties are not allowed " \ "('additionalDefinition' was unexpected)" in message assert not mock_generate_python.called
def test_bad_data_type_in_schema(mock_generate_python, plugin_config_file, artifact_file): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message exp_error = "'badDataType' is not valid under any of the given schemas" assert exp_error in message assert not mock_generate_python.called
def test_non_existing_entry_file(mock_relative_path, plugin_config_file, plugin_config_content, artifact_file): entry_module, _ = plugin_config_content['entryPoint'].split(':') with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message exp_message = "No module named {module}".format(module=entry_module) assert exp_message in message
def build(plugin_config, upload_artifact, generate_only, skip_id_validation): """ Build the plugin code and generate upload artifact file using the configuration provided in the plugin config file. """ # Set upload artifact to None if -g is true. if generate_only: upload_artifact = None with command_error_handler(): build_internal.build(plugin_config, upload_artifact, generate_only, skip_id_validation)
def test_schema_bad_format(mock_generate_python, plugin_config_file, artifact_file, schema_file): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert ('Failed to load schemas because {!r} is not a valid json file.' ' Error: Extra data: line 2 column 1 - line 2 column 9' ' (char 19 - 27)'.format(schema_file)) in message assert not mock_generate_python.called
def test_no_plugin_file(mock_generate_python, plugin_config_file, artifact_file): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert ("Unable to read plugin config file" " '/not/a/real/file/plugin_config.yml'" "\nError code: 2. Error message: No such file or" " directory") in message assert not mock_generate_python.called
def test_plugin_bad_format(mock_generate_python, plugin_config_file, artifact_file): with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert ('Command failed because the plugin config file ' 'provided as input \'{}\' was not valid yaml. ' 'Verify the file contents. ' 'Error position: 3:9'.format(plugin_config_file)) in message assert not mock_generate_python.called
def test_build_success_with_patched_dependencies( mock_relative_path, mock_install_deps, mock_generate_python, mock_plugin_manifest, plugin_config_file, artifact_file, codegen_gen_py_inputs, json_format_file_patched, json_format_content): build.build(plugin_config_file, artifact_file, False, False) with open(json_format_file_patched, 'r') as f: json_format_text = f.read() assert json_format_text == json_format_content
def test_generate_only_success(mock_generate_python, mock_prep_artifact, plugin_config_file, artifact_file, codegen_gen_py_inputs): gen_py = codegen_gen_py_inputs build.build(plugin_config_file, artifact_file, True, False) mock_generate_python.assert_called_once_with(gen_py.name, gen_py.source_dir, gen_py.plugin_content_dir, gen_py.schema_dict) assert not mock_prep_artifact.called
def test_plugin_schema_not_file( mock_generate_python, plugin_config_file, artifact_file, schema_file ): # Delete the schema file and create a dir there instead os.remove(schema_file) os.mkdir(schema_file) with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert message == "The path {} should be a file but is not.".format(schema_file) assert not mock_generate_python.called
def test_schema_file_bad_permission(mock_generate_python, plugin_config_file, artifact_file, schema_file): # Make it so we can't read the file os.chmod(schema_file, 0000) with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert ( 'Unable to load schemas from \'{}\'\nError code: 13.' ' Error message: Permission denied'.format(schema_file)) in message assert not mock_generate_python.called
def test_plugin_src_not_dir(mock_relative_path, mock_generate_python, plugin_config_file, artifact_file, src_dir): # Delete the src dir folder and create a file there instead os.rmdir(src_dir) with open(src_dir, 'w') as f: f.write('writing to create file') with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert message == ("The path '{}' should be a" " directory but is not.".format(src_dir)) assert not mock_generate_python.called
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)
def test_schema_file_bad_permission(mock_generate_python, plugin_config_file, artifact_file, schema_file): # Make it so we can't read the file if os.name == 'nt': pytest.skip( 'skipping this test on windows as os.chmod has issues removing' ' permissions on file') # # The schema_file can be made unreadable on windows using pypiwin32 but # since it adds dependency on pypiwin32 for the sdk, skipping this test # instead of potentially destabilizing the sdk by adding this dependency. # else: os.chmod(schema_file, 0000) with pytest.raises(exceptions.UserError) as err_info: build.build(plugin_config_file, artifact_file, False, False) message = err_info.value.message assert ( 'Unable to load schemas from \'{}\'\nError code: 13.' ' Error message: Permission denied'.format(schema_file)) in message assert not mock_generate_python.called
def test_id_validation_positive(mock_relative_path, mock_install_deps, mock_patch_dependencies, mock_import_plugin, plugin_config_file, artifact_file): build.build(plugin_config_file, artifact_file, False)
def test_id_validation_negative( mock_import_plugin, plugin_config_file, artifact_file ): with pytest.raises(exceptions.UserError): build.build(plugin_config_file, artifact_file, False, False)
def test_id_validation_positive( mock_import_plugin, plugin_config_file, artifact_file, skip_id_validation ): build.build(plugin_config_file, artifact_file, False, skip_id_validation)