summaryrefslogtreecommitdiff
path: root/tsp/jobs/publish.sh
blob: 9d5452c8c542431c67f37636aed37848dd6c88e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh

# Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Author: Aleksander Mistewicz <a.mistewicz@samsung.com>

export TSP_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)/.."

. "${TSP_DIR}/common.sh"

BUILD_NR="$1"
TARGET="$2"
IS_IDENTICAL="$3"

test -n "$BUILD_NR" || die "Missing argument: build nr"
test -n "$TARGET" || die "Missing argument: target"
test -d "${WS_PUBLISH}" || mkdir -p "${WS_PUBLISH}"

cd "${WS_PUBLISH}"

IMG_WORKDIR="${WS_TEST}/${BUILD_NR}"
DWN_WORKDIR="${WS_DOWNLOAD}/${BUILD_NR}"
WORKSPACE="/home/jenkins/pub"

get_value_of_key_from_dict() {
    KEY="$1"
    DICT="$2"

    test -n "$KEY" || die "Missing argument: key"
    test -n "$DICT" || die "Missing argument: dict"

    VALUE="$(awk -v KEY="${KEY}" '$1 == KEY {print $2}' "$DICT")"

    echo "$VALUE"
}

get_all_pairs() {
    FILE="$1"
    PREFIX="$2"

    test -n "$FILE" || die "Missing argument: file"
    test -n "$PREFIX" || die "Missing argument: prefix"
    test -f "$FILE" || die "No such file: $FILE"

    for key in "RETRIGGER_CNT" "FAILED" "CONNECT_CNT" "OPT_CRASH_CNT" \
        "JOURNAL_TOTAL_CNT" "JOURNAL_DIFF_CNT" "SERVICES_FAIL_CNT"
    do
        # FILE is read every time because sh does not preserve file endings (zsh does)
        eval "${PREFIX}${key}"=$(get_value_of_key_from_dict "$key" "$FILE")
    done
}

compare_with_snapshot() {
    test -f "$SYSCTL_FILE" || return
    test -f "$SNAPSHOT_SYSCTL_FILE" || return

    get_all_pairs "$SYSCTL_FILE" "PRE_"
    get_all_pairs "$SNAPSHOT_SYSCTL_FILE" "SNAP_"

    if [ "$PRE_FAILED" -gt 0 ]
    then
        echo "Fail"
        return
    fi

    if [ "$PRE_RETRIGGER_CNT" -gt 0 ]
    then
        echo "Unclear"
        return
    fi

    for key in "SERVICES_FAIL_CNT" "CONNECT_CNT" "OPT_CRASH_CNT" "SERVICES_FAIL_CNT"
        #"JOURNAL_TOTAL_CNT" "JOURNAL_DIFF_CNT"
    do
        PRE_KEY="PRE_$key"
        PRE_VAL="$(($PRE_KEY))"
        SNAP_KEY="SNAP_$key"
        SNAP_VAL="$(($SNAP_KEY))"
        echo "[$key] snap_value: $SNAP_VAL, pre_value: $PRE_VAL" >&2
        if [ "$PRE_VAL" -gt "$SNAP_VAL" ];
        then
            echo "Unclear"
            return
        fi
    done
    echo "OK"
}

PROJECT_FILE="${DWN_WORKDIR}/project.conf"
SR=$(head -n 1 "${PROJECT_FILE}")
PROFILE="$(sr2profile "${SR}")"
DIFF_FILE="$(get_userspace_dir "${TARGET}" "${DWN_WORKDIR}")/diff.report"
SYSCTL_FILE="${IMG_WORKDIR}/sysctl.result"
SCREENSHOT_FILE="${IMG_WORKDIR}/screenshot.png"
SNAPSHOT_SCREENSHOT_FILE="${WS_TEST}/snapshot_screenshot_${TARGET}-${PROFILE}.png"
SNAPSHOT_SYSCTL_FILE="${WS_TEST}/snapshot_sysctl_${TARGET}-${PROFILE}"
TAG_FILE="${IMG_WORKDIR}/tag"

SNAPSHOT_JSON="${WS_TEST}/snapshot_avocado-${TARGET}.json"
PRERELEASE_JSON="${IMG_WORKDIR}/avocado-results/latest/results.json"

if grep -q "Snapshot" "${DIFF_FILE}"
then
    echo "Processing snapshot: skip comparison, link new results"
    ln -Tsf "$SYSCTL_FILE" "$SNAPSHOT_SYSCTL_FILE"
    ln -Tsf "$PRERELEASE_JSON" "$SNAPSHOT_JSON"
    ln -Tsf "$SCREENSHOT_FILE" "$SNAPSHOT_SCREENSHOT_FILE"
    SNAPSHOT_WS_DOWNLOAD_NEW_PROFILE="${SNAPSHOT_WS_DOWNLOAD_NEW}-${PROFILE}"
    test -L "${SNAPSHOT_WS_DOWNLOAD_NEW_PROFILE}" && mv -T "${SNAPSHOT_WS_DOWNLOAD_NEW_PROFILE}" "${SNAPSHOT_WS_DOWNLOAD}-${PROFILE}"
    TAG="Snapshot"
else
    echo "Processing prerelease"
    if [ -n "${IS_IDENTICAL}" ]
    then
        echo "Prerelease is identical with snapshot"
        TAG="Identical"
    else
        echo "Compare sysctl with latest tested snapshot"
        if [ -f "$PRERELEASE_JSON" ]
        then
            TAG=$(compare_with_snapshot)
            AVOCADO_TAG=$("${TSP_DIR}/scripts/publish_cmp.py" "$SNAPSHOT_JSON" "$PRERELEASE_JSON")
        else
            echo "Prerelease file missing"
            TAG="Fail"
        fi
    fi
fi

test -n "$TAG" || TAG="Unclear"
echo "$TAG" > "$TAG_FILE"

test -n "$AVOCADO_TAG" || AVOCADO_TAG="$TAG"

echo "SR: $SR"
echo "TAG: $TAG"
echo "AVOCADO_TAG: $AVOCADO_TAG"
echo "TARGET: $TARGET"
echo "PROFILE: $PROFILE"

# Publish to database
update_db "$SR" "$AVOCADO_TAG" "$BUILD_NR" "$TARGET"

# Remove all queued WWW jobs
for i in $(tsresults | grep "queued.*WWW" | grep -o "^[0-9]*")
do
    tsresults -r "$i"
done

# Dispatch a new one
tsresults -L WWW sh "${TSP_DIR}/jobs/update_www.sh"