Skip to content

Commit 98561f9

Browse files
Improve integration tests and fix some intemittent tests (#3033)
Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
1 parent f23d41a commit 98561f9

24 files changed

Lines changed: 489 additions & 515 deletions

lib/livebook/apps.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ defmodule Livebook.Apps do
7676

7777
@doc """
7878
Returns if the given running app is authorized to given user.
79+
80+
Teams apps have authorization rules that can restrict access on
81+
per-user basis. This kind of authorization is not applicable to
82+
other types of apps, for those this function always returns `true`.
7983
"""
8084
@spec authorized?(App.t(), Livebook.Users.User.t()) :: boolean()
8185
def authorized?(app, user)
@@ -86,6 +90,8 @@ defmodule Livebook.Apps do
8690
Livebook.Hubs.TeamClient.user_app_access?(id, user.groups, slug)
8791
end
8892

93+
def authorized?(_app, _user), do: true
94+
8995
@doc """
9096
Updates the given app info across the cluster.
9197
"""

lib/livebook/hubs/team.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,13 @@ defmodule Livebook.Hubs.Team do
103103
end
104104

105105
defimpl Livebook.Hubs.Provider, for: Livebook.Hubs.Team do
106-
alias Livebook.Hubs.Team
107-
alias Livebook.Hubs.TeamClient
106+
alias Livebook.Hubs.{Team, TeamClient}
108107
alias Livebook.Teams.Requests
109108
alias Livebook.FileSystem
110109
alias Livebook.Secrets.Secret
111110

112111
@teams_key_prefix Livebook.Teams.Org.teams_key_prefix()
113-
@public_key_prefix Livebook.Hubs.Team.public_key_prefix()
112+
@public_key_prefix Team.public_key_prefix()
114113

115114
def load(team, fields) do
116115
{offline?, fields} = Map.pop(fields, :offline?, false)

lib/livebook/hubs/team_client.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ defmodule Livebook.Hubs.TeamClient do
6363
@spec get_secrets(String.t()) :: list(Secrets.Secret.t())
6464
def get_secrets(id) do
6565
GenServer.call(registry_name(id), :get_secrets)
66+
catch
67+
:exit, _ -> []
6668
end
6769

6870
@doc """
@@ -71,6 +73,8 @@ defmodule Livebook.Hubs.TeamClient do
7173
@spec get_file_systems(String.t()) :: list(FileSystem.t())
7274
def get_file_systems(id) do
7375
GenServer.call(registry_name(id), :get_file_systems)
76+
catch
77+
:exit, _ -> []
7478
end
7579

7680
@doc """
@@ -106,6 +110,8 @@ defmodule Livebook.Hubs.TeamClient do
106110
@spec get_agent_app_deployments(String.t()) :: list(Teams.AppDeployment.t())
107111
def get_agent_app_deployments(id) do
108112
GenServer.call(registry_name(id), :get_agent_app_deployments)
113+
catch
114+
:exit, _ -> []
109115
end
110116

111117
@doc """
@@ -137,7 +143,7 @@ defmodule Livebook.Hubs.TeamClient do
137143
@doc """
138144
Returns a list of cached environment variables.
139145
"""
140-
@spec get_environment_variables(String.t()) :: list(Teams.Agent.t())
146+
@spec get_environment_variables(String.t()) :: list(Teams.EnvironmentVariable.t())
141147
def get_environment_variables(id) do
142148
GenServer.call(registry_name(id), :get_environment_variables)
143149
end

lib/livebook/teams.ex

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,25 +197,17 @@ defmodule Livebook.Teams do
197197
end
198198

199199
@doc """
200-
Creates a new app deployment.
200+
Deploys the given app deployment.
201201
"""
202202
@spec deploy_app(Team.t(), Teams.AppDeployment.t()) ::
203203
:ok
204204
| {:error, Ecto.Changeset.t()}
205205
| {:transport_error, String.t()}
206206
def deploy_app(%Team{} = team, %Teams.AppDeployment{} = app_deployment) do
207207
case Requests.deploy_app(team, app_deployment) do
208-
{:ok, %{"id" => _id}} ->
209-
:ok
210-
211-
{:error, %{"errors" => %{"detail" => error}}} ->
212-
{:error, add_external_errors(app_deployment, %{"file" => [error]})}
213-
214-
{:error, %{"errors" => errors}} ->
215-
{:error, add_external_errors(app_deployment, errors)}
216-
217-
any ->
218-
any
208+
{:ok, %{"id" => _id}} -> :ok
209+
{:error, %{"errors" => errors}} -> {:error, add_external_errors(app_deployment, errors)}
210+
any -> any
219211
end
220212
end
221213

lib/livebook/teams/app_deployment.ex

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,27 @@ defmodule Livebook.Teams.AppDeployment do
4242
@doc """
4343
Creates a new app deployment from notebook.
4444
"""
45-
@spec new(Livebook.Notebook.t(), Livebook.FileSystem.File.t()) ::
45+
@spec new(Livebook.Notebook.t() | String.t(), Livebook.FileSystem.File.t()) ::
4646
{:ok, t()} | {:warning, list(String.t())} | {:error, FileSystem.error()}
47-
def new(notebook, files_dir) do
48-
with {:ok, source} <- fetch_notebook_source(notebook),
49-
{:ok, files} <- build_and_check_file_entries(notebook, source, files_dir),
47+
def new(%Livebook.Notebook{} = notebook, files_dir) do
48+
case Livebook.LiveMarkdown.notebook_to_livemd(notebook) do
49+
{source, []} -> new(notebook, source, files_dir)
50+
{_, warnings} -> {:warning, warnings}
51+
end
52+
end
53+
54+
@stamp_error "notebook does not have a stamp, disabling access to secrets and remote files"
55+
56+
def new(source, files_dir) when is_binary(source) do
57+
case Livebook.LiveMarkdown.notebook_from_livemd(source) do
58+
{notebook, %{warnings: [], stamp_verified?: true}} -> new(notebook, source, files_dir)
59+
{_, %{warnings: [], stamp_verified?: false}} -> {:warning, [@stamp_error]}
60+
{_, %{warnings: warnings}} -> {:warning, warnings}
61+
end
62+
end
63+
64+
def new(%Livebook.Notebook{} = notebook, source, files_dir) do
65+
with {:ok, files} <- build_and_check_file_entries(notebook, source, files_dir),
5066
{:ok, {_, zip_content}} <- :zip.create(~c"app_deployment.zip", files, [:memory]),
5167
:ok <- validate_size(zip_content) do
5268
md5_hash = :crypto.hash(:md5, zip_content)
@@ -66,13 +82,6 @@ defmodule Livebook.Teams.AppDeployment do
6682
end
6783
end
6884

69-
defp fetch_notebook_source(notebook) do
70-
case Livebook.LiveMarkdown.notebook_to_livemd(notebook) do
71-
{source, []} -> {:ok, source}
72-
{_, warnings} -> {:warning, warnings}
73-
end
74-
end
75-
7685
defp build_and_check_file_entries(notebook, source, files_dir) do
7786
notebook.file_entries
7887
|> Enum.filter(&(&1.type == :attachment))

lib/livebook/teams/connection.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ defmodule Livebook.Teams.Connection do
8585
:keep_state_and_data
8686
end
8787

88-
def handle_event(:info, message, @no_state, data) when elem(message, 0) in @expected_messages do
89-
handle_websocket_message(message, data)
90-
end
91-
9288
def handle_event(:info, message, @no_state, %{http_conn: nil})
9389
when elem(message, 0) in @expected_messages do
9490
:keep_state_and_data
9591
end
9692

93+
def handle_event(:info, message, @no_state, data) when elem(message, 0) in @expected_messages do
94+
handle_websocket_message(message, data)
95+
end
96+
9797
def handle_event(:info, _message, @no_state, _data) do
9898
:keep_state_and_data
9999
end
@@ -116,6 +116,10 @@ defmodule Livebook.Teams.Connection do
116116

117117
# Private
118118

119+
defp handle_websocket_message(_message, %{http_conn: nil} = data) do
120+
{:keep_state, data, {:next_event, :internal, :connect}}
121+
end
122+
119123
defp handle_websocket_message(message, data) do
120124
case WebSocket.receive(data.http_conn, data.ref, data.websocket, message) do
121125
{:ok, conn, websocket, binaries} ->

0 commit comments

Comments
 (0)