-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Elasticsearch docker test #1827
Changes from 7 commits
f1f89a5
c7000c0
85f468d
00f8eed
82c1fcb
a8d68ff
23b93cc
1b19132
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(@elastic_cluster_url, @elastic_index_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd move the cluster url into your elasticsearch helper module as a default arg (so users don't need to specify it w/ every call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use default args in |
||
|
||
# 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this might need a volume to really behave nicely