Skip to content

Run examples in CI #336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build-lint-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
- name: Run unit and proxy tests
run: make run-test-unit

- name: Run examples
run: make run-examples-scylla

- name: Build integration test binary
id: build-integration-test-bin
run: make build-integration-test-bin
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ if(CASS_BUILD_INTEGRATION_TESTS OR CASS_BUILD_UNIT_TESTS)
set(CASS_USE_LIBUV ON)
endif()

if(CASS_BUILD_EXAMPLES)
set(CASS_USE_LIBUV ON) # Some examples require libuv, e.g. "callbacks".
endif()

# Determine which driver target should be used as a dependency
set(PROJECT_LIB_NAME_TARGET scylla-cpp-driver)
if(CASS_USE_STATIC_LIBS OR
Expand Down
69 changes: 69 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,42 @@ CASSANDRA_NO_VALGRIND_TEST_FILTER := $(subst ${SPACE},${EMPTY},AsyncTests.Integr
:HeartbeatTests.Integration_Cassandra_HeartbeatFailed)
endif

ifndef SCYLLA_EXAMPLES_URI
# In sync with the docker compose file.
SCYLLA_EXAMPLES_URI := 172.43.0.2
endif

ifndef SCYLLA_EXAMPLES_TO_RUN
SCYLLA_EXAMPLES_TO_RUN := \
async \
basic \
batch \
bind_by_name \
callbacks \
collections \
concurrent_executions \
date_time \
duration \
maps \
named_parameters \
paging \
prepared \
simple \
ssl \
tracing \
tuple \
udt \
uuids \

# auth <- unimplemented `cass_cluster_set_authenticator_callbacks()`
# execution_profiles <- unimplemented `cass_statement_set_keyspace()`
# host_listener <- unimplemented `cass_cluster_set_host_listener_callback()`
# logging <- unimplemented `cass_cluster_set_host_listener_callback()`
# perf <- unimplemented `cass_cluster_set_num_threads_io()`, `cass_cluster_set_queue_size_io()`
# schema_meta <- unimplemented multiple schema-related functions
# cloud <- out of interest for us, not related to ScyllaDB
endif

ifndef CCM_COMMIT_ID
export CCM_COMMIT_ID := master
endif
Expand Down Expand Up @@ -216,6 +252,14 @@ build-integration-test-bin-if-missing:
cmake -DCASS_BUILD_INTEGRATION_TESTS=ON -DCMAKE_BUILD_TYPE=Release .. && (make -j 4 || make);\
}

build-examples:
@{\
echo "Building examples to ${EXAMPLES_DIR}";\
mkdir "${BUILD_DIR}" >/dev/null 2>&1 || true;\
cd "${BUILD_DIR}";\
cmake -DCASS_BUILD_INTEGRATION_TESTS=off -DCASS_BUILD_EXAMPLES=on -DCMAKE_BUILD_TYPE=Release .. && (make -j 4 || make);\
}

_update-rust-tooling:
@echo "Run rustup update"
@rustup update
Expand Down Expand Up @@ -298,3 +342,28 @@ endif

run-test-unit: install-cargo-if-missing _update-rust-tooling
@cd ${CURRENT_DIR}/scylla-rust-wrapper; RUSTFLAGS="${FULL_RUSTFLAGS}" cargo test

# Currently not used.
CQLSH := cqlsh

run-examples-scylla: build-examples
@sudo sh -c "echo 2097152 >> /proc/sys/fs/aio-max-nr"
@# Keep `SCYLLA_EXAMPLES_URI` in sync with the `scylla` service in `docker-compose.yml`.
@docker compose -f tests/examples_cluster/docker-compose.yml up -d --wait

@# Instead of using cqlsh, which would impose another dependency on the system,
@# we use a special example `drop_examples_keyspace` to drop the `examples` keyspace.
@# CQLSH_HOST=${SCYLLA_EXAMPLES_URI} ${CQLSH} -e "DROP KEYSPACE IF EXISTS EXAMPLES"; \

@echo "Running examples on scylla ${SCYLLA_VERSION}"
@for example in ${SCYLLA_EXAMPLES_TO_RUN} ; do \
echo -e "\nRunning example: $${example}"; \
build/examples/drop_examples_keyspace/drop_examples_keyspace ${SCYLLA_EXAMPLES_URI} || exit 1; \
build/examples/$${example}/$${example} ${SCYLLA_EXAMPLES_URI} || { \
echo "Example \`$${example}\` has failed!"; \
docker compose -f tests/examples_cluster/docker-compose.yml down; \
exit 42; \
}; \
done

