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

Load check values from yaml #43

Merged
merged 3 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 26 additions & 1 deletion lib/wanda/catalog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ defmodule Wanda.Catalog do

alias Wanda.Catalog.{
Check,
Condition,
Expectation,
Fact
Fact,
Value
}

@default_severity :critical
Expand Down Expand Up @@ -38,6 +40,7 @@ defmodule Wanda.Catalog do
name: name,
severity: map_severity(check),
facts: Enum.map(facts, &map_fact/1),
values: map_values(check),
expectations: Enum.map(expectations, &map_expectation/1)
}
end
Expand Down Expand Up @@ -69,4 +72,26 @@ defmodule Wanda.Catalog do
argument: argument
}
end

defp map_values(%{"values" => values}) do
Enum.map(values, &map_value/1)
end

defp map_values(_), do: []

defp map_value(%{"name" => name, "default" => default} = value) do
conditions = Map.get(value, "conditions", [])
arbulu89 marked this conversation as resolved.
Show resolved Hide resolved

%Value{
name: name,
default: default,
conditions:
Enum.map(conditions, fn %{"value" => value, "when" => expression} ->
%Condition{
value: value,
expression: expression
}
end)
}
end
end
5 changes: 3 additions & 2 deletions lib/wanda/catalog/check.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ defmodule Wanda.Catalog.Check do
Represents a check.
"""

alias Wanda.Catalog.{Expectation, Fact}
alias Wanda.Catalog.{Expectation, Fact, Value}

defstruct [:id, :name, :severity, :facts, :expectations]
defstruct [:id, :name, :severity, :facts, :values, :expectations]

@type t :: %__MODULE__{
id: String.t(),
name: String.t(),
severity: :warning | :critical,
facts: [Fact.t()],
values: [Value.t()],
expectations: [Expectation.t()]
}
end
12 changes: 12 additions & 0 deletions lib/wanda/catalog/condition.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Wanda.Catalog.Condition do
@moduledoc """
Represents a condition.
"""

defstruct [:value, :expression]

@type t :: %__MODULE__{
value: boolean() | number() | String.t(),
expression: String.t()
}
end
15 changes: 15 additions & 0 deletions lib/wanda/catalog/value.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Wanda.Catalog.Value do
@moduledoc """
Represents a value.
"""

alias Wanda.Catalog.Condition

defstruct [:name, :default, :conditions]

@type t :: %__MODULE__{
name: String.t(),
default: boolean() | number() | String.t(),
conditions: [Condition.t()]
}
end
68 changes: 68 additions & 0 deletions test/catalog_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
defmodule Wanda.CatalogTest do
use ExUnit.Case

alias Wanda.Catalog

alias Wanda.Catalog.{
Check,
Condition,
Expectation,
Fact,
Value
}

describe "checks catalog" do
test "should load a check from a yaml file properly" do
assert %Check{
id: "expect_check",
name: "Test check",
severity: :critical,
facts: [
%Fact{
name: "corosync_token_timeout",
gatherer: "corosync",
argument: "totem.token"
}
],
values: [],
expectations: [
%Expectation{
name: "timeout",
type: :expect,
expression: "corosync_token_timeout == 30000"
}
]
} = Catalog.get_check("expect_check")
end

test "should load a critical as default severity" do
assert %Check{severity: :critical} = Catalog.get_check("expect_same_check")
end

test "should load a warning severity" do
assert %Check{severity: :warning} = Catalog.get_check("warning_severity_check")
end

test "should load checks with values" do
assert %Check{
values: [
%Value{
conditions: [
%Condition{expression: "some_expression", value: 10},
%Condition{expression: "some_other_expression", value: 15}
],
default: 5,
name: "expected_value"
},
%Value{
conditions: [
%Condition{expression: "some_third_expression", value: 5}
],
default: 10,
name: "expected_higher_value"
}
]
} = Catalog.get_check("with_values_check")
end
end
end
30 changes: 30 additions & 0 deletions test/fixtures/catalog/with_values_check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
id: with_values
name: Test check
group: Test
description: |
Just a check
remediation: |
## Remediation
Remediation text
facts:
- name: jedi
gatherer: wandalorian
argument: -o
values:
- name: expected_value
default: 5
conditions:
- value: 10
when: some_expression
- value: 15
when: some_other_expression
- name: expected_higher_value
default: 10
conditions:
- value: 5
when: some_third_expression
expectations:
- name: some_expectation
expect: jedi == values.expected_value
- name: some_other_expectation
expect: jedi > values.expected_higher_value