summaryrefslogtreecommitdiff
path: root/spec/support
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-09-10 16:19:12 -0600
committermo khan <mo.khan@gmail.com>2020-09-10 16:19:12 -0600
commit6a56e4ff0047921f5afc4e106659e2bae1411da4 (patch)
tree905cd6a9aefba50769185d96e20578860a6a8e57 /spec/support
parent60e0be6501010961a8e6ac3248d2b0c976e75cda (diff)
test: start proxy server before suite and shutdown at end of suite
Diffstat (limited to 'spec/support')
-rw-r--r--spec/support/proxy_helper.rb64
-rw-r--r--spec/support/proxy_server.rb52
2 files changed, 61 insertions, 55 deletions
diff --git a/spec/support/proxy_helper.rb b/spec/support/proxy_helper.rb
index 8238167..1f4cd4f 100644
--- a/spec/support/proxy_helper.rb
+++ b/spec/support/proxy_helper.rb
@@ -1,67 +1,21 @@
# frozen_string_literal: true
+require 'support/proxy_server'
module ProxyHelper
- DOMAINS = [
- 'composer.test',
- 'goproxy.test',
- 'maven.test',
- 'npm.test',
- 'nuget.test',
- 'pypi.test',
- 'rubygems.test'
- ].freeze
-
- def x509_certificate(host)
- License::Management.root.join("tmp/#{host}.crt")
- end
-
- def generate_self_signed_certificate_for(host)
- Dir.chdir License::Management.root.join('tmp') do
- subject_alternative_names = DOMAINS.map { |x| "DNS:#{x}" }.join(',')
- system([
- "rm -f #{host}.*",
- "/usr/bin/openssl req -x509 -newkey rsa:4096 -keyout #{host}.key -out #{host}.crt -days 999 -nodes -subj '/C=/ST=/L=/O=/OU=/CN=*.test' -addext 'subjectAltName=#{subject_alternative_names}'",
- "cat #{host}.* > #{host}.pem"
- ].join("&&"))
- end
- end
-
- def start_proxy_server
- @proxy_server_pid ||=
- begin
- DOMAINS.each { |domain| add_host(domain, '127.0.0.1') }
- generate_self_signed_certificate_for('wildcard.test')
- spawn("/usr/sbin/haproxy -f #{fixture_file('haproxy.cfg')}")
- end
- end
-
- def add_host(name, ip)
- return if system("grep #{name} /etc/hosts")
-
- system("echo '#{ip} #{name}' >> /etc/hosts")
- end
-
- def stop_proxy_server
- return if !defined?(@proxy_server_pid) || @proxy_server_pid.nil?
-
- Process.kill("TERM", @proxy_server_pid)
- Process.wait(@proxy_server_pid)
- @proxy_server_pid = nil
+ def x509_certificate
+ License::Management.root.join("tmp/wildcard.test.crt")
end
end
RSpec.configure do |config|
config.include(ProxyHelper, type: :integration)
- config.before(:example, type: :integration) do
- start_proxy_server
+ config.before(:suite) do
+ pid = ProxyServer.instance.start
+ puts "START PROXY SERVER (#{pid})"
end
- config.after(:example, type: :integration) do
- stop_proxy_server
- system("rm -f /usr/local/share/ca-certificates/custom.*")
- system("rm -f /usr/lib/ssl/certs/custom.*")
- system("update-ca-certificates -v")
- system("c_rehash -v")
- system("/opt/asdf/installs/mono/6.8.0.123/bin/cert-sync /etc/ssl/certs/ca-certificates.crt")
+ config.after(:suite) do
+ puts "STOP PROXY SERVER (#{ProxyServer.instance.pid})"
+ ProxyServer.instance.stop
end
end
diff --git a/spec/support/proxy_server.rb b/spec/support/proxy_server.rb
new file mode 100644
index 0000000..721921a
--- /dev/null
+++ b/spec/support/proxy_server.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+class ProxyServer
+ DOMAINS = [
+ 'composer.test',
+ 'goproxy.test',
+ 'maven.test',
+ 'npm.test',
+ 'nuget.test',
+ 'pypi.test',
+ 'rubygems.test'
+ ].freeze
+
+ include Singleton
+
+ attr_accessor :pid
+
+ def start
+ DOMAINS.each { |domain| add_host(domain, '127.0.0.1') }
+ Dir.chdir License::Management.root.join('tmp') do
+ host = 'wildcard.test'
+ subject_alternative_names = DOMAINS.map { |x| "DNS:#{x}" }.join(',')
+ system([
+ "rm -f #{host}.*",
+ "/usr/bin/openssl req -x509 -newkey rsa:4096 -keyout #{host}.key -out #{host}.crt -days 999 -nodes -subj '/C=/ST=/L=/O=/OU=/CN=*.test' -addext 'subjectAltName=#{subject_alternative_names}'",
+ "cat #{host}.* > #{host}.pem"
+ ].join("&&"))
+ end
+ config_file = License::Management.root.join("spec/fixtures/haproxy.cfg")
+ self.pid = spawn("/usr/sbin/haproxy -f #{config_file}")
+ end
+
+ def stop(pid = self.pid)
+ return unless pid
+
+ Process.kill("TERM", pid)
+ Process.wait(pid)
+ system("rm -f /usr/local/share/ca-certificates/custom.*")
+ system("rm -f /usr/lib/ssl/certs/custom.*")
+ system("update-ca-certificates -v")
+ system("c_rehash -v")
+ system("/opt/asdf/installs/mono/6.8.0.123/bin/cert-sync /etc/ssl/certs/ca-certificates.crt")
+ end
+
+ private
+
+ def add_host(name, ip)
+ return if system("grep #{name} /etc/hosts")
+
+ system("echo '#{ip} #{name}' >> /etc/hosts")
+ end
+end