From 2465cf49d43a761084f87f2a412fbb402de0340f Mon Sep 17 00:00:00 2001 From: Joaquim Rocha Date: Mon, 11 Dec 2017 11:08:57 +0000 Subject: [PATCH] tests: Cover listing refs with different collection IDs in remotes This patch tests if refs of different collection IDs are returned together when listing refs from remote. Closes: #1587 Approved by: mwleeds --- tests/Makefile.am.inc | 1 + tests/make-multi-collection-id-repo.sh | 50 ++++++++++++++ tests/testlibrary.c | 94 ++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100755 tests/make-multi-collection-id-repo.sh diff --git a/tests/Makefile.am.inc b/tests/Makefile.am.inc index 8811f5a9..27603885 100644 --- a/tests/Makefile.am.inc +++ b/tests/Makefile.am.inc @@ -46,6 +46,7 @@ tests/test-basic.sh: tests/package_version.txt dist_installed_test_extra_scripts += \ buildutil/tap-driver.sh \ + tests/make-multi-collection-id-repo.sh \ tests/make-test-app.sh \ tests/make-test-runtime.sh \ tests/make-test-bundles.sh \ diff --git a/tests/make-multi-collection-id-repo.sh b/tests/make-multi-collection-id-repo.sh new file mode 100755 index 00000000..5dc973e2 --- /dev/null +++ b/tests/make-multi-collection-id-repo.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# +# make-multi-collection-id-repo.sh: Creates an ostree repository +# that will hold a different collection ID per ref. +# +# Copyright (C) 2017 Endless, Inc. +# +# Authors: +# Joaquim Rocha +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + +set -e + +. $(dirname $0)/libtest.sh + +REPO_DIR=$1 +REPO_NAME=$(basename $REPO_DIR) + +COLLECTION_ID_PREFIX=org.test.Collection + +ostree --repo=${REPO_DIR} init --mode=archive --collection-id=${COLLECTION_ID_PREFIX}1 + +for i in {1..3}; do + APP_REPO=test${i} + APP_REPO_DIR=`pwd`/repos/${APP_REPO} + APP_ID=org.test.Hello${i} + COLLECTION_ID=${COLLECTION_ID_PREFIX}${i} + + $(dirname $0)/make-test-app.sh ${APP_REPO} ${APP_ID} ${COLLECTION_ID} + ref=$(ostree --repo=${APP_REPO_DIR} refs | grep ${APP_ID}) + + ostree --repo=${REPO_DIR} remote add --no-gpg-verify --collection-id=${COLLECTION_ID} ${APP_REPO} file://${APP_REPO_DIR} + ostree --repo=${REPO_DIR} pull ${APP_REPO} ${ref} +done + +ostree --repo=${REPO_DIR} summary --update diff --git a/tests/testlibrary.c b/tests/testlibrary.c index 913d6ed0..982329fe 100644 --- a/tests/testlibrary.c +++ b/tests/testlibrary.c @@ -362,6 +362,97 @@ test_list_refs (void) g_assert_cmpint (refs->len, ==, 0); } +#ifdef FLATPAK_ENABLE_P2P +static void +create_multi_collection_id_repo (const char *repo_dir) +{ + int status; + g_autoptr(GError) error = NULL; + GSpawnFlags flags = G_SPAWN_DEFAULT; + g_autofree char *arg0 = NULL; + g_autofree char *argv_str = NULL; + + /* Create a repository in which each app has a different collection-id */ + arg0 = g_test_build_filename (G_TEST_DIST, "make-multi-collection-id-repo.sh", NULL); + const char *argv[] = { arg0, repo_dir, NULL }; + argv_str = g_strjoinv (" ", (char **) argv); + + if (g_test_verbose ()) + g_print ("running %s\n", argv_str); + else + flags |= G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL; + + g_test_message ("Spawning %s", argv_str); + g_spawn_sync (NULL, (char **) argv, NULL, flags, NULL, NULL, NULL, NULL, &status, &error); + g_assert_no_error (error); + g_assert_cmpint (status, ==, 0); +} + +static void +test_list_refs_in_remotes (void) +{ + int status; + const char *repo_name = "multi-refs-repo"; + GSpawnFlags flags = G_SPAWN_SEARCH_PATH; + g_autofree char *argv_str = NULL; + g_autofree char *repo_url = NULL; + g_autoptr(GPtrArray) refs = NULL; + g_autoptr(FlatpakInstallation) inst = NULL; + g_autoptr(GError) error = NULL; + g_autoptr(FlatpakRemote) remote = NULL; + g_autofree char *repo_dir = g_build_filename (testdir, repo_name, NULL); + g_autoptr(GHashTable) collection_ids = g_hash_table_new_full (g_str_hash, + g_str_equal, + NULL, NULL); + + create_multi_collection_id_repo (repo_dir); + + repo_url = g_strdup_printf ("file://%s", repo_dir); + + const char *argv[] = { "flatpak", "remote-add", "--user", "--no-gpg-verify", + repo_name, repo_url, NULL }; + + argv_str = g_strjoinv (" ", (char **) argv); + + if (g_test_verbose ()) + g_print ("running %s\n", argv_str); + else + flags |= G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL; + + /* Add the repo we created above, which holds one collection ID per ref */ + g_test_message ("Spawning %s", argv_str); + flags = G_SPAWN_SEARCH_PATH; + g_spawn_sync (NULL, (char **) argv, NULL, flags, NULL, NULL, NULL, NULL, &status, &error); + g_assert_no_error (error); + g_assert_cmpint (status, ==, 0); + + inst = flatpak_installation_new_user (NULL, &error); + g_assert_no_error (error); + + /* Ensure the remote can be successfully found */ + remote = flatpak_installation_get_remote_by_name (inst, repo_name, NULL, &error); + g_assert_no_error (error); + g_assert_nonnull (remote); + + /* List the refs in the remote we've just added */ + refs = flatpak_installation_list_remote_refs_sync (inst, repo_name, NULL, &error); + + g_assert_no_error (error); + g_assert_nonnull (refs); + g_assert (refs->len > 0); + + /* Ensure that the number of different collection IDs is the same as the + * number of apps */ + for (guint i = 0; i < refs->len; ++i) + { + FlatpakRef *ref = g_ptr_array_index (refs, i); + g_hash_table_add (collection_ids, (gchar *) flatpak_ref_get_collection_id (ref)); + } + + g_assert_cmpuint (g_hash_table_size (collection_ids), ==, refs->len); +} +#endif /* FLATPAK_ENABLE_P2P */ + static void test_list_remote_refs (void) { @@ -1095,6 +1186,9 @@ main (int argc, char *argv[]) g_test_add_func ("/library/list-remote-refs", test_list_remote_refs); g_test_add_func ("/library/list-refs", test_list_refs); g_test_add_func ("/library/install-launch-uninstall", test_install_launch_uninstall); +#ifdef FLATPAK_ENABLE_P2P + g_test_add_func ("/library/list-refs-in-remote", test_list_refs_in_remotes); +#endif /* FLATPAK_ENABLE_P2P */ g_test_add_func ("/library/list-updates", test_list_updates); global_setup ();