#!/bin/bash

. ./libtest.sh

test_only_branch() {
    fn_simulate_ostree_system

    new_brach="testos/buildmaster/newbranch1"
    os_repository_new_commit 1 1 $new_brach
    push_content="testos:${new_brach}"

    info "测试：推送更新内容,仅包含分支信息，推送内容：$push_content"

    echo "${push_content}" > $PUSH_FILE

    echo "检查更新"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_signal_success $update_finished_signal

    echo "部署下载"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$download_method > /dev/null
    moniter_signal_success $download_finished_signal
    

    echo "部署内容"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$deploy_method > /dev/null
    moniter_signal_success $deploy_finished_signal

    # 部署完成后的检查
    assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf "${bootcsum}"
    rev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $new_brach)
    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0/usr/bin/content-iteration "1"

    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0.origin "newbranch"
}

test_new_branch_and_commit() {
    fn_simulate_ostree_system
    new_brach="testos/buildmaster/newbranch1"
    os_repository_new_commit 1 1 $new_brach
    newrev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $new_brach)

    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo commit $metadata_string --add-metadata-string new_version=1.0.11 -b $BUILD_MASTER_BRACH -s "Build" --tree=dir=$OS_DATA

    push_content="testos:${new_brach}@$newrev"

    info "测试：推送更新内容:$push_content"

    echo "$push_content" > $PUSH_FILE

    echo "检查更新"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_signal_success $update_finished_signal

    echo "部署下载"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$download_method > /dev/null
    moniter_signal_success $download_finished_signal

    echo "部署内容"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$deploy_method > /dev/null
    moniter_signal_success $deploy_finished_signal

    # 部署完成后的检查
    assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf "${bootcsum}"
    # rev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $new_brach)
    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${newrev}.0/usr/bin/content-iteration "1"

    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${newrev}.0.origin "newbranch"
}

test_new_branch_and_old_commit() {
    echo "测试部署新分支 并且提交的commit在"
    fn_simulate_ostree_system
    new_brach="testos/buildmaster/newbranch1"

    # 再次提交保持不是最新的
    os_repository_new_commit 1 1 $new_brach
    newrev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $new_brach)

    os_repository_new_commit 1 1 $new_brach
    push_content="testos:${new_brach}@$newrev"

    info "测试：推送更新内容:$push_content"

    echo "$push_content" > $PUSH_FILE


    echo "检查更新"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_signal_success $update_finished_signal

    
    echo "部署下载"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$download_method > /dev/null
    moniter_signal_success $download_finished_signal

    echo "部署内容"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$deploy_method > /dev/null
    moniter_signal_success $deploy_finished_signal

    # 部署完成后的检查
    assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf "${bootcsum}"
    # rev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $new_brach)
    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${newrev}.0/usr/bin/content-iteration "1"

    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${newrev}.0.origin "newbranch"
}



test_old_branch_and_commit() {
    fn_simulate_ostree_system

    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo commit $metadata_string --add-metadata-string new_version=1.0.11 -b $BUILD_MASTER_BRACH -s "Build" --tree=dir=$OS_DATA
    newrev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $BUILD_MASTER_BRACH)

    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo commit $metadata_string --add-metadata-string new_version=1.0.12 -b $BUILD_MASTER_BRACH -s "Build" --tree=dir=$OS_DATA

    push_content="testos:${BUILD_MASTER_BRACH}@$newrev"
    info "测试分支@commit号，例如推送更新内容:$push_content"

    echo $push_content > $PUSH_FILE

    echo "检查更新"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_signal_success $update_finished_signal

    echo "部署下载"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$download_method > /dev/null
    moniter_signal_success $download_finished_signal

    echo "部署内容"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$deploy_method > /dev/null
    moniter_signal_success $deploy_finished_signal

    # 部署完成后的检查
    # rev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $new_brach)
    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${newrev}.0/usr/bin/sh "executable"
    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${newrev}.0.origin "newbranch"
}
test_on_push_update() {
    if [ -f "$PUSH_FILE" ]; then
        rm $PUSH_FILE | true
    fi

    echo ""
    echo "检查更新"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null

    moniter_string "$update_finished_signal success = True , upgrade_group = \[\]"

    info "测试 没有推送文件为空的情况下 提示最新。"
    echo "" > $PUSH_FILE

    echo ""
    echo "检查更新"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null

    moniter_string "$update_finished_signal success = True , upgrade_group = \[\]"

}
test_exeption_push() {
    fn_simulate_ostree_system

    new_brach="testos/buildmaster/newbranch1"
    os_repository_new_commit 1 1 $new_brach
    push_content="${new_brach}"

    info "测试：推送更新内容异常情况，推送内容：$push_content"

    echo "${push_content}" > $PUSH_FILE

    echo "检查更新"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null

    # 提示为最新
    moniter_string "$update_finished_signal success = True , upgrade_group = \[\]"

    info "测试：推送错误的分支。"
    push_content="testos:dddd"
    echo "${push_content}" > $PUSH_FILE
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_string "$update_finished_signal success = False , upgrade_group = \[''\], error_code = #0003"


    info "测试：修改错误的远程仓库名词。"
    push_content="testos:dddd"
    echo "[core]
repo_version=1
mode=bare

[remote "testos"]
url=http://localhost:8889/ostree/testos-repo
gpg-verify=false" > /tmp/ostree-auto-test/sysroot/ostree/repo/config
    echo "${push_content}" > $PUSH_FILE
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_string "$update_finished_signal success = True , upgrade_group = \[\]"

    # info "测试：推送错误的commit号。"
    # push_content="testos:${new_brach}@dsdsdsdsdsds"
    # echo "${push_content}" > $PUSH_FILE
    # gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    # moniter_string "$update_finished_signal success = False , upgrade_group = \[''\]"
}

