percy-common 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 843bb5ade878a82fa046f8024a37273a363310f0
4
- data.tar.gz: 36f86e902963f0b3f209836b980428ef21e76334
3
+ metadata.gz: da785e6e731d99a3e9a2443232e3dafc8f9e7139
4
+ data.tar.gz: 662d50ea490653c199ca08a243348e0c41340bae
5
5
  SHA512:
6
- metadata.gz: bef3758204d4b3ca170ca2b7c49636f54d623f8c26c2f8d5bb53d41feda0fb62068f92f52a26439f2ddb179839d3256644c34e68fbc39ac3b34b88df92218514
7
- data.tar.gz: 2104a221fc389ddccd5a9c2e0efaed5e74c97733725e714f5b6e2527c92838bdc5820b6cffe89018f1fbc3b10ea79d4dec01520bdf5392eb2fbfbd090a678834
6
+ metadata.gz: 101b3fda6f5c3fe97a81e6539eb5bbdd7f77ae720ebe19a808fe91d0231d6f5e7376291a12ce1091339c03c5a498a8fb6fc2e1113fb340ac8a2c38d77dde1a98
7
+ data.tar.gz: 4d23ec8e50cba05655c1dad3a3396d556bcb4904d21df120d2622e1f60f147d4073b99050ffd7ca2441d8156a8e6a3cfe4c118207c5bc5718c715eea68dbbfc3
data/README.md CHANGED
@@ -78,6 +78,49 @@ Percy::ProcessHelpers.gracefully_kill(pid)
78
78
 
79
79
  This will send `SIGTERM` to the process, wait up to 10 seconds, then send `SIGKILL` if it has not already shut down.
80
80
 
81
+ ### Percy::NetworkHelpers
82
+
83
+ #### `random_open_port`
84
+
85
+ Returns a random open port. This is guaranteed by the OS to be currently an unbound open port, but users must still handle race conditions where the port is bound by the time it is actually used.
86
+
87
+ ```ruby
88
+ require 'percy/network_helpers'
89
+
90
+ Percy::NetworkHelpers.random_open_port
91
+ ```
92
+
93
+ #### `verify_healthcheck(url:[, expected_body: 'ok', retry_wait_seconds: 0.5])`
94
+
95
+ Verify that a URL returns a specific body. Raises `Percy::NetworkHelpers::ServerDown` if the server is down or does not respond with the expected body.
96
+
97
+ ```ruby
98
+ require 'percy/network_helpers'
99
+
100
+ Percy::NetworkHelpers.verify_healthcheck('http://localhost/healthz')
101
+ ```
102
+
103
+ #### `verify_http_server_up(hostname[, port: nil, path: nil, retry_wait_seconds: 0.25])`
104
+
105
+ Verifies that a simple HTTP GET / request works against a hostname. Raises `Percy::NetworkHelpers::ServerDown` if the server is down or if the request times out.
106
+
107
+ ```ruby
108
+ require 'percy/network_helpers'
109
+
110
+ Percy::NetworkHelpers.verify_http_server_up('example.com')
111
+ Percy::NetworkHelpers.verify_http_server_up('localhost', port: 8080)
112
+ ```
113
+
114
+ #### `serve_static_directory(dir[, hostname: 'localhost', port: nil])`
115
+
116
+ Starts a simple local WEBrick server to serve a directory of static assets. This is a testing helper and should not be used in production.
117
+
118
+ ```ruby
119
+ require 'percy/network_helpers'
120
+
121
+ Percy::NetworkHelpers.serve_static_directory(File.expand_path('../test_data/', __FILE__))
122
+ ```
123
+
81
124
  ### Percy::Stats
82
125
 
83
126
  Client for recording Datadog metrics and automatically setting up Percy-specific environment tags.
@@ -1,5 +1,5 @@
1
1
  module Percy
2
2
  module Common
3
- VERSION = '1.3.0'.freeze
3
+ VERSION = '1.4.0'.freeze
4
4
  end
5
5
  end
@@ -0,0 +1,53 @@
1
+ require 'socket'
2
+ require 'excon'
3
+
4
+ module Percy
5
+ class NetworkHelpers
6
+ class ServerDown < RuntimeError; end
7
+
8
+ def self.random_open_port
9
+ # Using a port of "0" relies on the system to pick an open port.
10
+ server = TCPServer.new('127.0.0.1', 0)
11
+ port = server.addr[1]
12
+ server.close
13
+ port
14
+ end
15
+
16
+ def self.verify_healthcheck(url:, expected_body: 'ok', retry_wait_seconds: 0.5)
17
+ 10.times do
18
+ begin
19
+ response = Excon.get(url)
20
+ return true if response.body == expected_body
21
+ rescue Excon::Error::Socket, Excon::Error::Timeout
22
+ sleep retry_wait_seconds
23
+ end
24
+ end
25
+ raise ServerDown, "Healthcheck failed for #{url}"
26
+ end
27
+
28
+ def self.verify_http_server_up(hostname, port: nil, path: nil, retry_wait_seconds: 0.25)
29
+ 10.times do
30
+ begin
31
+ Excon.get("http://#{hostname}#{port.nil? ? '' : ':' + port.to_s}#{path || ''}")
32
+ return true
33
+ rescue Excon::Error::Socket, Excon::Error::Timeout
34
+ sleep retry_wait_seconds
35
+ end
36
+ end
37
+ raise ServerDown, "Server is down: #{hostname}"
38
+ end
39
+
40
+ def self.serve_static_directory(dir, hostname: 'localhost', port: nil)
41
+ port ||= random_open_port
42
+
43
+ # Note: using this form of popen to keep stdout and stderr silent and captured.
44
+ process = IO.popen(
45
+ [
46
+ 'ruby', '-run', '-e', 'httpd', dir, '-p', port.to_s, err: [:child, :out],
47
+ ].flatten,
48
+ )
49
+ verify_http_server_up(hostname, port: port)
50
+ process.pid
51
+ end
52
+ end
53
+ end
data/percy-common.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
 
19
19
  spec.add_dependency 'dogstatsd-ruby', '~> 1.6'
20
20
  spec.add_dependency 'syslog-logger', '~> 1.6'
21
+ spec.add_dependency 'excon', '~> 0.57'
21
22
 
22
23
  spec.add_development_dependency 'bundler', '~> 1.15'
23
24
  spec.add_development_dependency 'rake', '~> 10.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: percy-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Perceptual Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-07 00:00:00.000000000 Z
11
+ date: 2017-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dogstatsd-ruby
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: excon
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.57'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.57'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -129,6 +143,7 @@ files:
129
143
  - lib/percy/common/version.rb
130
144
  - lib/percy/keyword_struct.rb
131
145
  - lib/percy/logger.rb
146
+ - lib/percy/network_helpers.rb
132
147
  - lib/percy/process_helpers.rb
133
148
  - lib/percy/stats.rb
134
149
  - percy-common.gemspec
@@ -151,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
166
  version: '0'
152
167
  requirements: []
153
168
  rubyforge_project:
154
- rubygems_version: 2.6.12
169
+ rubygems_version: 2.4.8
155
170
  signing_key:
156
171
  specification_version: 4
157
172
  summary: Server-side common library for Percy.