def source_tarballs_created_by_whack_can_be_installed(ops): with _package_source(testing.HelloWorld.BUILD) as package_source_dir: with create_temporary_dir() as tarball_dir: source_tarball = ops.create_source_tarball(package_source_dir, tarball_dir) with create_temporary_dir() as target_dir: ops.install(source_tarball.path, target_dir, params={}) output = _check_output([os.path.join(target_dir, "hello")]) assert_equal(testing.HelloWorld.EXPECTED_OUTPUT, output)
def downloader_can_download_files_over_http(): downloader = Downloader(NoCachingStrategy()) with create_temporary_dir() as server_root: files.write_file(os.path.join(server_root, "hello"), "Hello there!") with httpserver.start_static_http_server(server_root) as http_server: with create_temporary_dir() as download_dir: download_path = os.path.join(download_dir, "file") url = http_server.static_url("hello") downloader.download(url, download_path) assert_equal("Hello there!", files.read_file(download_path))
def download_fails_if_http_request_returns_404(): downloader = Downloader(NoCachingStrategy()) with create_temporary_dir() as server_root: with httpserver.start_static_http_server(server_root) as http_server: with create_temporary_dir() as download_dir: download_path = os.path.join(download_dir, "file") url = http_server.static_url("hello") try: downloader.download(url, download_path) assert False except DownloadError: pass
def error_is_raised_if_build_script_is_missing(): files = [ plain_file("whack/whack.json", json.dumps({})), ] with create_temporary_dir(files) as package_source_dir: package_source = PackageSource.local(package_source_dir) request = create_package_request(package_source, {}) with create_temporary_dir() as target_dir: assert_raises( FileNotFoundError, ("whack/build script not found in package source {0}".format(package_source_dir), ), lambda: build(request, target_dir), )
def built_package_can_be_deployed_to_different_directory(ops): package_files = [ plain_file("message", "Hello there"), sh_script_description(".bin/hello", "cat {0}/message".format(WHACK_ROOT)), ] with create_temporary_dir(package_files) as package_dir: with create_temporary_dir() as install_dir: ops.deploy(package_dir, install_dir) output = _check_output([os.path.join(install_dir, "bin/hello")]) assert_equal(b"Hello there", output) assert _is_deployed(install_dir) assert not _is_deployed(package_dir)
def writing_source_raises_error_if_file_is_missing(): with create_temporary_dir() as package_source_dir: whack_description = { "sourcePaths": ["name"] } write_files(package_source_dir, [ plain_file("whack/whack.json", json.dumps(whack_description)), ]) with _fetch_source(package_source_dir) as package_source: with create_temporary_dir() as target_dir: assert_raises( FileNotFoundError, lambda: package_source.write_to(target_dir) )
def can_fetch_package_source_from_local_tarball(): with create_temporary_dir() as temp_dir: def create_source(package_source_dir): tarball_path = os.path.join(temp_dir, "package.tar.gz") return create_tarball(tarball_path, package_source_dir) _assert_package_source_can_be_written_to_target_dir(create_source)
def _package_source(build_script, description): files = [ plain_file("whack/whack.json", json.dumps(description)), sh_script_description("whack/build", build_script), ] with create_temporary_dir(files) as package_source_dir: yield PackageSource.local(package_source_dir)
def can_install_package_when_build_step_is_disabled_if_pre_built_package_can_be_found(create_operations): with _package_source(testing.HelloWorld.BUILD) as package_source_dir: with start_index_server() as index_server: indices = [index_server.index_url()] operations_for_build = create_operations(indices=indices) with create_temporary_dir() as temp_dir: package_tarball = operations_for_build.get_package_tarball(package_source_dir, temp_dir) index_server.add_package_tarball(package_tarball) with create_temporary_dir() as install_dir: operations = create_operations(enable_build=False, indices=indices) operations.install(package_source_dir, install_dir) output = _check_output([os.path.join(install_dir, "hello")]) assert_equal(testing.HelloWorld.EXPECTED_OUTPUT, output)
def writing_package_source_includes_directories_specified_in_description(): with create_temporary_dir() as package_source_dir: whack_description = { "sourcePaths": ["names"] } write_files(package_source_dir, [ plain_file("whack/whack.json", json.dumps(whack_description)), plain_file("names/bob", "Bob"), ]) with _fetch_source(package_source_dir) as package_source: with create_temporary_dir() as target_dir: package_source.write_to(target_dir) assert_equal( "Bob", read_file(os.path.join(target_dir, "names/bob")) )
def parameters_are_passed_to_test_command_as_environment_variables(operations): source_files = [ plain_file("whack/whack.json", json.dumps({"test": '[ "$VERSION" = "1" ]'})), plain_file("zero", "0"), ] with create_temporary_dir(source_files) as package_source_dir: test_result = operations.test(package_source_dir, params={"version": "1"}) assert_equal(True, test_result.passed)
def test_install(ops, build, params, expected_output): with _package_source(build) as package_source_dir: with create_temporary_dir() as install_dir: ops.install(package_source_dir, install_dir, params=params) output = _check_output([os.path.join(install_dir, "hello")]) assert_equal(expected_output, output) assert _is_deployed(install_dir)
def getting_package_leaves_undeployed_build_in_target_directory(ops): with _package_source(testing.HelloWorld.BUILD) as package_source_dir: with create_temporary_dir() as target_dir: ops.get_package(package_source_dir, target_dir, params={}) output = _check_output([os.path.join(target_dir, "hello")]) assert_equal(testing.HelloWorld.EXPECTED_OUTPUT, output) assert not _is_deployed(target_dir)
def test_command_is_run_in_root_of_source_dir(operations): source_files = [ plain_file( "whack/whack.json", json.dumps({"test": "exit `cat zero || echo 1`", "sourcePaths": ["whack", "zero"]}) ), plain_file("zero", "0"), ] with create_temporary_dir(source_files) as package_source_dir: test_result = operations.test(package_source_dir) assert_equal(True, test_result.passed)
def packages_can_be_installed_from_html_index(create_operations): with _package_source(testing.HelloWorld.BUILD) as package_source_dir: with start_index_server() as index_server: source = index_server.add_source(package_source_dir) with create_temporary_dir() as target_dir: operations = create_operations(indices=[index_server.index_url()]) operations.install(source.full_name, target_dir, params={}) output = _check_output([os.path.join(target_dir, "hello")]) assert_equal(testing.HelloWorld.EXPECTED_OUTPUT, output)
def _assert_package_source_can_be_written_to_target_dir(source_filter, indices=None): with _create_temporary_package_source_dir() as package_source_dir: package_source_name = source_filter(package_source_dir) with _fetch_source(package_source_name, indices) as package_source: with create_temporary_dir() as target_dir: package_source.write_to(target_dir) assert_equal( "Bob", read_file(os.path.join(target_dir, "whack/name")) )
def params_are_passed_to_build_script_during_get_package(ops): _TEST_BUILDER_BUILD = r"""#!/bin/sh set -e cd $1 echo '#!/bin/sh' >> hello echo echo ${VERSION} >> hello chmod +x hello """ with _package_source(_TEST_BUILDER_BUILD) as package_source_dir: with create_temporary_dir() as target_dir: ops.get_package(package_source_dir, target_dir, params={"version": "1"}) output = _check_output([os.path.join(target_dir, "hello")]) assert_equal(b"1\n", output)
def install_works_with_relative_path_for_install_dir(): _BUILD = r"""#!/bin/sh set -e INSTALL_DIR=$1 mkdir -p $INSTALL_DIR/bin cat > $INSTALL_DIR/bin/hello << EOF #!/bin/sh echo Hello there EOF chmod +x $INSTALL_DIR/bin/hello """ with _temporary_package_source(_BUILD) as package_source_dir: with create_temporary_dir() as install_dir: with _change_dir(install_dir): _install(package_source_dir, ".") output = _check_output(os.path.join(install_dir, "bin/hello")) assert_equal(b"Hello there\n", output)
def start_index_server(): with create_temporary_dir() as server_root: with start_static_http_server(server_root) as http_server: yield IndexServer(http_server)
def build_uses_params_as_environment_variables_in_build(): with _package_source("echo $VERSION > $1/version", {}) as package_source: with create_temporary_dir() as target_dir: build(create_package_request(package_source, {"version": "42"}), target_dir) assert_equal("42\n", read_file(os.path.join(target_dir, "version")))
def _source_package_with_description(description): with create_temporary_dir() as package_source_dir: write_files(package_source_dir, [ plain_file("whack/whack.json", json.dumps(description)), ]) yield PackageSource.local(package_source_dir)
def _package_source(build): return create_temporary_dir([sh_script_description("whack/build", build)])
def _temporary_xdg_cache_dir(): key = "XDG_CACHE_HOME" with create_temporary_dir() as cache_dir: with _updated_env({key: cache_dir}): yield
def explicit_params_override_default_params(): description = {"defaultParams": {"version": "42"}} with _package_source("echo $VERSION > $1/version", description) as package_source: with create_temporary_dir() as target_dir: build(create_package_request(package_source, {"version": "43"}), target_dir) assert_equal("43\n", read_file(os.path.join(target_dir, "version")))
def build_uses_default_value_for_param_if_param_not_explicitly_set(): description = {"defaultParams": {"version": "42"}} with _package_source("echo $VERSION > $1/version", description) as package_source: with create_temporary_dir() as target_dir: build(create_package_request(package_source, {}), target_dir) assert_equal("42\n", read_file(os.path.join(target_dir, "version")))
def _temporary_static_server(): with create_temporary_dir() as server_root: with start_static_http_server(server_root) as server: yield server
def whack_test_passes_if_test_command_has_zero_return_code(operations): source_files = [plain_file("whack/whack.json", json.dumps({"test": "true"}))] with create_temporary_dir(source_files) as package_source_dir: test_result = operations.test(package_source_dir) assert_equal(True, test_result.passed)
def whack_test_fails_if_test_is_not_set_in_whack_json(operations): source_files = [plain_file("whack/whack.json", json.dumps({}))] with create_temporary_dir(source_files) as package_source_dir: test_result = operations.test(package_source_dir) assert_equal(False, test_result.passed)
def name_of_package_source_is_unknown_if_whack_json_does_not_exist(): with create_temporary_dir() as package_source_dir: package_source = PackageSource.local(package_source_dir) assert_equal("unknown", package_source.name())
def error_is_raised_if_build_step_is_disabled_and_pre_built_package_cannot_be_found(create_operations): operations = create_operations(enable_build=False) with _package_source(testing.HelloWorld.BUILD) as package_source_dir: with create_temporary_dir() as target_dir: assert_raises(PackageNotAvailableError, lambda: operations.install(package_source_dir, target_dir))