|
| 1 | +defmodule Logflare.Backends.Adaptor.Last9AdaptorTest do |
| 2 | + use Logflare.DataCase, async: false |
| 3 | + |
| 4 | + alias Logflare.Backends |
| 5 | + alias Logflare.Backends.Adaptor |
| 6 | + alias Logflare.Backends.Adaptor.HttpBased |
| 7 | + alias Logflare.Backends.AdaptorSupervisor |
| 8 | + alias Logflare.SystemMetrics.AllLogsLogged |
| 9 | + alias Opentelemetry.Proto.Collector.Logs.V1.ExportLogsServiceRequest |
| 10 | + alias Opentelemetry.Proto.Collector.Logs.V1.ExportLogsServiceResponse |
| 11 | + |
| 12 | + @subject Adaptor.Last9Adaptor |
| 13 | + @tesla_adapter Tesla.Adapter.Finch |
| 14 | + |
| 15 | + @valid_config %{ |
| 16 | + region: "US-WEST-1", |
| 17 | + username: "testuser", |
| 18 | + password: "testpassword" |
| 19 | + } |
| 20 | + @valid_config_input Map.new(@valid_config, fn {k, v} -> {Atom.to_string(k), v} end) |
| 21 | + |
| 22 | + defp backend_data(_ctx) do |
| 23 | + user = insert(:user) |
| 24 | + source = insert(:source, user: user) |
| 25 | + |
| 26 | + backend = |
| 27 | + insert(:backend, |
| 28 | + type: :last9, |
| 29 | + sources: [source], |
| 30 | + config: @valid_config |
| 31 | + ) |
| 32 | + |
| 33 | + [backend: backend, source: source] |
| 34 | + end |
| 35 | + |
| 36 | + setup do |
| 37 | + start_supervised!(AllLogsLogged) |
| 38 | + insert(:plan) |
| 39 | + :ok |
| 40 | + end |
| 41 | + |
| 42 | + describe "config typecast and validation" do |
| 43 | + test "enforces required options" do |
| 44 | + changeset = Adaptor.cast_and_validate_config(@subject, %{}) |
| 45 | + refute changeset.valid? |
| 46 | + assert errors_on(changeset).region == ["can't be blank"] |
| 47 | + assert errors_on(changeset).username == ["can't be blank"] |
| 48 | + assert errors_on(changeset).password == ["can't be blank"] |
| 49 | + end |
| 50 | + |
| 51 | + test "validates region" do |
| 52 | + changeset = |
| 53 | + Adaptor.cast_and_validate_config(@subject, %{ |
| 54 | + @valid_config_input |
| 55 | + | "region" => "invalid-region" |
| 56 | + }) |
| 57 | + |
| 58 | + refute changeset.valid? |
| 59 | + assert errors_on(changeset).region == ["is invalid"] |
| 60 | + end |
| 61 | + |
| 62 | + test "accepts valid config" do |
| 63 | + changeset = |
| 64 | + Adaptor.cast_and_validate_config(@subject, @valid_config_input) |
| 65 | + |
| 66 | + assert changeset.valid? |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + describe "test_connection/1" do |
| 71 | + setup :backend_data |
| 72 | + |
| 73 | + test "succceeds on 200 response", ctx do |
| 74 | + response_body = |
| 75 | + %ExportLogsServiceResponse{partial_success: nil} |
| 76 | + |> Protobuf.encode() |
| 77 | + |
| 78 | + mock_adapter(2, fn env -> |
| 79 | + assert env.method == :post |
| 80 | + assert env.url == "https://otlp.last9.io/v1/logs" |
| 81 | + assert Tesla.get_header(env, "authorization") == "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk" |
| 82 | + |
| 83 | + {:ok, |
| 84 | + %Tesla.Env{ |
| 85 | + status: 200, |
| 86 | + body: response_body, |
| 87 | + headers: [{"content-type", "application/x-protobuf"}] |
| 88 | + }} |
| 89 | + end) |
| 90 | + |
| 91 | + assert :ok = @subject.test_connection({ctx.source, ctx.backend}) |
| 92 | + assert :ok = @subject.test_connection(ctx.backend) |
| 93 | + end |
| 94 | + |
| 95 | + test "returns error on failure", ctx do |
| 96 | + error_responses = [ |
| 97 | + {:ok, %Tesla.Env{status: 401, body: "forbidden"}}, |
| 98 | + {:error, :nxdomain} |
| 99 | + ] |
| 100 | + |
| 101 | + for response <- error_responses do |
| 102 | + mock_adapter(fn _env -> response end) |
| 103 | + assert {:error, _reason} = @subject.test_connection(ctx.backend) |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + describe "logs ingestion" do |
| 109 | + setup :backend_data |
| 110 | + |
| 111 | + setup %{source: source, backend: backend} do |
| 112 | + start_supervised!({AdaptorSupervisor, {source, backend}}) |
| 113 | + :timer.sleep(250) |
| 114 | + :ok |
| 115 | + end |
| 116 | + |
| 117 | + test "sends logs via REST API", %{source: source} do |
| 118 | + this = self() |
| 119 | + ref = make_ref() |
| 120 | + |
| 121 | + mock_adapter(fn env -> |
| 122 | + assert Tesla.build_url(env) == "https://otlp.last9.io/v1/logs" |
| 123 | + assert env.method == :post |
| 124 | + assert Tesla.get_header(env, "content-type") == "application/x-protobuf" |
| 125 | + assert Tesla.get_header(env, "authorization") == "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk" |
| 126 | + |
| 127 | + send(this, {ref, IO.iodata_to_binary(env.body)}) |
| 128 | + {:ok, %Tesla.Env{status: 200, body: ""}} |
| 129 | + end) |
| 130 | + |
| 131 | + log_events = build_list(3, :log_event, source: source) |
| 132 | + |
| 133 | + assert {:ok, _} = Backends.ingest_logs(log_events, source) |
| 134 | + assert_receive {^ref, body}, 5000 |
| 135 | + assert request = Protobuf.decode(body, ExportLogsServiceRequest) |
| 136 | + assert %{resource_logs: [%{scope_logs: [%{log_records: [_, _, _]}]}]} = request |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + describe "redact_config/1" do |
| 141 | + test "redacts username and password" do |
| 142 | + redacted_config = @subject.redact_config(@valid_config) |
| 143 | + assert redacted_config.password == "REDACTED" |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + defp mock_adapter(calls_num \\ 1, function) do |
| 148 | + stub(@tesla_adapter) |
| 149 | + |
| 150 | + HttpBased.Client |
| 151 | + |> expect(:new, calls_num, fn opts -> |
| 152 | + HttpBased.Client |
| 153 | + |> Mimic.call_original(:new, [opts]) |
| 154 | + |> Logflare.Tesla.MockAdapter.replace(function) |
| 155 | + end) |
| 156 | + end |
| 157 | +end |
0 commit comments