cucumber-varnishtest 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39d4bcc496c7bb96948d20cc14ec16d763e8f29e
4
+ data.tar.gz: 24bd3493559ddf26b32b65280c0099368b583ebb
5
+ SHA512:
6
+ metadata.gz: b36a2bb13b4a6ccd29db36f5fe2a76c3d55154101da16a5bb669a1469751dd786e10ec1d9122437e3b03b07412623b258858876047ce4bd16d705958e3b9d091
7
+ data.tar.gz: aef22699953e61133528e4fea1c321e3fafe7529cc2c2b8c0936c031a831a2afdfe48a64ee883da5d6361f94262bd0dc836e05d5d8ae3e168137d90053eeeadd
@@ -0,0 +1,12 @@
1
+ # This is the .varnishtest file that is
2
+ # generated via Cucumber.
3
+ generated.varnishtest
4
+
5
+ # This is the output of failed
6
+ # varnishtest runs
7
+ varnishtest.err
8
+ varnishtest.out
9
+
10
+ pkg
11
+
12
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'cucumber'
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 Nick Stielau
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,135 @@
1
+ # Varnishtest Cucumber
2
+
3
+ This let's you use Gherkin, the language of BDD, to write behavior-level
4
+ checks for your VCL.
5
+
6
+ ## Project status
7
+ This is currently a Proof of Concept.
8
+
9
+ I think this is probably the easiest/best way to get started with
10
+ testing your Varnish config, but it might take a bit of extended as
11
+ you test out your use-cases.
12
+
13
+ Definitely fork and don't rely on this code not changing, though ;)
14
+
15
+ I'm working on getting this packaged as a ruby gem to further simplify
16
+ the setup for getting testable VCLs. Once it's packaged and versioned
17
+ it should be in a sustainable, usable place.
18
+
19
+ ## To run
20
+
21
+ To run, simply run `cucumber` from this directory.
22
+
23
+ You can use your own VCL and write your own features in a .feature file.
24
+
25
+ ```
26
+ ± |master ✗| → cucumber
27
+ Feature: Static Server Headers
28
+
29
+ Scenario: Simple Single Request # features/varnishtest.feature:3
30
+ Given varnish running with example.vcl # features/step_definitions/varnish_steps.rb:4
31
+ When we GET /images.png # features/step_definitions/varnish_steps.rb:9
32
+ Then the response code should be 200 # features/step_definitions/varnish_steps.rb:29
33
+ And it should pass varnishtest # features/step_definitions/varnish_steps.rb:25
34
+
35
+ Scenario: Checking Response Headers # features/varnishtest.feature:9
36
+ Given varnish running with example.vcl # features/step_definitions/varnish_steps.rb:4
37
+ When we GET /images.png # features/step_definitions/varnish_steps.rb:9
38
+ Then the response header X-Served-By should be "My App Server" # features/step_definitions/varnish_steps.rb:21
39
+ And it should pass varnishtest # features/step_definitions/varnish_steps.rb:25
40
+
41
+ Scenario: POST should never be cached # features/varnishtest.feature:15
42
+ Given varnish running with example.vcl # features/step_definitions/varnish_steps.rb:4
43
+ When we POST /form # features/step_definitions/varnish_steps.rb:9
44
+ Then there should be 1 passed requests # features/step_definitions/varnish_steps.rb:49
45
+ And it should pass varnishtest # features/step_definitions/varnish_steps.rb:25
46
+
47
+ Scenario: Multiple Requests without warming # features/varnishtest.feature:21
48
+ Given varnish running with example.vcl # features/step_definitions/varnish_steps.rb:4
49
+ When we GET /images.png # features/step_definitions/varnish_steps.rb:9
50
+ And we GET /images2.png # features/step_definitions/varnish_steps.rb:9
51
+ And we GET /images2.png # features/step_definitions/varnish_steps.rb:9
52
+ Then there should be 2 cache misses # features/step_definitions/varnish_steps.rb:41
53
+ Then there should be 1 cache hit # features/step_definitions/varnish_steps.rb:37
54
+ And it should pass varnishtest # features/step_definitions/varnish_steps.rb:25
55
+
56
+ Scenario: Check Dynamic header # features/varnishtest.feature:30
57
+ Given varnish running with example.vcl # features/step_definitions/varnish_steps.rb:4
58
+ When we GET /monalisa.png # features/step_definitions/varnish_steps.rb:9
59
+ Then the response header X-Requested-URL should be /monalisa.png # features/step_definitions/varnish_steps.rb:21
60
+ And it should pass varnishtest # features/step_definitions/varnish_steps.rb:25
61
+
62
+ 5 scenarios (5 passed)
63
+ 23 steps (23 passed)
64
+ 0m7.467s
65
+ ```
66
+
67
+ ## Installation
68
+
69
+ Check out [this example](https://github.com/nstielau/cucumber-varnishtest-example).
70
+
71
+ Add the following line to your Gemfile, preferably in the test or cucumber group:
72
+
73
+ ```
74
+ gem 'cucumber-varnishtest', :require => false
75
+ ```
76
+
77
+ Then add the following line to your env.rb to make the step definitions available in your features:
78
+
79
+ ```
80
+ require 'cucumber/varnishtest'
81
+ ```
82
+
83
+ ## How to use your own VCL
84
+
85
+ Simply put the .vcl file in this directory, and specify in your
86
+ `Given` clause of your Scenario:
87
+
88
+ ```
89
+ Scenario: Multiple Requests without warming
90
+ Given varnish running with MyCustomConfig.vcl <==== YOUR VCL FILE
91
+ When we request /images.png
92
+ Then it should pass varnishtest
93
+ ```
94
+
95
+ ## Dependencies
96
+
97
+ Make sure you have 'varnishtest' installed!
98
+
99
+ ```
100
+ # On OSX
101
+ $ brew install varnish
102
+ $ varnishtest -h
103
+ usage: varnishtest [options] file ...
104
+ -b size # Set internal buffer size (default: 512K)
105
+ -D name=val # Define macro
106
+ -i # Find varnishd in build tree
107
+ -j jobs # Run this many tests in parallel
108
+ -k # Continue on test failure
109
+ -L # Always leave temporary vtc.*
110
+ -l # Leave temporary vtc.* if test fails
111
+ -n iterations # Run tests this many times
112
+ -q # Quiet mode: report only failures
113
+ -t duration # Time tests out after this long
114
+ -v # Verbose mode: always report test log
115
+ -W # Enable the witness facility for locking
116
+ ```
117
+
118
+ Install cucumber
119
+
120
+ ```
121
+ gem install bundler
122
+ bundle install
123
+ bundle exec cucumber --version
124
+ ```
125
+
126
+ ## References
127
+
128
+ Read more about varnish test at
129
+ * http://www.clock.co.uk/blog/getting-started-with-varnishtest
130
+ * http://blog.zenika.com/2012/08/27/introducing-varnishtest/
131
+
132
+ ## Thanks
133
+
134
+ Thanks to
135
+ * jayzes for examples on how to [package cucumber steps](http://github.com/jayzes/cucumber-api-steps)
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cucumber/varnishtest/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cucumber-varnishtest"
7
+ s.version = Cucumber::Varnishtest::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Nick Stielau"]
10
+ s.email = ["nick.stielau@gmail.com"]
11
+ s.homepage = "https://github.com/nstielau/varnishtest_cucumber"
12
+ s.summary = %q{Cucumber steps to easily create and execute varnishtest tests.}
13
+ s.description = %q{Cucumber steps to easily create and execute varnishtest tests.}
14
+
15
+ s.required_ruby_version = '>= 1.9.3'
16
+
17
+ s.add_dependency 'cucumber', '>= 2.0.2'
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,11 @@
1
+ # This is a really basic vcl file
2
+
3
+ backend default {
4
+ .host = "${s1_addr}";
5
+ .port = "${s1_port}";
6
+ }
7
+
8
+ sub vcl_deliver {
9
+ set resp.http.X-Served-By = "My App Server";
10
+ set resp.http.X-Requested-URL = req.url;
11
+ }
@@ -0,0 +1,56 @@
1
+ require './lib/cucumber/varnishtest/context_manager'
2
+ require './lib/cucumber/varnishtest/request'
3
+
4
+ Given(/^varnish running with (.*)$/) do |vcl_file|
5
+ # Load a particular varnish VCL file
6
+ ContextManager.instance.vcl_file = vcl_file
7
+ end
8
+
9
+ When(/^we (GET|PATCH|POST|PUT|DELETE) (.*)$/) do |method, path|
10
+ ContextManager.instance.request path, method
11
+ end
12
+
13
+ Then(/^the response should be 200$/) do
14
+ ContextManager.instance.client_expect 'resp.status', 200
15
+ end
16
+
17
+ Then(/^the response length should be (\d+)$/) do |bytes|
18
+ ContextManager.instance.client_expect 'resp.bodylen', bytes.to_i
19
+ end
20
+
21
+ Then(/^the response header (.*) should be (.*)$/) do |header, value|
22
+ ContextManager.instance.client_expect "resp.http.#{header}", "#{value}"
23
+ end
24
+
25
+ Then(/^it should pass varnishtest$/) do
26
+ ContextManager.instance.test!
27
+ end
28
+
29
+ Then(/^the response code should be (\d+)$/) do |code|
30
+ ContextManager.instance.client_expect "resp.status", 200
31
+ end
32
+
33
+ Then(/^the server should receive (\d+) requests$/) do |count|
34
+ ContextManager.instance.expected_request_count = count
35
+ end
36
+
37
+ Then(/^there should be (\d+) cache hit[s]*$/) do |count|
38
+ ContextManager.instance.expected_cache_hits = count
39
+ end
40
+
41
+ Then(/^there should be (\d+) cache misses$/) do |count|
42
+ ContextManager.instance.expected_cache_misses = count
43
+ end
44
+
45
+ Then(/^there should be (\d+) piped request[s]*$/) do |count|
46
+ ContextManager.instance.expected_piped_requests = count
47
+ end
48
+
49
+ Then(/^there should be (\d+) passed request[s]*$/) do |count|
50
+ ContextManager.instance.expected_passed_requests = count
51
+ end
52
+
53
+ After do |scenario|
54
+ # Reset the context manager for a fresh scenario
55
+ ContextManager.reset!
56
+ end
File without changes
@@ -0,0 +1,34 @@
1
+ Feature: Static Server Headers
2
+
3
+ Scenario: Simple Single Request
4
+ Given varnish running with example.vcl
5
+ When we GET /images.png
6
+ Then the response code should be 200
7
+ And it should pass varnishtest
8
+
9
+ Scenario: Checking Response Headers
10
+ Given varnish running with example.vcl
11
+ When we GET /images.png
12
+ Then the response header X-Served-By should be "My App Server"
13
+ And it should pass varnishtest
14
+
15
+ Scenario: POST should never be cached
16
+ Given varnish running with example.vcl
17
+ When we POST /form
18
+ Then there should be 1 passed requests
19
+ And it should pass varnishtest
20
+
21
+ Scenario: Multiple Requests without warming
22
+ Given varnish running with example.vcl
23
+ When we GET /images.png
24
+ And we GET /images2.png
25
+ And we GET /images2.png
26
+ Then there should be 2 cache misses
27
+ Then there should be 1 cache hit
28
+ And it should pass varnishtest
29
+
30
+ Scenario: Check Dynamic header
31
+ Given varnish running with example.vcl
32
+ When we GET /monalisa.png
33
+ Then the response header X-Requested-URL should be /monalisa.png
34
+ And it should pass varnishtest
@@ -0,0 +1 @@
1
+ require 'cucumber/varnishtest/steps'
@@ -0,0 +1,86 @@
1
+ class ContextManager
2
+ @instance = nil
3
+
4
+ VARNISHTEST_FILE = "generated.varnishtest"
5
+
6
+ # Server level expectations
7
+ attr_writer :expected_cache_hits
8
+ attr_writer :expected_cache_misses
9
+ attr_writer :expected_request_count
10
+ attr_writer :expected_piped_requests
11
+ attr_writer :expected_passed_requests
12
+
13
+ def self.instance()
14
+ # Get instance from class
15
+ @instance ||= self.new()
16
+ end
17
+
18
+ def self.reset!
19
+ # Clear out the instance, start fresh
20
+ @instance = nil
21
+ end
22
+
23
+ def vcl_file=(vcl_file)
24
+ @vcl_file = vcl_file
25
+ raise "VCL file '#{vcl_file}' not found." unless File.exist?(vcl_file)
26
+ end
27
+
28
+ def request(path, method)
29
+ @requests ||= []
30
+ @requests << VarnishtestRequest.new(path, method)
31
+ end
32
+
33
+ def client_expect(attribute, value)
34
+ @client_expectations ||= {}
35
+ @client_expectations[attribute] = value
36
+ end
37
+
38
+ def varnishtest_conf
39
+ return <<-eos
40
+ varnishtest "This is my first test"
41
+
42
+ server s1 {
43
+ #{@requests.map{"rxreq\ntxresp -body \"hello world\""}.join("\naccept\n\n")}
44
+ } -start
45
+
46
+ varnish v1 -vcl {
47
+ #{File.read(@vcl_file)}
48
+ } -start
49
+
50
+ client c1 {
51
+ #{
52
+ (@requests || []).map{|req| req.to_varnishtest}.join("\n\n")
53
+ }
54
+
55
+ #{
56
+ (@client_expectations || []).map do |attribute, value|
57
+ "expect #{attribute} == #{value}"
58
+ end.join("\n")
59
+ }
60
+ } -run
61
+
62
+ #{@expected_cache_hits.nil? ? "" : "varnish v1 -expect cache_hit == #{@expected_cache_hits}"}
63
+ #{@expected_cache_misses.nil? ? "" : "varnish v1 -expect cache_miss == #{@expected_cache_misses}"}
64
+ #{@expected_request_count.nil? ? "" : "varnish v1 -expect client_req == #{@expected_request_count}"}
65
+ #{@expected_piped_requests.nil? ? "" : "varnish v1 -expect s_pipe == #{@expected_piped_requests}"}
66
+ #{@expected_passed_requests.nil? ? "" : "varnish v1 -expect s_pass == #{@expected_passed_requests}"}
67
+ eos
68
+ end
69
+
70
+ def test!
71
+ File.open(VARNISHTEST_FILE, 'w') do |file|
72
+ file.write(varnishtest_conf)
73
+ end
74
+ output = `varnishtest #{VARNISHTEST_FILE}`
75
+ result = $? == 0
76
+
77
+ File.open('varnishtest.out', 'w') do |file|
78
+ file.write(output)
79
+ end
80
+
81
+ raise [
82
+ output.split("\n").select{|line| line.match("---- v1")},
83
+ "See #{VARNISHTEST_FILE} and varnishtest.err debuggin'"
84
+ ].join("\n\n\n\n") unless result
85
+ end
86
+ end
@@ -0,0 +1,14 @@
1
+ class VarnishtestRequest
2
+ def initialize(path, method)
3
+ @path = path
4
+ @method = method
5
+ end
6
+
7
+ def to_varnishtest
8
+ return <<-eos
9
+ txreq -url "#{@path}" -req #{@method}
10
+ rxresp
11
+
12
+ eos
13
+ end
14
+ end
@@ -0,0 +1,56 @@
1
+ require 'cucumber/varnishtest/context_manager'
2
+ require 'cucumber/varnishtest/request'
3
+
4
+ Given(/^varnish running with (.*)$/) do |vcl_file|
5
+ # Load a particular varnish VCL file
6
+ ContextManager.instance.vcl_file = vcl_file
7
+ end
8
+
9
+ When(/^we (GET|POST) (.*)$/) do |method, path|
10
+ ContextManager.instance.request path, method
11
+ end
12
+
13
+ Then(/^the response should be 200$/) do
14
+ ContextManager.instance.client_expect 'resp.status', 200
15
+ end
16
+
17
+ Then(/^the response length should be (\d+)$/) do |bytes|
18
+ ContextManager.instance.client_expect 'resp.bodylen', bytes.to_i
19
+ end
20
+
21
+ Then(/^the response header (.*) should be (.*)$/) do |header, value|
22
+ ContextManager.instance.client_expect "resp.http.#{header}", "#{value}"
23
+ end
24
+
25
+ Then(/^it should pass varnishtest$/) do
26
+ ContextManager.instance.test!
27
+ end
28
+
29
+ Then(/^the response code should be (\d+)$/) do |code|
30
+ ContextManager.instance.client_expect "resp.status", 200
31
+ end
32
+
33
+ Then(/^the server should receive (\d+) requests$/) do |count|
34
+ ContextManager.instance.expected_request_count = count
35
+ end
36
+
37
+ Then(/^there should be (\d+) cache hit[s]*$/) do |count|
38
+ ContextManager.instance.expected_cache_hits = count
39
+ end
40
+
41
+ Then(/^there should be (\d+) cache misses$/) do |count|
42
+ ContextManager.instance.expected_cache_misses = count
43
+ end
44
+
45
+ Then(/^there should be (\d+) piped request[s]*$/) do |count|
46
+ ContextManager.instance.expected_piped_requests = count
47
+ end
48
+
49
+ Then(/^there should be (\d+) passed request[s]*$/) do |count|
50
+ ContextManager.instance.expected_passed_requests = count
51
+ end
52
+
53
+ After do |scenario|
54
+ # Reset the context manager for a fresh scenario
55
+ ContextManager.reset!
56
+ end
@@ -0,0 +1,5 @@
1
+ module Cucumber
2
+ module Varnishtest
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cucumber-varnishtest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nick Stielau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cucumber
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.2
27
+ description: Cucumber steps to easily create and execute varnishtest tests.
28
+ email:
29
+ - nick.stielau@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - cucumber-varnishtest.gemspec
40
+ - example.vcl
41
+ - features/step_definitions/varnish_steps.rb
42
+ - features/support/env.rb
43
+ - features/varnishtest.feature
44
+ - lib/cucumber/varnishtest.rb
45
+ - lib/cucumber/varnishtest/context_manager.rb
46
+ - lib/cucumber/varnishtest/request.rb
47
+ - lib/cucumber/varnishtest/steps.rb
48
+ - lib/cucumber/varnishtest/version.rb
49
+ homepage: https://github.com/nstielau/varnishtest_cucumber
50
+ licenses: []
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: 1.9.3
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.0.14
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Cucumber steps to easily create and execute varnishtest tests.
72
+ test_files:
73
+ - features/step_definitions/varnish_steps.rb
74
+ - features/support/env.rb
75
+ - features/varnishtest.feature