Skip to content

Add TestServer.SSH#54

Merged
danschultzer merged 1 commit into
mainfrom
richard/add-ssh
May 21, 2026
Merged

Add TestServer.SSH#54
danschultzer merged 1 commit into
mainfrom
richard/add-ssh

Conversation

@richard-ash
Copy link
Copy Markdown
Collaborator

No description provided.

@richard-ash richard-ash marked this pull request as ready for review March 19, 2026 21:16
@richard-ash richard-ash force-pushed the richard/add-ssh branch 3 times, most recently from 1f89cfb to 0fe9f1f Compare March 21, 2026 21:28
Comment thread lib/test_server/ssh/channel.ex Outdated
Comment thread lib/test_server/ssh/channel.ex Outdated
Comment thread lib/test_server/ssh/channel.ex Outdated
Comment thread lib/test_server/ssh/channel.ex Outdated
Comment thread lib/test_server/ssh/channel.ex Outdated
Comment thread lib/test_server/ssh/instance.ex Outdated
Comment thread lib/test_server/ssh/instance.ex Outdated
Comment thread lib/test_server/ssh/instance.ex Outdated
Comment thread lib/test_server/ssh/instance.ex Outdated
Comment thread lib/test_server/ssh.ex Outdated
@richard-ash richard-ash force-pushed the richard/add-ssh branch 8 times, most recently from 99e376d to 8798c67 Compare April 2, 2026 22:22
Comment thread lib/test_server/ssh/server.ex Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread lib/test_server/ssh.ex Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread lib/test_server/ssh.ex Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread lib/test_server/ssh/instance.ex Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread test/test_server/ssh_test.exs Outdated
@richard-ash richard-ash force-pushed the richard/add-ssh branch 3 times, most recently from ab429b1 to 089dd44 Compare April 3, 2026 21:16
@richard-ash richard-ash force-pushed the richard/refactor-http-websocket branch from 48b96e7 to 65e2e94 Compare April 4, 2026 19:19
@richard-ash richard-ash force-pushed the richard/add-ssh branch 2 times, most recently from eb5647e to 9f345f8 Compare April 6, 2026 20:26
Comment thread lib/test_server/ssh/instance.ex Outdated
Comment thread lib/test_server/ssh/instance.ex Outdated
Comment thread lib/test_server/ssh/instance.ex Outdated
@richard-ash richard-ash force-pushed the richard/add-ssh branch 3 times, most recently from a7c2b93 to 146a4c4 Compare April 7, 2026 19:03
@richard-ash richard-ash force-pushed the richard/refactor-http-websocket branch from 65e2e94 to 9be9f90 Compare April 7, 2026 19:15
@richard-ash richard-ash force-pushed the richard/add-ssh branch 3 times, most recently from cc36c29 to 78b31c6 Compare April 7, 2026 19:58
@richard-ash richard-ash force-pushed the richard/add-ssh branch 3 times, most recently from 354f99d to f5d3b6e Compare April 13, 2026 22:06
Comment thread lib/test_server/ssh/channel.ex Outdated
Comment thread lib/test_server/ssh/instance.ex Outdated
Comment thread lib/test_server/ssh/instance.ex Outdated
Comment thread lib/test_server/ssh/channel.ex Outdated
Comment thread lib/test_server/ssh/instance.ex Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread lib/test_server/ssh.ex
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread test/test_server/ssh_test.exs
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread test/test_server/ssh_test.exs Outdated
Comment thread lib/test_server/ssh/channel.ex Outdated
Comment thread lib/test_server/ssh/channel.ex Outdated
Comment thread lib/test_server/ssh/instance.ex Outdated
Comment thread lib/test_server/ssh/channel.ex Outdated
@richard-ash richard-ash force-pushed the richard/add-ssh branch 2 times, most recently from c9c243a to 848163a Compare April 22, 2026 17:35
Comment thread lib/test_server/ssh/channel.ex Outdated
Comment on lines +27 to +28
"#{TestServer.format_instance(TestServer.SSH, state.instance)} received an unexpected SSH channel up message for channel ID #{channel_id} on connection #{inspect(connection)}."
|> append_formatted_channels(state.instance)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This goes all the way down, but a single pipe shouldn't really be used.

@richard-ash richard-ash force-pushed the richard/refactor-http-websocket branch 3 times, most recently from 20aee7b to 7b41e97 Compare April 28, 2026 18:25
Base automatically changed from richard/refactor-http-websocket to main April 28, 2026 18:26
An error occurred while trying to automatically change base from richard/refactor-http-websocket to main April 28, 2026 18:26
Comment thread lib/test_server/ssh/channel.ex Outdated

