common.sh 4.3 KB
Newer Older
J
jingxiaolu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#!/bin/bash

# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
# isula-build licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: Danni Xia
# Create: 2020-03-01
X
xiadanni 已提交
14
# Description: common functions for tests
J
jingxiaolu 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

# check if legacy builder exists
function pre_check() {
    if pgrep isula-builder > /dev/null 2>&1; then
        echo "isula-builder is already running, please stop it first"
        exit 1
    fi
}

# start isula-builder
function start_isula_builder() {
    nohup isula-builder > /tmp/buildlog-daemon 2>&1 &
    pidofbuilder=$!

    # check if isula-builder is started
    builder_started=0
    for _ in $(seq 1 30); do
        if ! grep -i "is listening on" /tmp/buildlog-daemon > /dev/null 2>&1; then
            sleep 0.1
            continue
        else
            builder_started=1
            break
        fi
    done
    if [ "${builder_started}" -eq 0 ]; then
        echo "isula-builder start failed, log dir /tmp/buildlog-daemon"
        exit 1
    fi
}

X
xiadanni 已提交
46 47 48 49 50 51
function cleanup() {
    isula-build ctr-img rm -p > /dev/null 2>&1
    kill -9 "${pidofbuilder}" > /dev/null 2>&1
    rm -f /tmp/buildlog-*
}

J
jingxiaolu 已提交
52 53
# test build image without output
function test_build_without_output() {
X
xiadanni 已提交
54 55 56 57 58 59 60 61
    if ! isula-build ctr-img build --tag "$1":latest "$2" > /tmp/buildlog-client 2>&1; then
        echo "FAIL"
        echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build without output)"
        kill -9 "${pidofbuilder}"
        exit 1
    fi

    if ! isula-build ctr-img rm "$1":latest > /tmp/buildlog-client 2>&1; then
J
jingxiaolu 已提交
62 63 64 65 66 67 68 69 70
        echo "FAIL"
        echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build without output)"
        kill -9 "${pidofbuilder}"
        exit 1
    fi
}

# test build image with docker-archive output
function test_build_with_docker_archive_output() {
X
xiadanni 已提交
71
    if ! isula-build ctr-img build --output=docker-archive:/tmp/"$1".tar:"$1":latest "$2" > /tmp/buildlog-client 2>&1; then
J
jingxiaolu 已提交
72 73 74 75 76
        echo "FAIL"
        echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with docker-archive output)"
        kill -9 "${pidofbuilder}"
        exit 1
    else
X
xiadanni 已提交
77 78 79 80 81 82 83 84
        rm -f /tmp/"$1".tar
    fi

    if ! isula-build ctr-img rm "$1":latest > /tmp/buildlog-client 2>&1; then
        echo "FAIL"
        echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with docker-archive output)"
        kill -9 "${pidofbuilder}"
        exit 1
J
jingxiaolu 已提交
85 86 87
    fi
}

88
# test build image with docker-daemon output
J
jingxiaolu 已提交
89
function test_build_with_docker_daemon_output() {
90 91 92 93 94
    systemctl status docker > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        return 0
    fi

X
xiadanni 已提交
95
    if ! isula-build ctr-img build --output=docker-daemon:isula/"$1":latest "$2" > /tmp/buildlog-client 2>&1; then
J
jingxiaolu 已提交
96 97 98 99 100
        echo "FAIL"
        echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with docker-daemon output)"
        kill -9 "${pidofbuilder}"
        exit 1
    else
X
xiadanni 已提交
101 102 103 104 105 106 107 108
        docker rmi isula/"$1" > /dev/null 2>&1
    fi

    if ! isula-build ctr-img rm isula/"$1":latest > /tmp/buildlog-client 2>&1; then
        echo "FAIL"
        echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with docker-daemon output)"
        kill -9 "${pidofbuilder}"
        exit 1
J
jingxiaolu 已提交
109
    fi
110 111 112 113 114 115 116 117 118
}

# test build image with isulad output
function test_build_with_isulad_output() {
    systemctl status isulad > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        return 0
    fi

X
xiadanni 已提交
119
    if ! isula-build ctr-img build --output=isulad:isula/"$1":latest "$2" > /tmp/buildlog-client 2>&1; then
120 121 122 123 124
        echo "FAIL"
        echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with isulad output)"
        kill -9 "${pidofbuilder}"
        exit 1
    else
X
xiadanni 已提交
125
        isula rmi isula/"$1" > /dev/null 2>&1
126
    fi
J
jingxiaolu 已提交
127

X
xiadanni 已提交
128 129 130 131 132 133
    if ! isula-build ctr-img rm isula/"$1":latest > /tmp/buildlog-client 2>&1; then
        echo "FAIL"
        echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with isulad output)"
        kill -9 "${pidofbuilder}"
        exit 1
    fi
J
jingxiaolu 已提交
134
}