Skip to content

Commit

Permalink
tests: test support for unsuccessful error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jhenstridge committed Feb 12, 2021
1 parent e4411ac commit bc9492a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 9 deletions.
31 changes: 25 additions & 6 deletions tests/mock-snapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,7 @@ send_async_response (MockSnapd *self, SoupMessage *message, guint status_code, c
}

static void
send_error_response (MockSnapd *self, SoupMessage *message, guint status_code, const gchar *error_message, const gchar *kind)
send_error_response (MockSnapd *self, SoupMessage *message, guint status_code, const gchar *error_message, const gchar *kind, JsonNode *error_value)
{
g_autoptr(JsonBuilder) builder = json_builder_new ();
json_builder_begin_object (builder);
Expand All @@ -1903,6 +1903,10 @@ send_error_response (MockSnapd *self, SoupMessage *message, guint status_code, c
json_builder_set_member_name (builder, "kind");
json_builder_add_string_value (builder, kind);
}
if (error_value != NULL) {
json_builder_set_member_name (builder, "value");
json_builder_add_value (builder, error_value);
}
json_builder_end_object (builder);

g_autoptr(JsonNode) response = make_response (self, "error", status_code, json_builder_get_root (builder), NULL, NULL);
Expand All @@ -1912,31 +1916,31 @@ send_error_response (MockSnapd *self, SoupMessage *message, guint status_code, c
static void
send_error_bad_request (MockSnapd *self, SoupMessage *message, const gchar *error_message, const gchar *kind)
{
send_error_response (self, message, 400, error_message, kind);
send_error_response (self, message, 400, error_message, kind, NULL);
}

static void
send_error_unauthorized (MockSnapd *self, SoupMessage *message, const gchar *error_message, const gchar *kind)
{
send_error_response (self, message, 401, error_message, kind);
send_error_response (self, message, 401, error_message, kind, NULL);
}

static void
send_error_forbidden (MockSnapd *self, SoupMessage *message, const gchar *error_message, const gchar *kind)
{
send_error_response (self, message, 403, error_message, kind);
send_error_response (self, message, 403, error_message, kind, NULL);
}

static void
send_error_not_found (MockSnapd *self, SoupMessage *message, const gchar *error_message, const gchar *kind)
{
send_error_response (self, message, 404, error_message, kind);
send_error_response (self, message, 404, error_message, kind, NULL);
}

static void
send_error_method_not_allowed (MockSnapd *self, SoupMessage *message, const gchar *error_message)
{
send_error_response (self, message, 405, error_message, NULL);
send_error_response (self, message, 405, error_message, NULL, NULL);
}

static void
Expand Down Expand Up @@ -4384,6 +4388,21 @@ handle_snapctl (MockSnapd *self, SoupMessage *message)
g_string_append_printf (stdout_text, ":%s", arg);
}

if (!g_strcmp0 (context_id, "return-error")) {
g_autoptr(JsonBuilder) builder = json_builder_new ();
json_builder_begin_object (builder);
json_builder_set_member_name (builder, "stdout");
json_builder_add_string_value (builder, stdout_text->str);
json_builder_set_member_name (builder, "stderr");
json_builder_add_string_value (builder, "STDERR");
json_builder_set_member_name (builder, "exit-code");
json_builder_add_int_value (builder, 1);
json_builder_end_object (builder);

send_error_response (self, message, 200, "unsuccessful with exit code: 1", "unsuccessful", json_builder_get_root (builder));
return;
}

g_autoptr(JsonBuilder) builder = json_builder_new ();
json_builder_begin_object (builder);
json_builder_set_member_name (builder, "stdout");
Expand Down
67 changes: 64 additions & 3 deletions tests/test-glib.c
Original file line number Diff line number Diff line change
Expand Up @@ -7625,11 +7625,13 @@ test_run_snapctl_sync (void)
g_auto(GStrv) args = g_strsplit ("arg1;arg2", ";", -1);
g_autofree gchar *stdout_output = NULL;
g_autofree gchar *stderr_output = NULL;
gboolean result = snapd_client_run_snapctl_sync (client, "ABC", args, &stdout_output, &stderr_output, NULL, &error);
int exit_code = 0;
gboolean result = snapd_client_run_snapctl2_sync (client, "ABC", args, &stdout_output, &stderr_output, &exit_code, NULL, &error);
g_assert_no_error (error);
g_assert_true (result);
g_assert_cmpstr (stdout_output, ==, "STDOUT:ABC:arg1:arg2");
g_assert_cmpstr (stderr_output, ==, "STDERR");
g_assert_cmpint (exit_code, ==, 0);
}

static void
Expand All @@ -7639,12 +7641,14 @@ snapctl_cb (GObject *object, GAsyncResult *result, gpointer user_data)

g_autofree gchar *stdout_output = NULL;
g_autofree gchar *stderr_output = NULL;
int exit_code = 0;
g_autoptr(GError) error = NULL;
gboolean r = snapd_client_run_snapctl_finish (SNAPD_CLIENT (object), result, &stdout_output, &stderr_output, &error);
gboolean r = snapd_client_run_snapctl2_finish (SNAPD_CLIENT (object), result, &stdout_output, &stderr_output, &exit_code, &error);
g_assert_no_error (error);
g_assert_true (r);
g_assert_cmpstr (stdout_output, ==, "STDOUT:ABC:arg1:arg2");
g_assert_cmpstr (stderr_output, ==, "STDERR");
g_assert_cmpint (exit_code, ==, 0);

g_main_loop_quit (data->loop);
}
Expand All @@ -7663,10 +7667,65 @@ test_run_snapctl_async (void)
snapd_client_set_socket_path (client, mock_snapd_get_socket_path (snapd));

g_auto(GStrv) args = g_strsplit ("arg1;arg2", ";", -1);
snapd_client_run_snapctl_async (client, "ABC", args, NULL, snapctl_cb, async_data_new (loop, snapd));
snapd_client_run_snapctl2_async (client, "ABC", args, NULL, snapctl_cb, async_data_new (loop, snapd));
g_main_loop_run (loop);
}

static void
test_run_snapctl_unsuccessful (void)
{
g_autoptr(MockSnapd) snapd = mock_snapd_new ();

g_autoptr(GError) error = NULL;
g_assert_true (mock_snapd_start (snapd, &error));

g_autoptr(SnapdClient) client = snapd_client_new ();
snapd_client_set_socket_path (client, mock_snapd_get_socket_path (snapd));

g_auto(GStrv) args = g_strsplit ("arg1;arg2", ";", -1);
g_autofree gchar *stdout_output = NULL;
g_autofree gchar *stderr_output = NULL;
int exit_code = 0;
gboolean result = snapd_client_run_snapctl2_sync (client, "return-error", args, &stdout_output, &stderr_output, &exit_code, NULL, &error);
g_assert_no_error (error);
g_assert_true (result);
g_assert_cmpstr (stdout_output, ==, "STDOUT:return-error:arg1:arg2");
g_assert_cmpstr (stderr_output, ==, "STDERR");
g_assert_cmpint (exit_code, ==, 1);
}

static void
test_run_snapctl_legacy (void)
{
g_autoptr(MockSnapd) snapd = mock_snapd_new ();

g_autoptr(GError) error = NULL;
g_assert_true (mock_snapd_start (snapd, &error));

g_autoptr(SnapdClient) client = snapd_client_new ();
snapd_client_set_socket_path (client, mock_snapd_get_socket_path (snapd));

g_auto(GStrv) args = g_strsplit ("arg1;arg2", ";", -1);
g_autofree gchar *stdout_output = NULL;
g_autofree gchar *stderr_output = NULL;
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gboolean result = snapd_client_run_snapctl_sync (client, "ABC", args, &stdout_output, &stderr_output, NULL, &error);
G_GNUC_END_IGNORE_DEPRECATIONS
g_assert_no_error (error);
g_assert_true (result);
g_assert_cmpstr (stdout_output, ==, "STDOUT:ABC:arg1:arg2");
g_assert_cmpstr (stderr_output, ==, "STDERR");

/* Unsuccessful exit codes are still reported as errors by the old API */
g_clear_pointer (&stdout_output, g_free);
g_clear_pointer (&stderr_output, g_free);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
result = snapd_client_run_snapctl_sync (client, "return-error", args, &stdout_output, &stderr_output, NULL, &error);
G_GNUC_END_IGNORE_DEPRECATIONS
g_assert_false (result);
g_assert_error (error, SNAPD_ERROR, SNAPD_ERROR_UNSUCCESSFUL);
}

static void
test_download_sync (void)
{
Expand Down Expand Up @@ -7998,6 +8057,8 @@ main (int argc, char **argv)
g_test_add_func ("/aliases/prefer-async", test_aliases_prefer_async);
g_test_add_func ("/run-snapctl/sync", test_run_snapctl_sync);
g_test_add_func ("/run-snapctl/async", test_run_snapctl_async);
g_test_add_func ("/run-snapctl/unsuccessful", test_run_snapctl_unsuccessful);
g_test_add_func ("/run-snapctl/legacy", test_run_snapctl_legacy);
g_test_add_func ("/download/sync", test_download_sync);
g_test_add_func ("/download/async", test_download_async);
g_test_add_func ("/download/channel-revision", test_download_channel_revision);
Expand Down

0 comments on commit bc9492a

Please sign in to comment.