test_mutil_update_and_deploy() {
    echo "测试部署新分支 并且提交的commit在"
    fn_simulate_ostree_system
    
    # 模拟发布新版本的分支
    new_brach="testos/buildmaster/newbranch1"
    os_repository_new_commit 1 1 $new_brach

    # 模拟发布质量更新版本并且增加reflock标识
    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo commit $metadata_string --add-metadata-string=update-type=kylin-update-desktop-squit2 --add-metadata=reflock=true --add-metadata-string new_version=1.0.11 -b $BUILD_MASTER_BRACH -s "Build" --tree=dir=$OS_DATA
    quilty_rev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $BUILD_MASTER_BRACH)

    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo commit $metadata_string --add-metadata-string=update-type=kylin-update-desktop-squit --add-metadata=reflock=true --add-metadata-string new_version=1.0.11 -b $BUILD_MASTER_BRACH -s "Build" --tree=dir=$OS_DATA

    # 推送内容包含 新版本以及质量更新版本
    push_content="testos:${new_brach} testos:$BUILD_MASTER_BRACH testos:$BUILD_MASTER_BRACH@$quilty_rev"
    info "测试：推送更新内容:$push_content"
    echo "$push_content" > $PUSH_FILE


    echo "升级质量更新版本"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_signal_success $update_finished_signal
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.UpdateDownloadPartial "['kylin-update-desktop-squit2']" > /dev/null
    moniter_signal_success $download_finished_signal
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$deploy_method > /dev/null
    moniter_signal_success $deploy_finished_signal


    echo "第二次可以升级系统更新"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_signal_success $update_finished_signal
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.UpdateDownloadPartial "['kylin-update-desktop-system']" > /dev/null
    moniter_signal_success $download_finished_signal
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$deploy_method > /dev/null
    moniter_signal_success $deploy_finished_signal

    # 再次检查更新 不存在可更新内容
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_string "$update_finished_signal success = True , upgrade_group = \[\]"
}

