#!/bin/sh
# $Id$

# metadata (dnf,yum) repo

. ./sh/lib/setup
. ./sh/lib/repo-setup

ORIGREPO=
DESTINATION_REPO=
DEPENDENCY_SOLVER=3

setUp() {
    POLDEK="$POLDEK_NOCONF"
}

tearDown() {
}

testLoad()
{
    msg $POLDEK -s ../metadata/02.repo ls
    local n=$($POLDEK -s metadata/02.repo ls | grep fake_bash | wc -l)
    assertEquals "load failed" "$n" "1"
}

# sizes of 4 GiB and more become the largest size poldek keeps, as from rpm headers
testLoadLargeSizes()
{
    local dir="$TMPDIR/meta-large"
    local primary old new n

    rm -rf $dir
    mkdir -p $dir/repo
    cp -r metadata/repo_02/repodata $dir/repo/ || fail "cp failed"
    primary=$(ls $dir/repo/repodata/*-primary.xml.gz)
    old=$(sha256sum < $primary | cut -d' ' -f1)
    zcat $primary | sed 's/package="2237" installed="0"/package="5368709120" installed="5368709120"/' | gzip -n > $dir/primary.gz
    mv $dir/primary.gz $primary
    new=$(sha256sum < $primary | cut -d' ' -f1)
    sed -i "s/>$old</>$new</" $dir/repo/repodata/repomd.xml
    printf '[r]\nname=R\nenabled=1\nbaseurl=repo\n' > $dir/r.repo

    n=$($POLDEK -s $dir/r.repo desc fake_bash | grep -E '^(Size|Package size): +4096.0 MB' | wc -l)
    assertEquals "sizes should be 4096.0 MB" "$n" "2"
}

# createrepo names SHA-1 checksums "sha" or "sha1"
testLoadSha1Checksums()
{
    local dir="$TMPDIR/meta-sha1"
    local type f old new n

    for type in sha sha1; do
        rm -rf $dir
        mkdir -p $dir/repo
        cp -r metadata/repo_02/repodata $dir/repo/ || fail "cp failed"
        for f in $dir/repo/repodata/*.xml.gz; do
            old=$(sha256sum < $f | cut -d' ' -f1)
            new=$(sha1sum < $f | cut -d' ' -f1)
            sed -i "s|<checksum type=\"sha256\">$old<|<checksum type=\"$type\">$new<|" $dir/repo/repodata/repomd.xml
        done
        printf '[r]\nname=R\nenabled=1\nbaseurl=repo\n' > $dir/r.repo

        n=$($POLDEK -s $dir/r.repo ls | grep fake_bash | wc -l)
        assertEquals "repository with $type checksums not loaded" "1" "$n"
    done
}

testLoadZst()
{
    msg $POLDEK -s ../metadata/02zst.repo ls
    local n=$($POLDEK -s metadata/02zst.repo ls | grep fake_bash | wc -l)
    assertEquals "load failed" "$n" "1"
}

# the baseurl text around a $variable is used as is, '%' included
testLoadPercentInBaseurl()
{
    local dir="$TMPDIR/meta"

    rm -rf $dir
    mkdir -p "$dir/repo%20dir/linux"
    cp -r metadata/repo_02/repodata "$dir/repo%20dir/linux/" || fail "cp failed"
    printf '[r]\nname=R\nenabled=1\nbaseurl=repo%%20dir/$baseos/\n' > $dir/r.repo

    local n=$($POLDEK -s $dir/r.repo ls | grep fake_bash | wc -l)
    assertEquals "load failed" "$n" "1"
}

# after --up fetches a changed repository, ls lists its packages, not the
# ones before the change
testUpRefreshesStubIndex()
{
    local dir="$TMPDIR/meta-up"
    local src="m,type=metadata test://$dir/"
    local primary old new n

    rm -rf $dir
    mkdir -p $dir
    cp -r metadata/repo_02/repodata $dir/ || fail "cp failed"
    $POLDEK_RAW_UP -Osource="$src" --up || fail "up failed"

    # rename fake_bash; primary.xml is named after its checksum
    primary=$(ls $dir/repodata/*-primary.xml.gz)
    old=$(sha256sum < $primary | cut -d' ' -f1)
    zcat $primary | sed 's|<name>fake_bash</name>|<name>fake_zsh</name>|' | gzip -n > $dir/primary.gz
    new=$(sha256sum < $dir/primary.gz | cut -d' ' -f1)
    rm -f $primary
    mv $dir/primary.gz $dir/repodata/$new-primary.xml.gz
    sed -i "s/$old/$new/g" $dir/repodata/repomd.xml
    $POLDEK_RAW_UP -Osource="$src" --up || fail "up failed"

    # ls reads the stub index, which --up refreshes
    n=$($POLDEK_RAW_UP -q -Osource="$src" --cmd ls | grep -c '^fake_zsh-')
    assertEquals "ls should list the renamed package" "1" "$n"
}

# a new repomd.xml whose primary.xml cannot be fetched fails the update
testUpFailsWithoutNewPrimary()
{
    local dir="$TMPDIR/meta-noprimary"
    local src="m,type=metadata test://$dir/"
    local op primary old new

    for op in --up --upa; do
        rm -rf $dir $CACHEDIR/*
        mkdir -p $dir
        cp -r metadata/repo_02/repodata $dir/ || fail "cp failed"
        $POLDEK_RAW_UP -Osource="$src" --up || fail "up failed"

        primary=$(ls $dir/repodata/*-primary.xml.gz)
        old=$(sha256sum < $primary | cut -d' ' -f1)
        new=$(echo changed | sha256sum | cut -d' ' -f1)
        sed -i "s/$old/$new/g" $dir/repodata/repomd.xml

        $POLDEK_RAW_UP -Osource="$src" $op
        assertTrue "$op ended with exit code 0" "[ $? -ne 0 ]"
    done
}


. ./sh/lib/shunit2