alias TestServer.SSH.Instance

defstruct [:instance, :channel, :custom]
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The :custom doesn't feel right, is there a better name?

Comment thread lib/test_server/ssh/channel.ex Outdated
Comment on lines +162 to +199
defp append_formatted_channels(message, instance) do
channels = Enum.split_with(Instance.channels(instance), &is_nil(&1.channel_id))

"""
#{message}

#{format_channels(channels)}
"""
end

defp format_channels({[], []}) do
"No available channels."
end

defp format_channels({[], used}) do
"""
No available channels. The following channels have been claimed:

#{Instance.format_channels(used)}
"""
end

defp format_channels({available, _used}) do
"""
Available channels:

#{Instance.format_channels(available)}
"""
end

defp send_error(connection, channel_id, state, {exception, stacktrace}) do
Instance.report_error(state.instance, {exception, stacktrace})

error_message = Exception.format(:error, exception, stacktrace)
:ssh_connection.send(connection, channel_id, error_message)

{state, [exit_status: 1]}
end
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved up above handle_ssh_msg as it's the first place it is referenced.

Comment thread lib/test_server/ssh/channel.ex Outdated
Comment on lines +99 to +121
case result do
{:ok, {:noreply, custom}} ->
{%{state | custom: custom}, []}

{:ok, {:reply, {data, opts}, custom}} ->
:ssh_connection.send(connection, channel_id, data)

stderr = Keyword.get(opts, :stderr)
stderr && :ssh_connection.send(connection, channel_id, 1, to_string(stderr))

{%{state | custom: custom}, opts}

{:error, :not_found} ->
message =
"#{TestServer.format_instance(TestServer.SSH, state.instance)} received an unexpected SSH message of type #{type}:"
|> append_formatted_frame(frame)
|> append_formatted_handlers(state.instance)

send_error(connection, channel_id, state, {RuntimeError.exception(message), []})

{:error, {exception, stacktrace}} ->
send_error(connection, channel_id, state, {exception, stacktrace})
end
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it look better if we make this case just the function header? Not sure if a case here is easier to read than just having three functions.

Comment thread lib/test_server/ssh/instance.ex Outdated
@spec dispatch(pid(), {:channel_up, non_neg_integer(), pid()}) ::
{:ok, {reference(), keyword(), TestServer.stacktrace()}}
| {:error, :not_found}
| {:error, {:already_taken, TestServer.SSH.channel()}}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is :already_taken the best describer? I wonder if there's something more OTP/Elixir like, but maybe it's fine as per the not_found above. But I also think there may be a bit of mixed language of used vs taken?

Comment thread lib/test_server/ssh/instance.ex Outdated
defp ensure_function!(fun) when is_function(fun), do: :ok
defp ensure_function!(fun), do: raise(BadFunctionError, term: fun)

@spec dispatch(pid(), {:channel_up, non_neg_integer(), pid()}) ::
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of non_neg_integer isn't this just an ssh channel id? may even exist in the :ssh module as a type.

Comment thread lib/test_server/ssh/instance.ex Outdated
GenServer.call(instance, {:register, {:channel, {options, stacktrace}}})
end

@spec register(pid(), {:handle, {reference(), keyword(), TestServer.stacktrace()}}) ::
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reference is the is actually the channel right? We can use the type from SSH?

Comment thread lib/test_server/ssh/instance.ex Outdated
GenServer.call(instance, {:dispatch, {:channel_up, channel_id, connection}})
end

@spec dispatch(pid(), {:handle, reference(), reference(), tuple(), map()}) ::
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be SSH.channel(), and maybe there is types for the next two in :ssh?.

Comment thread lib/test_server/ssh/instance.ex Outdated
%{
options: options,
channels: [],
connections: %{},
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't used right?

@richard-ash richard-ash force-pushed the richard/add-ssh branch 2 times, most recently from 4a04923 to d1ff0d4 Compare April 28, 2026 20:25
@danschultzer danschultzer force-pushed the richard/add-ssh branch 6 times, most recently from 6d849d0 to dc7e815 Compare May 9, 2026 03:40
Co-Authored-By: Dan Schultzer <1254724+danschultzer@users.noreply.github.com>

# Conflicts:
#	lib/test_server/http.ex
@danschultzer
Copy link
Copy Markdown
Owner

🤩

@danschultzer danschultzer merged commit 64ed8dd into main May 21, 2026
13 checks passed
@danschultzer danschultzer deleted the richard/add-ssh branch May 21, 2026 01:01
@danschultzer danschultzer mentioned this pull request May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants