Skip to content

Commit

Permalink
Support snap-revision-not-available, snap-channel-not-available errors
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-ancell committed Nov 14, 2018
1 parent 236bf13 commit 8b9753a
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 18 deletions.
6 changes: 5 additions & 1 deletion snapd-glib/snapd-error.h
Expand Up @@ -57,6 +57,8 @@ G_BEGIN_DECLS
* @SNAPD_ERROR_NOT_IN_STORE: the requested snap is not in the store.
* @SNAPD_ERROR_AUTH_CANCELLED: authentication was cancelled by the user.
* @SNAPD_ERROR_NOT_CLASSIC: snap not compatible with classic mode.
* @SNAPD_ERROR_REVISION_NOT_AVAILABLE: requested snap revision not available.
* @SNAPD_ERROR_CHANNEL_NOT_AVAILABLE: requested snap channel not available.
*
* Error codes returned by snapd operations.
*
Expand Down Expand Up @@ -90,7 +92,9 @@ typedef enum
SNAPD_ERROR_NOT_FOUND,
SNAPD_ERROR_NOT_IN_STORE,
SNAPD_ERROR_AUTH_CANCELLED,
SNAPD_ERROR_NOT_CLASSIC
SNAPD_ERROR_NOT_CLASSIC,
SNAPD_ERROR_REVISION_NOT_AVAILABLE,
SNAPD_ERROR_CHANNEL_NOT_AVAILABLE
} SnapdError;

/**
Expand Down
10 changes: 10 additions & 0 deletions snapd-glib/snapd-json.c
Expand Up @@ -297,6 +297,16 @@ parse_error_response (JsonObject *root, GError **error)
SNAPD_ERROR,
SNAPD_ERROR_NOT_CLASSIC,
message);
else if (g_strcmp0 (kind, "snap-revision-not-available") == 0)
g_set_error_literal (error,
SNAPD_ERROR,
SNAPD_ERROR_REVISION_NOT_AVAILABLE,
message);
else if (g_strcmp0 (kind, "snap-channel-not-available") == 0)
g_set_error_literal (error,
SNAPD_ERROR,
SNAPD_ERROR_CHANNEL_NOT_AVAILABLE,
message);
else {
switch (status_code) {
case SOUP_STATUS_BAD_REQUEST:
Expand Down
4 changes: 3 additions & 1 deletion snapd-qt/Snapd/request.h
Expand Up @@ -56,7 +56,9 @@ class Q_DECL_EXPORT QSnapdRequest : public QObject
NotFound,
NotInStore,
AuthCancelled,
NotClassic
NotClassic,
RevisionNotAvailable,
ChannelNotAvailable
};
Q_ENUM(QSnapdError)

Expand Down
6 changes: 6 additions & 0 deletions snapd-qt/request.cpp
Expand Up @@ -153,6 +153,12 @@ void QSnapdRequest::finish (void *error)
case SNAPD_ERROR_NOT_CLASSIC:
d->error = QSnapdRequest::QSnapdError::NotClassic;
break;
case SNAPD_ERROR_REVISION_NOT_AVAILABLE:
d->error = QSnapdRequest::QSnapdError::RevisionNotAvailable;
break;
case SNAPD_ERROR_CHANNEL_NOT_AVAILABLE:
d->error = QSnapdRequest::QSnapdError::ChannelNotAvailable;
break;
default:
/* This indicates we should add a new entry here... */
d->error = QSnapdRequest::QSnapdError::UnknownError;
Expand Down
44 changes: 28 additions & 16 deletions tests/mock-snapd.c
Expand Up @@ -1783,9 +1783,9 @@ send_error_forbidden (MockSnapd *snapd, SoupMessage *message, const gchar *error
}

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

static void
Expand Down Expand Up @@ -2615,7 +2615,7 @@ handle_snap (MockSnapd *snapd, SoupMessage *message, const gchar *name)
if (snap != NULL)
send_sync_response (snapd, message, 200, make_snap_node (snap), NULL);
else
send_error_not_found (snapd, message, "cannot find snap");
send_error_not_found (snapd, message, "cannot find snap", NULL);
}
else if (strcmp (message->method, "POST") == 0) {
g_autoptr(JsonNode) request = NULL;
Expand Down Expand Up @@ -2660,9 +2660,21 @@ handle_snap (MockSnapd *snapd, SoupMessage *message, const gchar *name)
return;
}

