Skip to content

Commit ee7064f

Browse files
authored
Merge pull request #336 from wprzytula/run-examples-in-ci
Run examples in CI
2 parents 4b24e48 + 3453eb4 commit ee7064f

File tree

25 files changed

+453
-1961
lines changed

25 files changed

+453
-1961
lines changed

.github/workflows/build-lint-and-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ jobs:
3838
- name: Run unit and proxy tests
3939
run: make run-test-unit
4040

41+
- name: Run examples
42+
run: make run-examples-scylla
43+
4144
- name: Build integration test binary
4245
id: build-integration-test-bin
4346
run: make build-integration-test-bin

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ if(CASS_BUILD_INTEGRATION_TESTS OR CASS_BUILD_UNIT_TESTS)
6969
set(CASS_USE_LIBUV ON)
7070
endif()
7171

72+
if(CASS_BUILD_EXAMPLES)
73+
set(CASS_USE_LIBUV ON) # Some examples require libuv, e.g. "callbacks".
74+
endif()
75+
7276
# Determine which driver target should be used as a dependency
7377
set(PROJECT_LIB_NAME_TARGET scylla-cpp-driver)
7478
if(CASS_USE_STATIC_LIBS OR

Makefile

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,42 @@ CASSANDRA_NO_VALGRIND_TEST_FILTER := $(subst ${SPACE},${EMPTY},AsyncTests.Integr
122122
:HeartbeatTests.Integration_Cassandra_HeartbeatFailed)
123123
endif
124124

125+
ifndef SCYLLA_EXAMPLES_URI
126+
# In sync with the docker compose file.
127+
SCYLLA_EXAMPLES_URI := 172.43.0.2
128+
endif
129+
130+
ifndef SCYLLA_EXAMPLES_TO_RUN
131+
SCYLLA_EXAMPLES_TO_RUN := \
132+
async \
133+
basic \
134+
batch \
135+
bind_by_name \
136+
callbacks \
137+
collections \
138+
concurrent_executions \
139+
date_time \
140+
duration \
141+
maps \
142+
named_parameters \
143+
paging \
144+
prepared \
145+
simple \
146+
ssl \
147+
tracing \
148+
tuple \
149+
udt \
150+
uuids \
151+
152+
# auth <- unimplemented `cass_cluster_set_authenticator_callbacks()`
153+
# execution_profiles <- unimplemented `cass_statement_set_keyspace()`
154+
# host_listener <- unimplemented `cass_cluster_set_host_listener_callback()`
155+
# logging <- unimplemented `cass_cluster_set_host_listener_callback()`
156+
# perf <- unimplemented `cass_cluster_set_num_threads_io()`, `cass_cluster_set_queue_size_io()`
157+
# schema_meta <- unimplemented multiple schema-related functions
158+
# cloud <- out of interest for us, not related to ScyllaDB
159+
endif
160+
125161
ifndef CCM_COMMIT_ID
126162
export CCM_COMMIT_ID := master
127163
endif
@@ -216,6 +252,14 @@ build-integration-test-bin-if-missing:
216252
cmake -DCASS_BUILD_INTEGRATION_TESTS=ON -DCMAKE_BUILD_TYPE=Release .. && (make -j 4 || make);\
217253
}
218254

255+
build-examples:
256+
@{\
257+
echo "Building examples to ${EXAMPLES_DIR}";\
258+
mkdir "${BUILD_DIR}" >/dev/null 2>&1 || true;\
259+
cd "${BUILD_DIR}";\
260+
cmake -DCASS_BUILD_INTEGRATION_TESTS=off -DCASS_BUILD_EXAMPLES=on -DCMAKE_BUILD_TYPE=Release .. && (make -j 4 || make);\
261+
}
262+
219263
_update-rust-tooling:
220264
@echo "Run rustup update"
221265
@rustup update
@@ -298,3 +342,28 @@ endif
298342

299343
run-test-unit: install-cargo-if-missing _update-rust-tooling
300344
@cd ${CURRENT_DIR}/scylla-rust-wrapper; RUSTFLAGS="${FULL_RUSTFLAGS}" cargo test
345+
346+
# Currently not used.
347+
CQLSH := cqlsh
348+
349+
run-examples-scylla: build-examples
350+
@sudo sh -c "echo 2097152 >> /proc/sys/fs/aio-max-nr"
351+
@# Keep `SCYLLA_EXAMPLES_URI` in sync with the `scylla` service in `docker-compose.yml`.
352+
@docker compose -f tests/examples_cluster/docker-compose.yml up -d --wait
353+
354+
@# Instead of using cqlsh, which would impose another dependency on the system,
355+
@# we use a special example `drop_examples_keyspace` to drop the `examples` keyspace.
356+
@# CQLSH_HOST=${SCYLLA_EXAMPLES_URI} ${CQLSH} -e "DROP KEYSPACE IF EXISTS EXAMPLES"; \
357+
358+
@echo "Running examples on scylla ${SCYLLA_VERSION}"
359+
@for example in ${SCYLLA_EXAMPLES_TO_RUN} ; do \
360+
echo -e "\nRunning example: $${example}"; \
361+
build/examples/drop_examples_keyspace/drop_examples_keyspace ${SCYLLA_EXAMPLES_URI} || exit 1; \
362+
build/examples/$${example}/$${example} ${SCYLLA_EXAMPLES_URI} || { \
363+
echo "Example \`$${example}\` has failed!"; \
364+
docker compose -f tests/examples_cluster/docker-compose.yml down; \
365+
exit 42; \
366+
}; \
367+
done
368+
369+
@docker compose -f tests/examples_cluster/docker-compose.yml down --remove-orphans
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
drop_examples_keyspace

examples/dse/date_range/CMakeLists.txt renamed to examples/drop_examples_keyspace/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
cmake_minimum_required(VERSION 3.15)
22

33
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ".")
4-
set(PROJECT_EXAMPLE_NAME date_range)
4+
set(PROJECT_EXAMPLE_NAME drop_examples_keyspace)
55

66
file(GLOB EXAMPLE_SRC_FILES *.c)
77
include_directories(${INCLUDES})
88
add_executable(${PROJECT_EXAMPLE_NAME} ${EXAMPLE_SRC_FILES})
9-
target_link_libraries(${PROJECT_EXAMPLE_NAME} ${PROJECT_LIB_NAME_TARGET} ${DSE_LIBS})
9+
target_link_libraries(${PROJECT_EXAMPLE_NAME} ${PROJECT_LIB_NAME_TARGET} ${CASS_LIBS})
1010
add_dependencies(${PROJECT_EXAMPLE_NAME} ${PROJECT_LIB_NAME_TARGET})
1111

1212
set_target_properties(${PROJECT_EXAMPLE_NAME} PROPERTIES FOLDER "Examples"

examples/dse/gssapi/gssapi.c renamed to examples/drop_examples_keyspace/drop_examples_keyspace.c

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,54 +25,77 @@
2525
For more information, please refer to <http://unlicense.org/>
2626
*/
2727

28-
#include <dse.h>
29-
3028
#include <stdio.h>
31-
#include <string.h>
29+
#include <stdlib.h>
3230

33-
int main(int argc, char* argv[]) {
34-
/* Setup and connect to cluster */
35-
CassFuture* connect_future = NULL;
31+
#include "cassandra.h"
32+
33+
void print_error(CassFuture* future) {
34+
const char* message;
35+
size_t message_length;
36+
cass_future_error_message(future, &message, &message_length);
37+
fprintf(stderr, "Error: %.*s\n", (int)message_length, message);
38+
}
39+
40+
CassCluster* create_cluster(const char* hosts) {
3641
CassCluster* cluster = cass_cluster_new();
37-
CassSession* session = cass_session_new();
38-
char* hosts = "127.0.0.1";
39-
if (argc > 1) {
40-
hosts = argv[1];
42+
cass_cluster_set_contact_points(cluster, hosts);
43+
return cluster;
44+
}
45+
46+
CassError connect_session(CassSession* session, const CassCluster* cluster) {
47+
CassError rc = CASS_OK;
48+
CassFuture* future = cass_session_connect(session, cluster);
49+
50+
cass_future_wait(future);
51+
rc = cass_future_error_code(future);
52+
if (rc != CASS_OK) {
53+
print_error(future);
4154
}
55+
cass_future_free(future);
4256

43-
cass_log_set_level(CASS_LOG_INFO);
57+
return rc;
58+
}
4459

45-
/* Add contact points */
46-
cass_cluster_set_contact_points(cluster, hosts);
60+
CassError execute_query(CassSession* session, const char* query) {
61+
CassError rc = CASS_OK;
62+
CassFuture* future = NULL;
63+
CassStatement* statement = cass_statement_new(query, 0);
4764

48-
/* Hostname resolution is typically necessary when authenticating with Kerberos. */
49-
cass_cluster_set_use_hostname_resolution(cluster, cass_true);
65+
future = cass_session_execute(session, statement);
66+
cass_future_wait(future);
5067

51-
cass_cluster_set_dse_gssapi_authenticator(cluster, "dse", "cassandra@DATASTAX.COM");
68+
rc = cass_future_error_code(future);
69+
if (rc != CASS_OK) {
70+
print_error(future);
71+
}
5272

53-
/* Provide the cluster object as configuration to connect the session */
54-
connect_future = cass_session_connect(session, cluster);
73+
cass_future_free(future);
74+
cass_statement_free(statement);
5575

56-
if (cass_future_error_code(connect_future) == CASS_OK) {
57-
CassFuture* close_future = NULL;
76+
return rc;
77+
}
5878

59-
printf("Successfully connected!\n");
79+
int main(int argc, char* argv[]) {
80+
CassCluster* cluster = NULL;
81+
CassSession* session = cass_session_new();
82+
char* hosts = "127.0.0.1";
6083

61-
/* Close the session */
62-
close_future = cass_session_close(session);
63-
cass_future_wait(close_future);
64-
cass_future_free(close_future);
65-
} else {
66-
/* Handle error */
67-
const char* message;
68-
size_t message_length;
69-
cass_future_error_message(connect_future, &message, &message_length);
70-
fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length, message);
84+
if (argc > 1) {
85+
hosts = argv[1];
7186
}
87+
cluster = create_cluster(hosts);
88+
89+
if (connect_session(session, cluster) != CASS_OK) {
90+
cass_cluster_free(cluster);
91+
cass_session_free(session);
92+
return -1;
93+
}
94+
95+
execute_query(session, "DROP KEYSPACE IF EXISTS examples");
7296

73-
cass_future_free(connect_future);
74-
cass_cluster_free(cluster);
7597
cass_session_free(session);
98+
cass_cluster_free(cluster);
7699

77100
return 0;
78101
}

0 commit comments

Comments
 (0)