Skip to content

Commit

Permalink
Added shared util library for tests, introduced a wait loop to check …
Browse files Browse the repository at this point in the history
…for kong clustering (#21)
  • Loading branch information
srb3 authored Mar 3, 2021
1 parent 4b50662 commit c992e46
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 42 deletions.
27 changes: 6 additions & 21 deletions test/integration/default/controls/default.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
require 'net/http'
require 'uri'

api = input('kong-api-endpoint')
proxy = input('kong-proxy-endpoint')

# set up service and route so we can test
uri = URI.parse("#{api}/services")
request = Net::HTTP::Post.new(uri)
request.set_form_data(
'name' => 'test',
'url' => 'http://httpbin.org'
)
require_relative '../../libraries/kong_util'

wait("#{api}/clustering/status")

Net::HTTP.start(uri.hostname, uri.port).request(request)
post("#{api}/services", { 'name' => 'test', 'url' => 'http://httpbin.org' })

uri = URI.parse("#{api}/services/test/routes")
request = Net::HTTP::Post.new(uri)
request.set_form_data(
'name' => 'testRoute',
'paths' => '/test'
)
post("#{api}/services/test/routes", { 'name' => 'testRoute', 'paths' => '/test' })

Net::HTTP.start(uri.hostname, uri.port).request(request)
cluster_members = JSON.parse(http("#{api}/clustering/status", method: 'GET').body)

# start testing
cluster_members = JSON.parse(http("#{api}/clustering/status",
method: 'GET').body)
describe cluster_members do
it { should_not be_empty }
end
Expand Down
27 changes: 6 additions & 21 deletions test/integration/hybrid_external_database/controls/default.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
require 'net/http'
require 'uri'

api = input('kong-api-endpoint')
proxy = input('kong-proxy-endpoint')

# set up service and route so we can test
uri = URI.parse("#{api}/services")
request = Net::HTTP::Post.new(uri)
request.set_form_data(
'name' => 'test',
'url' => 'http://httpbin.org'
)
require_relative '../../libraries/kong_util'

wait("#{api}/clustering/status")

Net::HTTP.start(uri.hostname, uri.port).request(request)
post("#{api}/services", { 'name' => 'test', 'url' => 'http://httpbin.org' })

uri = URI.parse("#{api}/services/test/routes")
request = Net::HTTP::Post.new(uri)
request.set_form_data(
'name' => 'testRoute',
'paths' => '/test'
)
post("#{api}/services/test/routes", { 'name' => 'testRoute', 'paths' => '/test' })

Net::HTTP.start(uri.hostname, uri.port).request(request)
cluster_members = JSON.parse(http("#{api}/clustering/status", method: 'GET').body)

# start testing
cluster_members = JSON.parse(http("#{api}/clustering/status",
method: 'GET').body)
describe cluster_members do
it { should_not be_empty }
end
Expand Down
28 changes: 28 additions & 0 deletions test/integration/libraries/kong_util.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'net/http'
require 'uri'

def wait(url, max=500)
count = 0
while count <= max
begin
uri = URI.parse(url)
response = Net::HTTP.get_response(uri)
raise "Bad response from kong gateway: #{response.code}" if response.code.to_i != 200

raise 'empty cluster body' if JSON.parse(response.body).empty?

break
rescue Exception => e
count += 1
sleep 1
next
end
end
end

def post(url, data)
uri = URI.parse(url)
request = Net::HTTP::Post.new(uri)
request.set_form_data(data)
Net::HTTP.start(uri.hostname, uri.port).request(request)
end

0 comments on commit c992e46

Please sign in to comment.