Blame tests/test-pull-collections.sh

rpm-build 0fba15
#!/bin/bash
rpm-build 0fba15
#
rpm-build 0fba15
# Copyright © 2017 Endless Mobile, Inc.
rpm-build 0fba15
#
rpm-build 0fba15
# SPDX-License-Identifier: LGPL-2.0+
rpm-build 0fba15
#
rpm-build 0fba15
# This library is free software; you can redistribute it and/or
rpm-build 0fba15
# modify it under the terms of the GNU Lesser General Public
rpm-build 0fba15
# License as published by the Free Software Foundation; either
rpm-build 0fba15
# version 2 of the License, or (at your option) any later version.
rpm-build 0fba15
#
rpm-build 0fba15
# This library is distributed in the hope that it will be useful,
rpm-build 0fba15
# but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 0fba15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 0fba15
# Lesser General Public License for more details.
rpm-build 0fba15
#
rpm-build 0fba15
# You should have received a copy of the GNU Lesser General Public
rpm-build 0fba15
# License along with this library; if not, write to the
rpm-build 0fba15
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
rpm-build 0fba15
# Boston, MA 02111-1307, USA.
rpm-build 0fba15
rpm-build 0fba15
set -euo pipefail
rpm-build 0fba15
rpm-build 0fba15
. $(dirname $0)/libtest.sh
rpm-build 0fba15
rpm-build 0fba15
echo '1..7'
rpm-build 0fba15
rpm-build 0fba15
cd ${test_tmpdir}
rpm-build 0fba15
rpm-build 0fba15
do_commit() {
rpm-build 0fba15
    local repo=$1
rpm-build 0fba15
    local branch=$2
rpm-build 0fba15
    shift 2
rpm-build 0fba15
rpm-build 0fba15
    mkdir -p files
rpm-build 0fba15
    pushd files
rpm-build 0fba15
    local GPG_ARGS=""
rpm-build 0fba15
    if has_gpgme; then
rpm-build 0fba15
        GPG_ARGS="--gpg-homedir=${TEST_GPG_KEYHOME} --gpg-sign=${TEST_GPG_KEYID_1}"
rpm-build 0fba15
    fi
rpm-build 0fba15
    ${CMD_PREFIX} ostree --repo="../${repo}" commit -s "Test ${repo} commit for branch ${branch}" -b "${branch}" ${GPG_ARGS} "$@" > "../${branch}-checksum"
rpm-build 0fba15
    popd
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
do_summary() {
rpm-build 0fba15
    local repo=$1
rpm-build 0fba15
    shift 1
rpm-build 0fba15
rpm-build 0fba15
    local GPG_ARGS=""
rpm-build 0fba15
    if has_gpgme; then
rpm-build 0fba15
        GPG_ARGS="--gpg-homedir=${TEST_GPG_KEYHOME} --gpg-sign=${TEST_GPG_KEYID_1}"
rpm-build 0fba15
    fi
rpm-build 0fba15
    ${CMD_PREFIX} ostree "--repo=${repo}" summary --update ${GPG_ARGS}
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
do_collection_ref_show() {
rpm-build 0fba15
    local repo=$1
rpm-build 0fba15
    local branch=$2
rpm-build 0fba15
    shift 2
rpm-build 0fba15
rpm-build 0fba15
    echo -n "collection ID: "
rpm-build 0fba15
    if ${CMD_PREFIX} ostree "--repo=${repo}" show --print-metadata-key=ostree.collection-binding $(cat "${branch}-checksum")
rpm-build 0fba15
    then :
rpm-build 0fba15
    else return 1
rpm-build 0fba15
    fi
rpm-build 0fba15
    echo -n "refs: "
rpm-build 0fba15
    if ${CMD_PREFIX} ostree "--repo=${repo}" show --print-metadata-key=ostree.ref-binding $(cat "${branch}-checksum")
rpm-build 0fba15
    then return 0
rpm-build 0fba15
    else return 1
rpm-build 0fba15
    fi
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
ensure_no_collection_ref() {
rpm-build 0fba15
    local repo=$1
rpm-build 0fba15
    local branch=$2
rpm-build 0fba15
    local error=$3
rpm-build 0fba15
    shift 3
rpm-build 0fba15
rpm-build 0fba15
    if do_collection_ref_show "${repo}" "${branch}" >/dev/null
rpm-build 0fba15
    then
rpm-build 0fba15
	assert_not_reached "${error}"
rpm-build 0fba15
    fi
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
do_join() {
rpm-build 0fba15
    local IFS=', '
rpm-build 0fba15
    echo "$*"
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
ensure_collection_ref() {
rpm-build 0fba15
    local repo=$1
rpm-build 0fba15
    local branch=$2
rpm-build 0fba15
    local collection_id=$3
rpm-build 0fba15
    shift 3
rpm-build 0fba15
    local refs=$(do_join "$@")
rpm-build 0fba15
rpm-build 0fba15
    do_collection_ref_show "${repo}" "${branch}" >"${branch}-meta"
rpm-build 0fba15
    assert_file_has_content "${branch}-meta" "^collection ID: '${collection_id}'$"
rpm-build 0fba15
    assert_file_has_content "${branch}-meta" "^refs: \['${refs}'\]$"
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
do_remote_add() {
rpm-build 0fba15
    local repo=$1
rpm-build 0fba15
    local remote_repo=$2
rpm-build 0fba15
    shift 2
rpm-build 0fba15
rpm-build 0fba15
    local GPG_ARGS=""
rpm-build 0fba15
    if has_gpgme; then
rpm-build 0fba15
        GPG_ARGS="--gpg-import=${test_tmpdir}/gpghome/key1.asc"
rpm-build 0fba15
    fi
rpm-build 0fba15
    ${CMD_PREFIX} ostree "--repo=${repo}" remote add "${remote_repo}-remote" "file://$(pwd)/${remote_repo}" "$@" ${GPG_ARGS}
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
do_pull() {
rpm-build 0fba15
    local repo=$1
rpm-build 0fba15
    local remote_repo=$2
rpm-build 0fba15
    local branch=$3
rpm-build 0fba15
    shift 3
rpm-build 0fba15
rpm-build 0fba15
    if ${CMD_PREFIX} ostree "--repo=${repo}" pull "${remote_repo}-remote" "${branch}"
rpm-build 0fba15
    then return 0
rpm-build 0fba15
    else return 1
rpm-build 0fba15
    fi
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
do_local_pull() {
rpm-build 0fba15
    local repo=$1
rpm-build 0fba15
    local remote_repo=$2
rpm-build 0fba15
    local branch=$3
rpm-build 0fba15
    shift 3
rpm-build 0fba15
rpm-build 0fba15
    if ${CMD_PREFIX} ostree "--repo=${repo}" pull-local "${remote_repo}" "${branch}"
rpm-build 0fba15
    then return 0
rpm-build 0fba15
    else return 1
rpm-build 0fba15
    fi
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
# Create a repo without the collection ID.
rpm-build 0fba15
mkdir no-collection-repo
rpm-build 0fba15
ostree_repo_init no-collection-repo
rpm-build 0fba15
do_commit no-collection-repo goodncref1
rpm-build 0fba15
do_commit no-collection-repo sortofbadncref1 --add-metadata-string=ostree.collection-binding=org.example.Ignored
rpm-build 0fba15
do_summary no-collection-repo
rpm-build 0fba15
ensure_no_collection_ref \
rpm-build 0fba15
    no-collection-repo \
rpm-build 0fba15
    goodncref1 \
rpm-build 0fba15
    "commits in repository without collection ID shouldn't normally contain the ostree.commit.collection metadata information"
rpm-build 0fba15
ensure_collection_ref no-collection-repo sortofbadncref1 'org.example.Ignored' 'sortofbadncref1'
rpm-build 0fba15
rpm-build 0fba15
echo "ok 1 setup remote repo without collection ID"
rpm-build 0fba15
rpm-build 0fba15
# Create a repo with a collection ID.
rpm-build 0fba15
mkdir collection-repo
rpm-build 0fba15
ostree_repo_init collection-repo
rpm-build 0fba15
do_commit collection-repo badcref1 # has no collection ref
rpm-build 0fba15
# We set the repo collection ID in this hacky way to get the commit
rpm-build 0fba15
# without the collection ID.
rpm-build 0fba15
echo "collection-id=org.example.CollectionRepo" >>collection-repo/config
rpm-build 0fba15
do_commit collection-repo badcref2 --add-metadata-string=ostree.collection-binding=org.example.Whatever
rpm-build 0fba15
do_commit collection-repo badcref3 --add-metadata-string=ostree.collection-binding=
rpm-build 0fba15
do_commit collection-repo goodcref1
rpm-build 0fba15
# create a badcref4 ref with a commit that has goodcref1 in its collection ref metadata
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=collection-repo refs --create=badcref4 $(cat goodcref1-checksum)
rpm-build 0fba15
do_summary collection-repo
rpm-build 0fba15
ensure_no_collection_ref \
rpm-build 0fba15
    collection-repo \
rpm-build 0fba15
    badcref1 \
rpm-build 0fba15
    "commit in badcref1 should not have the collection ref metadata information"
rpm-build 0fba15
ensure_collection_ref collection-repo badcref2 'org.example.Whatever' 'badcref2'
rpm-build 0fba15
ensure_collection_ref collection-repo badcref3 '' 'badcref3'
rpm-build 0fba15
ensure_collection_ref collection-repo goodcref1 'org.example.CollectionRepo' 'goodcref1'
rpm-build 0fba15
rpm-build 0fba15
echo "ok 2 setup remote repo with collection ID"
rpm-build 0fba15
rpm-build 0fba15
# Create a local repo without the collection ID.
rpm-build 0fba15
mkdir no-collection-local-repo
rpm-build 0fba15
ostree_repo_init no-collection-local-repo
rpm-build 0fba15
do_commit no-collection-local-repo goodnclref1
rpm-build 0fba15
do_commit no-collection-local-repo sortofbadnclref1 --add-metadata-string=ostree.collection-binding=org.example.IgnoredLocal
rpm-build 0fba15
do_summary no-collection-local-repo
rpm-build 0fba15
ensure_no_collection_ref \
rpm-build 0fba15
    no-collection-local-repo \
rpm-build 0fba15
    goodnclref1 \
rpm-build 0fba15
    "commits in repository without collection ID shouldn't normally contain the ostree.commit.collection metadata information"
rpm-build 0fba15
ensure_collection_ref no-collection-local-repo sortofbadnclref1 'org.example.IgnoredLocal' 'sortofbadnclref1'
rpm-build 0fba15
rpm-build 0fba15
echo "ok 3 setup local repo without collection ID"
rpm-build 0fba15
rpm-build 0fba15
# Create a local repo with a collection ID.
rpm-build 0fba15
mkdir collection-local-repo
rpm-build 0fba15
ostree_repo_init collection-local-repo
rpm-build 0fba15
do_commit collection-local-repo badclref1 # has no collection ref
rpm-build 0fba15
# We set the repo collection ID in this hacky way to get the commit
rpm-build 0fba15
# without the collection ID.
rpm-build 0fba15
echo "collection-id=org.example.CollectionRepoLocal" >>collection-local-repo/config
rpm-build 0fba15
do_commit collection-local-repo badclref2 --add-metadata-string=ostree.collection-binding=org.example.WhateverLocal
rpm-build 0fba15
do_commit collection-local-repo badclref3 --add-metadata-string=ostree.collection-binding=
rpm-build 0fba15
do_commit collection-local-repo goodclref1
rpm-build 0fba15
# create a badclref4 ref with a commit that has goodclref1 in its collection ref metadata
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=collection-local-repo refs --create=badclref4 $(cat goodclref1-checksum)
rpm-build 0fba15
do_summary collection-local-repo
rpm-build 0fba15
ensure_no_collection_ref \
rpm-build 0fba15
    collection-local-repo \
rpm-build 0fba15
    badclref1 \
rpm-build 0fba15
    "commit in badclref1 should not have the collection ref metadata information"
rpm-build 0fba15
ensure_collection_ref collection-local-repo badclref2 'org.example.WhateverLocal' 'badclref2'
rpm-build 0fba15
ensure_collection_ref collection-local-repo badclref3 '' 'badclref3'
rpm-build 0fba15
ensure_collection_ref collection-local-repo goodclref1 'org.example.CollectionRepoLocal' 'goodclref1'
rpm-build 0fba15
rpm-build 0fba15
echo "ok 4 setup local repo with collection ID"
rpm-build 0fba15
rpm-build 0fba15
# Create a local repository where we pull the branches from the remotes as normal, using GPG.
rpm-build 0fba15
mkdir local
rpm-build 0fba15
ostree_repo_init local
rpm-build 0fba15
do_remote_add local collection-repo --collection-id org.example.CollectionRepo
rpm-build 0fba15
do_remote_add local no-collection-repo
rpm-build 0fba15
rpm-build 0fba15
do_pull local no-collection-repo goodncref1
rpm-build 0fba15
do_pull local no-collection-repo sortofbadncref1
rpm-build 0fba15
if do_pull local collection-repo badcref1
rpm-build 0fba15
then
rpm-build 0fba15
    assert_not_reached "pulling a commit without collection ID from a repo with collection ID should fail"
rpm-build 0fba15
fi
rpm-build 0fba15
if do_pull local collection-repo badcref2
rpm-build 0fba15
then
rpm-build 0fba15
    assert_not_reached "pulling a commit with a mismatched collection ID from a repo with collection ID should fail"
rpm-build 0fba15
fi
rpm-build 0fba15
if do_pull local collection-repo badcref3
rpm-build 0fba15
then
rpm-build 0fba15
    assert_not_reached "pulling a commit with empty collection ID from repo with collection ID should fail"
rpm-build 0fba15
fi
rpm-build 0fba15
do_pull local collection-repo goodcref1
rpm-build 0fba15
if do_pull local collection-repo badcref4
rpm-build 0fba15
then
rpm-build 0fba15
    assert_not_reached "pulling a commit that was not requested from repo with collection ID should fail"
rpm-build 0fba15
fi
rpm-build 0fba15
rpm-build 0fba15
echo "ok 5 pull refs from remote repos"
rpm-build 0fba15
rpm-build 0fba15
do_local_pull local no-collection-local-repo goodnclref1
rpm-build 0fba15
do_local_pull local no-collection-local-repo sortofbadnclref1
rpm-build 0fba15
if do_local_pull local collection-local-repo badclref1
rpm-build 0fba15
then
rpm-build 0fba15
    assert_not_reached "pulling a commit without collection ID from a repo with collection ID should fail"
rpm-build 0fba15
fi
rpm-build 0fba15
if do_local_pull local collection-local-repo badclref2
rpm-build 0fba15
then
rpm-build 0fba15
    assert_not_reached "pulling a commit with a mismatched collection ID from a repo with collection ID should fail"
rpm-build 0fba15
fi
rpm-build 0fba15
if do_local_pull local collection-local-repo badclref3
rpm-build 0fba15
then
rpm-build 0fba15
    assert_not_reached "pulling a commit with empty collection ID from repo with collection ID should fail"
rpm-build 0fba15
fi
rpm-build 0fba15
do_local_pull local collection-local-repo goodclref1
rpm-build 0fba15
if do_local_pull local collection-local-repo badclref4
rpm-build 0fba15
then
rpm-build 0fba15
    assert_not_reached "pulling a commit that was not requested from repo with collection ID should fail"
rpm-build 0fba15
fi
rpm-build 0fba15
rpm-build 0fba15
echo "ok 6 pull refs from local repos"
rpm-build 0fba15
rpm-build 0fba15
ostree_repo_init local-mirror
rpm-build 0fba15
do_remote_add local-mirror collection-repo --collection-id org.example.CollectionRepo
rpm-build 0fba15
# Generate a summary in the local mirror; don't use do_summary to avoid gpg
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=local-mirror summary --update
rpm-build 0fba15
summarysig=$(sha256sum < local-mirror/summary | cut -f 1 -d ' ')
rpm-build 0fba15
# Mirror subset of refs: A collection-ref version of https://github.com/ostreedev/ostree/issues/846
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=local-mirror find-remotes --pull --mirror --finders=config org.example.CollectionRepo goodcref1
rpm-build 0fba15
newsummarysig=$(sha256sum < local-mirror/summary | cut -f 1 -d ' ')
rpm-build 0fba15
assert_streq ${summarysig} ${newsummarysig}
rpm-build 0fba15
rpm-build 0fba15
echo "ok 7 mirror pull subset of collection-refs with summary"