Repository navigation
fix(dispatch): failed fleet dispatch reports failure, not success #911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| defmodule Hypatia.FleetDispatcherHonestyTest do | ||
| # Mutates process-global env (HYPATIA_RHODIBOT_URL, :verisimdb_data_path), | ||
| # so it cannot run concurrently with other tests. | ||
| use ExUnit.Case, async: false | ||
|
|
||
| alias Hypatia.FleetDispatcher | ||
|
|
||
| @finding %{ | ||
| type: :fix_suggestion, | ||
| repo: "test-repo", | ||
| file: "scripts/deploy.sh", | ||
| issue: "Unquoted variable", | ||
| suggestion: "Add double quotes" | ||
| } | ||
|
|
||
| setup do | ||
| original_path = Application.get_env(:hypatia, :verisimdb_data_path) | ||
|
|
||
| on_exit(fn -> | ||
| System.delete_env("HYPATIA_RHODIBOT_URL") | ||
| Application.put_env(:hypatia, :verisimdb_data_path, original_path) | ||
| end) | ||
|
|
||
| :ok | ||
| end | ||
|
|
||
| # Serve exactly one HTTP request with the given status and body on an | ||
| # ephemeral localhost port; returns the base URL. | ||
| defp one_shot_server(status, body) do | ||
| {:ok, listen} = :gen_tcp.listen(0, [:binary, packet: :raw, active: false, reuseaddr: true]) | ||
| {:ok, port} = :inet.port(listen) | ||
|
|
||
| spawn_link(fn -> | ||
| {:ok, sock} = :gen_tcp.accept(listen) | ||
| {:ok, _request} = :gen_tcp.recv(sock, 0, 5_000) | ||
|
|
||
| :gen_tcp.send( | ||
| sock, | ||
| "HTTP/1.1 #{status} X\r\ncontent-type: application/json\r\n" <> | ||
| "content-length: #{byte_size(body)}\r\nconnection: close\r\n\r\n" <> body | ||
| ) | ||
|
|
||
| :gen_tcp.close(sock) | ||
| :gen_tcp.close(listen) | ||
| end) | ||
|
|
||
| "http://127.0.0.1:#{port}" | ||
| end | ||
|
|
||
| test "a 2xx GraphQL response carrying errors is a failure, not a dispatch" do | ||
| url = one_shot_server(200, ~s({"errors":[{"message":"Unknown field \\"suggestFix\\""}]})) | ||
| System.put_env("HYPATIA_RHODIBOT_URL", url) | ||
|
|
||
| assert {:error, {:live_dispatch_failed, "rhodibot", {:graphql_errors, [_]}}} = | ||
| FleetDispatcher.dispatch_finding(@finding) | ||
| end | ||
|
|
||
| test "a 2xx GraphQL response with data is a live dispatch" do | ||
| url = one_shot_server(200, ~s({"data":{"suggestFix":{"success":true,"prNumber":1}}})) | ||
| System.put_env("HYPATIA_RHODIBOT_URL", url) | ||
|
|
||
| assert {:ok, :dispatched} = FleetDispatcher.dispatch_finding(@finding) | ||
| end | ||
|
|
||
| test "a non-2xx status from a configured URL is an error" do | ||
| url = one_shot_server(500, "{}") | ||
| System.put_env("HYPATIA_RHODIBOT_URL", url) | ||
|
|
||
| assert {:error, {:live_dispatch_failed, "rhodibot", {:http_status, 500}}} = | ||
| FleetDispatcher.dispatch_finding(@finding) | ||
| end | ||
|
|
||
| test "an unreachable configured URL is an error, not a file dispatch" do | ||
| {:ok, listen} = :gen_tcp.listen(0, []) | ||
| {:ok, port} = :inet.port(listen) | ||
| :gen_tcp.close(listen) | ||
| System.put_env("HYPATIA_RHODIBOT_URL", "http://127.0.0.1:#{port}") | ||
|
|
||
| assert {:error, {:live_dispatch_failed, "rhodibot", _}} = | ||
| FleetDispatcher.dispatch_finding(@finding) | ||
| end | ||
|
|
||
| test "an unwritable manifest with no URL configured is an error" do | ||
| blocker = | ||
| Path.join( | ||
| System.tmp_dir!(), | ||
| "hypatia-manifest-blocker-#{System.unique_integer([:positive])}" | ||
| ) | ||
|
|
||
| File.write!(blocker, "a file, so mkdir beneath it fails") | ||
| on_exit(fn -> File.rm(blocker) end) | ||
| Application.put_env(:hypatia, :verisimdb_data_path, blocker) | ||
|
|
||
| assert {:error, {:manifest_write_failed, _}} = FleetDispatcher.dispatch_finding(@finding) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Treat a GraphQL response with no
dataand noerrorsas a failure.{:ok, %{}}accepts any JSON object, for example{}or{"foo":1}. These responses carry no GraphQL result. Dispatch then returns{:ok, :dispatched}for a body that does not confirm success. This is the same false-success class that the PR removes. Also, a body with"data": nulland noerrorspasses. Require a mapdatavalue that is notnil.🐛 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents