-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathcommon-build-env-test.sh
executable file
·201 lines (176 loc) · 9.08 KB
/
common-build-env-test.sh
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env bash
# A "unit test" for Bash libraries used in build/test scripts.
#
# The following only applies to changes made to this file as part of YugaByte development.
#
# Portions Copyright (c) YugaByte, Inc.
#
# 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.
#
set -euo pipefail
# shellcheck source=build-support/common-build-env.sh
. "${0%/*}/common-build-env.sh"
assert_equals() {
local yb_fatal_quiet=false
if [[ $# -lt 2 ]]; then
fatal "assert_equals requires at least 2 arguments"
fi
expected_value=$1
actual_value=$2
shift 2
local extra_details="$*"
if [[ -n $extra_details ]]; then
extra_details=" $extra_details"
fi
if [[ "$expected_value" != "$actual_value" ]]; then
fatal "Assertion failed." \
"Expected: '$expected_value', got: '$actual_value'.$extra_details"
fi
}
# -------------------------------------------------------------------------------------------------
# Testing detecting build type by Jenkins job name.
# -------------------------------------------------------------------------------------------------
test_build_type_detection_by_jenkins_job_name() {
expect_num_args 2 "$@"
local expected_build_type=$1
local jenkins_job_name=$2
(
unset YB_COMPILER_TYPE
unset build_type
JOB_NAME="$jenkins_job_name"
set_build_type_based_on_jenkins_job_name
assert_equals "$expected_build_type" "$build_type" "Jenkins job name: $jenkins_job_name"
)
}
yb_log_quiet=true
test_build_type_detection_by_jenkins_job_name asan my-asan-job
test_build_type_detection_by_jenkins_job_name asan Asan-my-job
test_build_type_detection_by_jenkins_job_name asan my-job-ASAN
test_build_type_detection_by_jenkins_job_name debug my-debug-job
test_build_type_detection_by_jenkins_job_name debug deBuG-my-job
test_build_type_detection_by_jenkins_job_name debug my-job-debug
test_build_type_detection_by_jenkins_job_name fastdebug my-fastdebug-job
test_build_type_detection_by_jenkins_job_name fastdebug fasTdeBug-my-job
test_build_type_detection_by_jenkins_job_name fastdebug my-job-fastdebug
test_build_type_detection_by_jenkins_job_name release my-relEase-job
test_build_type_detection_by_jenkins_job_name release releasE-job
test_build_type_detection_by_jenkins_job_name release my-job-RELEASE
test_build_type_detection_by_jenkins_job_name tsan my-tsan-job
test_build_type_detection_by_jenkins_job_name tsan TSAN-my-job
test_build_type_detection_by_jenkins_job_name tsan my-job-tsan
test_build_type_detection_by_jenkins_job_name debug random-job-name
test_build_type_detection_by_jenkins_job_name debug releasewithoutwordseparator-job-name
test_build_type_detection_by_jenkins_job_name debug job-name-noseparatorfastdebug
# -------------------------------------------------------------------------------------------------
# Test determining compiler type based on the Jenkins job name.
# -------------------------------------------------------------------------------------------------
test_compiler_detection_by_jenkins_job_name() {
expect_num_args 2 "$@"
local expected_compiler_type=$1
local jenkins_job_name=$2
(
unset YB_COMPILER_TYPE
JOB_NAME="$jenkins_job_name"
set_compiler_type_based_on_jenkins_job_name
assert_equals "$expected_compiler_type" "$YB_COMPILER_TYPE" "compiler type"
)
}
test_compiler_detection_by_jenkins_job_name gcc my-job-gcc
test_compiler_detection_by_jenkins_job_name gcc gcc-my-job
test_compiler_detection_by_jenkins_job_name clang my-job-clang
test_compiler_detection_by_jenkins_job_name clang clang-my-job
test_compiler_detection_by_jenkins_job_name "" random-job-name
# -------------------------------------------------------------------------------------------------
# Test determining build type to pass to our CMakeLists.txt as CMAKE_BUILD_TYPE and compiler type.
# -------------------------------------------------------------------------------------------------
test_set_cmake_build_type_and_compiler_type() {
expect_num_args 6 "$@"
local _build_type=$1
validate_build_type "$_build_type"
local os_type=$2
if [[ ! "$os_type" =~ ^(darwin|linux-gnu) ]]; then
fatal "Unexpected value for the mock OSTYPE: '$os_type'"
fi
local compiler_type_preference=$3
if [[ ! "$compiler_type_preference" =~ ^(gcc[0-9]*|clang[0-9]*|auto|N/A)$ ]]; then
fatal "Invalid value for compiler_type_preference: '$compiler_type_preference'"
fi
local test_case_details="Build type: $_build_type, "
local test_case_details+="OS type: $os_type, "
local test_case_details+="compiler type preference: $compiler_type_preference."
local expected_cmake_build_type=$4
local expected_compiler_type=$5
local expected_exit_code=$6
set +e
(
set -e
if [[ "$compiler_type_preference" == "auto" ]]; then
unset YB_COMPILER_TYPE
else
YB_COMPILER_TYPE=$compiler_type_preference
fi
unset cmake_build_type
build_type=$_build_type
OSTYPE=$os_type
yb_fatal_quiet=true
set_cmake_build_type_and_compiler_type
assert_equals "$expected_cmake_build_type" "$cmake_build_type" "$test_case_details" \
"Note: comparing CMake build type."
assert_equals "$expected_compiler_type" "$YB_COMPILER_TYPE" "$test_case_details" \
"Note: comparing compiler type."
)
local exit_code=$?
set -e
assert_equals "$expected_exit_code" "$exit_code"
}
arch=$( uname -m )
# Parameters: build_type OSTYPE Compiler Expected Expected
# type build_type YB_COMPILER_
# preference TYPE
# The last parameter is expected exit code (0 or 1).
test_set_cmake_build_type_and_compiler_type asan darwin auto fastdebug clang 0
test_set_cmake_build_type_and_compiler_type asan darwin clang fastdebug clang 0
test_set_cmake_build_type_and_compiler_type asan darwin gcc N/A N/A 1
test_set_cmake_build_type_and_compiler_type asan linux-gnu clang12 fastdebug clang12 0
test_set_cmake_build_type_and_compiler_type asan linux-gnu gcc N/A N/A 1
test_set_cmake_build_type_and_compiler_type asan linux-gnu gcc11 N/A gcc11 1
test_set_cmake_build_type_and_compiler_type tsan linux-gnu clang12 fastdebug clang12 0
test_set_cmake_build_type_and_compiler_type tsan linux-gnu gcc N/A N/A 1
test_set_cmake_build_type_and_compiler_type tsan linux-gnu gcc11 N/A gcc11 1
test_set_cmake_build_type_and_compiler_type debug darwin auto debug clang 0
test_set_cmake_build_type_and_compiler_type debug darwin clang debug clang 0
test_set_cmake_build_type_and_compiler_type debug linux-gnu clang debug clang 0
test_set_cmake_build_type_and_compiler_type debug linux-gnu gcc debug gcc 0
test_set_cmake_build_type_and_compiler_type debug linux-gnu gcc11 debug gcc11 0
test_set_cmake_build_type_and_compiler_type FaStDeBuG darwin auto fastdebug clang 0
test_set_cmake_build_type_and_compiler_type FaStDeBuG darwin clang fastdebug clang 0
test_set_cmake_build_type_and_compiler_type FaStDeBuG linux-gnu clang fastdebug clang 0
test_set_cmake_build_type_and_compiler_type FaStDeBuG linux-gnu gcc fastdebug gcc 0
test_set_cmake_build_type_and_compiler_type release darwin auto release clang 0
test_set_cmake_build_type_and_compiler_type release darwin clang release clang 0
test_set_cmake_build_type_and_compiler_type release linux-gnu clang release clang 0
test_set_cmake_build_type_and_compiler_type release linux-gnu gcc release gcc 0
test_set_cmake_build_type_and_compiler_type release linux-gnu gcc11 release gcc11 0
# Test cases where there is difference between architectures.
if [[ $arch == "x86_64" ]]; then
clangN=clang14
clangA=clang13
else
clangN=clang12
clangA=clang12
fi
test_set_cmake_build_type_and_compiler_type debug linux-gnu auto debug $clangN 0
test_set_cmake_build_type_and_compiler_type FaStDeBuG linux-gnu auto fastdebug $clangN 0
test_set_cmake_build_type_and_compiler_type release linux-gnu auto release $clangN 0
test_set_cmake_build_type_and_compiler_type tsan linux-gnu auto fastdebug $clangN 0
test_set_cmake_build_type_and_compiler_type asan linux-gnu auto fastdebug $clangA 0
# -------------------------------------------------------------------------------------------------
echo "${0##/*} succeeded"