diff --git a/lib/azurex/blob.ex b/lib/azurex/blob.ex index a2398c2..a189f7c 100644 --- a/lib/azurex/blob.ex +++ b/lib/azurex/blob.ex @@ -10,14 +10,14 @@ defmodule Azurex.Blob do @typep optional_string :: String.t() | nil - def list_containers(conf_element \\ :azurex) do + def list_containers do %HTTPoison.Request{ - url: Config.api_url(conf_element) <> "/", + url: Config.api_url() <> "/", params: [comp: "list"] } |> SharedKey.sign( - storage_account_name: Config.storage_account_name(conf_element), - storage_account_key: Config.storage_account_key(conf_element) + storage_account_name: Config.storage_account_name(), + storage_account_key: Config.storage_account_key() ) |> HTTPoison.request() |> case do @@ -71,34 +71,32 @@ defmodule Azurex.Blob do ) :: :ok | {:error, HTTPoison.AsyncResponse.t() | HTTPoison.Error.t() | HTTPoison.Response.t()} - def put_blob(name, blob, content_type, container \\ nil, options \\ []) + def put_blob(name, blob, content_type, container \\ nil, params \\ []) - def put_blob(name, {:stream, bitstream}, content_type, container, options) do + def put_blob(name, {:stream, bitstream}, content_type, container, params) do content_type = content_type || "application/octet-stream" bitstream |> Stream.transform( fn -> [] end, fn chunk, acc -> - with {:ok, block_id} <- Block.put_block(container, chunk, name, options) do + with {:ok, block_id} <- Block.put_block(container, chunk, name, params) do {[], [block_id | acc]} end end, fn acc -> - Block.put_block_list(acc, container, name, content_type, options) + Block.put_block_list(acc, container, name, content_type, params) end ) |> Stream.run() end - def put_blob(name, blob, content_type, container, options) do - {params, options} = Keyword.pop(options, :params, []) - {conf_element, _options} = Keyword.pop(options, :config_element, :azurex) + def put_blob(name, blob, content_type, container, params) do content_type = content_type || "application/octet-stream" %HTTPoison.Request{ method: :put, - url: get_url(container, conf_element, name), + url: get_url(container, name), params: params, body: blob, headers: [ @@ -109,8 +107,8 @@ defmodule Azurex.Blob do options: [recv_timeout: :infinity] } |> SharedKey.sign( - storage_account_name: Config.storage_account_name(conf_element), - storage_account_key: Config.storage_account_key(conf_element), + storage_account_name: Config.storage_account_name(), + storage_account_key: Config.storage_account_key(), content_type: content_type ) |> HTTPoison.request() @@ -182,17 +180,17 @@ defmodule Azurex.Blob do {:ok, HTTPoison.Response.t()} | {:error, term()} def copy_blob(source_name, destination_name, container \\ nil) do content_type = "application/octet-stream" - source_url = get_url(container, :azurex, source_name) + source_url = get_url(container, source_name) headers = [{"x-ms-copy-source", source_url}, {"content-type", content_type}] %HTTPoison.Request{ method: :put, - url: get_url(container, :azurex, destination_name), + url: get_url(container, destination_name), headers: headers } |> SharedKey.sign( - storage_account_name: Config.storage_account_name(:azurex), - storage_account_key: Config.storage_account_key(:azurex), + storage_account_name: Config.storage_account_name(), + storage_account_key: Config.storage_account_key(), content_type: content_type ) |> HTTPoison.request() @@ -219,18 +217,17 @@ defmodule Azurex.Blob do def blob_request(name, container, method, options) do {params, options} = Keyword.pop(options, :params, []) {headers, options} = Keyword.pop(options, :headers, []) - {conf_element, options} = Keyword.pop(options, :config_element, :azurex) %HTTPoison.Request{ method: method, - url: get_url(container, conf_element, name), + url: get_url(container, name), params: params, headers: headers, options: options } |> SharedKey.sign( - storage_account_name: Config.storage_account_name(conf_element), - storage_account_key: Config.storage_account_key(conf_element) + storage_account_name: Config.storage_account_name(), + storage_account_key: Config.storage_account_key() ) end @@ -248,12 +245,9 @@ defmodule Azurex.Blob do @spec list_blobs(optional_string) :: {:ok, binary()} | {:error, HTTPoison.AsyncResponse.t() | HTTPoison.Error.t() | HTTPoison.Response.t()} - def list_blobs(container \\ nil, options \\ []) do - {params, options} = Keyword.pop(options, :params, []) - {conf_element, _options} = Keyword.pop(options, :config_element, :azurex) - + def list_blobs(container \\ nil, params \\ []) do %HTTPoison.Request{ - url: "#{Config.api_url(conf_element)}/#{get_container(container, conf_element)}", + url: "#{Config.api_url()}/#{get_container(container)}", params: [ comp: "list", @@ -261,8 +255,8 @@ defmodule Azurex.Blob do ] ++ params } |> SharedKey.sign( - storage_account_name: Config.storage_account_name(conf_element), - storage_account_key: Config.storage_account_key(conf_element) + storage_account_name: Config.storage_account_name(), + storage_account_key: Config.storage_account_key() ) |> HTTPoison.request() |> case do @@ -275,20 +269,20 @@ defmodule Azurex.Blob do @doc """ Returns the url for a container (defaults to the one in `Azurex.Blob.Config`) """ - @spec get_url(optional_string, String.t()) :: String.t() - def get_url(container, conf_element) do - "#{Config.api_url(conf_element)}/#{get_container(container, conf_element)}" + @spec get_url(optional_string) :: String.t() + def get_url(container) do + "#{Config.api_url()}/#{get_container(container)}" end @doc """ Returns the url for a file in a container (defaults to the one in `Azurex.Blob.Config`) """ @spec get_url(optional_string, String.t()) :: String.t() - def get_url(container, conf_element, blob_name) do - "#{get_url(container, conf_element)}/#{blob_name}" + def get_url(container, blob_name) do + "#{get_url(container)}/#{blob_name}" end - defp get_container(container, conf_element) do - container || Config.default_container(conf_element) + defp get_container(container) do + container || Config.default_container() end end diff --git a/lib/azurex/blob/block.ex b/lib/azurex/blob/block.ex index ced67ba..a89811e 100644 --- a/lib/azurex/blob/block.ex +++ b/lib/azurex/blob/block.ex @@ -19,16 +19,14 @@ defmodule Azurex.Blob.Block do """ @spec put_block(String.t(), bitstring(), String.t(), list()) :: {:ok, String.t()} | {:error, term()} - def put_block(container, chunk, name, options) do - {params, options} = Keyword.pop(options, :params, []) - {conf_element, _options} = Keyword.pop(options, :config_element, :azurex) + def put_block(container, chunk, name, params) do block_id = build_block_id() content_type = "application/octet-stream" params = [{:comp, "block"}, {:blockid, block_id} | params] %HTTPoison.Request{ method: :put, - url: Blob.get_url(container, conf_element, name), + url: Blob.get_url(container, name), params: params, body: chunk, headers: [ @@ -37,8 +35,8 @@ defmodule Azurex.Blob.Block do ] } |> SharedKey.sign( - storage_account_name: Config.storage_account_name(conf_element), - storage_account_key: Config.storage_account_key(conf_element), + storage_account_name: Config.storage_account_name(), + storage_account_key: Config.storage_account_key(), content_type: content_type ) |> HTTPoison.request() @@ -56,9 +54,7 @@ defmodule Azurex.Blob.Block do """ @spec put_block_list(list(), String.t(), String.t(), String.t() | nil, list()) :: :ok | {:error, term()} - def put_block_list(block_ids, container, name, blob_content_type, options) do - {params, options} = Keyword.pop(options, :params, []) - {conf_element, _options} = Keyword.pop(options, :config_element, :azurex) + def put_block_list(block_ids, container, name, blob_content_type, params) do params = [{:comp, "blocklist"} | params] content_type = "text/plain; charset=UTF-8" blob_content_type = blob_content_type || "application/octet-stream" @@ -78,7 +74,7 @@ defmodule Azurex.Blob.Block do %HTTPoison.Request{ method: :put, - url: Blob.get_url(container, conf_element, name), + url: Blob.get_url(container, name), params: params, body: body, headers: [ @@ -87,8 +83,8 @@ defmodule Azurex.Blob.Block do ] } |> SharedKey.sign( - storage_account_name: Config.storage_account_name(conf_element), - storage_account_key: Config.storage_account_key(conf_element), + storage_account_name: Config.storage_account_name(), + storage_account_key: Config.storage_account_key(), content_type: content_type ) |> HTTPoison.request() diff --git a/lib/azurex/blob/config.ex b/lib/azurex/blob/config.ex index b6f2d23..ec7b298 100644 --- a/lib/azurex/blob/config.ex +++ b/lib/azurex/blob/config.ex @@ -8,28 +8,27 @@ defmodule Azurex.Blob.Config do or `storage_account_connection_string` required. """ - defp conf(config_element), do: Application.get_env(config_element, __MODULE__, []) + defp conf, do: Application.get_env(:azurex, __MODULE__, []) @doc """ Azure endpoint url, optional Defaults to `https://{name}.blob.core.windows.net` where `name` is the `storage_account_name` """ - @spec api_url(atom()) :: String.t() - # TODO this, and below functions are public therefore we should probably default this - def api_url(config_element) do + @spec api_url :: String.t() + def api_url do cond do - api_url = Keyword.get(conf(config_element), :api_url) -> api_url - api_url = get_connection_string_value("BlobEndpoint", config_element) -> api_url - true -> "https://#{storage_account_name(config_element)}.blob.core.windows.net" + api_url = Keyword.get(conf(), :api_url) -> api_url + api_url = get_connection_string_value("BlobEndpoint") -> api_url + true -> "https://#{storage_account_name()}.blob.core.windows.net" end end @doc """ Azure container name, optional. """ - @spec default_container(atom()) :: String.t() | nil - def default_container(config_element) do - Keyword.get(conf(config_element), :default_container) || + @spec default_container :: String.t() | nil + def default_container do + Keyword.get(conf(), :default_container) || raise "Must specify `container` because the default container was not provided." end @@ -37,10 +36,10 @@ defmodule Azurex.Blob.Config do Azure storage account name. Required if `storage_account_connection_string` not set. """ - @spec storage_account_name(atom()) :: String.t() - def storage_account_name(config_element) do - case Keyword.get(conf(config_element), :storage_account_name) do - nil -> get_connection_string_value("AccountName", config_element) + @spec storage_account_name :: String.t() + def storage_account_name do + case Keyword.get(conf(), :storage_account_name) do + nil -> get_connection_string_value("AccountName") storage_account_name -> storage_account_name end || raise @missing_envs_error_msg end @@ -49,10 +48,10 @@ defmodule Azurex.Blob.Config do Azure storage account access key. Base64 encoded, as provided by azure UI. Required if `storage_account_connection_string` not set. """ - @spec storage_account_key(atom()) :: binary - def storage_account_key(config_element) do - case Keyword.get(conf(config_element), :storage_account_key) do - nil -> get_connection_string_value("AccountKey", config_element) + @spec storage_account_key :: binary + def storage_account_key do + case Keyword.get(conf(), :storage_account_key) do + nil -> get_connection_string_value("AccountKey") key -> key end |> Kernel.||(raise @missing_envs_error_msg) @@ -63,9 +62,9 @@ defmodule Azurex.Blob.Config do Azure storage account connection string. Required if `storage_account_name` or `storage_account_key` not set. """ - @spec storage_account_connection_string(atom()) :: String.t() | nil - def storage_account_connection_string(config_element), - do: Keyword.get(conf(config_element), :storage_account_connection_string) + @spec storage_account_connection_string :: String.t() | nil + def storage_account_connection_string, + do: Keyword.get(conf(), :storage_account_connection_string) @spec parse_connection_string(nil | binary) :: map @doc """ @@ -97,9 +96,9 @@ defmodule Azurex.Blob.Config do @doc """ Returns the value in the connection string given the string key. """ - @spec get_connection_string_value(String.t(), atom()) :: String.t() | nil - def get_connection_string_value(key, config_element) do - storage_account_connection_string(config_element) + @spec get_connection_string_value(String.t()) :: String.t() | nil + def get_connection_string_value(key) do + storage_account_connection_string() |> parse_connection_string |> Map.get(key) end diff --git a/lib/azurex/blob/container.ex b/lib/azurex/blob/container.ex index 9baf0df..0546be7 100644 --- a/lib/azurex/blob/container.ex +++ b/lib/azurex/blob/container.ex @@ -5,15 +5,15 @@ defmodule Azurex.Blob.Container do alias Azurex.Blob.Config alias Azurex.Authorization.SharedKey - def head_container(container, conf_element \\ :azurex) do + def head_container(container) do %HTTPoison.Request{ - url: Config.api_url(conf_element) <> "/" <> container, + url: Config.api_url() <> "/" <> container, params: [restype: "container"], method: :head } |> SharedKey.sign( - storage_account_name: Config.storage_account_name(conf_element), - storage_account_key: Config.storage_account_key(conf_element) + storage_account_name: Config.storage_account_name(), + storage_account_key: Config.storage_account_key() ) |> HTTPoison.request() |> case do @@ -24,15 +24,15 @@ defmodule Azurex.Blob.Container do end end - def create(container, conf_element \\ :azurex) do + def create(container) do %HTTPoison.Request{ - url: Config.api_url(conf_element) <> "/" <> container, + url: Config.api_url() <> "/" <> container, params: [restype: "container"], method: :put } |> SharedKey.sign( - storage_account_name: Config.storage_account_name(conf_element), - storage_account_key: Config.storage_account_key(conf_element), + storage_account_name: Config.storage_account_name(), + storage_account_key: Config.storage_account_key(), content_type: "application/octet-stream" ) |> HTTPoison.request() diff --git a/lib/azurex/blob/shared_access_signature.ex b/lib/azurex/blob/shared_access_signature.ex index a17b2f1..d815a57 100644 --- a/lib/azurex/blob/shared_access_signature.ex +++ b/lib/azurex/blob/shared_access_signature.ex @@ -25,7 +25,7 @@ defmodule Azurex.Blob.SharedAccessSignature do """ @spec sas_url(String.t(), String.t(), [{atom(), any()}]) :: String.t() def sas_url(container, resource, opts \\ []) do - base_url = Azurex.Blob.Config.api_url(:azurex) + base_url = Azurex.Blob.Config.api_url() resource_type = Keyword.get(opts, :resource_type, :container) permissions = Keyword.get(opts, :permissions, [:read]) from = Keyword.get(opts, :from, DateTime.utc_now()) @@ -38,8 +38,8 @@ defmodule Azurex.Blob.SharedAccessSignature do resource, {from, expiry}, permissions, - Azurex.Blob.Config.storage_account_name(:azurex), - Azurex.Blob.Config.storage_account_key(:azurex) + Azurex.Blob.Config.storage_account_name(), + Azurex.Blob.Config.storage_account_key() ) "#{Path.join(base_url, resource)}?#{token}" diff --git a/test/azurex/blob/config_test.exs b/test/azurex/blob/config_test.exs index fea174e..8cac19b 100644 --- a/test/azurex/blob/config_test.exs +++ b/test/azurex/blob/config_test.exs @@ -21,17 +21,17 @@ defmodule Azurex.Blob.ConfigTest do test "returns configured env" do put_config(storage_account_name: "samplename") - assert storage_account_name(:azurex) == "samplename" + assert storage_account_name() == "samplename" end test "returns based on storage_account_connection_string env" do put_config(storage_account_connection_string: @sample_connection_string) - assert storage_account_name(:azurex) == "cs_samplename" + assert storage_account_name() == "cs_samplename" end test "error no env set" do put_config() - assert_raise RuntimeError, fn -> storage_account_name(:azurex) end + assert_raise RuntimeError, &storage_account_name/0 end end @@ -39,51 +39,51 @@ defmodule Azurex.Blob.ConfigTest do test "returns configured env" do put_config(storage_account_key: Base.encode64("sample key")) - assert storage_account_key(:azurex) == "sample key" + assert storage_account_key() == "sample key" end test "returns based on storage_account_connection_string env" do put_config(storage_account_connection_string: @sample_connection_string) - assert storage_account_key(:azurex) == "cs_sample_key" + assert storage_account_key() == "cs_sample_key" end test "error no env set" do put_config() - assert_raise RuntimeError, fn -> storage_account_key(:azurex) end + assert_raise RuntimeError, &storage_account_key/0 end end describe "default_container/0" do test "returns configured env" do Application.put_env(:azurex, Azurex.Blob.Config, default_container: "sample_container_name") - assert default_container(:azurex) == "sample_container_name" + assert default_container() == "sample_container_name" end test "env not set" do put_config() - assert_raise RuntimeError, fn -> default_container(:azures) end + assert_raise RuntimeError, &default_container/0 end end describe "api_url/0" do test "returns api_url from config" do - assert api_url(:azurex) == "http://127.0.0.1:10000/devstoreaccount1" + assert api_url() == "http://127.0.0.1:10000/devstoreaccount1" end test "returns configured env" do put_config(api_url: "https://example.com") - assert api_url(:azurex) == "https://example.com" + assert api_url() == "https://example.com" end test "returns url based on storage_account_name env" do put_config(storage_account_name: "sample-name") - assert api_url(:azurex) == "https://sample-name.blob.core.windows.net" + assert api_url() == "https://sample-name.blob.core.windows.net" end test "returns url based on storage_account_connection_string env" do put_config(storage_account_connection_string: @sample_connection_string) - assert api_url(:azurex) == "https://cs_samplename.blob.core.windows.net" + assert api_url() == "https://cs_samplename.blob.core.windows.net" end test "returns url based on BlobEndPoint in storage_account_connection_string env" do @@ -91,12 +91,12 @@ defmodule Azurex.Blob.ConfigTest do @sample_connection_string <> ";BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1" put_config(storage_account_connection_string: connection_string) - assert api_url(:azurex) == "http://127.0.0.1:10000/devstoreaccount1" + assert api_url() == "http://127.0.0.1:10000/devstoreaccount1" end test "error no env set" do put_config() - assert_raise RuntimeError, fn -> api_url(:azurex) end + assert_raise RuntimeError, &api_url/0 end end @@ -104,19 +104,19 @@ defmodule Azurex.Blob.ConfigTest do test "success" do put_config(storage_account_connection_string: "Key=value") - assert get_connection_string_value("Key", :azurex) == "value" + assert get_connection_string_value("Key") == "value" end test "env not in connection_string" do put_config(storage_account_connection_string: "Key=value") - assert get_connection_string_value("Invalid", :azurex) == nil + assert get_connection_string_value("Invalid") == nil end test "connection_string env not set" do put_config() - assert get_connection_string_value("Invalid", :azurex) == nil + assert get_connection_string_value("Invalid") == nil end end end diff --git a/test/azurex_test.exs b/test/azurex_test.exs index 038b815..ae70423 100644 --- a/test/azurex_test.exs +++ b/test/azurex_test.exs @@ -22,48 +22,12 @@ defmodule AzurexTest do url: _, params: [param1: "value1"], headers: [ - {"Authorization", authz_header}, + {"Authorization", _}, {"x-ms-version", _}, {"x-ms-date", _}, {"Content-Type", "application/json"} ], options: [recv_timeout: 60_000, timeout: 60_000] } = request - - expected_authz_prefix = "SharedKey dummystorageaccount" - - assert String.starts_with?(authz_header, expected_authz_prefix) == true, - "expected '#{authz_header}' to have prefix '#{expected_authz_prefix}'" - end - - test "HTTPoison request uses specified config element for storage account details" do - Application.put_env(:something_else, Azurex.Blob.Config, - storage_account_name: "another_account", - storage_account_key: - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==" - ) - - request = - Azurex.Blob.blob_request("name", "container", :get, - recv_timeout: 60_000, - timeout: 60_000, - params: [param1: "value1"], - headers: [{"Content-Type", "application/json"}], - config_element: :something_else - ) - - assert %HTTPoison.Request{ - headers: [ - {"Authorization", authz_header}, - {"x-ms-version", _}, - {"x-ms-date", _}, - {"Content-Type", _} - ] - } = request - - expected_authz_prefix = "SharedKey another_account" - - assert String.starts_with?(authz_header, expected_authz_prefix) == true, - "expected '#{authz_header}' to have prefix '#{expected_authz_prefix}'" end end diff --git a/test/integration/Readme.md b/test/integration/Readme.md index 9ffca77..a4033bb 100644 --- a/test/integration/Readme.md +++ b/test/integration/Readme.md @@ -24,19 +24,3 @@ https://azure.microsoft.com/en-us/products/storage/storage-explorer/#overview See how to set up Azurite here: https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azurite?tabs=visual-studio - -Some integration tests exercise the capability to inject different connection -strings (including storage accounts) per request. For this reason, to run the -full suite, we need 2 azurite instances: - -``` -mkdir tmp/azurite{1,2} - -# In one shell: -cd tmp/azurite1 -azurite # default flag values - -# In another shell: -cd /tmp/azurite2 -azurite -- --blobPort 11000 --queuePort 11001 --tablePort 11002 -``` diff --git a/test/integration/blob_integration_test.exs b/test/integration/blob_integration_test.exs index 32092ac..06377e7 100644 --- a/test/integration/blob_integration_test.exs +++ b/test/integration/blob_integration_test.exs @@ -7,16 +7,12 @@ defmodule Azurex.BlobIntegrationTests do @sample_file_contents "sample file\ncontents\n" @integration_testing_container "integrationtestingcontainer" - def setup_azurite1 do + setup do # set integration test env in case another test has overwritten it AzuriteSetup.set_env() - AzuriteSetup.create_test_containers() - AzuriteSetup.create_test_blob() end describe "upload and download a blob" do - setup do: setup_azurite1() - test "using default container" do blob_name = make_blob_name() @@ -91,8 +87,6 @@ defmodule Azurex.BlobIntegrationTests do end describe "head blob" do - setup do: setup_azurite1() - test "using default container" do blob_name = make_blob_name() @@ -131,8 +125,6 @@ defmodule Azurex.BlobIntegrationTests do end describe "copying a blob" do - setup do: setup_azurite1() - setup do blob_name = make_blob_name() @@ -155,8 +147,6 @@ defmodule Azurex.BlobIntegrationTests do end describe "list blobs" do - setup do: setup_azurite1() - test "simple, not checking result" do assert {:ok, _result_not_checked} = Blob.list_blobs() end @@ -172,16 +162,12 @@ defmodule Azurex.BlobIntegrationTests do end describe "test containers" do - setup do: setup_azurite1() - test "list containers" do assert {:ok, _results} = Blob.list_containers() end end describe "delete blob" do - setup do: setup_azurite1() - test "delete_blob/3 deletes the blob from the container" do blob_name = make_blob_name() @@ -197,69 +183,6 @@ defmodule Azurex.BlobIntegrationTests do end end - describe "overriding the storage account configuration" do - setup do - AzuriteSetup.set_env( - "AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=http://127.0.0.1:11000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:11001/devstoreaccount1;TableEndpoint=http://127.0.0.1:11002/devstoreaccount1", - :other_config - ) - - AzuriteSetup.create_test_containers(:other_config) - - # Put some dummy config in here pointing at an _incorrect_ account name. - # If we don't do this, and this test runs first, the test of the error - # case fails due to an exception being raised by the library about - # missing config. This is existing behaviour of the library that we - # didn't think needed to change. - AzuriteSetup.set_env( - "AccountName=idontexist;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;DefaultEndpointsProtocol=http;BlobEndpoint=http://127.0.0.1:12000/devstoreaccount1;QueueEndpoint=http://127.0.0.1:12001/devstoreaccount1;TableEndpoint=http://127.0.0.1:12002/devstoreaccount1", - :azurex - ) - end - - test "can use different config elements" do - blob_name = make_blob_name() - - assert Blob.put_blob( - blob_name, - @sample_file_contents, - "text/plain", - @integration_testing_container, - timeout: 10, - ignored_param: "ignored_param_value", - config_element: :other_config - ) == :ok - - assert Blob.get_blob( - blob_name, - @integration_testing_container, - timeout: 10, - config_element: :other_config - ) == {:ok, @sample_file_contents} - end - - test "cannot fetch blobs from one storage account that were written to another" do - blob_name = make_blob_name() - - assert Blob.put_blob( - blob_name, - @sample_file_contents, - "text/plain", - @integration_testing_container, - timeout: 10, - ignored_param: "ignored_param_value", - config_element: :other_config - ) == :ok - - assert {:error, _} = - Blob.get_blob( - blob_name, - @integration_testing_container, - timeout: 10 - ) - end - end - defp make_blob_name do escaped_time = DateTime.utc_now() diff --git a/test/support/azurite_setup.ex b/test/support/azurite_setup.ex index f5035c5..adb02a5 100644 --- a/test/support/azurite_setup.ex +++ b/test/support/azurite_setup.ex @@ -9,26 +9,26 @@ defmodule AzuriteSetup do @integration_testing_container "integrationtestingcontainer" @test_blob_name "test_blob" - def set_env(conn_str \\ @connection_string, config_element \\ :azurex) do - Application.put_env(config_element, Azurex.Blob.Config, + def set_env do + Application.put_env(:azurex, Azurex.Blob.Config, default_container: @default_container, - storage_account_connection_string: conn_str + storage_account_connection_string: @connection_string ) end - def create_test_containers(config_element \\ :azurex) do + def create_test_containers do Enum.each( [ @default_container, @integration_testing_container ], - &create_test_container(&1, config_element) + &create_test_container(&1) ) end - defp create_test_container(container, config_element) do + defp create_test_container(container) do container - |> Azurex.Blob.Container.create(config_element) + |> Azurex.Blob.Container.create() |> case do {:ok, _} -> :ok {:error, :already_exists} -> :ok diff --git a/test/test_helper.exs b/test/test_helper.exs index 7f28561..2044bf4 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1 +1,13 @@ ExUnit.start(exclude: [:integration]) + +Application.get_env(:ex_unit, :include) +|> Enum.member?(:integration) +|> case do + true -> + AzuriteSetup.set_env() + AzuriteSetup.create_test_containers() + AzuriteSetup.create_test_blob() + + false -> + :ok +end