Replace the protobuf hex package (agent.pb.ex) and hand-written validator
(validator.ex) with a pure Gleam implementation: wire format primitives,
all 23 message types, encode/decode functions, and validation merged into
decode. Thin Elixir wrappers in agent.ex preserve the existing struct API.
- Wire format: varint, length-delimited, double (IEEE 754 via FFI), tag encode/decode
- Types: all proto3 message types as Gleam custom types with enums and oneof
- Decode: field-level parsing with integrated validation (UUID, IP, hostname, limits)
- Encode: proto3 default omission, bytes_tree concatenation, map/repeated fields
- Wrappers: Elixir structs with encode/decode delegating to Gleam, enum atom conversion
- Remove {:protobuf, "~> 0.12"} dependency
Reviewed-on: graham/towerops-web#104
89 lines
2.2 KiB
Elixir
89 lines
2.2 KiB
Elixir
defmodule ToweropsWeb.AgentChannelCheckProtoTest do
|
|
use ExUnit.Case, async: true
|
|
|
|
alias Towerops.Agent.Check, as: CheckProto
|
|
alias Towerops.Agent.CheckList
|
|
alias Towerops.Agent.DnsCheckConfig
|
|
alias Towerops.Agent.HttpCheckConfig
|
|
alias Towerops.Agent.SslCheckConfig
|
|
alias Towerops.Agent.TcpCheckConfig
|
|
|
|
describe "Check protobuf encoding" do
|
|
test "encodes HTTP check correctly" do
|
|
check = %CheckProto{
|
|
id: "test-id",
|
|
check_type: "http",
|
|
interval_seconds: 60,
|
|
timeout_ms: 5000,
|
|
http: %HttpCheckConfig{
|
|
url: "https://example.com",
|
|
method: "GET",
|
|
expected_status: 200,
|
|
verify_ssl: true,
|
|
regex: "",
|
|
follow_redirects: true
|
|
}
|
|
}
|
|
|
|
binary = CheckList.encode(%CheckList{checks: [check]})
|
|
assert is_binary(binary)
|
|
assert byte_size(binary) > 0
|
|
end
|
|
|
|
test "encodes TCP check correctly" do
|
|
check = %CheckProto{
|
|
id: "test-id",
|
|
check_type: "tcp",
|
|
interval_seconds: 60,
|
|
timeout_ms: 5000,
|
|
tcp: %TcpCheckConfig{
|
|
host: "example.com",
|
|
port: 443,
|
|
send: "",
|
|
expect: ""
|
|
}
|
|
}
|
|
|
|
binary = CheckList.encode(%CheckList{checks: [check]})
|
|
assert is_binary(binary)
|
|
assert byte_size(binary) > 0
|
|
end
|
|
|
|
test "encodes DNS check correctly" do
|
|
check = %CheckProto{
|
|
id: "test-id",
|
|
check_type: "dns",
|
|
interval_seconds: 60,
|
|
timeout_ms: 5000,
|
|
dns: %DnsCheckConfig{
|
|
hostname: "example.com",
|
|
server: "8.8.8.8",
|
|
record_type: "A",
|
|
expected: "1.2.3.4"
|
|
}
|
|
}
|
|
|
|
binary = CheckList.encode(%CheckList{checks: [check]})
|
|
assert is_binary(binary)
|
|
assert byte_size(binary) > 0
|
|
end
|
|
|
|
test "encodes SSL check correctly" do
|
|
check = %CheckProto{
|
|
id: "test-id",
|
|
check_type: "ssl",
|
|
interval_seconds: 60,
|
|
timeout_ms: 5000,
|
|
ssl: %SslCheckConfig{
|
|
host: "example.com",
|
|
port: 443,
|
|
warning_days: 30
|
|
}
|
|
}
|
|
|
|
binary = CheckList.encode(%CheckList{checks: [check]})
|
|
assert is_binary(binary)
|
|
assert byte_size(binary) > 0
|
|
end
|
|
end
|
|
end
|