Skip to content
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

Serialize an ExecutionCompleted json cloud event #11

Merged
merged 5 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/wanda/execution/agent_check_result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Wanda.Execution.AgentCheckResult do

alias Wanda.Execution.{ExpectationEvaluation, ExpectationEvaluationError}

@derive Jason.Encoder
defstruct [
:agent_id,
:facts,
Expand Down
1 change: 1 addition & 0 deletions lib/wanda/execution/check_result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Wanda.Execution.CheckResult do

alias Wanda.Execution.{AgentCheckResult, ExpectationResult}

@derive Jason.Encoder
defstruct [
:check_id,
:expectation_results,
Expand Down
1 change: 1 addition & 0 deletions lib/wanda/execution/expectation_evaluation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Wanda.Execution.ExpectationEvaluation do
Represents the evaluation of an expectation.
"""

@derive Jason.Encoder
defstruct [
:name,
:return_value,
Expand Down
1 change: 1 addition & 0 deletions lib/wanda/execution/expectation_evaluation_error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Wanda.Execution.ExpectationEvaluationError do
Represents an error occurred during the evaluation of an expectation.
"""

@derive Jason.Encoder
defstruct [
:name,
:message,
Expand Down
1 change: 1 addition & 0 deletions lib/wanda/execution/expectation_result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Wanda.Execution.ExpectationResult do
Represents the result of an expectation.
"""

@derive Jason.Encoder
defstruct [
:name,
:result,
Expand Down
1 change: 1 addition & 0 deletions lib/wanda/execution/result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Wanda.Execution.Result do

alias Wanda.Execution.CheckResult

@derive Jason.Encoder
defstruct [
:execution_id,
:group_id,
Expand Down
8 changes: 7 additions & 1 deletion lib/wanda/messaging/adapters/amqp.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ defmodule Wanda.Messaging.Adapters.AMQP do

alias Wanda.Messaging.Adapters.AMQP.Publisher

alias Wanda.Messaging.Mapper

@impl true
def publish(routing_key, message), do: Publisher.publish_message(message, routing_key)
def publish(routing_key, message),
do:
message
|> Mapper.to_json()
|> Publisher.publish_message(routing_key)
end
29 changes: 28 additions & 1 deletion lib/wanda/messaging/mapper.ex
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
defmodule Wanda.Messaging.Mapper do
@moduledoc """
Maps messages to events.
Maps messages to and from events.
"""
alias Cloudevents.Format.V_1_0.Event, as: CloudEvent

alias Wanda.JsonSchema

@execution_completed_event "trento.checks.v1.ExecutionCompleted"

# TODO: move this in the contract repository, keep this module to map domain structure to events.

@spec from_json(binary()) :: {:ok, CloudEvent.t()} | {:error, any}
Expand All @@ -15,4 +17,29 @@ defmodule Wanda.Messaging.Mapper do
{:ok, cloud_event}
end
end

@spec to_json(struct()) :: binary() | {:error, any()}
def to_json(event) do
Comment on lines +21 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to return :ok or :error here, because conceptually nobody shouldn't be passing unsupported events to this function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I fully understand.
What do you mean? Could you elaborate a bit more?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The caller of this function should know if an event is supported or not, hence the tagged tuple is not needed here IMHO

with {:ok, {type, identifier}} <- extract_metadata(event),
{:ok, cloud_event} <- build_cloud_event(identifier, type, event) do
Cloudevents.to_json(cloud_event)
end
end

defp build_cloud_event(id, type, event) do
%{
"specversion" => "1.0",
"id" => id,
"type" => type,
"source" => "https://github.com/trento-project/wanda",
"data" => event
}
|> CloudEvent.from_map()
end

@spec extract_metadata(any()) :: {:ok, {String.t(), String.t()}} | {:error, any()}
defp extract_metadata(%Wanda.Execution.Result{execution_id: execution_id}),
do: {:ok, {@execution_completed_event, execution_id}}

defp extract_metadata(_), do: {:error, :unsupported_event}
end
53 changes: 53 additions & 0 deletions test/fixtures/events/execution_completed_succesfully.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"data": {
"check_results": [
{
"agents_check_results": [
{
"agent_id": "agent_1",
"expectation_evaluations": [
{
"name": "timeout",
"return_value": true,
"type": "expect"
}
],
"facts": {
"corosync_token_timeout": 30000
}
},
{
"agent_id": "agent_2",
"expectation_evaluations": [
{
"name": "timeout",
"return_value": true,
"type": "expect"
}
],
"facts": {
"corosync_token_timeout": 30000
}
}
],
"check_id": "expect_check",
"expectation_results": [
{
"name": "timeout",
"result": true,
"type": "expect"
}
],
"result": "passing"
}
],
"execution_id": "d9955082-42c9-4835-b53a-3a4d3b2e4a9c",
"group_id": "421af233-dbb2-4328-907e-8b355b00bde8",
"result": "passing"
},
"datacontenttype": "application/json",
"id": "d9955082-42c9-4835-b53a-3a4d3b2e4a9c",
"source": "https://github.com/trento-project/wanda",
"specversion": "1.0",
"type": "trento.checks.v1.ExecutionCompleted"
}
43 changes: 43 additions & 0 deletions test/messaging/mapper_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule Wanda.MapperTest do
@moduledoc false

use ExUnit.Case

import Wanda.Factory

alias Wanda.Messaging.Mapper

alias Wanda.Support.Messaging.DummyEvent

describe "serializing outgoing events" do
test "should produce an error for unsupported events" do
assert {:error, :unsupported_event} = Mapper.to_json(%DummyEvent{})
end

test "should produce a serialized ExecutionCompletedV1 cloud event" do
execution_result =
build(:execution_result,
execution_id: "d9955082-42c9-4835-b53a-3a4d3b2e4a9c",
group_id: "421af233-dbb2-4328-907e-8b355b00bde8"
)

expected_cloud_event =
File.read!("test/fixtures/events/execution_completed_succesfully.json")
|> String.replace(["\r", "\n", " "], "")

assert expected_cloud_event == Mapper.to_json(execution_result)
end

test "should produce an ExecutionCompletedV1 with a valid event data, as per the schema" do
execution_result = build(:execution_result)
event_type = "trento.checks.v1.ExecutionCompleted"

%{"data" => event_data, "type" => ^event_type} =
execution_result
|> Mapper.to_json()
|> Jason.decode!()

assert :ok = Wanda.JsonSchema.validate(event_type, event_data)
end
end
end
50 changes: 50 additions & 0 deletions test/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ defmodule Wanda.Factory do
use ExMachina

alias Wanda.Execution.{
AgentCheckResult,
CheckResult,
ExpectationEvaluation,
ExpectationResult,
Fact,
Result,
Target
}

Expand All @@ -22,4 +27,49 @@ defmodule Wanda.Factory do
value: Map.get(attrs, :value, Faker.StarWars.planet())
}
end

def execution_result_factory(attrs) do
%Result{
execution_id: Map.get(attrs, :execution_id, UUID.uuid4()),
group_id: Map.get(attrs, :group_id, UUID.uuid4()),
check_results: [
%CheckResult{
agents_check_results: [
%AgentCheckResult{
agent_id: "agent_1",
expectation_evaluations: [
%ExpectationEvaluation{
name: "timeout",
return_value: true,
type: :expect
}
],
facts: %{"corosync_token_timeout" => 30_000}
},
%AgentCheckResult{
agent_id: "agent_2",
expectation_evaluations: [
%ExpectationEvaluation{
name: "timeout",
return_value: true,
type: :expect
}
],
facts: %{"corosync_token_timeout" => 30_000}
}
],
check_id: "expect_check",
expectation_results: [
%ExpectationResult{
name: "timeout",
result: true,
type: :expect
}
],
result: :passing
}
],
result: :passing
}
end
end
7 changes: 7 additions & 0 deletions test/support/messaging/dummy_event.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Wanda.Support.Messaging.DummyEvent do
@moduledoc false

defstruct [
:some_property
]
end