snap = find_store_snap_by_name (snapd, name, NULL, NULL);
if (snap == NULL) {
send_error_not_found (snapd, message, "cannot install, snap not found", "snap-not-found");
return;
}

snap = find_store_snap_by_name (snapd, name, channel, NULL);
if (snap == NULL) {
send_error_not_found (snapd, message, "no snap revision on specified channel", "snap-channel-not-available");
return;
}

snap = find_store_snap_by_name (snapd, name, channel, revision);
if (snap == NULL) {
send_error_bad_request (snapd, message, "cannot install, snap not found", "snap-not-found");
send_error_not_found (snapd, message, "no snap revision available as specified", "snap-revision-not-available");
return;
}

Expand Down Expand Up @@ -2871,16 +2883,16 @@ handle_icon (MockSnapd *snapd, SoupMessage *message, const gchar *path)
}

if (!g_str_has_suffix (path, "/icon")) {
send_error_not_found (snapd, message, "not found");
send_error_not_found (snapd, message, "not found", NULL);
return;
}
name = g_strndup (path, strlen (path) - strlen ("/icon"));

snap = find_snap (snapd, name);
if (snap == NULL)
send_error_not_found (snapd, message, "cannot find snap");
send_error_not_found (snapd, message, "cannot find snap", NULL);
else if (snap->icon_data == NULL)
send_error_not_found (snapd, message, "not found");
send_error_not_found (snapd, message, "not found", NULL);
else
send_response (message, 200, snap->icon_mime_type,
(const guint8 *) g_bytes_get_data (snap->icon_data, NULL),
Expand Down Expand Up @@ -3328,7 +3340,7 @@ handle_change (MockSnapd *snapd, SoupMessage *message, const gchar *change_id)

change = get_change (snapd, change_id);
if (change == NULL) {
send_error_not_found (snapd, message, "cannot find change");
send_error_not_found (snapd, message, "cannot find change", NULL);
return;
}
mock_change_progress (snapd, change);
Expand All @@ -3343,7 +3355,7 @@ handle_change (MockSnapd *snapd, SoupMessage *message, const gchar *change_id)

change = get_change (snapd, change_id);
if (change == NULL) {
send_error_not_found (snapd, message, "cannot find change");
send_error_not_found (snapd, message, "cannot find change", NULL);
return;
}

Expand Down Expand Up @@ -3609,7 +3621,7 @@ handle_buy (MockSnapd *snapd, SoupMessage *message)

snap = find_store_snap_by_id (snapd, snap_id);
if (snap == NULL) {
send_error_not_found (snapd, message, "not found"); // FIXME: Check is error snapd returns
send_error_not_found (snapd, message, "not found", NULL); // FIXME: Check is error snapd returns
return;
}

Expand Down Expand Up @@ -3773,19 +3785,19 @@ handle_aliases (MockSnapd *snapd, SoupMessage *message)
if (json_object_has_member (o, "snap")) {
snap = find_snap (snapd, json_object_get_string_member (o, "snap"));
if (snap == NULL) {
send_error_not_found (snapd, message, "cannot find snap");
send_error_not_found (snapd, message, "cannot find snap", NULL);
return;
}
}
else if (alias != NULL) {
snap = find_snap_by_alias (snapd, alias);
if (snap == NULL) {
send_error_not_found (snapd, message, "cannot find snap");
send_error_not_found (snapd, message, "cannot find snap", NULL);
return;
}
}
else {
send_error_not_found (snapd, message, "cannot find snap");
send_error_not_found (snapd, message, "cannot find snap", NULL);
return;
}

Expand All @@ -3796,12 +3808,12 @@ handle_aliases (MockSnapd *snapd, SoupMessage *message)

app_name = json_object_get_string_member (o, "app");
if (app_name == NULL) {
send_error_not_found (snapd, message, "No app specified");
send_error_not_found (snapd, message, "No app specified", NULL);
return;
}
app = mock_snap_find_app (snap, app_name);
if (app == NULL) {
send_error_not_found (snapd, message, "App not found");
send_error_not_found (snapd, message, "App not found", NULL);
return;
}
mock_app_add_manual_alias (app, alias, TRUE);
Expand Down Expand Up @@ -4039,7 +4051,7 @@ handle_request (SoupServer *server,
else if (strcmp (path, "/v2/users") == 0)
handle_users (snapd, message);
else
send_error_not_found (snapd, message, "not found");
send_error_not_found (snapd, message, "not found", NULL);
}

static gboolean
Expand Down
42 changes: 42 additions & 0 deletions tests/test-glib.c
Expand Up @@ -4662,6 +4662,46 @@ test_install_not_available (void)
g_assert_false (result);
}

