Skip to content

Commit

Permalink
feat: create datastore & correct migration (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
pichoemr authored Apr 5, 2022
1 parent 6273cf7 commit 75fd28c
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 88 deletions.
9 changes: 8 additions & 1 deletion apps/lenra/lib/lenra/services/datastore_services.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Lenra.DatastoreServices do
@moduledoc """
The service that manages the different possible actions on a datastore.
"""
alias ApplicationRunner.DatastoreServices
alias ApplicationRunner.{Datastore, DatastoreServices}
alias Lenra.Repo
require Logger

Expand All @@ -12,6 +12,13 @@ defmodule Lenra.DatastoreServices do
|> Repo.transaction()
end

def create_environment_user_datastore(multi) do
multi
|> Ecto.Multi.insert(:inserted_datastore, fn %{inserted_env: env} ->
Datastore.new(env.id, %{"name" => "UserDatas"})
end)
end

def update(datastore_id, params) do
datastore_id
|> DatastoreServices.update(params)
Expand Down
11 changes: 10 additions & 1 deletion apps/lenra/lib/lenra/services/environment_services.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Lenra.EnvironmentServices do
The service that manages the different possible actions on an environment.
"""
import Ecto.Query
alias Lenra.{Environment, Repo, UserEnvironmentAccess}
alias Lenra.{DatastoreServices, Environment, Repo}
require Logger

def all(app_id) do
Expand All @@ -25,9 +25,18 @@ defmodule Lenra.EnvironmentServices do
def create(application_id, creator_id, params) do
Ecto.Multi.new()
|> Ecto.Multi.insert(:inserted_env, Environment.new(application_id, creator_id, nil, params))
|> DatastoreServices.create_environment_user_datastore()
|> Repo.transaction()
end

def create_with_app(multi, creator_id, params) do
multi
|> Ecto.Multi.insert(:inserted_env, fn %{inserted_application: app} ->
Environment.new(app.id, creator_id, nil, params)
end)
|> DatastoreServices.create_environment_user_datastore()
end

def update(env, params) do
Ecto.Multi.new()
|> Ecto.Multi.update(:updated_env, Environment.update(env, params))
Expand Down
7 changes: 3 additions & 4 deletions apps/lenra/lib/lenra/services/lenra_application_services.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Lenra.LenraApplicationServices do
alias Lenra.{
ApplicationMainEnv,
Environment,
EnvironmentServices,
LenraApplication,
Repo,
UserEnvironmentAccess
Expand Down Expand Up @@ -43,12 +44,10 @@ defmodule Lenra.LenraApplicationServices do
def create(user_id, params) do
Ecto.Multi.new()
|> Ecto.Multi.insert(:inserted_application, LenraApplication.new(user_id, params))
|> Ecto.Multi.insert(:inserted_main_env, fn %{inserted_application: app} ->
Environment.new(app.id, user_id, nil, %{name: "live", is_ephemeral: false, is_public: false})
end)
|> EnvironmentServices.create_with_app(user_id, %{name: "live", is_ephemeral: false, is_public: false})
|> Ecto.Multi.insert(:application_main_env, fn %{
inserted_application: app,
inserted_main_env: env
inserted_env: env
} ->
ApplicationMainEnv.new(app.id, env.id)
end)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Lenra.Repo.Migrations.DatastoreCascade do
use Ecto.Migration

def change do
drop(constraint(:datastores, "datastores_environment_id_fkey"))

alter table(:datastores) do
modify(:environment_id, references(:environments, on_delete: :delete_all))
end
end
end
2 changes: 1 addition & 1 deletion apps/lenra/test/lenra/services/data_services_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ defmodule Lenra.DataServicesTest do
end

test "should not update data if env_id not the same", %{env_id: env_id, user_id: user_id} do
{:ok, %{inserted_main_env: environment}} =
{:ok, %{inserted_env: environment}} =
LenraApplicationServices.create(user_id, %{
name: "test-update",
color: "FFFFFF",
Expand Down
4 changes: 2 additions & 2 deletions apps/lenra/test/lenra/services/datastore_services_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Lenra.DatastoreServicesTest do
end

test "should create datastore if datastore same name but different env_id", %{env_id: env_id, user_id: user_id} do
{:ok, %{inserted_main_env: environment}} =
{:ok, %{inserted_env: environment}} =
LenraApplicationServices.create(user_id, %{
name: "test-update",
color: "FFFFFF",
Expand All @@ -52,7 +52,7 @@ defmodule Lenra.DatastoreServicesTest do
end

test "should create datastore if different name but same env_id", %{env_id: env_id, user_id: user_id} do
{:ok, %{inserted_main_env: environment}} =
{:ok, %{inserted_env: environment}} =
LenraApplicationServices.create(user_id, %{
name: "test-update",
color: "FFFFFF",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ defmodule Lenra.DeploymentServicesTest do
end

test "deployment but wrong environment", %{app: app} do
{:ok, %{inserted_main_env: wrong_env}} =
{:ok, %{inserted_env: wrong_env}} =
LenraApplicationServices.create(app.creator_id, %{
name: "wrong_app",
color: "FFFFFF",
Expand Down
4 changes: 2 additions & 2 deletions apps/lenra_web/lib/lenra_web/application_runner_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule LenraWeb.ApplicationRunnerAdapter do
@behaviour ApplicationRunner.AdapterBehavior

alias ApplicationRunner.{Data, EnvState, SessionState}
alias Lenra.{DataServices, DatastoreServices, Environment, LenraApplication, OpenfaasServices, User}
alias Lenra.{DataServices, OpenfaasServices, User}
require Logger

@impl true
Expand Down Expand Up @@ -68,7 +68,7 @@ defmodule LenraWeb.ApplicationRunnerAdapter do
}) do
case DataServices.get_old_data(user.id, env_id) do
nil -> {:ok, %{}}
%Data{} = data -> {:ok, data}
%Data{} = data -> {:ok, data.data}
end
end

Expand Down
156 changes: 80 additions & 76 deletions apps/lenra_web/test/channels/app_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule LenraWeb.AppChannelTest do
use LenraWeb.ChannelCase, async: false
alias ApplicationRunner.ListenersCache

alias ApplicationRunner.Datastore

alias Lenra.{
ApplicationMainEnv,
Build,
Expand Down Expand Up @@ -66,82 +68,84 @@ defmodule LenraWeb.AppChannelTest do
refute_push("ui", _)
end

# TODO: This test use service that no longer match new data system.

# test "Base use case with simple app", %{socket: socket, user: user} do
# # owstub
# # |> FaasStub.expect_deploy_app_once(%{"ok" => "200"})

# Ecto.Multi.new()
# |> Ecto.Multi.insert(
# :inserted_application,
# LenraApplication.new(user.id, %{
# name: "Counter",
# color: "FFFFFF",
# icon: "60189"
# })
# )
# |> Ecto.Multi.insert(:inserted_env, fn %{inserted_application: app} ->
# Environment.new(app.id, user.id, nil, %{name: "live", is_ephemeral: false, is_public: false})
# end)
# |> Ecto.Multi.insert(:inserted_build, fn %{inserted_application: app} ->
# Build.new(user.id, app.id, @build_number, %{status: :success})
# end)
# |> Ecto.Multi.insert(:application_main_env, fn %{inserted_application: app, inserted_env: env} ->
# ApplicationMainEnv.new(app.id, env.id)
# end)
# |> Ecto.Multi.update(:updated_env, fn %{inserted_env: env, inserted_build: build} ->
# Ecto.Changeset.change(env, deployed_build_id: build.id)
# end)
# |> Ecto.Multi.insert(:inserted_deployment, fn %{
# inserted_application: app,
# inserted_env: env,
# inserted_build: build
# } ->
# Deployment.new(app.id, env.id, build.id, user.id, %{})
# end)
# |> Repo.transaction()

# app = Repo.get_by(LenraApplication, name: "Counter")

# owstub =
# FaasStub.create_faas_stub()
# |> FaasStub.stub_app(app.service_name, @build_number)

# # Base use case. Call InitData then MainUI then call the listener
# # and the next MainUI should not be called but taken from cache instead
# owstub
# |> FaasStub.stub_request_once(@manifest)
# |> FaasStub.stub_request_once(@data)
# |> FaasStub.stub_request_once(@widget)
# |> FaasStub.stub_request_once(@data2)
# |> FaasStub.stub_request_once(@widget2)

# # Join the channel
# {:ok, _reply, socket} = my_subscribe_and_join(socket, %{"app" => app.service_name})

# # Check that the correct data is stored into the socket
# assert %{
# user: ^user
# } = socket.assigns

# # Check that we receive a "ui" event with the final UI
# assert_push("ui", @expected_ui)

# # We simulate an event from the UI
# push(socket, "run", %{"code" => @listener_code})

# # Check that we receive a "patchUi" event with corresponding patch
# assert_push("patchUi", @expected_patch_ui)

# Process.unlink(socket.channel_pid)
# ref = leave(socket)

# assert_reply(ref, :ok)

# # Waiting for monitor to write measurements in db
# :timer.sleep(500)
# end
test "Base use case with simple app", %{socket: socket, user: user} do
# owstub
# |> FaasStub.expect_deploy_app_once(%{"ok" => "200"})

Ecto.Multi.new()
|> Ecto.Multi.insert(
:inserted_application,
LenraApplication.new(user.id, %{
name: "Counter",
color: "FFFFFF",
icon: "60189"
})
)
|> Ecto.Multi.insert(:inserted_env, fn %{inserted_application: app} ->
Environment.new(app.id, user.id, nil, %{name: "live", is_ephemeral: false, is_public: false})
end)
|> Ecto.Multi.insert(:inserted_datastore, fn %{inserted_env: env} ->
Datastore.new(env.id, %{"name" => "UserDatas"})
end)
|> Ecto.Multi.insert(:inserted_build, fn %{inserted_application: app} ->
Build.new(user.id, app.id, @build_number, %{status: :success})
end)
|> Ecto.Multi.insert(:application_main_env, fn %{inserted_application: app, inserted_env: env} ->
ApplicationMainEnv.new(app.id, env.id)
end)
|> Ecto.Multi.update(:updated_env, fn %{inserted_env: env, inserted_build: build} ->
Ecto.Changeset.change(env, deployed_build_id: build.id)
end)
|> Ecto.Multi.insert(:inserted_deployment, fn %{
inserted_application: app,
inserted_env: env,
inserted_build: build
} ->
Deployment.new(app.id, env.id, build.id, user.id, %{})
end)
|> Repo.transaction()

app = Repo.get_by(LenraApplication, name: "Counter")

owstub =
FaasStub.create_faas_stub()
|> FaasStub.stub_app(app.service_name, @build_number)

# Base use case. Call InitData then MainUI then call the listener
# and the next MainUI should not be called but taken from cache instead
owstub
|> FaasStub.stub_request_once(@manifest)
|> FaasStub.stub_request_once(@data)
|> FaasStub.stub_request_once(@widget)
|> FaasStub.stub_request_once(@data2)
|> FaasStub.stub_request_once(@widget2)

# Join the channel
{:ok, _reply, socket} = my_subscribe_and_join(socket, %{"app" => app.service_name})

# Check that the correct data is stored into the socket
assert %{
user: ^user
} = socket.assigns

# Check that we receive a "ui" event with the final UI

assert_push("ui", @expected_ui)

# We simulate an event from the UI
push(socket, "run", %{"code" => @listener_code})

# Check that we receive a "patchUi" event with corresponding patch
assert_push("patchUi", @expected_patch_ui)

Process.unlink(socket.channel_pid)
ref = leave(socket)

assert_reply(ref, :ok)

# Waiting for monitor to write measurements in db
:timer.sleep(500)
end

test "Join app channel with unauthorized user", %{socket: _socket, user: user} do
Ecto.Multi.new()
Expand Down

0 comments on commit 75fd28c

Please sign in to comment.