diff --git a/config/test.exs b/config/test.exs index 797dbe8cb0..9c996c22c4 100644 --- a/config/test.exs +++ b/config/test.exs @@ -154,3 +154,7 @@ config :console, Console.Deployments.Metrics.Provider.NewRelic, plug: {Req.Test, Console.Deployments.Metrics.Provider.NewRelic} config :bamboo, :refute_timeout, 10 + +config :elasticsearch_test, + cluster_url: "http://localhost:9200", + index_name: "testindex" diff --git a/docker-compose.yml b/docker-compose.yml index fa413f6ec1..b0f3df28fb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,20 @@ services: POSTGRES_PASSWORD: postgres volumes: - database_data:/var/lib/postgresql/data + es: + image: elasticsearch:8.8.0 + restart: always + ports: + - 9200:9200 + environment: + - node.name=es01 + - cluster.name=docker-es-cluster + - cluster.initial_master_nodes=es01 + - xpack.security.enabled=false + volumes: + - es_data:/usr/share/elasticsearch/data volumes: database_data: + driver: local + es_data: driver: local \ No newline at end of file diff --git a/lib/console/logs/provider/elastic.ex b/lib/console/logs/provider/elastic.ex index 78f17418c6..5bab03278c 100644 --- a/lib/console/logs/provider/elastic.ex +++ b/lib/console/logs/provider/elastic.ex @@ -92,6 +92,12 @@ defmodule Console.Logs.Provider.Elastic do defp add_facets(q, _), do: q defp facets(resp) do + # this populates kubernetes.node field with an empty map if doesn't already exist + resp = case resp do + %{"kubernetes" => %{"node" => %{}}} -> resp + _ -> put_in(resp, ~w(kubernetes node), %{}) + end + put_in(resp, ~w(kubernetes node labels), nil) |> put_in(~w(kubernetes labels), nil) |> Map.take(~w(kubernetes cloud container cluster)) diff --git a/lib/mix/tasks/elasticsearch_setup.ex b/lib/mix/tasks/elasticsearch_setup.ex new file mode 100644 index 0000000000..52ad9965ce --- /dev/null +++ b/lib/mix/tasks/elasticsearch_setup.ex @@ -0,0 +1,31 @@ +defmodule Mix.Tasks.ElasticsearchSetup do + use Mix.Task + require Tesla + + @elastic_cluster_url Application.compile_env(:elasticsearch_test, :cluster_url) + @elastic_index_name Application.compile_env(:elasticsearch_test, :index_name) + + def run(_) do + HTTPoison.start() + with true <- index_exists?(@elastic_cluster_url, @elastic_index_name) do + IO.puts("Elasticsearch index #{@elastic_index_name} already exists at #{@elastic_cluster_url}. Will delete") + delete_index(@elastic_cluster_url, @elastic_index_name) + IO.puts("Deleted index #{@elastic_index_name} at #{@elastic_cluster_url}") + end + + create_index(@elastic_cluster_url, @elastic_index_name) + IO.puts("Created fresh index #{@elastic_index_name} at #{@elastic_cluster_url}") + end + + def index_exists?(base_url, index_name) do + Tesla.get!(base_url <> "/#{index_name}").status == 200 + end + + def delete_index(base_url, index_name) do + Tesla.delete!(base_url <> "/#{index_name}") + end + + def create_index(base_url, index_name) do + HTTPoison.put!(base_url <> "/#{index_name}", "", []) + end +end diff --git a/lib/mix/tasks/elasticsearch_teardown.ex b/lib/mix/tasks/elasticsearch_teardown.ex new file mode 100644 index 0000000000..05a127004c --- /dev/null +++ b/lib/mix/tasks/elasticsearch_teardown.ex @@ -0,0 +1,16 @@ +defmodule Mix.Tasks.ElasticsearchTeardown do + use Mix.Task + require Tesla + + @elastic_cluster_url Application.compile_env(:elasticsearch_test, :cluster_url) + @elastic_index_name Application.compile_env(:elasticsearch_test, :index_name) + + def run(_) do + delete_index(@elastic_cluster_url, @elastic_index_name) + IO.puts("Deleted index #{@elastic_index_name} at #{@elastic_cluster_url}") + end + + def delete_index(base_url, index_name) do + Tesla.delete!(base_url <> "/#{index_name}") + end +end diff --git a/mix.exs b/mix.exs index 6ee4f98f54..e815d9f1b5 100644 --- a/mix.exs +++ b/mix.exs @@ -164,6 +164,7 @@ defmodule Console.MixProject do {:bamboo, "~> 2.3", override: true}, {:hut, "~> 1.3", manager: :rebar3, override: true}, {:ex_doc, "~> 0.16", only: :dev, runtime: false, override: true}, + {:tesla, "~> 1.13"}, # if using the Mint adapter: {:castore, "~> 1.0", override: true}, @@ -181,7 +182,7 @@ defmodule Console.MixProject do [ "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"], "ecto.reset": ["ecto.drop", "ecto.setup"], - test: ["ecto.create --quiet", "ecto.migrate", "test"] + test: ["ecto.create --quiet", "ecto.migrate", "elasticsearch_setup", "test", "elasticsearch_teardown"] ] end end diff --git a/mix.lock b/mix.lock index 96ee4c7d40..291206fbe4 100644 --- a/mix.lock +++ b/mix.lock @@ -8,6 +8,7 @@ "accessible": {:hex, :accessible, "0.3.0", "932f6ade6ea839212fea1735ab4ffdc39a3843a622f2ea5d28f0ab41c14b4435", [:mix], [], "hexpm", "13a11b0611ab82f7b9098a88465b5674f729c02bd613216243c123c65f90f296"}, "apq": {:hex, :apq, "2.0.1", "955cd8e28ff8213097d54e171764141cb76c2d7977027b3e6697af2bef4c8e29", [:mix], [{:absinthe_plug, "~> 1.5", [hex: :absinthe_plug, repo: "hexpm", optional: false]}], "hexpm", "22d9bb29d61901770275fe2255c3da8bf31552a503ba55558dbbad53a371012c"}, "argon2_elixir": {:hex, :argon2_elixir, "4.1.0", "2f242afe47c373663cb404eb75e792f749507075ed737b49685a9f2edcb401df", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "2ecb6f2ca2cca34b28e546224661bf2a85714516d2713c7313c5ffe8bdade7cf"}, + "aws_auth": {:hex, :aws_auth, "0.7.2", "ae67eb824d6f4699759fa6eaa6955008b2f11beafdf3254ec8ab3469d12e6fa3", [:mix], [], "hexpm", "8b01e9e6fa41ef9f06b3a01a9add4953453c32fada8694fe3bb4028776e83f28"}, "bamboo": {:hex, :bamboo, "2.3.1", "85029339f01c3dd59071cfd2b7b18e826aa7fc172cc324d75603bb99d95b6544", [:mix], [{:hackney, ">= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.4 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "895b2993ed195b2b0fa79c0d5a1d36aa529e817b6df257e4a10745459048d505"}, "bamboo_phoenix": {:hex, :bamboo_phoenix, "1.0.0", "f3cc591ffb163ed0bf935d256f1f4645cd870cf436545601215745fb9cc9953f", [:mix], [{:bamboo, ">= 2.0.0", [hex: :bamboo, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.3.0", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "6db88fbb26019c84a47994bb2bd879c0887c29ce6c559bc6385fd54eb8b37dee"}, "bamboo_smtp": {:hex, :bamboo_smtp, "4.2.2", "e9f57a2300df9cb496c48751bd7668a86a2b89aa2e79ccaa34e0c46a5f64c3ae", [:mix], [{:bamboo, "~> 2.2.0", [hex: :bamboo, repo: "hexpm", optional: false]}, {:gen_smtp, "~> 1.2.0", [hex: :gen_smtp, repo: "hexpm", optional: false]}], "hexpm", "28cac2ec8adaae02aed663bf68163992891a3b44cfd7ada0bebe3e09bed7207f"}, @@ -26,7 +27,7 @@ "crontab": {:hex, :crontab, "1.1.13", "3bad04f050b9f7f1c237809e42223999c150656a6b2afbbfef597d56df2144c5", [:mix], [{:ecto, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "d67441bec989640e3afb94e123f45a2bc42d76e02988c9613885dc3d01cf7085"}, "dataloader": {:hex, :dataloader, "2.0.1", "fa06b057b432b993203003fbff5ff040b7f6483a77e732b7dfc18f34ded2634f", [:mix], [{:ecto, ">= 3.4.3 and < 4.0.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:opentelemetry_process_propagator, "~> 0.2.1 or ~> 0.3", [hex: :opentelemetry_process_propagator, repo: "hexpm", optional: true]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "da7ff00890e1b14f7457419b9508605a8e66ae2cc2d08c5db6a9f344550efa11"}, "db_connection": {:hex, :db_connection, "2.7.0", "b99faa9291bb09892c7da373bb82cba59aefa9b36300f6145c5f201c7adf48ec", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "dcf08f31b2701f857dfc787fbad78223d61a32204f217f15e881dd93e4bdd3ff"}, - "decimal": {:hex, :decimal, "2.2.0", "df3d06bb9517e302b1bd265c1e7f16cda51547ad9d99892049340841f3e15836", [:mix], [], "hexpm", "af8daf87384b51b7e611fb1a1f2c4d4876b65ef968fa8bd3adf44cff401c7f21"}, + "decimal": {:hex, :decimal, "2.3.0", "3ad6255aa77b4a3c4f818171b12d237500e63525c2fd056699967a3e7ea20f62", [:mix], [], "hexpm", "a4d66355cb29cb47c3cf30e71329e58361cfcb37c34235ef3bf1d7bf3773aeac"}, "decorator": {:hex, :decorator, "1.4.0", "a57ac32c823ea7e4e67f5af56412d12b33274661bb7640ec7fc882f8d23ac419", [:mix], [], "hexpm", "0a07cedd9083da875c7418dea95b78361197cf2bf3211d743f6f7ce39656597f"}, "deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"}, "earmark": {:hex, :earmark, "1.4.10", "bddce5e8ea37712a5bfb01541be8ba57d3b171d3fa4f80a0be9bcf1db417bcaf", [:mix], [{:earmark_parser, ">= 1.4.10", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "12dbfa80810478e521d3ffb941ad9fbfcbbd7debe94e1341b4c4a1b2411c1c27"}, @@ -35,6 +36,8 @@ "ecto_enum": {:hex, :ecto_enum, "1.4.0", "d14b00e04b974afc69c251632d1e49594d899067ee2b376277efd8233027aec8", [:mix], [{:ecto, ">= 3.0.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:ecto_sql, "> 3.0.0", [hex: :ecto_sql, repo: "hexpm", optional: false]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, repo: "hexpm", optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "8fb55c087181c2b15eee406519dc22578fa60dd82c088be376d0010172764ee4"}, "ecto_explain": {:hex, :ecto_explain, "0.1.2", "a9d504cbd4adc809911f796d5ef7ebb17a576a6d32286c3d464c015bd39d5541", [:mix], [], "hexpm", "1d0e7798ae30ecf4ce34e912e5354a0c1c832b7ebceba39298270b9a9f316330"}, "ecto_sql": {:hex, :ecto_sql, "3.12.1", "c0d0d60e85d9ff4631f12bafa454bc392ce8b9ec83531a412c12a0d415a3a4d0", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.12", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "aff5b958a899762c5f09028c847569f7dfb9cc9d63bdb8133bff8a5546de6bf5"}, + "elastic": {:hex, :elastic, "3.6.0", "9d126bcc5d33bb6cc004847bf35b33b8a88d5bd75789356343729e71676a27f2", [:mix], [{:aws_auth, "~> 0.7.1", [hex: :aws_auth, repo: "hexpm", optional: false]}, {:httpotion, "~> 3.1", [hex: :httpotion, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "40f158705cfb166e1ff51915d6a1a2604bc2cf77633fe7da306b4f2ff704f043"}, + "elasticsearch": {:hex, :elasticsearch, "1.0.1", "8339538d90af6b280f10ecd02b1eae372f09373e629b336a13461babf7366495", [:mix], [{:httpoison, ">= 0.0.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:poison, ">= 0.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:sigaws, "~> 0.7", [hex: :sigaws, repo: "hexpm", optional: true]}, {:vex, "~> 0.6", [hex: :vex, repo: "hexpm", optional: false]}], "hexpm", "83e7d8b8bee3e7e19a06ab4d357d24845ac1da894e79678227fd52c0b7f71867"}, "elixir_make": {:hex, :elixir_make, "0.9.0", "6484b3cd8c0cee58f09f05ecaf1a140a8c97670671a6a0e7ab4dc326c3109726", [:mix], [], "hexpm", "db23d4fd8b757462ad02f8aa73431a426fe6671c80b200d9710caf3d1dd0ffdb"}, "elixpath": {:git, "https://github.com/mtannaan/elixpath.git", "78d301bb82f8bd74a120399f16b905b9a4b25468", []}, "erlsom": {:hex, :erlsom, "1.5.1", "c8fe2babd33ff0846403f6522328b8ab676f896b793634cfe7ef181c05316c03", [:rebar3], [], "hexpm", "7965485494c5844dd127656ac40f141aadfa174839ec1be1074e7edf5b4239eb"}, @@ -64,8 +67,10 @@ "hash_ring": {:hex, :hash_ring, "0.4.2", "65d7e84ee911c2f07e53193711013ec9b9761c5eb180720e31224b776c98f502", [:rebar3], [], "hexpm", "c327d99b1e2af41b50e5c4803a9bee7932db2c0f39ad7854a9fdad7e42444066"}, "hpax": {:hex, :hpax, "1.0.0", "28dcf54509fe2152a3d040e4e3df5b265dcb6cb532029ecbacf4ce52caea3fd2", [:mix], [], "hexpm", "7f1314731d711e2ca5fdc7fd361296593fc2542570b3105595bb0bc6d0fad601"}, "http_stream": {:hex, :http_stream, "1.0.0", "d40cc69eefb380508c01e004571159c26779902564558bffe1517a57764e019e", [:mix], [{:castore, "~> 0.1.7", [hex: :castore, repo: "hexpm", optional: true]}, {:httpoison, "~> 1.7.0", [hex: :httpoison, repo: "hexpm", optional: true]}, {:mint, "~> 1.1.0", [hex: :mint, repo: "hexpm", optional: true]}], "hexpm", "efa64a8800b7365a6e77b0647b289b9fa41fbbbb44a117c8ddc9b8c6fc69e126"}, - "httpoison": {:hex, :httpoison, "1.8.1", "df030d96de89dad2e9983f92b0c506a642d4b1f4a819c96ff77d12796189c63e", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "35156a6d678d6d516b9229e208942c405cf21232edd632327ecfaf4fd03e79e0"}, + "httpoison": {:hex, :httpoison, "1.8.2", "9eb9c63ae289296a544842ef816a85d881d4a31f518a0fec089aaa744beae290", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "2bb350d26972e30c96e2ca74a1aaf8293d61d0742ff17f01e0279fef11599921"}, + "httpotion": {:hex, :httpotion, "3.1.0", "14d20d9b0ce4e86e253eb91e4af79e469ad949f57a5d23c0a51b2f86559f6589", [:mix], [{:ibrowse, "~> 4.4", [hex: :ibrowse, repo: "hexpm", optional: false]}], "hexpm", "2e1f3da5398258f67be9522793c2ccef157d3c9f7a4f69ec8e87184393efe9e0"}, "hut": {:hex, :hut, "1.4.0", "7a1238ec00f95c9ec75412587ee11ac652eca308a7f4b8cc9629746d579d6cf0", [:"erlang.mk", :rebar3], [], "hexpm", "7af8704b9bae98a336f70d9560fc3c97f15665265fa603dbd05352e63d6ebb03"}, + "ibrowse": {:hex, :ibrowse, "4.4.2", "7fe943ba6cb88514dca631c7408c4a7897ce69fb0905244e2bfbc839d42f8d45", [:rebar3], [], "hexpm", "f088cee1faf6514b18c7783e8bc64c628d140a239786dc1f58fe9766e9584f41"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, "inet_cidr": {:hex, :inet_cidr, "1.0.4", "a05744ab7c221ca8e395c926c3919a821eb512e8f36547c062f62c4ca0cf3d6e", [:mix], [], "hexpm", "64a2d30189704ae41ca7dbdd587f5291db5d1dda1414e0774c29ffc81088c1bc"}, "inflex": {:hex, :inflex, "2.0.0", "db69d542b8fdb23ac667f9bc0c2395a3983fa2da6ae2efa7ab5dc541928f7a75", [:mix], [], "hexpm", "c018852409bd48b03ad96ed53594186bc074bdd1519043a0ad1fa5697aac4399"}, @@ -133,6 +138,7 @@ "tzdata": {:hex, :tzdata, "1.1.1", "20c8043476dfda8504952d00adac41c6eda23912278add38edc140ae0c5bcc46", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a69cec8352eafcd2e198dea28a34113b60fdc6cb57eb5ad65c10292a6ba89787"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, "uniq": {:hex, :uniq, "0.6.1", "369660ecbc19051be526df3aa85dc393af5f61f45209bce2fa6d7adb051ae03c", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "6426c34d677054b3056947125b22e0daafd10367b85f349e24ac60f44effb916"}, + "vex": {:hex, :vex, "0.9.2", "fe061acc9e0907d983d46b51bf35d58176f0fe6eb7ba3b33c9336401bf42b6d1", [:mix], [], "hexpm", "76e709a9762e98c6b462dfce92e9b5dfbf712839227f2da8add6dd11549b12cb"}, "websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"}, "websock_adapter": {:hex, :websock_adapter, "0.5.8", "3b97dc94e407e2d1fc666b2fb9acf6be81a1798a2602294aac000260a7c4a47d", [:mix], [{:bandit, ">= 0.6.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "315b9a1865552212b5f35140ad194e67ce31af45bcee443d4ecb96b5fd3f3782"}, "websockex": {:hex, :websockex, "0.4.3", "92b7905769c79c6480c02daacaca2ddd49de936d912976a4d3c923723b647bf0", [:mix], [], "hexpm", "95f2e7072b85a3a4cc385602d42115b73ce0b74a9121d0d6dbbf557645ac53e4"}, diff --git a/test/console/graphql/queries/observability_queries_test.exs b/test/console/graphql/queries/observability_queries_test.exs index 32e7b1a415..f532bc27d4 100644 --- a/test/console/graphql/queries/observability_queries_test.exs +++ b/test/console/graphql/queries/observability_queries_test.exs @@ -3,9 +3,13 @@ defmodule Console.GraphQl.ObservabilityQueriesTest do alias Kube.Dashboard use Mimic import KubernetesScaffolds + import ElasticsearchUtils setup :set_mimic_global + @elastic_cluster_url Application.compile_env(:elasticsearch_test, :cluster_url) + @elastic_index_name Application.compile_env(:elasticsearch_test, :index_name) + describe "dashboards" do test "it can list dashboards for a repo" do user = insert(:user) @@ -192,6 +196,33 @@ defmodule Console.GraphQl.ObservabilityQueriesTest do assert line["log"] == "a log" end + test "it can fetch logs from an elasticsearch index" do + user = insert(:user) + svc = insert(:service, read_bindings: [%{user_id: user.id}]) + + # the index gets set up using a mix task before the test is run, so we can index directly + log_document(svc, "valid log message") |> index_doc() + log_document(svc, "another valid log message") |> index_doc() + refresh() + + # Instead of using expect to mock the logs provider, we use the elasticsearch index + deployment_settings(logging: %{enabled: true, driver: :elastic, elastic: %{ + host: @elastic_cluster_url, + index: @elastic_index_name + }}) + + + {:ok, %{data: %{"logAggregation" => [first_line, second_line]}}} = run_query(""" + query Logs($serviceId: ID!) { + logAggregation(serviceId: $serviceId) { timestamp log } + } + """, %{"serviceId" => svc.id}, %{current_user: user}) + + # reverse chronological order + assert first_line["log"] == "another valid log message" + assert second_line["log"] == "valid log message" + end + test "it will authz" do user = insert(:user) svc = insert(:service) diff --git a/test/support/elasticsearch_utils.ex b/test/support/elasticsearch_utils.ex new file mode 100644 index 0000000000..af2a976b73 --- /dev/null +++ b/test/support/elasticsearch_utils.ex @@ -0,0 +1,35 @@ +defmodule ElasticsearchUtils do + @moduledoc """ + Helper functions for elasticsearch. This uses Tesla and HTTPoison to directly make the HTTP requests. + In all cases, base_url is the url for the cluster (ex. if running locally, something like "http://localhost:9200"), + and index_name is the name of the index to use. + """ + + require Tesla + + @default_cluster_url Application.compile_env(:elasticsearch_test, :cluster_url) + @default_index_name Application.compile_env(:elasticsearch_test, :index_name) + + def log_document(service, message) do + %{ + "@timestamp" => Timex.now(), + "message" => message, + "kubernetes" => %{ + "namespace" => service.namespace + }, + "cluster" => %{ + "handle" => service.cluster.handle + } + } + end + + def index_doc(doc, base_url \\ @default_cluster_url, index_name \\ @default_index_name) do + HTTPoison.post!(base_url <> "/#{index_name}/_doc", Jason.encode!(doc), + "Content-Type": "application/json" + ) + end + + def refresh(base_url \\ @default_cluster_url, index_name \\ @default_index_name) do + HTTPoison.post!(base_url <> "/#{index_name}/_refresh", "") + end +end