static void
test_install_channel_not_available (void)
{
g_autoptr(MockSnapd) snapd = NULL;
g_autoptr(SnapdClient) client = NULL;
gboolean result;
g_autoptr(GError) error = NULL;

snapd = mock_snapd_new ();
mock_snapd_add_store_snap (snapd, "snap");
g_assert_true (mock_snapd_start (snapd, &error));

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

result = snapd_client_install2_sync (client, SNAPD_INSTALL_FLAGS_NONE, "snap", "channel", NULL, NULL, NULL, NULL, &error);
g_assert_error (error, SNAPD_ERROR, SNAPD_ERROR_CHANNEL_NOT_AVAILABLE);
g_assert_false (result);
}

static void
test_install_revision_not_available (void)
{
g_autoptr(MockSnapd) snapd = NULL;
g_autoptr(SnapdClient) client = NULL;
gboolean result;
g_autoptr(GError) error = NULL;

snapd = mock_snapd_new ();
mock_snapd_add_store_snap (snapd, "snap");
g_assert_true (mock_snapd_start (snapd, &error));

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

result = snapd_client_install2_sync (client, SNAPD_INSTALL_FLAGS_NONE, "snap", NULL, "1.1", NULL, NULL, NULL, &error);
g_assert_error (error, SNAPD_ERROR, SNAPD_ERROR_REVISION_NOT_AVAILABLE);
g_assert_false (result);
}

static void
test_install_snapd_restart (void)
{
Expand Down Expand Up @@ -7235,6 +7275,8 @@ main (int argc, char **argv)
g_test_add_func ("/install/channel", test_install_channel);
g_test_add_func ("/install/revision", test_install_revision);
g_test_add_func ("/install/not-available", test_install_not_available);
g_test_add_func ("/install/channel-not-available", test_install_channel_not_available);
g_test_add_func ("/install/revision-not-available", test_install_revision_not_available);
g_test_add_func ("/install/snapd-restart", test_install_snapd_restart);
g_test_add_func ("/install/async-snapd-restart", test_install_async_snapd_restart);
g_test_add_func ("/install/auth-cancelled", test_install_auth_cancelled);
Expand Down
32 changes: 32 additions & 0 deletions tests/test-qt.cpp
Expand Up @@ -3703,6 +3703,36 @@ test_install_not_available ()
g_assert_cmpint (installRequest->error (), ==, QSnapdRequest::NotFound);
}

static void
test_install_channel_not_available ()
{
g_autoptr(MockSnapd) snapd = mock_snapd_new ();
mock_snapd_add_store_snap (snapd, "snap");
g_assert_true (mock_snapd_start (snapd, NULL));

QSnapdClient client;
client.setSocketPath (mock_snapd_get_socket_path (snapd));

QScopedPointer<QSnapdInstallRequest> installRequest (client.install ("snap", "channel"));
installRequest->runSync ();
g_assert_cmpint (installRequest->error (), ==, QSnapdRequest::ChannelNotAvailable);
}

static void
test_install_revision_not_available ()
{
g_autoptr(MockSnapd) snapd = mock_snapd_new ();
mock_snapd_add_store_snap (snapd, "snap");
g_assert_true (mock_snapd_start (snapd, NULL));

QSnapdClient client;
client.setSocketPath (mock_snapd_get_socket_path (snapd));

QScopedPointer<QSnapdInstallRequest> installRequest (client.install ("snap", NULL, "1.1"));
installRequest->runSync ();
g_assert_cmpint (installRequest->error (), ==, QSnapdRequest::RevisionNotAvailable);
}

static void
test_install_snapd_restart ()
{
Expand Down Expand Up @@ -5644,6 +5674,8 @@ main (int argc, char **argv)
g_test_add_func ("/install/channel", test_install_channel);
g_test_add_func ("/install/revision", test_install_revision);
g_test_add_func ("/install/not-available", test_install_not_available);
g_test_add_func ("/install/channel-not-available", test_install_channel_not_available);
g_test_add_func ("/install/revision-not-available", test_install_revision_not_available);
g_test_add_func ("/install/snapd-restart", test_install_snapd_restart);
g_test_add_func ("/install/async-snapd-restart", test_install_async_snapd_restart);
g_test_add_func ("/install/auth-cancelled", test_install_auth_cancelled);
Expand Down

0 comments on commit 8b9753a

Please sign in to comment.