From 639196b807b6af812391fc539729a01851af4411 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Kadl=C4=8D=C3=ADk?= <frostyx@email.cz>
Date: Thu, 15 Feb 2018 23:38:02 +0100
Subject: [PATCH] [beaker-tests-sanity] test custom method

---
 .../files/quick-package.sh                         |  49 +++++
 .../runtest-custom-method.sh                       | 218 +++++++++++++++++++++
 2 files changed, 267 insertions(+)
 create mode 100755 beaker-tests/Sanity/copr-cli-basic-operations/files/quick-package.sh
 create mode 100755 beaker-tests/Sanity/copr-cli-basic-operations/runtest-custom-method.sh

diff --git a/beaker-tests/Sanity/copr-cli-basic-operations/files/quick-package.sh b/beaker-tests/Sanity/copr-cli-basic-operations/files/quick-package.sh
new file mode 100755
index 00000000..ce8ed621
--- /dev/null
+++ b/beaker-tests/Sanity/copr-cli-basic-operations/files/quick-package.sh
@@ -0,0 +1,49 @@
+#! /bin/sh -x
+
+set -e
+
+generate_specfile()
+{
+    test -n "$DESTDIR" && mkdir -p "$DESTDIR"
+
+    test -n "$BUILDDEPS" && {
+        for i in $BUILDDEPS; do
+            rpm -q $i
+        done
+    }
+
+    if ${HOOK_PAYLOAD-false}; then
+        test -f hook_payload
+        test "$(cat hook_payload)" = "{\"a\": \"b\"}"
+    else
+        ! test -f hook_payload
+    fi
+
+cat > "${DESTDIR-.}"/quick-package.spec <<\EOF
+Name:           quick-package
+Version:        0
+Release:        0%{?dist}
+Summary:        dummy package
+License:        GPL
+URL:            http://example.com/
+
+%{!?_pkgdocdir: %global _pkgdocdir %{_docdir}/%{name}-%{version}}
+
+%description
+nothing
+
+
+%install
+mkdir -p $RPM_BUILD_ROOT/%{_pkgdocdir}
+echo "this does nothing" > $RPM_BUILD_ROOT/%{_pkgdocdir}/README
+
+
+%files
+%doc %{_pkgdocdir}/README
+
+
+%changelog
+* Thu Jun 05 2014 Pavel Raiskup <praiskup@redhat.com> - 0-1
+- does nothing!
+EOF
+}
diff --git a/beaker-tests/Sanity/copr-cli-basic-operations/runtest-custom-method.sh b/beaker-tests/Sanity/copr-cli-basic-operations/runtest-custom-method.sh
new file mode 100755
index 00000000..179be76e
--- /dev/null
+++ b/beaker-tests/Sanity/copr-cli-basic-operations/runtest-custom-method.sh
@@ -0,0 +1,218 @@
+#!/bin/bash
+
+. /usr/bin/rhts-environment.sh || exit 1
+. /usr/share/beakerlib/beakerlib.sh || exit 1
+
+export RESULTDIR=`mktemp -d`
+
+export TESTPATH="$( builtin cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+
+export IN=$TESTPATH/action-tasks.json
+export OUT=$TESTPATH/action-results.out.json
+
+if [[ ! $FRONTEND_URL ]]; then
+    FRONTEND_URL="http://copr-fe-dev.cloud.fedoraproject.org"
+fi
+
+
+NAME_VAR="TEST$(date +%s)"
+
+
+parse_build_id()
+{
+   local id
+   id=$(grep 'Created builds:' "$rlRun_LOG" | sed 's/.* //')
+   test -n "$id" || return 1
+   export BUILD_ID=$id
+}
+
+cleanup_resultdir ()
+(
+    rm -rf "$RESULTDIR/*"
+)
+
+check_resultdir ()
+(
+    set -e
+    cd "$RESULTDIR/fedora-rawhide-x86_64"
+    # @FIXME
+    #test -f "$RESULTDIR"/script
+    for i in $FILES; do
+        echo "checking that $i exists in resultdir"
+        test -f "$i"
+    done
+
+    NV=$1
+
+    echo "checking that only one srpm exists"
+    set -- *.src.rpm
+    test 1 -eq "$#"
+
+    echo "checking that srpm version is fine"
+    case $1 in
+        $NV*.src.rpm) ;; # OK
+        *) false ;;
+    esac
+)
+
+check_http_status ()
+{
+   grep "HTTP/1.1 $1" "$rlRun_LOG"
+   echo "FOOO0000000000000000"
+   ls "$rlRun_LOG"
+}
+
+quick_package_script ()
+{
+    cp "$TESTPATH/files/quick-package.sh" script
+    echo "$1" >> script
+}
+
+
+rlJournalStart
+    rlPhaseStartTest Test
+    rlRun "export WORKDIR=\`mktemp -d\`"
+    rlRun "test -n \"\$WORKDIR\""
+
+    rlRun 'cd "$WORKDIR"'
+    rlRun 'echo workdir: $WORKDIR'
+
+
+    rlLogInfo "Create the project $PROJECT"
+    PROJECT=custom-1-$NAME_VAR
+    rlRun 'copr-cli create "$PROJECT" --chroot fedora-rawhide-x86_64'
+
+
+    rlLogInfo "Test add-package && build"
+    rlRun 'cleanup_resultdir'
+    rlRun 'quick_package_script generate_specfile'
+    rlRun 'copr add-package-custom "$PROJECT" \
+        --name quick-package \
+        --script script \
+        --script-chroot fedora-rawhide-x86_64'
+    rlRun -s 'copr build-package "$PROJECT" --name quick-package --nowait'
+    rlRun 'parse_build_id'
+    rlRun 'copr watch-build $BUILD_ID'
+    rlRun 'copr download-build $BUILD_ID --dest $RESULTDIR'
+    rlRun 'FILES="success" check_resultdir quick-package-0-0'
+
+
+    rlLogInfo "Test edit-package && --resultdir"
+    rlRun 'cleanup_resultdir'
+    rlRun 'quick_package_script "DESTDIR=rrr generate_specfile"'
+    rlRun 'copr edit-package-custom "$PROJECT" \
+        --name quick-package \
+        --script script \
+        --script-chroot fedora-rawhide-x86_64'
+    rlRun -s 'copr build-package "$PROJECT" --name quick-package --nowait'
+    rlRun 'parse_build_id'
+    # Should fail, there's no spec file in expected resultdir=.
+    rlRun 'copr watch-build $BUILD_ID' 4
+
+    rlRun 'copr edit-package-custom "$PROJECT" \
+        --name quick-package \
+        --script script \
+        --script-resultdir rrr \
+        --script-chroot fedora-rawhide-x86_64'
+    rlRun -s 'copr build-package "$PROJECT" --name quick-package --nowait'
+    rlRun 'parse_build_id'
+    rlRun 'copr watch-build $BUILD_ID'
+    rlRun 'copr download-build $BUILD_ID --dest $RESULTDIR'
+    rlRun 'FILES="success" check_resultdir quick-package-0-0'
+
+
+    rlLogInfo "Test that builddeps get propagated"
+    builddeps="automake autoconf spax"
+    rlRun 'cleanup_resultdir'
+    rlRun 'quick_package_script "BUILDDEPS=xxx generate_specfile"'
+    rlRun 'copr edit-package-custom "$PROJECT" \
+        --name quick-package \
+        --script script \
+        --script-builddeps "$builddeps" \
+        --script-chroot fedora-rawhide-x86_64'
+    rlRun -s 'copr build-package "$PROJECT" --name quick-package --nowait'
+    rlRun 'parse_build_id'
+    # Invalid BUILDDEPS value, should fail
+    rlRun 'copr watch-build $BUILD_ID' 4
+
+    rlRun 'quick_package_script "BUILDDEPS=\"$builddeps\" generate_specfile"'
+    rlRun 'copr edit-package-custom "$PROJECT" \
+        --name quick-package \
+        --script script \
+        --script-builddeps "$builddeps" \
+        --script-chroot fedora-rawhide-x86_64'
+    rlRun -s 'copr build-package "$PROJECT" --name quick-package --nowait'
+    rlRun 'parse_build_id'
+    # Invalid BUILDDEPS value, should fail
+    rlRun 'copr watch-build $BUILD_ID'
+    rlRun 'copr download-build $BUILD_ID --dest $RESULTDIR'
+    rlRun 'FILES="success" check_resultdir quick-package-0-0'
+
+
+    rlLogInfo "check that hook_payload get's created"
+    rlRun 'cleanup_resultdir'
+    rlRun 'quick_package_script "HOOK_PAYLOAD=: generate_specfile"'
+    rlRun 'copr edit-package-custom "$PROJECT" \
+        --name quick-package \
+        --script script \
+        --script-chroot fedora-rawhide-x86_64 \
+        --webhook-rebuild on'
+    rlRun -s 'copr build-package "$PROJECT" --name quick-package --nowait'
+    rlRun 'parse_build_id'
+    rlLogInfo "Still should fail, since this build is not triggered by webhook."
+    rlRun 'copr watch-build $BUILD_ID' 4
+
+    trigger_url="$FRONTEND_URL/webhooks/custom/$copr_id/webhook_secret/quick-package/"
+    rlRun -s 'curl -I "$trigger_url"' 0 # GET can't work
+    rlRun 'check_http_status 405'
+
+    content_type_option=' -H "Content-Type: application/json"'
+    data_option=' --data '\''{"a": "b"}'\'
+
+    rlLogInfo "full cmd would be: curl -X POST $content_type_option $data_option $trigger_url"
+    rlRun "build_id=\$(curl -X POST $data_option \"$trigger_url\")" 0
+    rlLogInfo "Still fails since the POST data are not json"
+    rlRun 'copr watch-build $BUILD_ID' 4
+
+    rlLogInfo "Still fails since the POST data are not json"
+    rlRun "build_id=\$(curl -X POST $content_type_option $data_option \"$trigger_url\")" 0
+    rlLogInfo "Should succeed finally"
+    # @FIXME
+    # rlRun 'copr watch-build $build_id'
+    # rlRun 'copr download-build $build_id --dest $RESULTDIR'
+    # rlRun 'FILES="success" check_resultdir quick-package-0-0'
+
+
+    rlLogInfo "basic buildcustom command, with fedora-latest-x86_64 chroot (default)"
+    rlRun 'cleanup_resultdir'
+    rlRun 'quick_package_script "generate_specfile"'
+    rlRun -s "copr buildcustom $PROJECT --script script --nowait"
+    rlRun 'parse_build_id'
+    rlRun 'copr watch-build $BUILD_ID'
+    rlRun 'copr download-build $BUILD_ID --dest $RESULTDIR'
+    rlRun 'FILES="success" check_resultdir quick-package-0-0'
+
+
+    rlLogInfo "buildcustom with --builddeps"
+    builddeps='postgresql-devel'
+    rlRun 'cleanup_resultdir'
+    rlRun "quick_package_script 'BUILDDEPS=\"$builddeps\" generate_specfile'"
+    rlRun -s "copr buildcustom $PROJECT --script script --script-builddeps \"$builddeps\" --nowait"
+    rlRun 'parse_build_id'
+    rlRun 'copr watch-build $BUILD_ID'
+    rlRun 'copr download-build $BUILD_ID --dest $RESULTDIR'
+    rlRun 'FILES="success" check_resultdir quick-package-0-0'
+
+
+    rlLogInfo "buildcustom with --builddeps and --resultdir"
+    destdir=abc
+    rlRun 'cleanup_resultdir'
+    rlRun "quick_package_script 'BUILDDEPS=\"$builddeps\" DESTDIR=$destdir generate_specfile'"
+    rlRun -s "copr buildcustom $PROJECT --script script --script-resultdir=$destdir --script-builddeps \"$builddeps\" --nowait"
+    rlRun 'parse_build_id'
+    rlRun 'copr watch-build $BUILD_ID'
+    rlRun 'copr download-build $BUILD_ID --dest $RESULTDIR'
+    rlRun 'FILES="success" check_resultdir quick-package-0-0'
+
+    rlPhaseEnd
+rlJournalEnd
-- 
2.13.6