@docker compose -f tests/examples_cluster/docker-compose.yml down --remove-orphans
1 change: 1 addition & 0 deletions examples/drop_examples_keyspace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop_examples_keyspace
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
cmake_minimum_required(VERSION 3.15)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ".")
set(PROJECT_EXAMPLE_NAME date_range)
set(PROJECT_EXAMPLE_NAME drop_examples_keyspace)

file(GLOB EXAMPLE_SRC_FILES *.c)
include_directories(${INCLUDES})
add_executable(${PROJECT_EXAMPLE_NAME} ${EXAMPLE_SRC_FILES})
target_link_libraries(${PROJECT_EXAMPLE_NAME} ${PROJECT_LIB_NAME_TARGET} ${DSE_LIBS})
target_link_libraries(${PROJECT_EXAMPLE_NAME} ${PROJECT_LIB_NAME_TARGET} ${CASS_LIBS})
add_dependencies(${PROJECT_EXAMPLE_NAME} ${PROJECT_LIB_NAME_TARGET})

set_target_properties(${PROJECT_EXAMPLE_NAME} PROPERTIES FOLDER "Examples"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,54 +25,77 @@
For more information, please refer to <http://unlicense.org/>
*/

#include <dse.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
/* Setup and connect to cluster */
CassFuture* connect_future = NULL;
#include "cassandra.h"

void print_error(CassFuture* future) {
const char* message;
size_t message_length;
cass_future_error_message(future, &message, &message_length);
fprintf(stderr, "Error: %.*s\n", (int)message_length, message);
}

CassCluster* create_cluster(const char* hosts) {
CassCluster* cluster = cass_cluster_new();
CassSession* session = cass_session_new();
char* hosts = "127.0.0.1";
if (argc > 1) {
hosts = argv[1];
cass_cluster_set_contact_points(cluster, hosts);
return cluster;
}

CassError connect_session(CassSession* session, const CassCluster* cluster) {
CassError rc = CASS_OK;
CassFuture* future = cass_session_connect(session, cluster);

cass_future_wait(future);
rc = cass_future_error_code(future);
if (rc != CASS_OK) {
print_error(future);
}
cass_future_free(future);

cass_log_set_level(CASS_LOG_INFO);
return rc;
}

/* Add contact points */
cass_cluster_set_contact_points(cluster, hosts);
CassError execute_query(CassSession* session, const char* query) {
CassError rc = CASS_OK;
CassFuture* future = NULL;
CassStatement* statement = cass_statement_new(query, 0);

/* Hostname resolution is typically necessary when authenticating with Kerberos. */
cass_cluster_set_use_hostname_resolution(cluster, cass_true);
future = cass_session_execute(session, statement);
cass_future_wait(future);

cass_cluster_set_dse_gssapi_authenticator(cluster, "dse", "[email protected]");
rc = cass_future_error_code(future);
if (rc != CASS_OK) {
print_error(future);
}

/* Provide the cluster object as configuration to connect the session */
connect_future = cass_session_connect(session, cluster);
cass_future_free(future);
cass_statement_free(statement);

if (cass_future_error_code(connect_future) == CASS_OK) {
CassFuture* close_future = NULL;
return rc;
}

printf("Successfully connected!\n");
int main(int argc, char* argv[]) {
CassCluster* cluster = NULL;
CassSession* session = cass_session_new();
char* hosts = "127.0.0.1";

/* Close the session */
close_future = cass_session_close(session);
cass_future_wait(close_future);
cass_future_free(close_future);
} else {
/* Handle error */
const char* message;
size_t message_length;
cass_future_error_message(connect_future, &message, &message_length);
fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length, message);
if (argc > 1) {
hosts = argv[1];
}
cluster = create_cluster(hosts);

if (connect_session(session, cluster) != CASS_OK) {
cass_cluster_free(cluster);
cass_session_free(session);
return -1;
}

execute_query(session, "DROP KEYSPACE IF EXISTS examples");

cass_future_free(connect_future);
cass_cluster_free(cluster);
cass_session_free(session);
cass_cluster_free(cluster);

return 0;
}
Loading