Skip to content

Commit

Permalink
Support snap-not-classic error
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-ancell committed Nov 13, 2018
1 parent 45ef5e1 commit 57a93bb
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 2 deletions.
4 changes: 3 additions & 1 deletion snapd-glib/snapd-error.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ G_BEGIN_DECLS
* @SNAPD_ERROR_NOT_FOUND: the requested snap couldn't be found.
* @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.
*
* Error codes returned by snapd operations.
*
Expand Down Expand Up @@ -88,7 +89,8 @@ typedef enum
SNAPD_ERROR_NETWORK_TIMEOUT,
SNAPD_ERROR_NOT_FOUND,
SNAPD_ERROR_NOT_IN_STORE,
SNAPD_ERROR_AUTH_CANCELLED
SNAPD_ERROR_AUTH_CANCELLED,
SNAPD_ERROR_NOT_CLASSIC
} SnapdError;

/**
Expand Down
5 changes: 5 additions & 0 deletions snapd-glib/snapd-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,11 @@ parse_error_response (JsonObject *root, GError **error)
SNAPD_ERROR,
SNAPD_ERROR_AUTH_CANCELLED,
message);
else if (g_strcmp0 (kind, "snap-not-classic") == 0)
g_set_error_literal (error,
SNAPD_ERROR,
SNAPD_ERROR_NOT_CLASSIC,
message);
else {
switch (status_code) {
case SOUP_STATUS_BAD_REQUEST:
Expand Down
3 changes: 2 additions & 1 deletion snapd-qt/Snapd/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class Q_DECL_EXPORT QSnapdRequest : public QObject
NetworkTimeout,
NotFound,
NotInStore,
AuthCancelled
AuthCancelled,
NotClassic
};
Q_ENUM(QSnapdError)

Expand Down
3 changes: 3 additions & 0 deletions snapd-qt/request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ void QSnapdRequest::finish (void *error)
case SNAPD_ERROR_AUTH_CANCELLED:
d->error = QSnapdRequest::QSnapdError::AuthCancelled;
break;
case SNAPD_ERROR_NOT_CLASSIC:
d->error = QSnapdRequest::QSnapdError::NotClassic;
break;
default:
/* This indicates we should add a new entry here... */
d->error = QSnapdRequest::QSnapdError::UnknownError;
Expand Down
4 changes: 4 additions & 0 deletions tests/mock-snapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2653,6 +2653,10 @@ handle_snap (MockSnapd *snapd, SoupMessage *message, const gchar *name)
send_error_bad_request (message, "requires classic confinement", "snap-needs-classic-system");
return;
}
if (classic && strcmp (snap->confinement, "classic") != 0) {
send_error_bad_request (message, "snap not compatible with --classic", "snap-not-classic");
return;
}
if (strcmp (snap->confinement, "devmode") == 0 && !devmode) {
send_error_bad_request (message, "requires devmode or confinement override", "snap-needs-devmode");
return;
Expand Down
23 changes: 23 additions & 0 deletions tests/test-glib.c
Original file line number Diff line number Diff line change
Expand Up @@ -4351,6 +4351,28 @@ test_install_classic (void)
g_assert_cmpstr (mock_snap_get_confinement (mock_snapd_find_snap (snapd, "snap")), ==, "classic");
}

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

snapd = mock_snapd_new ();
mock_snapd_set_on_classic (snapd, TRUE);
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));

g_assert_null (mock_snapd_find_snap (snapd, "snap"));
result = snapd_client_install2_sync (client, SNAPD_INSTALL_FLAGS_CLASSIC, "snap", NULL, NULL, NULL, NULL, NULL, &error);
g_assert_error (error, SNAPD_ERROR, SNAPD_ERROR_NOT_CLASSIC);
g_assert_false (result);
}

static void
test_install_needs_classic_system (void)
{
Expand Down Expand Up @@ -7099,6 +7121,7 @@ main (int argc, char **argv)
g_test_add_func ("/install/progress", test_install_progress);
g_test_add_func ("/install/needs-classic", test_install_needs_classic);
g_test_add_func ("/install/classic", test_install_classic);
g_test_add_func ("/install/not-classic", test_install_not_classic);
g_test_add_func ("/install/needs-classic-system", test_install_needs_classic_system);
g_test_add_func ("/install/needs-devmode", test_install_needs_devmode);
g_test_add_func ("/install/devmode", test_install_devmode);
Expand Down
18 changes: 18 additions & 0 deletions tests/test-qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3466,6 +3466,23 @@ test_install_classic ()
g_assert_cmpstr (mock_snap_get_confinement (mock_snapd_find_snap (snapd, "snap")), ==, "classic");
}

static void
test_install_not_classic ()
{
g_autoptr(MockSnapd) snapd = mock_snapd_new ();
mock_snapd_set_on_classic (snapd, TRUE);
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));

g_assert_null (mock_snapd_find_snap (snapd, "snap"));
QScopedPointer<QSnapdInstallRequest> installRequest (client.install (QSnapdClient::Classic, "snap"));
installRequest->runSync ();
g_assert_cmpint (installRequest->error (), ==, QSnapdRequest::NotClassic);
}

static void
test_install_needs_classic_system ()
{
Expand Down Expand Up @@ -5537,6 +5554,7 @@ main (int argc, char **argv)
g_test_add_func ("/install/progress", test_install_progress);
g_test_add_func ("/install/needs-classic", test_install_needs_classic);
g_test_add_func ("/install/classic", test_install_classic);
g_test_add_func ("/install/not-classic", test_install_not_classic);
g_test_add_func ("/install/needs-classic-system", test_install_needs_classic_system);
g_test_add_func ("/install/needs-devmode", test_install_needs_devmode);
g_test_add_func ("/install/devmode", test_install_devmode);
Expand Down

0 comments on commit 57a93bb

Please sign in to comment.