summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorILYA Khlopotov <iilyak@apache.org>2020-02-04 09:30:10 -0800
committerILYA Khlopotov <iilyak@apache.org>2020-02-04 10:02:38 -0800
commitd233f8179aa2716a921ae469ac9d44385274d95a (patch)
treeb634535a8cee37cc6d7f8393b3b9caa1fc450976
parent3b9f04db97bf07fddd65dadef5fb101f1c436510 (diff)
downloadcouchdb-d233f8179aa2716a921ae469ac9d44385274d95a.tar.gz
Add basic test case for b3 fix
-rw-r--r--src/chttpd/test/exunit/test_helper.exs2
-rw-r--r--src/chttpd/test/exunit/tracing_test.exs101
2 files changed, 103 insertions, 0 deletions
diff --git a/src/chttpd/test/exunit/test_helper.exs b/src/chttpd/test/exunit/test_helper.exs
new file mode 100644
index 000000000..314050085
--- /dev/null
+++ b/src/chttpd/test/exunit/test_helper.exs
@@ -0,0 +1,2 @@
+ExUnit.configure(formatters: [JUnitFormatter, ExUnit.CLIFormatter])
+ExUnit.start()
diff --git a/src/chttpd/test/exunit/tracing_test.exs b/src/chttpd/test/exunit/tracing_test.exs
new file mode 100644
index 000000000..b50ef936e
--- /dev/null
+++ b/src/chttpd/test/exunit/tracing_test.exs
@@ -0,0 +1,101 @@
+defmodule Couch.Test.OpenTracing do
+ use Couch.Test.ExUnit.Case
+ alias Couch.Test.Setup
+ alias Couch.Test.Setup.Step
+ alias Couch.Test.Utils
+ import Couch.DBTest, only: [retry_until: 1]
+
+ defp create_admin(user_name, password) do
+ hashed = String.to_charlist(:couch_passwords.hash_admin_password(password))
+ :config.set('admins', String.to_charlist(user_name), hashed, false)
+ end
+
+ defp base_url() do
+ addr = :config.get('chttpd', 'bind_address', '127.0.0.1')
+ port = :mochiweb_socket_server.get(:chttpd, :port)
+ "http://#{addr}:#{port}"
+ end
+
+ setup_all context do
+ test_ctx = :test_util.start_couch([:chttpd])
+ :ok = create_admin("adm", "pass")
+
+ Map.merge(context, %{
+ base_url: base_url(),
+ user: "adm",
+ pass: "pass"
+ })
+ end
+
+ setup context do
+ db_name = Utils.random_name("db")
+ session = Couch.login(context.base_url, context.user, context.pass)
+
+ on_exit(fn ->
+ delete_db(session, db_name)
+ end)
+
+ create_db(session, db_name)
+
+ Map.merge(context, %{
+ db_name: db_name,
+ session: session
+ })
+ end
+
+ def create_db(session, db_name, opts \\ []) do
+ retry_until(fn ->
+ resp = Couch.Session.put(session, "/#{db_name}", opts)
+ assert resp.status_code in [201, 202]
+ assert resp.body == %{"ok" => true}
+ {:ok, resp}
+ end)
+ end
+
+ def delete_db(session, db_name) do
+ retry_until(fn ->
+ resp = Couch.Session.delete(session, "/#{db_name}")
+ assert resp.status_code in [200, 202, 404]
+ {:ok, resp}
+ end)
+ end
+
+ def create_doc(session, db_name, body) do
+ retry_until(fn ->
+ resp = Couch.Session.post(session, "/#{db_name}", body: body)
+ assert resp.status_code in [201, 202]
+ assert resp.body["ok"]
+ {:ok, resp}
+ end)
+ end
+
+ defp trace_id() do
+ :couch_util.to_hex(:crypto.strong_rand_bytes(16))
+ end
+
+ defp span_id() do
+ :couch_util.to_hex(:crypto.strong_rand_bytes(8))
+ end
+
+ describe "Open Tracing" do
+ test "should return success with combined b3 header", ctx do
+ %{session: session, db_name: db_name} = ctx
+ doc = '{"mr": "rockoartischocko"}'
+ {:ok, _} = create_doc(session, db_name, doc)
+
+ resp =
+ retry_until(fn ->
+ b3 = "#{trace_id()}-#{span_id()}-#{span_id()}"
+
+ response =
+ Couch.Session.get(session, "/#{db_name}/_all_docs", headers: [b3: b3])
+
+ assert %HTTPotion.Response{} = response
+ response
+ end)
+
+ assert resp.status_code == 200, "Expected 200, got: #{resp.status_code}"
+ assert length(resp.body["rows"]) == 1
+ end
+ end
+end