|
| 1 | +-module(oc_reporter_datadog_SUITE). |
| 2 | + |
| 3 | +-include_lib("opencensus/include/opencensus.hrl"). |
| 4 | +-include_lib("common_test/include/ct.hrl"). |
| 5 | +-include_lib("eunit/include/eunit.hrl"). |
| 6 | + |
| 7 | +-export([all/0, init_per_testcase/2]). |
| 8 | +-export([test_eunit/1, test_reports_spans/1]). |
| 9 | + |
| 10 | +all() -> [test_eunit, test_reports_spans]. |
| 11 | + |
| 12 | +init_per_testcase(_, Config) -> |
| 13 | + Span = #span{ |
| 14 | + name = <<"foo">>, |
| 15 | + trace_id = 1, |
| 16 | + span_id = 1, |
| 17 | + start_time = wts:timestamp(), |
| 18 | + end_time = wts:timestamp(), |
| 19 | + attributes = #{foo => "bar"} |
| 20 | + }, |
| 21 | + Options = oc_reporter_datadog:init([{http_client, fun mock_client/3}]), |
| 22 | + [{span, Span}, {options, Options} | Config]. |
| 23 | + |
| 24 | +mock_client(Address, Headers, JSON) -> |
| 25 | + self() ! {http, lists:flatten(Address), Headers, JSON}, |
| 26 | + ok. |
| 27 | + |
| 28 | +test_reports_spans(Config) -> |
| 29 | + Options = ?config(options, Config), |
| 30 | + Span = ?config(span, Config), |
| 31 | + oc_reporter_datadog:report([Span], Options), |
| 32 | + receive |
| 33 | + {http, "http://localhost:8126/v0.3/traces", _, JSON} -> |
| 34 | + Data = jsx:decode(JSON, [return_maps]), |
| 35 | + [[RSpan]] = Data, |
| 36 | + #{<<"name">> := <<"foo">>, |
| 37 | + <<"trace_id">> := 1, |
| 38 | + <<"span_id">> := 1, |
| 39 | + <<"type">> := <<"custom">>, |
| 40 | + <<"duration">> := _, |
| 41 | + <<"meta">> := #{<<"foo">> := <<"bar">>}} = RSpan, |
| 42 | + ok; |
| 43 | + Msg -> |
| 44 | + ct:fail("Unknown message: ~p", [Msg]) |
| 45 | + after |
| 46 | + 1000 -> ct:fail("Didn't received message in 1s") |
| 47 | + end. |
| 48 | + |
| 49 | +%% EUNIT TESTS ================================================================= |
| 50 | + |
| 51 | +test_eunit(_Config) -> eunit:test(?MODULE, []). |
| 52 | + |
| 53 | +init_return_value_contains_needed_keys_test_() -> |
| 54 | + Opts = oc_reporter_datadog:init([]), |
| 55 | + {"test that init value contains needed keys", inparallel, |
| 56 | + [?_assert(maps:is_key(service, Opts)), |
| 57 | + ?_assert(maps:is_key(host, Opts)), |
| 58 | + ?_assert(maps:is_key(port, Opts)), |
| 59 | + ?_assert(maps:is_key(type, Opts))]}. |
| 60 | + |
| 61 | +init_sets_proper_fields_to_requested_values_test_() -> |
| 62 | + {"test that init sets proper fields in resulting map", inparallel, |
| 63 | + [init_sets(host, "foo"), |
| 64 | + init_sets(port, 6666), |
| 65 | + init_sets(service, "foo"), |
| 66 | + init_sets(type, "foo")]}. |
| 67 | + |
| 68 | +init_sets(Key, Value) -> |
| 69 | + Opts = oc_reporter_datadog:init([{Key, Value}]), |
| 70 | + ?_assertEqual(Value, maps:get(Key, Opts)). |
0 commit comments