From c992e46bad48964999d4a852c6ef03e64626584d Mon Sep 17 00:00:00 2001 From: Stephen Brown Date: Wed, 3 Mar 2021 10:21:31 +0000 Subject: [PATCH] Added shared util library for tests, introduced a wait loop to check for kong clustering (#21) --- test/integration/default/controls/default.rb | 27 ++++-------------- .../controls/default.rb | 27 ++++-------------- test/integration/libraries/kong_util.rb | 28 +++++++++++++++++++ 3 files changed, 40 insertions(+), 42 deletions(-) create mode 100644 test/integration/libraries/kong_util.rb diff --git a/test/integration/default/controls/default.rb b/test/integration/default/controls/default.rb index c1c7597..46ba403 100644 --- a/test/integration/default/controls/default.rb +++ b/test/integration/default/controls/default.rb @@ -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 diff --git a/test/integration/hybrid_external_database/controls/default.rb b/test/integration/hybrid_external_database/controls/default.rb index c1c7597..46ba403 100644 --- a/test/integration/hybrid_external_database/controls/default.rb +++ b/test/integration/hybrid_external_database/controls/default.rb @@ -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 diff --git a/test/integration/libraries/kong_util.rb b/test/integration/libraries/kong_util.rb new file mode 100644 index 0000000..e69a0c6 --- /dev/null +++ b/test/integration/libraries/kong_util.rb @@ -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