Skip to content

Commit

Permalink
Working tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rados13 committed Feb 29, 2024
1 parent 812a386 commit f3cf859
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 72 deletions.
3 changes: 0 additions & 3 deletions lib/s3/s3_source.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ defmodule Membrane.AWS.S3.Source do
|> ExAws.S3.download_file(state.path, :memory, state.opts)
|> ExAws.stream!(state.aws_credentials)

# Uncomment if you need to gunzip (and add dependency :stream_gzip)
# |> StreamGzip.gunzip()

{[stream_format: {:output, %RemoteStream{type: :bytestream}}], file_stream}
end

Expand Down
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule Membrane.AWS.Mixfile do

def application do
[
extra_applications: []
extra_applications: [:logger]
]
end

Expand All @@ -62,7 +62,8 @@ defmodule Membrane.AWS.Mixfile do

# Test dependency
{:bypass, "~> 2.1", only: :test},
{:hackney, ">= 0.0.0", only: :test}
{:httpoison, "~> 2.0", only: :test},
{:mox, "~> 1.0", only: :test}
]
end

Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"ex_doc": {:hex, :ex_doc, "0.30.6", "5f8b54854b240a2b55c9734c4b1d0dd7bdd41f71a095d42a70445c03cf05a281", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bd48f2ddacf4e482c727f9293d9498e0881597eae6ddc3d9562bd7923375109f"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"},
"httpoison": {:hex, :httpoison, "2.2.1", "87b7ed6d95db0389f7df02779644171d7319d319178f6680438167d7b69b1f3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "51364e6d2f429d80e14fe4b5f8e39719cacd03eb3f9a9286e61e216feac2d2df"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
Expand All @@ -25,6 +26,7 @@
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mox": {:hex, :mox, "1.1.0", "0f5e399649ce9ab7602f72e718305c0f9cdc351190f72844599545e4996af73c", [:mix], [], "hexpm", "d44474c50be02d5b72131070281a5d3895c0e7a95c780e90bc0cfe712f633a13"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
"numbers": {:hex, :numbers, "5.2.4", "f123d5bb7f6acc366f8f445e10a32bd403c8469bdbce8ce049e1f0972b607080", [:mix], [{:coerce, "~> 1.0", [hex: :coerce, repo: "hexpm", optional: false]}, {:decimal, "~> 1.9 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "eeccf5c61d5f4922198395bf87a465b6f980b8b862dd22d28198c5e6fab38582"},
"parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"},
Expand Down
85 changes: 39 additions & 46 deletions test/s3_source_test.exs
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
defmodule Membrane.AWS.S3.SourceTest do
use ExUnit.Case, async: true
use ExUnit.Case

import Membrane.Testing.Assertions
import Membrane.ChildrenSpec
import Support.BypassHelpers
import Mox

alias Membrane.AWS.S3.Source
alias Membrane.Testing.{Pipeline, Sink}

@bucket_name "bucket"

describe "file pass through pipeline" do
setup [:start_bypass]
setup :verify_on_exit!
setup :set_mox_from_context

test "whole file in one chunk", %{bypass: bypass} do
test "whole file in one chunk" do
data = for i <- 0..10_000, do: <<i::8>>, into: <<>>
file_name = "test.txt"

setup_multipart_download_backend(
bypass,
"bucket",
"test.txt",
data
)
setup_multipart_download_backend(@bucket_name, file_name, data)

assert pipeline =
Pipeline.start_link_supervised!(
spec:
child(:s3_source, %Source{
bucket: "bucket",
path: "test.txt",
aws_credentials: exaws_config_for_bypass(bypass)
bucket: @bucket_name,
path: file_name,
aws_credentials: [
access_key_id: "dummy",
secret_access_key: "dummy"
]
})
|> child(:sink, Sink),
test_process: self()
Expand All @@ -44,29 +46,28 @@ defmodule Membrane.AWS.S3.SourceTest do
assert_end_of_stream(pipeline, :sink)
end

test "file splitted to 16-bytes chunks", %{bypass: bypass} do
test "file splitted to 16-bytes chunks" do
chunk_size = 16
file_name = "test2.txt"

data = for i <- 0..(16 ** 2), do: <<i::8>>, into: <<>>

splitted_binary =
for <<chunk::binary-size(chunk_size) <- data>>, do: <<chunk::binary-size(chunk_size)>>

setup_multipart_download_backend(
bypass,
"bucket",
"test2.txt",
data
)
setup_multipart_download_backend(@bucket_name, file_name, data)

assert pipeline =
Pipeline.start_link_supervised!(
spec:
child(:s3_source, %Source{
bucket: "bucket",
path: "test2.txt",
aws_credentials: exaws_config_for_bypass(bypass),
opts: [chunk_size: chunk_size]
bucket: @bucket_name,
path: file_name,
opts: [chunk_size: chunk_size],
aws_credentials: [
access_key_id: "dummy",
secret_access_key: "dummy"
]
})
|> child(:sink, Sink),
test_process: self()
Expand All @@ -87,40 +88,32 @@ defmodule Membrane.AWS.S3.SourceTest do
end

defp setup_multipart_download_backend(
bypass,
bucket_name,
path,
file_body
) do
request_path = "/#{bucket_name}/#{path}"

Bypass.expect(bypass, fn conn ->
case conn do
%{method: "HEAD", request_path: ^request_path} ->
conn
|> Plug.Conn.put_resp_header("Content-Length", file_body |> byte_size |> to_string)
|> Plug.Conn.send_resp(200, "")
request_path = "https://s3.amazonaws.com/#{bucket_name}/#{path}"

%{method: "GET", request_path: ^request_path, req_headers: headers} ->
headers = Map.new(headers)
stub(ExAws.Request.HttpMock, :request, fn
:head, ^request_path, _req_body, _headers, _http_opts ->
content_length = file_body |> byte_size |> to_string

"bytes=" <> range = Map.fetch!(headers, "range")
{:ok, %{status_code: 200, headers: %{"Content-Length" => content_length}}}

[first, second | _] = String.split(range, "-")
:get, ^request_path, _req_body, headers, _http_opts ->
headers = Map.new(headers)

first = String.to_integer(first)
second = String.to_integer(second)
"bytes=" <> range = Map.fetch!(headers, "range")

# IO.inspect({first, second, second - first + 1}, label: :difference)
[first, second | _] = String.split(range, "-")

<<_head::binary-size(first), payload::binary-size(second - first + 1), _rest::binary>> =
file_body
first = String.to_integer(first)
second = String.to_integer(second)

# IO.inspect(byte_size(payload), label: :WTF_PAYLOAD)
<<_head::binary-size(first), payload::binary-size(second - first + 1), _rest::binary>> =
file_body

conn
|> Plug.Conn.send_resp(200, payload)
end
{:ok, %{status_code: 200, body: payload}}
end)
end
end
21 changes: 0 additions & 21 deletions test/support/bypass_helpers.ex

This file was deleted.

3 changes: 3 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
Mox.defmock(ExAws.Request.HttpMock, for: ExAws.Request.HttpClient)
Application.put_env(:ex_aws, :http_client, ExAws.Request.HttpMock)

ExUnit.start(capture_log: true)

0 comments on commit f3cf859

Please sign in to comment.