From 83fea47695f1b40d0faf09e17149f383d5842030 Mon Sep 17 00:00:00 2001 From: Stas Date: Wed, 27 Sep 2023 15:33:10 +0200 Subject: [PATCH] update tests --- lib/supavisor/client_handler.ex | 6 +- lib/supavisor/monitoring/tenant.ex | 2 +- test/integration/proxy_test.exs | 2 +- test/supavisor/client_handler_test.exs | 2 +- test/supavisor/db_handler_test.exs | 3 +- test/supavisor/prom_ex_test.exs | 4 +- test/supavisor/syn_handler_test.exs | 2 +- test/supavisor/tenants_test.exs | 138 ++---------------- .../cluster_tenants_controller_test.exs | 84 ----------- test/support/fixtures/tenants_fixtures.ex | 25 ++-- 10 files changed, 32 insertions(+), 236 deletions(-) delete mode 100644 test/supavisor_web/controllers/cluster_tenants_controller_test.exs diff --git a/lib/supavisor/client_handler.ex b/lib/supavisor/client_handler.ex index 1585b4c2..eb0f8b0c 100644 --- a/lib/supavisor/client_handler.ex +++ b/lib/supavisor/client_handler.ex @@ -436,15 +436,13 @@ defmodule Supavisor.ClientHandler do {:cluster, {list2str(rest2), alias_or_tenant}} {a, rest2} -> - {:single, {list2str([a | rest2]), alias_or_tenant}} + {:single, {list2str(Enum.reverse([a | rest2])), alias_or_tenant}} end end end @spec list2str([String.t()]) :: String.t() - defp list2str(list) do - Enum.join(list, ".") - end + defp list2str(list), do: Enum.join(list, ".") def decode_startup_packet(<>) do with {:ok, payload} <- decode_startup_packet_payload(rest) do diff --git a/lib/supavisor/monitoring/tenant.ex b/lib/supavisor/monitoring/tenant.ex index 483890e2..0b41fcf9 100644 --- a/lib/supavisor/monitoring/tenant.ex +++ b/lib/supavisor/monitoring/tenant.ex @@ -6,7 +6,7 @@ defmodule Supavisor.PromEx.Plugins.Tenant do alias Supavisor, as: S - @tags [:tenant, :user, :mode] + @tags [:tenant, :user, :mode, :type] @impl true def polling_metrics(opts) do diff --git a/test/integration/proxy_test.exs b/test/integration/proxy_test.exs index 98dd98e6..d2611846 100644 --- a/test/integration/proxy_test.exs +++ b/test/integration/proxy_test.exs @@ -157,7 +157,7 @@ defmodule Supavisor.Integration.ProxyTest do :timer.sleep(500) [{_, client_pid, _}] = - Supavisor.get_local_manager({"proxy_tenant", "transaction", :transaction}) + Supavisor.get_local_manager({{:single, "proxy_tenant"}, "transaction", :transaction}) |> :sys.get_state() |> then(& &1[:tid]) |> :ets.tab2list() diff --git a/test/supavisor/client_handler_test.exs b/test/supavisor/client_handler_test.exs index 5df76921..c99e5886 100644 --- a/test/supavisor/client_handler_test.exs +++ b/test/supavisor/client_handler_test.exs @@ -18,7 +18,7 @@ defmodule Supavisor.ClientHandlerTest do end test "consist cluster" do - username = "username" + username = "some.user.cluster.alias" {t, {u, a}} = ClientHandler.parse_user_info(username) assert {t, {u, a}} == {:cluster, {"some.user", "alias"}} end diff --git a/test/supavisor/db_handler_test.exs b/test/supavisor/db_handler_test.exs index fd1ca498..e944e49e 100644 --- a/test/supavisor/db_handler_test.exs +++ b/test/supavisor/db_handler_test.exs @@ -11,7 +11,8 @@ defmodule Supavisor.DbHandlerTest do tenant: "test_tenant", user_alias: "test_user_alias", user: "user", - mode: :transaction + mode: :transaction, + replica_type: :single } {:ok, :connect, data, {_, next_event, _}} = Db.init(args) diff --git a/test/supavisor/prom_ex_test.exs b/test/supavisor/prom_ex_test.exs index f1f62bff..73f3fae8 100644 --- a/test/supavisor/prom_ex_test.exs +++ b/test/supavisor/prom_ex_test.exs @@ -27,9 +27,9 @@ defmodule Supavisor.PromExTest do Process.sleep(500) metrics = PromEx.get_metrics() assert metrics =~ "tenant=\"#{@tenant}\"" - DynamicSupervisor.stop(proxy, user) + GenServer.stop(proxy) Process.sleep(500) - Supavisor.stop({@tenant, user, :transaction}) + Supavisor.stop({{:single, @tenant}, "postgres", :transaction}) Process.sleep(500) refute PromEx.get_metrics() =~ "tenant=\"#{@tenant}\"" end diff --git a/test/supavisor/syn_handler_test.exs b/test/supavisor/syn_handler_test.exs index fd4e4a54..40d03e5c 100644 --- a/test/supavisor/syn_handler_test.exs +++ b/test/supavisor/syn_handler_test.exs @@ -4,7 +4,7 @@ defmodule Supavisor.SynHandlerTest do require Logger alias Ecto.Adapters.SQL.Sandbox - @id {"syn_tenant", "postgres", :session} + @id {{:single, "syn_tenant"}, "postgres", :session} test "resolving conflict" do node2 = :"secondary@127.0.0.1" diff --git a/test/supavisor/tenants_test.exs b/test/supavisor/tenants_test.exs index 974850d2..542e18b3 100644 --- a/test/supavisor/tenants_test.exs +++ b/test/supavisor/tenants_test.exs @@ -70,70 +70,12 @@ defmodule Supavisor.TenantsTest do assert %Ecto.Changeset{} = Tenants.change_tenant(tenant) end - test "get_user/3" do + test "get_user/4" do _tenant = tenant_fixture() - assert {:error, :not_found} = Tenants.get_user("no_user", "no_tenant", "") - assert {:ok, %{tenant: _, user: _}} = Tenants.get_user("postgres", "dev_tenant", "") - end - end - - describe "clusters" do - alias Supavisor.Tenants.Cluster - - import Supavisor.TenantsFixtures - - @invalid_attrs %{tenant_external_id: nil, type: nil} - - test "list_clusters/0 returns all clusters" do - cluster = cluster_fixture() - assert Tenants.list_clusters() == [cluster] - end - - test "get_cluster!/1 returns the cluster with given id" do - cluster = cluster_fixture() - assert Tenants.get_cluster!(cluster.id) == cluster - end - - test "create_cluster/1 with valid data creates a cluster" do - valid_attrs = %{tenant_external_id: "some tenant_external_id", type: "some type"} - - assert {:ok, %Cluster{} = cluster} = Tenants.create_cluster(valid_attrs) - assert cluster.tenant_external_id == "some tenant_external_id" - assert cluster.type == "some type" - end - - test "create_cluster/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Tenants.create_cluster(@invalid_attrs) - end - - test "update_cluster/2 with valid data updates the cluster" do - cluster = cluster_fixture() - - update_attrs = %{ - tenant_external_id: "some updated tenant_external_id", - type: "some updated type" - } - - assert {:ok, %Cluster{} = cluster} = Tenants.update_cluster(cluster, update_attrs) - assert cluster.tenant_external_id == "some updated tenant_external_id" - assert cluster.type == "some updated type" - end - - test "update_cluster/2 with invalid data returns error changeset" do - cluster = cluster_fixture() - assert {:error, %Ecto.Changeset{}} = Tenants.update_cluster(cluster, @invalid_attrs) - assert cluster == Tenants.get_cluster!(cluster.id) - end + assert {:error, :not_found} = Tenants.get_user(:single, "no_user", "no_tenant", "") - test "delete_cluster/1 deletes the cluster" do - cluster = cluster_fixture() - assert {:ok, %Cluster{}} = Tenants.delete_cluster(cluster) - assert_raise Ecto.NoResultsError, fn -> Tenants.get_cluster!(cluster.id) end - end - - test "change_cluster/1 returns a cluster changeset" do - cluster = cluster_fixture() - assert %Ecto.Changeset{} = Tenants.change_cluster(cluster) + assert {:ok, %{tenant: _, user: _}} = + Tenants.get_user(:single, "postgres", "dev_tenant", "") end end @@ -142,22 +84,21 @@ defmodule Supavisor.TenantsTest do import Supavisor.TenantsFixtures - @invalid_attrs %{active: nil} + @invalid_attrs %{active: nil, alias: nil} + @valid_attrs %{active: true, alias: "some_alias"} test "list_clusters/0 returns all clusters" do cluster = cluster_fixture() - assert Tenants.list_clusters() == [cluster] + assert Tenants.list_clusters() |> Repo.preload(:cluster_tenants) == [cluster] end test "get_cluster!/1 returns the cluster with given id" do cluster = cluster_fixture() - assert Tenants.get_cluster!(cluster.id) == cluster + assert Tenants.get_cluster!(cluster.id) |> Repo.preload(:cluster_tenants) == cluster end test "create_cluster/1 with valid data creates a cluster" do - valid_attrs = %{active: true} - - assert {:ok, %Cluster{} = cluster} = Tenants.create_cluster(valid_attrs) + assert {:ok, %Cluster{} = cluster} = Tenants.create_cluster(@valid_attrs) assert cluster.active == true end @@ -167,16 +108,15 @@ defmodule Supavisor.TenantsTest do test "update_cluster/2 with valid data updates the cluster" do cluster = cluster_fixture() - update_attrs = %{active: false} - assert {:ok, %Cluster{} = cluster} = Tenants.update_cluster(cluster, update_attrs) - assert cluster.active == false + assert {:ok, %Cluster{} = cluster} = Tenants.update_cluster(cluster, @valid_attrs) + assert cluster.active == true end test "update_cluster/2 with invalid data returns error changeset" do cluster = cluster_fixture() assert {:error, %Ecto.Changeset{}} = Tenants.update_cluster(cluster, @invalid_attrs) - assert cluster == Tenants.get_cluster!(cluster.id) + assert cluster == Tenants.get_cluster!(cluster.id) |> Repo.preload(:cluster_tenants) end test "delete_cluster/1 deletes the cluster" do @@ -190,58 +130,4 @@ defmodule Supavisor.TenantsTest do assert %Ecto.Changeset{} = Tenants.change_cluster(cluster) end end - - describe "cluster_tenants" do - alias Supavisor.Tenants.ClusterTenants - - import Supavisor.TenantsFixtures - - @invalid_attrs %{active: nil} - - test "list_cluster_tenants/0 returns all cluster_tenants" do - cluster_tenants = cluster_tenants_fixture() - assert Tenants.list_cluster_tenants() == [cluster_tenants] - end - - test "get_cluster_tenants!/1 returns the cluster_tenants with given id" do - cluster_tenants = cluster_tenants_fixture() - assert Tenants.get_cluster_tenants!(cluster_tenants.id) == cluster_tenants - end - - test "create_cluster_tenants/1 with valid data creates a cluster_tenants" do - valid_attrs = %{active: true} - - assert {:ok, %ClusterTenants{} = cluster_tenants} = Tenants.create_cluster_tenants(valid_attrs) - assert cluster_tenants.active == true - end - - test "create_cluster_tenants/1 with invalid data returns error changeset" do - assert {:error, %Ecto.Changeset{}} = Tenants.create_cluster_tenants(@invalid_attrs) - end - - test "update_cluster_tenants/2 with valid data updates the cluster_tenants" do - cluster_tenants = cluster_tenants_fixture() - update_attrs = %{active: false} - - assert {:ok, %ClusterTenants{} = cluster_tenants} = Tenants.update_cluster_tenants(cluster_tenants, update_attrs) - assert cluster_tenants.active == false - end - - test "update_cluster_tenants/2 with invalid data returns error changeset" do - cluster_tenants = cluster_tenants_fixture() - assert {:error, %Ecto.Changeset{}} = Tenants.update_cluster_tenants(cluster_tenants, @invalid_attrs) - assert cluster_tenants == Tenants.get_cluster_tenants!(cluster_tenants.id) - end - - test "delete_cluster_tenants/1 deletes the cluster_tenants" do - cluster_tenants = cluster_tenants_fixture() - assert {:ok, %ClusterTenants{}} = Tenants.delete_cluster_tenants(cluster_tenants) - assert_raise Ecto.NoResultsError, fn -> Tenants.get_cluster_tenants!(cluster_tenants.id) end - end - - test "change_cluster_tenants/1 returns a cluster_tenants changeset" do - cluster_tenants = cluster_tenants_fixture() - assert %Ecto.Changeset{} = Tenants.change_cluster_tenants(cluster_tenants) - end - end end diff --git a/test/supavisor_web/controllers/cluster_tenants_controller_test.exs b/test/supavisor_web/controllers/cluster_tenants_controller_test.exs deleted file mode 100644 index c3e6aae1..00000000 --- a/test/supavisor_web/controllers/cluster_tenants_controller_test.exs +++ /dev/null @@ -1,84 +0,0 @@ -defmodule SupavisorWeb.ClusterTenantsControllerTest do - use SupavisorWeb.ConnCase - - import Supavisor.TenantsFixtures - - alias Supavisor.Tenants.ClusterTenants - - @create_attrs %{ - active: true - } - @update_attrs %{ - active: false - } - @invalid_attrs %{active: nil} - - setup %{conn: conn} do - {:ok, conn: put_req_header(conn, "accept", "application/json")} - end - - describe "index" do - test "lists all cluster_tenants", %{conn: conn} do - conn = get(conn, ~p"/api/cluster_tenants") - assert json_response(conn, 200)["data"] == [] - end - end - - describe "create cluster_tenants" do - test "renders cluster_tenants when data is valid", %{conn: conn} do - conn = post(conn, ~p"/api/cluster_tenants", cluster_tenants: @create_attrs) - assert %{"id" => id} = json_response(conn, 201)["data"] - - conn = get(conn, ~p"/api/cluster_tenants/#{id}") - - assert %{ - "id" => ^id, - "active" => true - } = json_response(conn, 200)["data"] - end - - test "renders errors when data is invalid", %{conn: conn} do - conn = post(conn, ~p"/api/cluster_tenants", cluster_tenants: @invalid_attrs) - assert json_response(conn, 422)["errors"] != %{} - end - end - - describe "update cluster_tenants" do - setup [:create_cluster_tenants] - - test "renders cluster_tenants when data is valid", %{conn: conn, cluster_tenants: %ClusterTenants{id: id} = cluster_tenants} do - conn = put(conn, ~p"/api/cluster_tenants/#{cluster_tenants}", cluster_tenants: @update_attrs) - assert %{"id" => ^id} = json_response(conn, 200)["data"] - - conn = get(conn, ~p"/api/cluster_tenants/#{id}") - - assert %{ - "id" => ^id, - "active" => false - } = json_response(conn, 200)["data"] - end - - test "renders errors when data is invalid", %{conn: conn, cluster_tenants: cluster_tenants} do - conn = put(conn, ~p"/api/cluster_tenants/#{cluster_tenants}", cluster_tenants: @invalid_attrs) - assert json_response(conn, 422)["errors"] != %{} - end - end - - describe "delete cluster_tenants" do - setup [:create_cluster_tenants] - - test "deletes chosen cluster_tenants", %{conn: conn, cluster_tenants: cluster_tenants} do - conn = delete(conn, ~p"/api/cluster_tenants/#{cluster_tenants}") - assert response(conn, 204) - - assert_error_sent 404, fn -> - get(conn, ~p"/api/cluster_tenants/#{cluster_tenants}") - end - end - end - - defp create_cluster_tenants(_) do - cluster_tenants = cluster_tenants_fixture() - %{cluster_tenants: cluster_tenants} - end -end diff --git a/test/support/fixtures/tenants_fixtures.ex b/test/support/fixtures/tenants_fixtures.ex index 45b3727d..66295fb9 100644 --- a/test/support/fixtures/tenants_fixtures.ex +++ b/test/support/fixtures/tenants_fixtures.ex @@ -49,24 +49,19 @@ defmodule Supavisor.TenantsFixtures do {:ok, cluster} = attrs |> Enum.into(%{ - active: true + active: true, + alias: "some_alias", + cluster_tenants: [ + %{ + type: "write", + cluster_alias: "some_alias", + tenant_external_id: "proxy_tenant", + active: true + } + ] }) |> Supavisor.Tenants.create_cluster() cluster end - - @doc """ - Generate a cluster_tenants. - """ - def cluster_tenants_fixture(attrs \\ %{}) do - {:ok, cluster_tenants} = - attrs - |> Enum.into(%{ - active: true - }) - |> Supavisor.Tenants.create_cluster_tenants() - - cluster_tenants - end end