Skip to content

Commit 2105c8b

Browse files
committed
Add boot test for sagittarius grpc server
1 parent e0a9cec commit 2105c8b

4 files changed

Lines changed: 107 additions & 0 deletions

File tree

.gitlab/ci/container-boot.gitlab-ci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
variables:
1818
SAGITTARIUS_IMAGE: ghcr.io/code0-tech/reticulum/ci-builds/sagittarius:${CI_PIPELINE_ID}-${VARIANT}
1919
CURL_IMAGE: curlimages/curl:8.5.0
20+
DOCKER_ARGS: ""
2021
before_script:
2122
- docker network create boot
2223
- >
@@ -37,6 +38,7 @@
3738
--network boot
3839
--network-alias sagittarius
3940
--volume $(pwd)/container/sagittarius/sagittarius.yml:/sagittarius/config/sagittarius.yml
41+
$DOCKER_ARGS
4042
$SAGITTARIUS_IMAGE
4143
$SAGITTARIUS_CMD &
4244
@@ -58,3 +60,18 @@ container:boot:sagittarius:rails-web:
5860
--retry-delay 3
5961
--retry-connrefused
6062
http://sagittarius:3000/health/liveness
63+
64+
container:boot:sagittarius:grpc:
65+
extends:
66+
- .container:boot:sagittarius
67+
variables:
68+
SAGITTARIUS_CMD: "bin/grpc_server"
69+
DOCKER_ARGS: "--publish 127.0.0.1:50051:50051 --env SAGITTARIUS_PREPARE_DATABASE=true"
70+
before_script:
71+
- apk add libc6-compat
72+
- bundle config set --local force_ruby_platform true
73+
- bundle install
74+
- !reference [.container:boot:sagittarius, before_script]
75+
script:
76+
- scripts/grpc_check_health --host 127.0.0.1:50051 --service liveness --retries 20
77+
- scripts/grpc_check_health --host 127.0.0.1:50051 --service readiness --retries 20

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
source "https://rubygems.org"
44

55
gem "erb"
6+
7+
gem "grpc", "~> 1.73"
8+
9+
gem "optparse", "~> 0.6.0"

Gemfile.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4+
bigdecimal (3.2.2)
45
erb (5.0.1)
6+
google-protobuf (4.31.1)
7+
bigdecimal
8+
rake (>= 13)
9+
googleapis-common-protos-types (1.20.0)
10+
google-protobuf (>= 3.18, < 5.a)
11+
grpc (1.73.0)
12+
google-protobuf (>= 3.25, < 5.0)
13+
googleapis-common-protos-types (~> 1.0)
14+
optparse (0.6.0)
15+
rake (13.3.0)
516

617
PLATFORMS
718
ruby
819

920
DEPENDENCIES
1021
erb
22+
grpc (~> 1.73)
23+
optparse (~> 0.6.0)
1124

1225
BUNDLED WITH
1326
2.6.6

scripts/grpc_check_health

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'grpc'
4+
require 'optparse'
5+
require 'grpc/health/v1/health_services_pb'
6+
7+
options = {
8+
retries: 5,
9+
backoff: 3,
10+
service: '',
11+
}
12+
13+
optparse = OptionParser.new do |opts|
14+
opts.banner = 'Usage: grpc_check_health [options]'
15+
16+
opts.on('-h', '--host [HOST]', String, 'Host of the grpc server to check') do |host|
17+
options[:host] = host
18+
end
19+
20+
opts.on('-r', '--retries [RETRIES]', Integer, "Amount of retries (Default: #{options[:retries]})") do |retries|
21+
options[:retries] = retries
22+
end
23+
24+
opts.on('-b', '--backoff [BACKOFF]', Integer, "Backoff in seconds for each attempt (Default: #{options[:backoff]})") do |backoff|
25+
options[:backoff] = backoff
26+
end
27+
28+
opts.on('-s', '--service [SERVICE]', String, "Service check health for (Default: '#{options[:service]}')") do |service|
29+
options[:service] = service
30+
end
31+
end
32+
optparse.parse!
33+
34+
missing = %i[host].select{ |param| options[param].nil? }
35+
unless missing.empty?
36+
puts "Missing arguments: #{missing.join(', ')}"
37+
puts
38+
puts optparse
39+
exit
40+
end
41+
42+
RetryException = Class.new(StandardError)
43+
44+
attempt = 0
45+
46+
stub = Grpc::Health::V1::Health::Stub.new(options[:host], :this_channel_is_insecure)
47+
begin
48+
message = Grpc::Health::V1::HealthCheckRequest.new(service: options[:service])
49+
50+
response = stub.check(message)
51+
52+
if response.status == :SERVING
53+
puts "Service '#{options[:service]}' on #{options[:host]} is #{response.status}"
54+
else
55+
raise RetryException
56+
end
57+
rescue GRPC::BadStatus, RetryException => e
58+
if e.is_a?(GRPC::BadStatus)
59+
message = e.inspect
60+
else
61+
message = "Service #{options[:service]} on #{options[:host]} is not SERVING (status is #{response&.status})"
62+
end
63+
64+
attempt += 1
65+
if attempt <= options[:retries]
66+
puts message
67+
puts "Retrying in #{options[:backoff]}s"
68+
sleep options[:backoff]
69+
retry
70+
end
71+
72+
abort message
73+
end

0 commit comments

Comments
 (0)