-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix b3 header #2519
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
Merged
Merged
Fix b3 header #2519
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ExUnit.configure(formatters: [JUnitFormatter, ExUnit.CLIFormatter]) | ||
| ExUnit.start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It seems like the
create_adminfunction along with some of the other function below would be useful to have in the shared Couch library. Any particular reason to not put these functions into thecouch.exmodule?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.
Few concerns:
create_admintoCouchexibit a chicken and egg problem.Couchimplements HTTP interface. We cannot use HTTP interface without adding admin first. In context of eunit tests we always cheated and created admin via config manipulation.create_adminan exception and still add it toCouchinspite of it not being HTTP based interface. However it will introduce a confusion. People useCouchmodule from elixir integration tests and the function wouldn't work in the context of an integration test.When I was writing this test I considered using
Step.User.new(:admin, roles: [:server_admin]). However since pluguble adapters [1] didn't make it to the framework and [2] doesn't have fabric2 support, so I had to do it differently. I followed the advise of the committers who believe that DRY is bad for tests and they should be as self contained as possible.[1]: #2036 - plugable adaptors based approach
[2]: #2039 - fabric only interface
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.
Ahhhh ok, thanks for the detailed explanation @iilyak 👍. It's outside the scope of this PR, but I wonder if it would make sense to add a tag like
@tag :with_adminlike we've done with dbs and what not [1], to inject an admin user context into the test and then we can abstract away whether the admin user was created through config or the http interface. As for framework vs anti DRY, initially with the Elixir suite we tried to avoid making a heavy framework, which I think is worthwhile, but at the same time there's common activities which should be easy to take care of, like creating dbs/docs/users/ddocs/etc.[1] https://github.com/apache/couchdb/blob/master/test/elixir/test/security_validation_test.exs#L151-L153