Skip to content

Commit af7f072

Browse files
committed
Do not send HTTP request when mock mode is on
1 parent 23448b5 commit af7f072

4 files changed

Lines changed: 61 additions & 23 deletions

File tree

.github/workflows/ci_ruby.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,19 @@ jobs:
3131
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
3232
- name: Run unit tests
3333
run: bundle exec rake test
34+
typecheck_and_lint:
35+
runs-on: ubuntu-latest
36+
env:
37+
CI: true
38+
steps:
39+
- name: Checkout code
40+
uses: actions/checkout@v3
41+
- name: Install Ruby and gems
42+
uses: ruby/setup-ruby@v1
43+
with:
44+
ruby-version: '3.4'
45+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
46+
- name: Lint
47+
run: bundle exec rubocop --parallel --color
48+
- name: Typecheck
49+
run: bundle exec srb tc

lib/data_plane_api/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# typed: true
22
# frozen_string_literal: true
33

4+
require 'booleans/kernel_extension'
45
require 'faraday'
56
require 'logger'
67

lib/data_plane_api/server.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class << self
1414
# @param name: Name of the server whose settings will be returned.
1515
# If `nil` then an array of settings of all servers under the passed `backend`
1616
# will be returned.
17-
#: (String?, String?, Configuration?) -> Faraday::Response
17+
#: (String?, String?, Configuration?) -> Faraday::Response?
1818
def get_runtime_settings(backend:, name: nil, config: nil)
1919
config ||= CONFIG
2020
if backend.nil? || backend.empty?
@@ -30,7 +30,7 @@ def get_runtime_settings(backend:, name: nil, config: nil)
3030

3131
# @param backend: Name of the backend
3232
# @param name: Name of the server whose transient settings should be updated.
33-
#: (String?, String?, Hash[top, top], Configuration?) -> Faraday::Response
33+
#: (String?, String?, Hash[top, top], Configuration?) -> Faraday::Response?
3434
def update_transient_settings(backend:, name:, settings:, config: nil)
3535
config ||= CONFIG
3636
if backend.nil? || backend.empty?
@@ -48,37 +48,44 @@ def update_transient_settings(backend:, name:, settings:, config: nil)
4848

4949
private
5050

51-
#: (Symbol, String | Pathname, Configuration) { (Faraday::Request) -> void } -> Faraday::Response
51+
#: (Symbol, String | Pathname, Configuration) { (Faraday::Request) -> void } -> Faraday::Response?
5252
def send_request(method:, path:, config:, &block)
5353
request = T.let(nil, T.nilable(Faraday::Request))
54-
response = config.connection.public_send(method, path) do |req|
54+
conn = config.connection
55+
response = conn.public_send(method, path) do |req|
5556
block.call(req)
5657
req.options.timeout = config.timeout
5758
request = req
59+
break if config.mock?
5860
end
5961

60-
log_communication(T.must(request), response, logger: config.logger)
62+
log_communication(
63+
conn.build_url(path),
64+
T.must(request),
65+
response,
66+
logger: config.logger,
67+
)
6168

6269
response
6370
end
6471

65-
#: (Faraday::Request, Faraday::Response, Logger?) -> void
66-
def log_communication(request, response, logger:)
72+
#: (String, Faraday::Request, Faraday::Response?, Logger?) -> void
73+
def log_communication(url, request, response, logger:)
6774
request_hash = {
6875
method: request.http_method,
69-
url: response.env.url,
76+
url: url,
7077
params: request.params,
7178
headers: request.headers,
7279
body: request.body,
7380
}
7481
response_hash = {
75-
status: response.status,
76-
body: response.body,
77-
headers: response.headers,
82+
status: response&.status,
83+
body: response&.body,
84+
headers: response&.headers,
7885
}
7986

8087
logger&.debug <<~REQ
81-
HAProxy #{request.http_method.to_s.upcase} #{response.env.url}
88+
HAProxy #{request.http_method.to_s.upcase} #{url}
8289
-----REQUEST-----
8390
#{::JSON.pretty_generate request_hash}
8491
-----RESPONSE-----

test/data_plane_api/server_test.rb

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,32 @@
55

66
module DataPlaneApi
77
class ServerTest < ::TestCase
8+
should 'not send the request in mock mode' do
9+
conf = config.tap do |c|
10+
c.mock = true
11+
end
12+
13+
response =
14+
Server.get_runtime_settings(backend: 'lolo', config: conf)
15+
16+
assert_nil response
17+
end
18+
819
should 'get runtime settings of all servers when backend does not exist' do
9-
response = http_cassette do
20+
response = T.must(http_cassette do
1021
Server.get_runtime_settings(backend: 'lolo', config: config)
11-
end
22+
end)
23+
1224

1325
assert_equal 200, response.status
1426
assert response.body.is_a?(::Array)
1527
assert_equal 0, response.body.length
1628
end
1729

1830
should 'get runtime settings of all servers' do
19-
response = http_cassette do
31+
response = T.must(http_cassette do
2032
Server.get_runtime_settings(backend: 'foo_bar', config: config)
21-
end
33+
end)
2234

2335
assert_equal 200, response.status
2436
assert response.body.is_a?(::Array)
@@ -40,9 +52,9 @@ class ServerTest < ::TestCase
4052
end
4153

4254
should 'get runtime settings of one server' do
43-
response = http_cassette do
55+
response = T.must(http_cassette do
4456
Server.get_runtime_settings(backend: 'foo_bar', name: 'foo_bar1', config: config)
45-
end
57+
end)
4658

4759
assert_equal 200, response.status
4860
assert response.body.is_a?(::Hash)
@@ -56,9 +68,9 @@ class ServerTest < ::TestCase
5668
end
5769

5870
should 'get runtime settings of one server which does not exist' do
59-
response = http_cassette do
71+
response = T.must(http_cassette do
6072
Server.get_runtime_settings(backend: 'foo_bar', name: 'lolo', config: config)
61-
end
73+
end)
6274

6375
assert_equal 500, response.status
6476
assert response.body.is_a?(::Hash)
@@ -70,7 +82,7 @@ class ServerTest < ::TestCase
7082

7183
should 'update admin_state of a server' do
7284
http_cassette do
73-
response = Server.get_runtime_settings(backend: 'foo_bar', name: 'foo_bar1', config: config)
85+
response = T.must Server.get_runtime_settings(backend: 'foo_bar', name: 'foo_bar1', config: config)
7486

7587
assert_equal 200, response.status
7688
assert response.body.is_a?(::Hash)
@@ -82,13 +94,14 @@ class ServerTest < ::TestCase
8294
assert_equal '12.0.5.102', server['address']
8395
assert_equal 4512, server['port']
8496

85-
response =
97+
response = T.must(
8698
Server.update_transient_settings(
8799
backend: 'foo_bar',
88100
name: 'foo_bar1',
89101
settings: { admin_state: :drain },
90102
config: config,
91-
)
103+
),
104+
)
92105

93106
assert_equal 200, response.status
94107
assert response.body.is_a?(::Hash)
@@ -104,6 +117,7 @@ class ServerTest < ::TestCase
104117

105118
private
106119

120+
#: -> Configuration
107121
def config
108122
Configuration.new(
109123
url: 'http://example.com',

0 commit comments

Comments
 (0)