Skip to content

Commit bec60b7

Browse files
authored
Make CI tests pass and update README.md (#1)
1 parent fca600c commit bec60b7

File tree

9 files changed

+121
-76
lines changed

9 files changed

+121
-76
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ jobs:
1717
fail-fast: false
1818
matrix:
1919
include:
20-
- os: ubuntu-20.04
20+
- os: ubuntu-24.04
2121
make: test-debug
2222
info: g++-9 + test-debug
2323

24-
- os: ubuntu-20.04
24+
- os: ubuntu-24.04
2525
make: test-release
2626
info: g++-9 + test-release
2727

2828
name: '${{matrix.os}}: ${{matrix.info}}'
2929
runs-on: ${{matrix.os}}
3030

3131
steps:
32-
- uses: actions/checkout@v2
32+
- uses: actions/checkout@v4
3333
with:
3434
submodules: true
3535

3636
- name: Reuse ccache directory
37-
uses: actions/cache@v2
37+
uses: actions/cache@v4
3838
with:
3939
path: ~/.ccache
4040
key: '${{matrix.os}} ${{matrix.info}} ccache-dir ${{github.ref}} run-${{github.run_number}}'
@@ -46,6 +46,7 @@ jobs:
4646
run: |
4747
(cd third_party && git clone -b develop --single-branch --depth 1 https://github.com/userver-framework/userver.git)
4848
sudo apt update
49+
sudo apt purge -y libpq5 libpq-dev postgresql-*
4950
sudo apt install --allow-downgrades -y postgresql $(cat third_party/userver/scripts/docs/en/deps/${{matrix.os}}.md | tr '\n' ' ')
5051
python3 -m pip install -r requirements.txt
5152
@@ -63,6 +64,8 @@ jobs:
6364
run: |
6465
make dist-clean
6566
make install PREFIX=`pwd`/local_installation/
67+
sed -i "s|/var/www/upastebin|`pwd`/local_installation/var/www/upastebin|g" \
68+
`pwd`/local_installation/etc/upastebin/static_config.yaml
6669
6770
- name: Test run after install
6871
if: matrix.make == 'test-release'

.github/workflows/docker.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,19 @@ jobs:
1414
tests:
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: actions/checkout@v2
17+
- uses: actions/checkout@v4
1818
with:
1919
submodules: true
2020

2121
- name: Reuse ccache directory
22-
uses: actions/cache@v2
22+
uses: actions/cache@v4
2323
with:
2424
path: .ccache
2525
key: 'ccache-dir-${{github.ref}}_run-${{github.run_number}}'
2626
restore-keys: |
2727
ccache-dir-${{github.ref}}_run-
2828
ccache-
2929
30-
- name: Setup ccache
31-
run: docker-compose run --rm upastebin-container bash -c 'ccache -M 2.0GB && ccache -s'
32-
3330
- name: Cmake
3431
run: make docker-cmake-release
3532

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ if(NOT userver_FOUND) # Fallback to subdirectory usage
1010

1111
# Compatibility mode: some systems don't support these features
1212
set(USERVER_FEATURE_CRYPTOPP_BLAKE2 OFF CACHE BOOL "" FORCE)
13-
set(USERVER_FEATURE_GRPC_CHANNELZ OFF CACHE BOOL "" FORCE)
14-
set(USERVER_FEATURE_REDIS_HI_MALLOC ON CACHE BOOL "" FORCE)
1513

1614
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/third_party/userver)
1715
message(STATUS "Using userver framework from third_party/userver")
@@ -79,3 +77,4 @@ file(GLOB CONFIGS_FILES ${CMAKE_CURRENT_SOURCE_DIR}/configs/*.yaml ${CMAKE_CURRE
7977

8078
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT ${PROJECT_NAME})
8179
install(FILES ${CONFIGS_FILES} DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/${PROJECT_NAME} COMPONENT ${PROJECT_NAME})
80+
install(DIRECTORY www-data/ DESTINATION ${CMAKE_INSTALL_LOCALSTATEDIR}/www/${PROJECT_NAME} COMPONENT ${PROJECT_NAME})

CMakePresets.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"version": 2,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 20,
6+
"patch": 0
7+
},
8+
"configurePresets": [
9+
{
10+
"name": "debug",
11+
"displayName": "Debug",
12+
"description": "Fully featured Debug build",
13+
"inherits": [
14+
"common-flags"
15+
],
16+
"binaryDir": "${sourceDir}/build-debug",
17+
"cacheVariables": {
18+
"CMAKE_BUILD_TYPE": "Debug",
19+
"USERVER_SANITIZE": "addr;ub"
20+
}
21+
},
22+
{
23+
"name": "release",
24+
"displayName": "Release",
25+
"description": "Fully featured Release build",
26+
"inherits": [
27+
"common-flags"
28+
],
29+
"binaryDir": "${sourceDir}/build-release",
30+
"cacheVariables": {
31+
"CMAKE_BUILD_TYPE": "Release"
32+
}
33+
},
34+
{
35+
"name": "common-flags",
36+
"hidden": true,
37+
"generator": "Ninja",
38+
"cacheVariables": {
39+
"USERVER_FEATURE_GRPC": "ON", // grpc template current
40+
"USERVER_FEATURE_POSTGRESQL": "ON", // postgresql template current
41+
"USERVER_FEATURE_MONGODB": "ON", // mongo template current
42+
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON"
43+
}
44+
}
45+
]
46+
}

Makefile

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,57 @@
1-
CMAKE_COMMON_FLAGS ?= -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
2-
CMAKE_DEBUG_FLAGS ?= -DUSERVER_SANITIZE=''
3-
CMAKE_RELEASE_FLAGS ?=
1+
PROJECT_NAME = upastebin
42
NPROCS ?= $(shell nproc)
53
CLANG_FORMAT ?= clang-format
6-
DOCKER_COMPOSE ?= docker-compose
7-
8-
# NOTE: use Makefile.local to override the options defined above.
9-
-include Makefile.local
10-
11-
CMAKE_DEBUG_FLAGS += -DCMAKE_BUILD_TYPE=Debug $(CMAKE_COMMON_FLAGS)
12-
CMAKE_RELEASE_FLAGS += -DCMAKE_BUILD_TYPE=Release $(CMAKE_COMMON_FLAGS)
4+
DOCKER_IMAGE ?= ghcr.io/userver-framework/ubuntu-24.04-userver:latest
5+
CMAKE_OPTS ?=
6+
# If we're under TTY, pass "-it" to "docker run"
7+
DOCKER_ARGS = $(shell /bin/test -t 0 && /bin/echo -it || echo)
8+
PRESETS ?= debug release debug-custom release-custom
139

1410
.PHONY: all
1511
all: test-debug test-release
1612

1713
# Run cmake
18-
.PHONY: cmake-debug
19-
cmake-debug:
20-
cmake -B build_debug $(CMAKE_DEBUG_FLAGS)
14+
.PHONY: $(addprefix cmake-, $(PRESETS))
15+
$(addprefix cmake-, $(PRESETS)): cmake-%:
16+
cmake --preset $* $(CMAKE_OPTS)
2117

22-
.PHONY: cmake-release
23-
cmake-release:
24-
cmake -B build_release $(CMAKE_RELEASE_FLAGS)
25-
26-
build_debug/CMakeCache.txt: cmake-debug
27-
build_release/CMakeCache.txt: cmake-release
18+
$(addsuffix /CMakeCache.txt, $(addprefix build-, $(PRESETS))): build-%/CMakeCache.txt:
19+
$(MAKE) cmake-$*
2820

2921
# Build using cmake
30-
.PHONY: build-debug build-release
31-
build-debug build-release: build-%: build_%/CMakeCache.txt
32-
cmake --build build_$* -j $(NPROCS) --target upastebin
22+
.PHONY: $(addprefix build-, $(PRESETS))
23+
$(addprefix build-, $(PRESETS)): build-%: build-%/CMakeCache.txt
24+
cmake --build build-$* -j $(NPROCS) --target $(PROJECT_NAME)
3325

3426
# Test
35-
.PHONY: test-debug test-release
36-
test-debug test-release: test-%: build-%
37-
cmake --build build_$* -j $(NPROCS) --target runtests-upastebin
38-
cd build_$* && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V
39-
pycodestyle tests
27+
.PHONY: $(addprefix test-, $(PRESETS))
28+
$(addprefix test-, $(PRESETS)): test-%: build-%/CMakeCache.txt
29+
cmake --build build-$* -j $(NPROCS)
30+
cd build-$* && ((test -t 1 && GTEST_COLOR=1 PYTEST_ADDOPTS="--color=yes" ctest -V) || ctest -V)
4031

4132
# Start the service (via testsuite service runner)
42-
.PHONY: service-start-debug service-start-release
43-
service-start-debug service-start-release: service-start-%: build-%
44-
cmake --build build_$* -v --target start-upastebin
33+
.PHONY: $(addprefix start-, $(PRESETS))
34+
$(addprefix start-, $(PRESETS)): start-%:
35+
cmake --build build-$* -v --target start-$(PROJECT_NAME)
4536

4637
# Cleanup data
47-
.PHONY: clean-debug clean-release
48-
clean-debug clean-release: clean-%:
49-
cmake --build build_$* --target clean
38+
.PHONY: $(addprefix clean-, $(PRESETS))
39+
$(addprefix clean-, $(PRESETS)): clean-%:
40+
cmake --build build-$* --target clean
5041

5142
.PHONY: dist-clean
5243
dist-clean:
53-
rm -rf build_*
44+
rm -rf build*
5445
rm -rf tests/__pycache__/
5546
rm -rf tests/.pytest_cache/
47+
rm -rf .ccache
48+
rm -rf .vscode/.cache
49+
rm -rf .vscode/compile_commands.json
5650

5751
# Install
58-
.PHONY: install-debug install-release
59-
install-debug install-release: install-%: build-%
60-
cmake --install build_$* -v --component upastebin
52+
.PHONY: $(addprefix install-, $(PRESETS))
53+
$(addprefix install-, $(PRESETS)): install-%: build-%
54+
cmake --install build-$* -v --component $(PROJECT_NAME)
6155

6256
.PHONY: install
6357
install: install-release
@@ -68,28 +62,17 @@ format:
6862
find src -name '*pp' -type f | xargs $(CLANG_FORMAT) -i
6963
find tests -name '*.py' -type f | xargs autopep8 -i
7064

71-
# Set environment for --in-docker-start
72-
export DB_CONNECTION := postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@service-postgres:5432/${POSTGRES_DB}
73-
74-
# Internal hidden targets that are used only in docker environment
75-
--in-docker-start-debug --in-docker-start-release: --in-docker-start-%: install-%
76-
psql ${DB_CONNECTION} -f ./postgresql/data/initial_data.sql
77-
/home/user/.local/bin/upastebin \
78-
--config /home/user/.local/etc/upastebin/static_config.yaml \
79-
--config_vars /home/user/.local/etc/upastebin/config_vars.docker.yaml
80-
81-
# Build and run service in docker environment
82-
.PHONY: docker-start-service-debug docker-start-service-release
83-
docker-start-service-debug docker-start-service-release: docker-start-service-%:
84-
$(DOCKER_COMPOSE) run -p 8080:8080 --rm upastebin-container make -- --in-docker-start-$*
85-
86-
# Start targets makefile in docker environment
87-
.PHONY: docker-cmake-debug docker-build-debug docker-test-debug docker-clean-debug docker-install-debug docker-cmake-release docker-build-release docker-test-release docker-clean-release docker-install-release
88-
docker-cmake-debug docker-build-debug docker-test-debug docker-clean-debug docker-install-debug docker-cmake-release docker-build-release docker-test-release docker-clean-release docker-install-release: docker-%:
89-
$(DOCKER_COMPOSE) run --rm upastebin-container make $*
90-
91-
# Stop docker container and remove PG data
92-
.PHONY: docker-clean-data
93-
docker-clean-data:
94-
$(DOCKER_COMPOSE) down -v
95-
rm -rf ./.pgdata
65+
# Start targets makefile in docker wrapper.
66+
# The docker mounts the whole service's source directory,
67+
# so you can do some stuff as you wish, switch back to host (non-docker) system
68+
# and still able to access the results.
69+
.PHONY: $(addprefix docker-cmake-, $(PRESETS)) $(addprefix docker-build-, $(PRESETS)) $(addprefix docker-test-, $(PRESETS)) $(addprefix docker-clean-, $(PRESETS))
70+
$(addprefix docker-cmake-, $(PRESETS)) $(addprefix docker-build-, $(PRESETS)) $(addprefix docker-test-, $(PRESETS)) $(addprefix docker-clean-, $(PRESETS)): docker-%:
71+
docker run $(DOCKER_ARGS) \
72+
--network=host \
73+
-v $$PWD:$$PWD \
74+
-w $$PWD \
75+
$(DOCKER_IMAGE) \
76+
env CCACHE_DIR=$$PWD/.ccache \
77+
HOME=$$HOME \
78+
$$PWD/run_as_user.sh $(shell /bin/id -u) $(shell /bin/id -g) make $*

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# upastebin
22

3-
Template of a C++ service that uses [userver framework](https://github.com/userver-framework/userver) with PostgreSQL.
3+
[![upastebin CI](https://github.com/userver-framework/upastebin/actions/workflows/ci.yml/badge.svg?branch=develop)](https://github.com/userver-framework/upastebin/actions/workflows/ci.yml)
4+
[![upastebin Docker](https://github.com/userver-framework/upastebin/actions/workflows/docker.yaml/badge.svg?branch=develop)](https://github.com/userver-framework/upastebin/actions/workflows/docker.ymal)
5+
6+
A C++ service that uses [userver framework](https://github.com/userver-framework/userver) with PostgreSQL. See [the article (ru)](https://habr.com/ru/companies/yandex/articles/894294/) for in depth description.
47

58

69
## Download and Build

configs/static_config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ components_manager:
3434
http-client:
3535
fs-task-processor: fs-task-processor
3636
resources-cache:
37-
dir: /home/segoon/projects/upastebin/www-data
37+
dir: /var/www/upastebin
3838
update-period: 1s
3939
fs-task-processor: fs-task-processor
4040

run_as_user.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
3+
# Exit on any error and treat unset variables as errors
4+
set -euo
5+
6+
OLD_UID=$1
7+
OLD_GID=$2
8+
shift; shift
9+
10+
groupadd --gid $OLD_GID --non-unique user
11+
useradd --uid $OLD_UID --gid $OLD_GID --non-unique user
12+
13+
sudo -E -u user "$@"

tests/test_crud.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ async def test_create_and_retrieve(service_client):
3232
response = await service_client.get('/api/v1/latest')
3333
assert response.status == 200
3434
json = response.json()
35+
assert json['items'][0]['ip']
36+
del json['items'][0]['ip']
3537
assert json == {
3638
'items': [
3739
{
3840
'author': 'foo',
39-
'ip': '::ffff:127.0.0.1',
4041
'id': uuid,
4142
'text': TEXT,
4243
}

0 commit comments

Comments
 (0)