test_mutil_downlod_and_deploy() {
    info "测试推送质量更新1 质量更新2 系统更新"
    fn_simulate_ostree_system
    
    # 模拟发布新版本的分支
    new_brach="testos/buildmaster/newbranch1"
    os_repository_new_commit 1 1 $new_brach

    # 模拟发布质量更新版本并且增加reflock标识
    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo commit $metadata_string --add-metadata-string=update-type=kylin-update-desktop-squit2 --add-metadata=reflock=true --add-metadata-string new_version=1.0.11 -b $BUILD_MASTER_BRACH -s "Build" --tree=dir=$OS_DATA
    quilty_rev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $BUILD_MASTER_BRACH)

    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo commit $metadata_string --add-metadata-string=update-type=kylin-update-desktop-squit --add-metadata=reflock=true --add-metadata-string new_version=1.0.11 -b $BUILD_MASTER_BRACH -s "Build" --tree=dir=$OS_DATA

    # 推送内容包含 新版本以及质量更新版本
    push_content="testos:${new_brach} testos:$BUILD_MASTER_BRACH testos:$BUILD_MASTER_BRACH@$quilty_rev"
    info "测试：推送更新内容:$push_content"
    echo "$push_content" > $PUSH_FILE


    echo "升级质量更新版本"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_signal_success $update_finished_signal

    info "下载质量更新2"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.UpdateDownloadPartial "['kylin-update-desktop-squit2']" > /dev/null
    moniter_signal_success $download_finished_signal

    info "下载系统更新"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.UpdateDownloadPartial "['kylin-update-desktop-system']" > /dev/null
    moniter_signal_success $download_finished_signal

    info "最终安装系统更新"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$deploy_method > /dev/null
    moniter_signal_success $deploy_finished_signal

    info "再次检查更新 不存在可更新内容"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_string "$update_finished_signal success = True , upgrade_group = \[\]"
}
test_delta_upgrade() {
    fn_simulate_ostree_system

    OS_DATA="/tmp/ostree-auto-test/osdata"
    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo static-delta generate --to=$BUILD_MASTER_BRACH
    # 服务器 提交新的可升级补丁
    cp /usr/bin/bash $OS_DATA
    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo commit $metadata_string --add-metadata-string new_version=1.0.11 -b $BUILD_MASTER_BRACH -s "Build" --tree=dir=$OS_DATA
    newrev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $BUILD_MASTER_BRACH)

    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo static-delta generate --to=$BUILD_MASTER_BRACH

    cp /usr/bin/apt-get $OS_DATA
    echo "1" > $OS_DATA/usr/bin/content-iteration
    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo commit $metadata_string --add-metadata-string new_version=1.0.11 -b $BUILD_MASTER_BRACH -s "Build" --tree=dir=$OS_DATA
    sedrev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $BUILD_MASTER_BRACH)

    # 产生delta文件
    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo static-delta generate --to=$BUILD_MASTER_BRACH
    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo summary -u

    # ---------------------------------------------构建离线更新源-----------------------------------------
    mkdir ${test_tmpdir}/offline-repo && ostree_repo_init ${test_tmpdir}/offline-repo --mode=archive

    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/offline-repo remote add --set=gpg-verify=false testos file://${test_tmpdir}/testos-repo

    # 拉取将要升级的源数据  需要拉取所有的元数据
    ${CMD_PREFIX} ostree --repo=${test_tmpdir}/offline-repo pull --depth=-1 --commit-metadata-only testos $BUILD_MASTER_BRACH

    # 防止源数据
    cp -r ${test_tmpdir}/testos-repo/deltas ${test_tmpdir}/offline-repo/

    cp ${test_tmpdir}/testos-repo/summary ${test_tmpdir}/offline-repo/

    rm -rf /media/OfflineSource/
    mkdir -p /media/OfflineSource/ostree-offline-repo

    cp -r ${test_tmpdir}/offline-repo/* /media/OfflineSource/ostree-offline-repo
    # ----------------------------------------------------------------------------------------------------

    ${CMD_PREFIX} ostree --repo=sysroot/ostree/repo remote delete testos

    ${CMD_PREFIX} ostree --repo=sysroot/ostree/repo remote add --set=gpg-verify=false testos file:///media/OfflineSource/ostree-offline-repo

    echo "testos:${BUILD_MASTER_BRACH}@${sedrev}" > $PUSH_FILE

    echo ""
    echo "检查更新"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$update_method > /dev/null
    moniter_signal_success $update_finished_signal

    echo "部署下载"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$download_method > /dev/null
    moniter_signal_success $download_finished_signal

    echo "部署内容"
    gdbus call --system --dest $bus_name --object-path $object_path --method $interface_name.$deploy_method > /dev/null
    moniter_signal_success $deploy_finished_signal

    # 部署完成后的检查
    assert_file_has_content sysroot/boot/loader/entries/ostree-2-testos.conf "${bootcsum}"
    rev=$(${CMD_PREFIX} ostree --repo=${test_tmpdir}/testos-repo rev-parse $BUILD_MASTER_BRACH)
    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0/usr/bin/content-iteration "1"
    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0.origin "newbranch"
}


# 测试 没有推送更新得情况，提示系统最新的。
test_on_push_update

# 测试：测试推送内容为：kylin:testos/branch1 升级当前分支最新版本
test_only_branch

# 测试：推送kylin:testos/branch1@323j23j23j23j2j3 带commit号 升级到指定commit
test_new_branch_and_commit

test_old_branch_and_commit

test_exeption_push

test_new_branch_and_old_commit

# 测试多推送更新情况下得更新
test_mutil_update_and_deploy
test_mutil_downlod_and_deploy

# 测试离线增量更新
test_delta_upgrade