summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorILYA Khlopotov <iilyak@apache.org>2020-02-04 09:30:10 -0800
committerPaul J. Davis <paul.joseph.davis@gmail.com>2020-03-02 12:26:22 -0600
commit8bcf5667cc30071b61dd140e413f59e421624789 (patch)
tree6e5f63719193424e6ec77ce767ddc1a2e1818f38
parentb4a7f6dc8c18f139ce2bc02691268eaf61a16e28 (diff)
downloadcouchdb-8bcf5667cc30071b61dd140e413f59e421624789.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