Skip to content
This repository was archived by the owner on Dec 8, 2021. It is now read-only.

Commit 889ac24

Browse files
authored
cleanup: build install test in regular builds (#932)
The spanner_install_test.cc is a small program to verify our installed libraries work correctly. It was only built by the "install" builds (ci/kokoro/install/*), and this was error prone as we often forgot to make changes to it. With this change the program is built as part of the regular builds, and then the install builds copy it outside the source tree to verify it compiles outside it too.
1 parent 28132e4 commit 889ac24

File tree

9 files changed

+117
-44
lines changed

9 files changed

+117
-44
lines changed

ci/kokoro/docker/build-in-docker-bazel-dependency.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ if [[ -n "${BAZEL_CONFIG}" ]]; then
5252
bazel_args+=(--config "${BAZEL_CONFIG}")
5353
fi
5454

55-
cd ci/test-install
55+
cp -r ci/test-install /var/tmp/test-install
56+
cp google/cloud/spanner/integration_tests/spanner_install_test.cc /var/tmp/test-install
57+
cd /var/tmp/test-install
58+
5659
"${BAZEL_BIN}" build "${bazel_args[@]}" \
5760
-- //...:all
5861

ci/kokoro/install/Dockerfile.fedora

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ RUN cmake --build . --target install
9090
WORKDIR /home/build/test-install-cmake
9191
ENV LD_LIBRARY_PATH=/home/build/google-cloud-spanner-install/lib64
9292
COPY ci/test-install /home/build/test-install-cmake
93+
COPY google/cloud/spanner/integration_tests/spanner_install_test.cc /home/build/test-install-cmake
9394
RUN env -u PKG_CONFIG_PATH cmake \
9495
-DCMAKE_PREFIX_PATH=/home/build/google-cloud-spanner-install \
9596
-H. -B/o

ci/test-install/WORKSPACE

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ workspace(name = "com_github_googleapis_google_cloud_cpp_spanner_test_install")
2626
# sha256 = "...",
2727
# )
2828
#
29-
# But we want to test that the *current* version is correct.
29+
30+
# But we want to test that the *current* version is correct, the CI builds
31+
# always have the source in /v.
3032
local_repository(
3133
name = "com_github_googleapis_google_cloud_cpp_spanner",
32-
path = "../..",
34+
path = "/v",
3335
)
3436

3537
load("@com_github_googleapis_google_cloud_cpp_spanner//bazel:google_cloud_cpp_spanner_deps.bzl", "google_cloud_cpp_spanner_deps")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ~~~
2+
# Copyright 2019 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# ~~~
16+
17+
function (google_cloud_cpp_test_name_to_target var fname)
18+
string(REPLACE "/"
19+
"_"
20+
target
21+
${fname})
22+
string(REPLACE ".cc"
23+
""
24+
target
25+
${target})
26+
set("${var}" "${target}" PARENT_SCOPE)
27+
endfunction ()

google/cloud/spanner/integration_tests/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package(default_visibility = ["//visibility:public"])
1717
licenses(["notice"]) # Apache 2.0
1818

1919
load(":spanner_client_integration_tests.bzl", "spanner_client_integration_tests")
20+
load(":spanner_client_install_tests.bzl", "spanner_client_install_tests")
2021

2122
[cc_test(
2223
name = "spanner_client_" + test.replace("/", "_").replace(".cc", ""),
@@ -32,3 +33,14 @@ load(":spanner_client_integration_tests.bzl", "spanner_client_integration_tests"
3233
"@com_google_googletest//:gtest",
3334
],
3435
) for test in spanner_client_integration_tests]
36+
37+
[cc_test(
38+
name = "spanner_client_" + test.replace("/", "_").replace(".cc", ""),
39+
srcs = [test],
40+
tags = ["integration-tests"],
41+
deps = [
42+
"//google/cloud/spanner:spanner_client",
43+
"//google/cloud/spanner:spanner_client_testing",
44+
"@com_github_googleapis_google_cloud_cpp//google/cloud:google_cloud_cpp_common",
45+
],
46+
) for test in spanner_client_install_tests]

google/cloud/spanner/integration_tests/CMakeLists.txt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
include(EnableClangTidy)
1818
include(EnableWerror)
19+
include(GoogleCloudCppSpannerFunctions)
1920

2021
if (MSVC)
2122
# MSVC warns about using localtime(), this is a valuable warning, so we do
@@ -42,25 +43,21 @@ function (spanner_client_define_integration_tests)
4243
client_stress_test.cc
4344
instance_admin_integration_test.cc
4445
rpc_failure_threshold_integration_test.cc)
46+
set(spanner_client_install_tests spanner_install_test.cc)
4547

4648
# Export the list of unit tests to a .bzl file so we do not need to maintain
4749
# the list in two places.
4850
export_list_to_bazel("spanner_client_integration_tests.bzl"
4951
"spanner_client_integration_tests")
52+
export_list_to_bazel("spanner_client_install_tests.bzl"
53+
"spanner_client_install_tests")
5054

5155
# Create a custom target so we can say "build all the tests"
5256
add_custom_target(spanner-client-integration-tests)
5357

5458
# Generate a target for each unit test.
5559
foreach (fname ${spanner_client_integration_tests})
56-
string(REPLACE "/"
57-
"_"
58-
target
59-
${fname})
60-
string(REPLACE ".cc"
61-
""
62-
target
63-
${target})
60+
google_cloud_cpp_test_name_to_target(target "${fname}")
6461
add_executable(${target} ${fname})
6562
target_link_libraries(${target}
6663
PRIVATE googleapis-c++::spanner_client
@@ -82,6 +79,21 @@ function (spanner_client_define_integration_tests)
8279
set_tests_properties(${target} PROPERTIES LABELS "integration-tests")
8380
add_dependencies(spanner-client-integration-tests ${target})
8481
endforeach ()
82+
83+
if (NOT GOOGLE_CLOUD_CPP_SPANNER_ENABLE_CXX_EXCEPTIONS)
84+
return()
85+
endif ()
86+
87+
foreach (fname ${spanner_client_install_tests})
88+
google_cloud_cpp_test_name_to_target(target "${fname}")
89+
add_executable(${target} ${fname})
90+
target_link_libraries(${target} PRIVATE googleapis-c++::spanner_client)
91+
google_cloud_cpp_add_clang_tidy(${target})
92+
google_cloud_cpp_add_common_options(${target})
93+
94+
add_test(NAME ${target} COMMAND ${target})
95+
set_tests_properties(${target} PROPERTIES LABELS "integration-tests")
96+
endforeach ()
8597
endfunction ()
8698

8799
# Only define the tests if testing is enabled. Package maintainers may not want
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# DO NOT EDIT -- GENERATED BY CMake -- Change the CMakeLists.txt file if needed
16+
17+
"""Automatically generated unit tests list - DO NOT EDIT."""
18+
19+
spanner_client_install_tests = [
20+
"spanner_install_test.cc",
21+
]

ci/test-install/spanner_install_test.cc renamed to google/cloud/spanner/integration_tests/spanner_install_test.cc

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515
#include "google/cloud/spanner/client.h"
1616
#include "google/cloud/spanner/database.h"
1717
#include "google/cloud/spanner/database_admin_client.h"
18+
#include "google/cloud/spanner/instance_admin_client.h"
1819
#include "google/cloud/internal/getenv.h"
1920
#include "google/cloud/internal/random.h"
2021
#include <functional>
2122

2223
namespace spanner = google::cloud::spanner;
2324

24-
std::function<void()> drop_database = []{};
25+
std::function<void()> drop_database = [] {};
2526

2627
int main(int argc, char* argv[]) try {
2728
if (argc != 1) {
2829
std::string const cmd = argv[0];
29-
auto last_slash = std::string(argv[0]).find_last_of("/");
30+
auto last_slash = std::string(argv[0]).find_last_of('/');
3031
std::cerr << "Usage: " << cmd.substr(last_slash + 1) << "\n";
3132
return 1;
3233
}
@@ -38,28 +39,40 @@ int main(int argc, char* argv[]) try {
3839
"The GOOGLE_CLOUD_PROJECT environment variable should be set to a "
3940
"non-empty value");
4041
}
41-
auto instance_id =
42-
google::cloud::internal::GetEnv("GOOGLE_CLOUD_CPP_SPANNER_INSTANCE")
43-
.value_or("");
44-
if (project_id.empty()) {
45-
throw std::runtime_error(
46-
"The GOOGLE_CLOUD_CPP_SPANNER_INSTANCE environment variable should be "
47-
"set to a non-empty value");
48-
}
42+
43+
// This program is used to test the libraries after they are installed. We
44+
// cannot use any of the functions in the testing support libraries as those
45+
// do not get installed.
46+
spanner::DatabaseAdminClient admin_client;
4947

5048
auto generator = google::cloud::internal::MakeDefaultPRNG();
49+
50+
auto instance_id = [&project_id, &generator] {
51+
spanner::InstanceAdminClient instance_admin{
52+
spanner::MakeInstanceAdminConnection()};
53+
std::vector<std::string> names;
54+
for (auto instance : instance_admin.ListInstances(project_id, {})) {
55+
if (!instance) throw std::runtime_error("Error reading instance list");
56+
auto full_name = instance->name();
57+
names.push_back(full_name.substr(full_name.rfind('/') + 1));
58+
}
59+
if (names.empty()) throw std::runtime_error("No instances in the project");
60+
61+
return names[std::uniform_int_distribution<std::size_t>(
62+
0, names.size() - 1)(generator)];
63+
}();
64+
5165
auto database_id =
5266
"db-" + google::cloud::internal::Sample(
5367
generator, 20, "abcdefghijlkmnopqrstuvwxyz0123456789");
5468

55-
namespace spanner = google::cloud::spanner;
5669
spanner::Database const database(project_id, instance_id, database_id);
70+
std::cout << "Will run the test in database: " << database.FullName() << "\n";
5771

5872
using google::cloud::future;
5973
using google::cloud::StatusOr;
6074

6175
std::cout << "Creating database [" << database_id << "] " << std::flush;
62-
spanner::DatabaseAdminClient admin_client;
6376
future<StatusOr<google::spanner::admin::database::v1::Database>>
6477
created_database =
6578
admin_client.CreateDatabase(database, {R"""(
@@ -76,21 +89,14 @@ int main(int argc, char* argv[]) try {
7689
) PRIMARY KEY (SingerId, AlbumId),
7790
INTERLEAVE IN PARENT Singers ON DELETE CASCADE)"""});
7891

79-
int i = 0;
80-
int const timeout = 120;
81-
while (++i < timeout) {
92+
for (;;) {
8293
auto status = created_database.wait_for(std::chrono::seconds(1));
8394
if (status == std::future_status::ready) break;
8495
std::cout << '.' << std::flush;
8596
}
86-
if (i >= timeout) {
87-
std::cout << " TIMEOUT\n";
88-
throw std::runtime_error("Timeout while creating database");
89-
}
9097
std::cout << " DONE\n";
9198

92-
StatusOr<google::spanner::admin::database::v1::Database> db =
93-
created_database.get();
99+
auto db = created_database.get();
94100
if (!db) throw std::runtime_error(db.status().message());
95101

96102
drop_database = [admin_client, database]() mutable {

google/cloud/spanner/samples/CMakeLists.txt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,7 @@
1616

1717
include(EnableClangTidy)
1818
include(EnableWerror)
19-
20-
function (google_cloud_cpp_test_name_to_target var fname)
21-
string(REPLACE "/"
22-
"_"
23-
target
24-
${fname})
25-
string(REPLACE ".cc"
26-
""
27-
target
28-
${target})
29-
set("${var}" "${target}" PARENT_SCOPE)
30-
endfunction ()
19+
include(GoogleCloudCppSpannerFunctions)
3120

3221
function (spanner_client_define_samples)
3322
set(spanner_client_integration_samples samples.cc)

0 commit comments

Comments
 (0)