From c3861d150693cbc2aaf5d70ea4c4e086b26d06b9 Mon Sep 17 00:00:00 2001 From: Zack Siri Date: Mon, 23 Dec 2024 16:05:52 +0700 Subject: [PATCH] Add test case for routing merging --- lib/uplink/clients/caddy/config/builder.ex | 6 +++-- test/scenarios/deployment.ex | 8 ++++++- .../clients/caddy/config/builder_test.exs | 24 ++++++++++++------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/uplink/clients/caddy/config/builder.ex b/lib/uplink/clients/caddy/config/builder.ex index 3e0b9e7..455c363 100644 --- a/lib/uplink/clients/caddy/config/builder.ex +++ b/lib/uplink/clients/caddy/config/builder.ex @@ -137,7 +137,7 @@ defmodule Uplink.Clients.Caddy.Config.Builder do end main_paths = - if main_routing do + if main_routing && main_routing.paths != [] do main_routing.paths else ["*"] @@ -198,6 +198,7 @@ defmodule Uplink.Clients.Caddy.Config.Builder do metadata.hosts |> Enum.concat(main_routing_hosts) |> Enum.uniq() + |> Enum.sort() main_route = %{ group: main_group, @@ -258,9 +259,10 @@ defmodule Uplink.Clients.Caddy.Config.Builder do hosts |> Enum.concat(routing_hosts) |> Enum.uniq() + |> Enum.sort() paths = - if routing do + if routing && routing.paths != [] do routing.paths else ["*"] diff --git a/test/scenarios/deployment.ex b/test/scenarios/deployment.ex index 0cded55..ff17097 100644 --- a/test/scenarios/deployment.ex +++ b/test/scenarios/deployment.ex @@ -24,6 +24,7 @@ defmodule Uplink.Scenarios.Deployment do "target" => 4000, "routing" => %{ "router_id" => 1, + "hosts" => ["another.com", "something.com"], "paths" => ["/configure*"] } }, @@ -31,7 +32,12 @@ defmodule Uplink.Scenarios.Deployment do %{ "slug" => "grpc", "source" => 49153, - "target" => 6000 + "target" => 6000, + "routing" => %{ + "router_id" => 1, + "hosts" => ["another.com", "something.com"], + "paths" => ["/*"] + } } ], "hosts" => ["something.com"], diff --git a/test/uplink/clients/caddy/config/builder_test.exs b/test/uplink/clients/caddy/config/builder_test.exs index 25b3546..d29764e 100644 --- a/test/uplink/clients/caddy/config/builder_test.exs +++ b/test/uplink/clients/caddy/config/builder_test.exs @@ -66,32 +66,38 @@ defmodule Uplink.Clients.Caddy.Config.BuilderTest do "provider" => %{"api_token" => "something", "name" => "cloudflare"} } = dns - assert %{routes: [first_route, second_route, third_route]} = server + assert %{routes: routes} = server + + routes = Enum.sort(routes) + + [first_route, second_route, third_route] = routes assert %{handle: [handle], match: [match]} = first_route assert %{handle: [second_handle], match: [second_match]} = second_route assert %{handle: [third_handle], match: [third_match]} = third_route + assert match.host == ["another.com", "something.com"] assert match.path == ["/configure*"] - assert third_match.path == ["*"] + assert second_match.path == ["/*"] - assert second_match.path == ["/how-to*"] + assert third_match.path == ["/how-to*"] - assert "grpc.something.com" in third_match.host + assert "grpc.something.com" in second_match.host + assert "grpc.another.com" in second_match.host - [third_upstream] = third_handle.upstreams + [second_upstream] = second_handle.upstreams - assert third_upstream.dial =~ "6000" + assert second_upstream.dial =~ "6000" assert %{handler: "reverse_proxy"} = handle assert %{host: _hosts} = match - [second_upstream] = second_handle.upstreams + [third_upstream] = third_handle.upstreams - assert %{protocol: "http", tls: %{}} = second_handle.transport + assert %{protocol: "http", tls: %{}} = third_handle.transport - assert second_upstream.dial == "proxy.webflow.com:80" + assert third_upstream.dial == "proxy.webflow.com:80" assert %{identity: identity} = admin assert %{identifiers: ["127.0.0.1"]} = identity