Skip to content

Commit 676a7b5

Browse files
authored
Conformance test for CPP SDK (agones-dev#1282)
Build CPP SDK requirements to a local folder, put this into CI cache, on the subsequent builds it would reduce the time of the build to a minute, building just cpp library and test binary. Contains main logic of the conformance test, which was taken from cpp-simple.
1 parent 91167eb commit 676a7b5

7 files changed

Lines changed: 293 additions & 2 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2020 Google LLC All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -ex
18+
GO111MODULE=off
19+
DIR=/go/src/agones.dev/agones/test/sdk/cpp/
20+
echo "$DIR"/sdk
21+
22+
# Copy all CPP SDK files into a new directory
23+
if [[ ! -d $DIR/sdk ]]
24+
then
25+
mkdir -p "$DIR"/sdk/.build
26+
cp -r /go/src/agones.dev/agones/sdks/cpp/* $DIR/sdk
27+
cd $DIR/sdk/.build
28+
cmake .. -DCMAKE_BUILD_TYPE=Release -DAGONES_SILENT_OUTPUT=OFF -DCMAKE_INSTALL_PREFIX=$DIR/sdk/.build -DAGONES_THIRDPARTY_INSTALL_PATH=$DIR/sdk/.build -G "Unix Makefiles" -Wno-dev
29+
else
30+
echo "Directory with cpp SDK third party dependencies \
31+
has already built - using cached version. \
32+
Use make clean-sdk-conformance-tests if you want to start from scratch"
33+
fi
34+
cd $DIR/sdk/.build
35+
cmake --build . --target install
36+
cd $DIR && mkdir -p .build && cd .build
37+
cmake .. -G "Unix Makefiles" \
38+
-DCMAKE_PREFIX_PATH=$DIR/sdk/.build \
39+
-Dagones_DIR=$DIR/sdk/.build/agones/cmake \
40+
-DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=.bin
41+
cmake --build . --target install
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2020 Google LLC All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -ex
18+
rm -rf /go/src/agones.dev/agones/test/sdk/cpp/.build
19+
rm -rf /go/src/agones.dev/agones/test/sdk/cpp/sdk
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2020 Google LLC All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -ex
18+
/go/src/agones.dev/agones/test/sdk/cpp/.build/.bin/cpp-simple

‎build/includes/sdk.mk‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ run-sdk-conformance-test: ensure-build-sdk-image
146146
$(MAKE) run-sdk-command COMMAND=build-sdk-test
147147
$(MAKE) run-sdk-conformance-no-build
148148

149+
run-sdk-conformance-test-cpp:
150+
$(MAKE) run-sdk-conformance-test SDK_FOLDER=cpp GRPC_PORT=9003 HTTP_PORT=9103
151+
149152
run-sdk-conformance-test-node:
150153
$(MAKE) run-sdk-conformance-test SDK_FOLDER=node GRPC_PORT=9002 HTTP_PORT=9102
151154

@@ -160,7 +163,7 @@ run-sdk-conformance-test-rest:
160163
$(MAKE) run-sdk-command COMMAND=clean SDK_FOLDER=restapi
161164

162165
# Run a conformance test for all SDKs supported
163-
run-sdk-conformance-tests: run-sdk-conformance-test-node run-sdk-conformance-test-go run-sdk-conformance-test-rust run-sdk-conformance-test-rest
166+
run-sdk-conformance-tests: run-sdk-conformance-test-node run-sdk-conformance-test-go run-sdk-conformance-test-rust run-sdk-conformance-test-rest run-sdk-conformance-test-cpp
164167

165168
# Clean package directories and binary files left
166169
# after building conformance tests for all SDKs supported

‎cloudbuild.yaml‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ steps:
3636
- '--key=$_CPP_SDK_BUILD_CACHE_KEY-$( checksum sdks/cpp/cmake/prerequisites.cmake )'
3737
waitFor: ['-']
3838

39+
- name: gcr.io/$PROJECT_ID/restore_cache
40+
id: cpp-sdk-conformance-restore-cache
41+
args:
42+
- '--bucket=gs://$_CACHE_BUCKET'
43+
- '--key=$_CPP_SDK_CONFORMANCE_CACHE_KEY-$( checksum sdks/cpp/cmake/prerequisites.cmake )'
44+
waitFor: ['-']
45+
46+
3947
- name: gcr.io/$PROJECT_ID/restore_cache
4048
id: rust-sdk-build-restore-cache
4149
args:
@@ -238,10 +246,23 @@ steps:
238246
- name: "make-docker"
239247
id: sdk-conformance
240248
dir: "build"
241-
args: [ "-j", "4", "--output-sync=target", "run-sdk-conformance-tests"]
249+
args: [ "-j", "5", "--output-sync=target", "run-sdk-conformance-tests"]
242250
waitFor:
243251
- build-images
244252

253+
#
254+
# Cache the CPP conformance build directory, to speed up subsequent builds (considerably)
255+
#
256+
257+
- name: 'gcr.io/$PROJECT_ID/save_cache'
258+
args:
259+
- '--bucket=gs://$_CACHE_BUCKET'
260+
- '--key=$_CPP_SDK_CONFORMANCE_CACHE_KEY-$( checksum sdks/cpp/cmake/prerequisites.cmake )'
261+
- '--path=test/sdk/cpp/sdk'
262+
id: cpp-sdk-conformance-save-cache
263+
waitFor:
264+
- sdk-conformance
265+
245266
#
246267
# Cache the Rust SDK build directory, to speed up subsequent builds (considerably)
247268
#
@@ -278,6 +299,7 @@ substitutions:
278299
_CACHE_BUCKET: agones-build-cache
279300
_HTMLTEST_CACHE_KEY: htmltest-0.10.1
280301
_CPP_SDK_BUILD_CACHE_KEY: cpp-sdk-build
302+
_CPP_SDK_CONFORMANCE_CACHE_KEY: cpp-sdk-conformance
281303
_RUST_SDK_BUILD_CACHE_KEY: rust-sdk-build
282304

283305
tags: ['ci']

‎test/sdk/cpp/CMakeLists.txt‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2020 Google LLC All Rights Reserved.
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+
cmake_minimum_required (VERSION 3.13.0)
16+
17+
project(cpp-simple CXX)
18+
19+
# Settings
20+
set(CMAKE_CXX_STANDARD 14)
21+
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
22+
set(CMAKE_CXX_EXTENSIONS OFF)
23+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
24+
set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
25+
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
26+
set(CMAKE_DEBUG_POSTFIX "d")
27+
28+
find_package(agones CONFIG REQUIRED)
29+
30+
if(WIN32)
31+
# Windows
32+
add_definitions(-D_WIN32_WINNT=0x0600)
33+
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT cpp-simple)
34+
elseif(APPLE)
35+
# Mac OS
36+
elseif(UNIX AND NOT APPLE)
37+
# Linux
38+
endif()
39+
40+
41+
include_directories(${agones_INCLUDE_DIRS})
42+
set(SRC_FILES "server.cc")
43+
44+
# Executable
45+
add_executable(${PROJECT_NAME} ${SRC_FILES})
46+
target_link_libraries(${PROJECT_NAME} PUBLIC agones)
47+
48+
if (MSVS)
49+
target_compile_options(${PROJECT_NAME} PUBLIC /wd4101 /wd4146 /wd4251 /wd4661)
50+
endif()
51+
52+
# Installation
53+
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME} RUNTIME DESTINATION .)

‎test/sdk/cpp/server.cc‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright 2020 Google LLC All Rights Reserved.
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+
// A server that starts, and then stops after 60 seconds.
16+
// This example really does nothing other than show how to integrate
17+
// the C++ SDK.
18+
19+
#include <agones/sdk.h>
20+
#include <grpc++/grpc++.h>
21+
#include <iostream>
22+
#include <thread>
23+
24+
// send health check pings
25+
void DoHealth(std::shared_ptr<agones::SDK> sdk) {
26+
bool ok = sdk->Health();
27+
std::cout << "Health ping " << (ok ? "sent" : "failed") << "\n" << std::flush;
28+
}
29+
30+
// watch GameServer Updates
31+
void WatchUpdates(std::shared_ptr<agones::SDK> sdk) {
32+
std::cout << "Starting to watch GameServer updates...\n" << std::flush;
33+
sdk->WatchGameServer([](const agones::dev::sdk::GameServer& gameserver) {
34+
std::cout << "GameServer Update:\n" //
35+
<< "\tname: " << gameserver.object_meta().name() << "\n" //
36+
<< "\tstate: " << gameserver.status().state() << "\n"
37+
<< std::flush;
38+
});
39+
}
40+
41+
int main() {
42+
std::cout << "C++ Game Server has started!\n"
43+
<< "Getting the instance of the SDK.\n" << std::flush;
44+
auto sdk = std::make_shared<agones::SDK>();
45+
46+
std::cout << "Attempting to connect...\n" << std::flush;
47+
if (!sdk->Connect()) {
48+
std::cerr << "Exiting!\n";
49+
return -1;
50+
}
51+
std::cout << "...handshake complete.\n" << std::flush;
52+
53+
DoHealth(sdk);
54+
std::thread watch(WatchUpdates, sdk);
55+
56+
std::cout << "Marking server as ready...\n" << std::flush;
57+
grpc::Status status = sdk->Ready();
58+
if (!status.ok()) {
59+
std::cerr << "Could not run Ready(): " << status.error_message()
60+
<< ". Exiting!\n";
61+
return -1;
62+
}
63+
std::cout << "...marked Ready\n" << std::flush;
64+
65+
status = sdk->Allocate();
66+
if (!status.ok()) {
67+
std::cerr << "Could not run Allocate(): " << status.error_message()
68+
<< ". Exiting!\n";
69+
return -1;
70+
}
71+
std::cout << "...marked Allocated\n" << std::flush;
72+
73+
std::chrono::seconds sec(1);
74+
status = sdk->Reserve(sec);
75+
if (!status.ok()) {
76+
std::cerr << "Could not run Reserve(): " << status.error_message()
77+
<< ". Exiting!\n";
78+
return -1;
79+
}
80+
std::cout << "...marked Reserved\n" << std::flush;
81+
82+
std::cout << "Getting GameServer details...\n" << std::flush;
83+
agones::dev::sdk::GameServer gameserver;
84+
status = sdk->GameServer(&gameserver);
85+
86+
if (!status.ok()) {
87+
std::cerr << "Could not run GameServer(): " << status.error_message()
88+
<< ". Exiting!\n";
89+
return -1;
90+
}
91+
92+
std::cout << "GameServer name: " << gameserver.object_meta().name() << "\n"
93+
<< std::flush;
94+
95+
std::cout << "Setting a label\n" << std::flush;
96+
status = sdk->SetLabel(
97+
"test-label",
98+
std::to_string(gameserver.object_meta().creation_timestamp()));
99+
if (!status.ok()) {
100+
std::cerr << "Could not run SetLabel(): " << status.error_message()
101+
<< ". Exiting!\n";
102+
return -1;
103+
}
104+
105+
std::cout << "Setting an annotation\n" << std::flush;
106+
status =
107+
sdk->SetAnnotation("test-annotation", gameserver.object_meta().uid());
108+
if (!status.ok()) {
109+
std::cerr << "Could not run SetAnnotation(): " << status.error_message()
110+
<< ". Exiting!\n";
111+
return -1;
112+
}
113+
114+
for (int i = 0; i < 2; i++) {
115+
int time = i * 10;
116+
std::cout << "Running for " + std::to_string(time) + " seconds !\n"
117+
<< std::flush;
118+
119+
std::this_thread::sleep_for(std::chrono::seconds(10));
120+
121+
if (i == 1) {
122+
std::cout << "Shutting down after 10 seconds...\n" << std::flush;
123+
grpc::Status status = sdk->Shutdown();
124+
if (!status.ok()) {
125+
std::cerr << "Could not run Shutdown():" << status.error_message()
126+
<< ". Exiting!\n";
127+
return -1;
128+
}
129+
std::cout << "...marked for Shutdown\n" << std::flush;
130+
}
131+
}
132+
watch.join();
133+
134+
return 0;
135+
}

0 commit comments

Comments
 (0)