Blame tests/test-fsck-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..11'
rpm-build 0fba15
rpm-build 0fba15
cd ${test_tmpdir}
rpm-build 0fba15
rpm-build 0fba15
# Create a new repository with one ref with the repository’s collection ID, and
rpm-build 0fba15
# one ref with a different collection ID (which should be in refs/mirrors).
rpm-build 0fba15
set_up_repo_with_collection_id() {
rpm-build 0fba15
  rm -rf repo files
rpm-build 0fba15
rpm-build 0fba15
  mkdir repo
rpm-build 0fba15
  ostree_repo_init repo --collection-id org.example.Collection
rpm-build 0fba15
rpm-build 0fba15
  mkdir files
rpm-build 0fba15
  pushd files
rpm-build 0fba15
  ${CMD_PREFIX} ostree --repo=../repo commit -s "Commit 1" -b ref1 > ../ref1-checksum
rpm-build 0fba15
  ${CMD_PREFIX} ostree --repo=../repo commit -s "Commit 2" --orphan --bind-ref ref2 --add-metadata-string=ostree.collection-binding=org.example.Collection2 > ../ref2-checksum
rpm-build 0fba15
  ${CMD_PREFIX} ostree --repo=../repo refs --collections --create=org.example.Collection2:ref2 $(cat ../ref2-checksum)
rpm-build 0fba15
  popd
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
# Create a new repository with one ref and no collection IDs.
rpm-build 0fba15
set_up_repo_without_collection_id() {
rpm-build 0fba15
  rm -rf repo files
rpm-build 0fba15
rpm-build 0fba15
  mkdir repo
rpm-build 0fba15
  ostree_repo_init repo
rpm-build 0fba15
rpm-build 0fba15
  mkdir files
rpm-build 0fba15
  pushd files
rpm-build 0fba15
  ${CMD_PREFIX} ostree --repo=../repo commit -s "Commit 3" -b ref3 --bind-ref ref4 > ../ref3-checksum
rpm-build 0fba15
  ${CMD_PREFIX} ostree --repo=../repo refs --create=ref4 $(cat ../ref3-checksum)
rpm-build 0fba15
  popd
rpm-build 0fba15
}
rpm-build 0fba15
rpm-build 0fba15
set_up_repo_with_collection_id
rpm-build 0fba15
rpm-build 0fba15
# fsck at this point should succeed
rpm-build 0fba15
${CMD_PREFIX} ostree fsck --repo=repo > fsck
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs in collections\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
# Drop the commit the ref points to, and drop the original ref so that fsck doesn’t prematurely fail on that.
rpm-build 0fba15
find repo/objects -name '*.commit' -delete -print | wc -l > commitcount
rpm-build 0fba15
assert_file_has_content commitcount "^2$"
rpm-build 0fba15
rpm-build 0fba15
rm repo/refs/heads/ref1
rpm-build 0fba15
rpm-build 0fba15
# fsck should now fail
rpm-build 0fba15
if ${CMD_PREFIX} ostree fsck --repo=repo > fsck; then
rpm-build 0fba15
    assert_not_reached "fsck unexpectedly succeeded after deleting commit!"
rpm-build 0fba15
fi
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs\.\.\.$"
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs in collections\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
echo "ok 1 fsck-collections"
rpm-build 0fba15
rpm-build 0fba15
# Try fsck in an old repository where refs/mirrors doesn’t exist to begin with.
rpm-build 0fba15
# It should succeed.
rpm-build 0fba15
set_up_repo_with_collection_id
rpm-build 0fba15
rm -rf repo/refs/mirrors
rpm-build 0fba15
rpm-build 0fba15
${CMD_PREFIX} ostree fsck --repo=repo > fsck
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs\.\.\.$"
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs in collections\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
echo "ok 2 fsck-collections in old repository"
rpm-build 0fba15
rpm-build 0fba15
# Test that fsck detects commits which are pointed to by refs, but which don’t
rpm-build 0fba15
# list those refs in their ref-bindings.
rpm-build 0fba15
set_up_repo_with_collection_id
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=repo refs --create=new-ref $(cat ref1-checksum)
rpm-build 0fba15
rpm-build 0fba15
# For compatibility we don't check for this by default
rpm-build 0fba15
${CMD_PREFIX} ostree fsck --repo=repo
rpm-build 0fba15
# fsck should now fail
rpm-build 0fba15
if ${CMD_PREFIX} ostree fsck --repo=repo --verify-bindings > fsck 2> fsck-error; then
rpm-build 0fba15
    assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
rpm-build 0fba15
fi
rpm-build 0fba15
assert_file_has_content fsck-error "Commit has no requested ref ‘new-ref’ in ref binding metadata (‘ref1’)"
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
echo "ok 3 fsck detects missing ref bindings"
rpm-build 0fba15
rpm-build 0fba15
# And the same where the ref is a collection–ref.
rpm-build 0fba15
set_up_repo_with_collection_id
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=repo refs --collections --create=org.example.Collection2:new-ref $(cat ref1-checksum)
rpm-build 0fba15
rpm-build 0fba15
# fsck should now fail
rpm-build 0fba15
if ${CMD_PREFIX} ostree fsck --repo=repo --verify-bindings > fsck 2> fsck-error; then
rpm-build 0fba15
    assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
rpm-build 0fba15
fi
rpm-build 0fba15
assert_file_has_content fsck-error "Commit has no requested ref ‘new-ref’ in ref binding metadata (‘ref1’)"
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs\.\.\.$"
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs in collections\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
echo "ok 4 fsck detects missing collection–ref bindings"
rpm-build 0fba15
rpm-build 0fba15
# Check that a ref with a different collection ID but the same ref name is caught.
rpm-build 0fba15
set_up_repo_with_collection_id
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=repo refs --collections --create=org.example.Collection2:ref1 $(cat ref1-checksum)
rpm-build 0fba15
rpm-build 0fba15
# fsck should now fail
rpm-build 0fba15
if ${CMD_PREFIX} ostree fsck --repo=repo --verify-bindings > fsck 2> fsck-error; then
rpm-build 0fba15
    assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
rpm-build 0fba15
fi
rpm-build 0fba15
assert_file_has_content fsck-error "Commit has collection ID ‘org\.example\.Collection’ in collection binding metadata, while the remote it came from has collection ID ‘org\.example\.Collection2’"
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs\.\.\.$"
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs in collections\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
echo "ok 5 fsck detects missing collection–ref bindings"
rpm-build 0fba15
rpm-build 0fba15
# Check that a commit with ref bindings which aren’t pointed to by refs is OK.
rpm-build 0fba15
set_up_repo_with_collection_id
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=repo refs --delete ref1
rpm-build 0fba15
rpm-build 0fba15
# fsck at this point should succeed
rpm-build 0fba15
${CMD_PREFIX} ostree fsck --repo=repo > fsck
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs in collections\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
echo "ok 6 fsck ignores unreferenced ref bindings"
rpm-build 0fba15
rpm-build 0fba15
# …but it’s not OK if we pass --verify-back-refs to fsck.
rpm-build 0fba15
if ${CMD_PREFIX} ostree fsck --repo=repo --verify-back-refs > fsck 2> fsck-error; then
rpm-build 0fba15
    assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
rpm-build 0fba15
fi
rpm-build 0fba15
assert_file_has_content fsck-error "Collection–ref (org\.example\.Collection, ref1) in bindings for commit .* does not exist"
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs\.\.\.$"
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs in collections\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
echo "ok 7 fsck ignores unreferenced ref bindings"
rpm-build 0fba15
rpm-build 0fba15
#
rpm-build 0fba15
# Now repeat most of the above tests with a repository without collection IDs.
rpm-build 0fba15
#
rpm-build 0fba15
rpm-build 0fba15
set_up_repo_without_collection_id
rpm-build 0fba15
rpm-build 0fba15
# fsck at this point should succeed
rpm-build 0fba15
${CMD_PREFIX} ostree fsck --repo=repo --verify-bindings > fsck
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs in collections\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
# Drop the commit the ref points to, and drop the original ref so that fsck doesn’t prematurely fail on that.
rpm-build 0fba15
find repo/objects -name '*.commit' -delete -print | wc -l > commitcount
rpm-build 0fba15
assert_file_has_content commitcount "^1$"
rpm-build 0fba15
rpm-build 0fba15
rm repo/refs/heads/ref3
rpm-build 0fba15
rpm-build 0fba15
# fsck should now fail
rpm-build 0fba15
if ${CMD_PREFIX} ostree fsck --repo=repo --verify-bindings > fsck; then
rpm-build 0fba15
    assert_not_reached "fsck unexpectedly succeeded after deleting commit!"
rpm-build 0fba15
fi
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
echo "ok 8 fsck-collections"
rpm-build 0fba15
rpm-build 0fba15
# Test that fsck detects commits which are pointed to by refs, but which don’t
rpm-build 0fba15
# list those refs in their ref-bindings.
rpm-build 0fba15
set_up_repo_without_collection_id
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=repo refs --create=new-ref $(cat ref3-checksum)
rpm-build 0fba15
rpm-build 0fba15
# fsck should now fail
rpm-build 0fba15
if ${CMD_PREFIX} ostree fsck --repo=repo --verify-bindings > fsck 2> fsck-error; then
rpm-build 0fba15
    assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
rpm-build 0fba15
fi
rpm-build 0fba15
assert_file_has_content fsck-error "Commit has no requested ref ‘new-ref’ in ref binding metadata (‘ref3’, ‘ref4’)"
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
echo "ok 9 fsck detects missing ref bindings"
rpm-build 0fba15
rpm-build 0fba15
# Check that a commit with ref bindings which aren’t pointed to by refs is OK.
rpm-build 0fba15
set_up_repo_without_collection_id
rpm-build 0fba15
${CMD_PREFIX} ostree --repo=repo refs --delete ref3
rpm-build 0fba15
rpm-build 0fba15
# fsck at this point should succeed
rpm-build 0fba15
${CMD_PREFIX} ostree fsck --repo=repo > fsck
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
echo "ok 10 fsck ignores unreferenced ref bindings"
rpm-build 0fba15
rpm-build 0fba15
# …but it’s not OK if we pass --verify-back-refs to fsck.
rpm-build 0fba15
if ${CMD_PREFIX} ostree fsck --repo=repo --verify-back-refs > fsck 2> fsck-error; then
rpm-build 0fba15
    assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
rpm-build 0fba15
fi
rpm-build 0fba15
assert_file_has_content fsck-error "Ref ‘ref3’ in bindings for commit .* does not exist"
rpm-build 0fba15
assert_file_has_content fsck "^Validating refs\.\.\.$"
rpm-build 0fba15
rpm-build 0fba15
echo "ok 11 fsck ignores unreferenced ref bindings"