urushiol 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/LICENSE +20 -0
- data/README.md +103 -0
- data/bin/urushiol +107 -0
- data/lib/client_test_base.rb +41 -0
- data/lib/meta/help.rb +17 -0
- data/lib/meta/version.rb +3 -0
- data/lib/server.rb +42 -0
- data/lib/test_case.rb +10 -0
- data/lib/txreq.rb +28 -0
- data/lib/txresp.rb +32 -0
- data/lib/urushiol.rb +89 -0
- data/lib/varnish.rb +46 -0
- data/lib/vcl.rb +21 -0
- data/lib/vtc.rb +45 -0
- data/meta/live_top.uru +7 -0
- data/meta/mock_top.uru +10 -0
- data/meta/test_skeleton.uru +30 -0
- data/test/01test.rb +15 -0
- data/test/02test.rb +27 -0
- data/test/03test.rb +29 -0
- data/test/04test.rb +31 -0
- data/urushiol.gemspec +24 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MmQ3Yjg3YjQ3ZjY0MjZjODBmODc1ZTE2NDA2YTlhYWUwNDA1MmIwMg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NGVmNDk0OTA3ODE0OWNjYjkwYWY3NmZiZWM4MWZkMTE5ZWY3ZmJhZg==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZTYyOTMwZDE0MTAxMzE2NDJhYmJlNTYxMDEwOTQ0YjcxOWM0YWVmYmMzMmFj
|
10
|
+
ZDA0YTI5YjhkNjU1ZjRhYjJmYTU0MTlhZjQ3M2QyMDlhOGI4MzkxNzE3NzI0
|
11
|
+
YzRlZmEwZDVkMjZmMmUzZWQ4Njc1NjNlOGM4MzE5ODUwMzAyNmU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MTdiMzA3MzcyMjA1MzBiNWI3NWQ3NTljYjY1NjNiMzUzZTc5ZjBhZjQ5MWY2
|
14
|
+
MWJhNjA2ZjcxNjE1YTBkOWFiNWM2ZTFmM2UxNjVhNDJhYzY0NmZlZjI0ZDdm
|
15
|
+
ZjBlNzFjNzZkYjExYWY1ZWM3NzYxYWVhOGEyYzIzZDFhZWYxNDg=
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 TV4
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
Urushiol - /ʊˈruːʃi.ɒl/ v0.5 BETA
|
2
|
+
========
|
3
|
+
|
4
|
+
Test framework written in Ruby to test varnish-cache routing and caching logic.
|
5
|
+
|
6
|
+
>The sap of the lacquer tree, today bearing the technical description of "urushiol-based lacquer," has traditionally been used in Japan. As the substance is poisonous to the touch until it dries, the creation of lacquerware has long been practiced only by skilled dedicated artisans.
|
7
|
+
|
8
|
+
Urushiol was born out of necessity. When we decided to migrate our reverse proxy routing from Apache to Varnish we noticed that this would not be a trivial matter.
|
9
|
+
As the migration came to a halt due to unexpected routing errors we decided that, having a proper way of testing our configs would not be a bad idea.
|
10
|
+
Urushiol is the product of that idea.
|
11
|
+
|
12
|
+
####Get up and running with Urushiol.
|
13
|
+
To start with there are some dependencies that must be satisfied.
|
14
|
+
* Varnish (a.t.m only supports 3.0.4) must be installed on the host computer.
|
15
|
+
* Ruby (tested on 1.9.3) must be installed on the host computer
|
16
|
+
|
17
|
+
Then there is just the matter of writing the tests. The project come bundled with some examples but lets look at the basics a bit more in depth.
|
18
|
+
|
19
|
+
Navigate to the project folder where you will se a folder: `Testing-Framework/` inside there you will se a `lib/` catalog and some `.rb` files that all follow the pattern `0Xtest.rb` these are the function-tests.
|
20
|
+
You should run each one of them to make sure that the framework actually work, if it doesn't I redirect you back to the dependencies.
|
21
|
+
You run Urushiol tests by for example typing `ruby 01test.rb` in your shell. You should be presented with `# top TEST /tmp/test.vtc passed (0.003)`
|
22
|
+
each and every time.
|
23
|
+
|
24
|
+
####Writing your own tests.
|
25
|
+
Now to write your own tests. It is assumed that you have a varnish `.vcl` file that you want to test.
|
26
|
+
Each test is written in ruby and requires `varnishtest_base.rb` which can be found in the `lib` folder within `Testing-Framework` like so: `require './lib/varnishtest_base'` for the functiontests.
|
27
|
+
From here on you have all the power of Urushiol at your fingertips.
|
28
|
+
|
29
|
+
Start out with instanciating a testcase like so:
|
30
|
+
|
31
|
+
`testcase = Urushiol::VarnishTestBase.new("vcl test")`
|
32
|
+
|
33
|
+
and that's it now you have a testcase you can run by calling `testcase.run`. At the moment that will be pretty uneventfull so lets load up a vcl-fil and mock some backends.
|
34
|
+
Urushiol has functionallity to do this on the fly. Simply `modified_vcl_file = testcase.mock_backends( vcl_file_path )`.
|
35
|
+
The `mock_backends` methods does a lot of things but in essence it takes the vcl's filepath and mocks upp servers to act as its backends and modifies the backends in the file to point to the given servers.
|
36
|
+
Now it's quite easy to test them as they will return statuscode 200 and the backend name as the body.
|
37
|
+
The method returns a modified version of the vclfile that has the backends rerouted to the mock_servers.
|
38
|
+
|
39
|
+
You can create your own servers with the `mock_server` method but that is covered extencivly in the function-tests.
|
40
|
+
|
41
|
+
To create a mock varnish one runs the `mock_varnish` method. To configure it one gives it a block of stuff to do. In this case we want to give it a vcl file; the modified vcl file.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
testcase.mock_varnish "v1" do |varnish|
|
45
|
+
varnish.vcl modified_vcl_file
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
This starts a mock varnish with the given configfile and it works just as a real varnish would, because it is a real varnish instance.
|
50
|
+
|
51
|
+
So now we have servers, and a varnish instance that points at the servers now all we need are some clients that asks varnish for stuff and tests its behaviour.
|
52
|
+
|
53
|
+
Say that we have a backend named `SuperBackend` in our vcl file and some logic that states that if your client's request's http.host == `superbackend.tv4.se` one should be passed through to the superbackend.
|
54
|
+
This behaviour we can now test:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
testcase.client_testcase "c1" do |test|
|
58
|
+
test.txreq do |req|
|
59
|
+
req.host "superbackend.tv4.se"
|
60
|
+
end
|
61
|
+
test.rxresp
|
62
|
+
test.expect "resp.status == 200"
|
63
|
+
test.expect "resp.body == SuperBackend"
|
64
|
+
end
|
65
|
+
```
|
66
|
+
|
67
|
+
I find this pretty selfexplanatory but here goes, we mock a client_testcase and define a request to transmit (`txreq`).
|
68
|
+
While creating the request we modify the header to have the given host. We then grab the respons and expect it to have status 200 and the backendname as a body.
|
69
|
+
|
70
|
+
now we are ready to go, this was a short demonstartion of what you CAN do. But there is more to Urushiol than this. Play around and look at the code.
|
71
|
+
|
72
|
+
The whole test would look something like this:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
require './lib/varnishtest_base'
|
76
|
+
|
77
|
+
testcase = Urushiol::VarnishTestBase.new("vcl test")
|
78
|
+
|
79
|
+
vcl_file_path = '../new.vcl'
|
80
|
+
|
81
|
+
modified_vcl_file = testcase.mock_backends( vcl_file_path )
|
82
|
+
|
83
|
+
testcase.mock_varnish "v1" do |varnish|
|
84
|
+
varnish.vcl modified_vcl_file
|
85
|
+
end
|
86
|
+
|
87
|
+
testcase.client_testcase "c1" do |test|
|
88
|
+
test.txreq do |req|
|
89
|
+
req.host "superbackend.tv4.se"
|
90
|
+
end
|
91
|
+
test.rxresp
|
92
|
+
test.expect "resp.status == 200"
|
93
|
+
test.expect "resp.body == SuperBackend"
|
94
|
+
end
|
95
|
+
|
96
|
+
testcase.run
|
97
|
+
```
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
data/bin/urushiol
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'urushiol'
|
3
|
+
require 'test_case'
|
4
|
+
require 'pathname'
|
5
|
+
require 'meta/help'
|
6
|
+
require 'meta/version'
|
7
|
+
|
8
|
+
def run_testfile file_ref
|
9
|
+
test = TestCase.new file_ref
|
10
|
+
|
11
|
+
puts "\nRunning test #{Pathname.new(file_ref).basename} : "
|
12
|
+
|
13
|
+
test.instance_eval("def run \n #{IO.read(file_ref)} \nend")
|
14
|
+
test.run
|
15
|
+
end
|
16
|
+
|
17
|
+
def run_typed_testfiles test_files, vcl_file, type
|
18
|
+
test_head = IO.readlines("#{root_path}meta/#{type}_top.uru").join('').sub(";file_ref;", vcl_file)
|
19
|
+
|
20
|
+
test_files.each do |test_file|
|
21
|
+
test = TestCase.new test_file
|
22
|
+
|
23
|
+
puts "\nRunning test #{Pathname.new(test_file).basename} :"
|
24
|
+
|
25
|
+
test.instance_eval("def run \n#{test_head}\n#{IO.read(test_file)} \nend")
|
26
|
+
|
27
|
+
test.run
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def create_test_file test_name
|
32
|
+
out_file = File.new("#{test_name}.uru", "w")
|
33
|
+
out_file << IO.readlines("#{root_path}meta/test_skeleton.uru").join('')
|
34
|
+
out_file.close
|
35
|
+
|
36
|
+
puts "#{test_name}.uru successfully created."
|
37
|
+
end
|
38
|
+
|
39
|
+
def root_path
|
40
|
+
File.dirname(__FILE__).to_s+"/../"
|
41
|
+
end
|
42
|
+
|
43
|
+
def pre_run_check(argv_index)
|
44
|
+
ARGV.each do |testfile|
|
45
|
+
if File.exists? testfile
|
46
|
+
yield testfile
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
if ARGV.empty?
|
52
|
+
puts "Welcome to Urushiol. The easy test framework for varnish vcl files.
|
53
|
+
Append -h flag for usage information."
|
54
|
+
|
55
|
+
elsif (ARGV.include?("--version") || ARGV.include?("-v"))
|
56
|
+
puts "Version : #{Urushiol::VERSION}"
|
57
|
+
|
58
|
+
elsif (ARGV.include?("--help") || ARGV.include?("-h"))
|
59
|
+
puts Urushiol::HELP
|
60
|
+
|
61
|
+
elsif (ARGV.include?("--live") || ARGV.include?("-l"))
|
62
|
+
live_flag = "--live" if ARGV.include?("--live")
|
63
|
+
live_flag = "-l" if ARGV.include?("-l")
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
vclfile = ARGV[ARGV.find_index(live_flag)+1]
|
68
|
+
test_files = ARGV[(ARGV.find_index(live_flag)+2)...ARGV.length]
|
69
|
+
run_typed_testfiles(test_files, vclfile, "live")
|
70
|
+
|
71
|
+
|
72
|
+
elsif (ARGV.include?("--mock") || ARGV.include?("-m"))
|
73
|
+
|
74
|
+
mock_flag = "--mock" if ARGV.include?("--mock")
|
75
|
+
mock_flag = "-m" if ARGV.include?("-m")
|
76
|
+
|
77
|
+
vclfile = ARGV[ARGV.find_index(mock_flag)+1]
|
78
|
+
test_files = ARGV[(ARGV.find_index(mock_flag)+2)...ARGV.length]
|
79
|
+
run_typed_testfiles(test_files, File.absolute_path(vclfile), "mock")
|
80
|
+
|
81
|
+
|
82
|
+
elsif ARGV.first.downcase == "integritycheck"
|
83
|
+
Dir.glob("#{root_path}test/**/*").each do |testfile|
|
84
|
+
run_testfile testfile
|
85
|
+
end
|
86
|
+
elsif ARGV.first == "generate"
|
87
|
+
if ARGV[1] != nil
|
88
|
+
create_test_file(ARGV[1])
|
89
|
+
else
|
90
|
+
puts "Please specify a name for your test."
|
91
|
+
end
|
92
|
+
elsif File.exists? ARGV.first
|
93
|
+
ARGV.each do |testfile|
|
94
|
+
if File.exists? testfile
|
95
|
+
run_testfile testfile
|
96
|
+
end
|
97
|
+
end
|
98
|
+
else
|
99
|
+
puts "Urushiol did not recognize \"#{ARGV.join(" ")} \" as argument(s)."
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'txreq'
|
2
|
+
|
3
|
+
module Urushiol
|
4
|
+
class ClientTestBase
|
5
|
+
class << self
|
6
|
+
def wait(name = "c")
|
7
|
+
"\nclient #{name} -wait"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
def initialize (name,config = "")
|
11
|
+
@client_test = "\nclient #{name} #{config}{"
|
12
|
+
end
|
13
|
+
|
14
|
+
def txreq(&block)
|
15
|
+
req = Txreq.new
|
16
|
+
yield req if block_given?
|
17
|
+
@client_test << req.get_source
|
18
|
+
end
|
19
|
+
|
20
|
+
def rxresp
|
21
|
+
@client_test << "\nrxresp"
|
22
|
+
end
|
23
|
+
|
24
|
+
def expect(criteria)
|
25
|
+
@client_test << "\nexpect #{criteria.strip}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def run
|
29
|
+
@client_test << "\n} -run\n"
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_source
|
33
|
+
if @client_test.end_with?("\n} -run\n")
|
34
|
+
@client_test
|
35
|
+
else
|
36
|
+
run
|
37
|
+
@client_test
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/meta/help.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
module Urushiol
|
2
|
+
HELP = "Usage: urushiol [-v] [-h] [-l] [-m] [<args>]
|
3
|
+
|
4
|
+
If no flags are specified <args> should be test file(s).
|
5
|
+
|
6
|
+
-v, --version Print the version and exit.
|
7
|
+
-h, --help Print this help.
|
8
|
+
-l, --live Runs tests on live vcl file; backends
|
9
|
+
will be real servers. <args> should be
|
10
|
+
path to vcl file and path to test file(s).
|
11
|
+
-m, --mock Runs tests on mocked vcl file; backends
|
12
|
+
will be mocked servers that return 200 as
|
13
|
+
status code and the backend name as body.
|
14
|
+
<args> should be path to vcl file and path
|
15
|
+
to test file(s).
|
16
|
+
"
|
17
|
+
end
|
data/lib/meta/version.rb
ADDED
data/lib/server.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'txresp'
|
2
|
+
|
3
|
+
module Urushiol
|
4
|
+
class Server
|
5
|
+
class << self
|
6
|
+
def wait(name)
|
7
|
+
"\nserver #{name} -wait"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize (name)
|
12
|
+
@server_string = "\nserver #{name} {"
|
13
|
+
end
|
14
|
+
|
15
|
+
def txresp
|
16
|
+
resp = Txresp.new
|
17
|
+
yield resp if block_given?
|
18
|
+
@server_string << resp.get_source
|
19
|
+
end
|
20
|
+
|
21
|
+
def rxreq
|
22
|
+
@server_string << "\nrxreq"
|
23
|
+
end
|
24
|
+
|
25
|
+
def expect(criteria)
|
26
|
+
@server_string << "\nexpect #{criteria}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def start
|
30
|
+
@server_string << "\n} -start"
|
31
|
+
end
|
32
|
+
|
33
|
+
def server_source
|
34
|
+
if @server_string.end_with?("\n} -start")
|
35
|
+
@server_string
|
36
|
+
else
|
37
|
+
start
|
38
|
+
@server_string
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/test_case.rb
ADDED
data/lib/txreq.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
module Urushiol
|
2
|
+
class Txreq
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@req = "\ntxreq"
|
6
|
+
end
|
7
|
+
|
8
|
+
def url(url)
|
9
|
+
@req << " -url #{url.strip}"
|
10
|
+
end
|
11
|
+
|
12
|
+
def protocol(proto)
|
13
|
+
@req << " -proto #{proto.strip}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def host(host)
|
17
|
+
@req << " -hdr \"Host: #{host.strip}\""
|
18
|
+
end
|
19
|
+
|
20
|
+
def request(type)
|
21
|
+
@req << " -req #{type.strip.upcase}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_source
|
25
|
+
@req
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/txresp.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Urushiol
|
2
|
+
class Txresp
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@resp = "\ntxresp"
|
6
|
+
end
|
7
|
+
|
8
|
+
def message(msg)
|
9
|
+
@resp << " -msg #{msg}"
|
10
|
+
end
|
11
|
+
|
12
|
+
def protocol(proto)
|
13
|
+
@resp << " -proto #{proto}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def status_code(status)
|
17
|
+
@resp << " -status #{status}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def header(hdr)
|
21
|
+
@resp << " -hdr \"#{hdr}\""
|
22
|
+
end
|
23
|
+
|
24
|
+
def body(body)
|
25
|
+
@resp << " -body \"#{body}\""
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_source
|
29
|
+
@resp
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/urushiol.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'vtc'
|
2
|
+
require 'vcl'
|
3
|
+
require 'client_test_base'
|
4
|
+
require 'server'
|
5
|
+
require 'varnish'
|
6
|
+
|
7
|
+
module Urushiol
|
8
|
+
class VarnishTestBase
|
9
|
+
def initialize (name,vcl_file_ref=nil)
|
10
|
+
@vtc_obj = Vtc.new(name)
|
11
|
+
@vcl_source = vcl_file_ref
|
12
|
+
end
|
13
|
+
|
14
|
+
def mock_varnish(name)
|
15
|
+
varnish = Varnish.new(name)
|
16
|
+
yield varnish if block_given?
|
17
|
+
@vtc_obj.append_config(varnish.varnish_source)
|
18
|
+
end
|
19
|
+
|
20
|
+
def mock_backends(source=@vcl_source)
|
21
|
+
if source != nil
|
22
|
+
@vtc_obj.mock_backends(Vcl.new(source).get_conf)
|
23
|
+
else
|
24
|
+
puts "\nCan't mock backends if no VCL file is specified.\n"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def server(&block)
|
29
|
+
if block_given?
|
30
|
+
@vtc_obj.append_config(yield Server)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def define_tests(tests)
|
35
|
+
@vtc_obj.mock_clients_and_tests(tests)
|
36
|
+
end
|
37
|
+
|
38
|
+
def client_testcase(name="c",config="",&block)
|
39
|
+
test = ClientTestBase.new(name,config)
|
40
|
+
yield test if block_given?
|
41
|
+
@vtc_obj.append_config(test.test_source)
|
42
|
+
end
|
43
|
+
|
44
|
+
def mock_server(name,&block)
|
45
|
+
server = Server.new(name)
|
46
|
+
yield server if block_given?
|
47
|
+
@vtc_obj.append_config(server.server_source)
|
48
|
+
end
|
49
|
+
|
50
|
+
def create_test_file(vtc)
|
51
|
+
File.open('/tmp/test.vtc', "wb") { |f| f.write(vtc.get_spec) }
|
52
|
+
end
|
53
|
+
|
54
|
+
def override_vcl_file(vcl_file)
|
55
|
+
File.open('/tmp/test.vcl', "wb") { |f| f.write(Vcl.new(vcl_file).get_conf) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def get_vcl
|
59
|
+
@vcl_source
|
60
|
+
end
|
61
|
+
|
62
|
+
def delay(time)
|
63
|
+
@vtc_obj.append_config("\ndelay #{time}")
|
64
|
+
end
|
65
|
+
|
66
|
+
def run
|
67
|
+
create_test_file(@vtc_obj)
|
68
|
+
varnishd_check = `which varnishd` ; result=$?.success?
|
69
|
+
if result == true
|
70
|
+
output=`varnishtest /tmp/test.vtc` ; result=$?.success?
|
71
|
+
if result == false
|
72
|
+
puts output
|
73
|
+
puts "\nVarnishtests returned errors, see stack trace above."
|
74
|
+
exit 1
|
75
|
+
else
|
76
|
+
puts "Test completed successfully without errors."
|
77
|
+
end
|
78
|
+
cleanup
|
79
|
+
else
|
80
|
+
puts "Varnish does not seem to be installed on your computer. Urushiol can't run without Varnish"
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
def cleanup
|
86
|
+
#system("rm /tmp/test.vtc")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/lib/varnish.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module Urushiol
|
2
|
+
class Varnish
|
3
|
+
def initialize (name)
|
4
|
+
@varnish_string = "\n# Start a varnish instance called \"#{name}\"\n"
|
5
|
+
@varnish_string << "varnish #{name}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def args(args_string)
|
9
|
+
@varnish_string << " -arg \"#{args_string}\""
|
10
|
+
end
|
11
|
+
|
12
|
+
def vcl_file(file_path)
|
13
|
+
args "-f #{file_path}"
|
14
|
+
end
|
15
|
+
|
16
|
+
def server_address(ip_adress,port)
|
17
|
+
args "-a #{ip_adress}:#{port}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def mock_server_address
|
21
|
+
server_address("127.0.0.1","8080")
|
22
|
+
end
|
23
|
+
|
24
|
+
def vcl(vcl_string)
|
25
|
+
@varnish_string << " -vcl {\n#{vcl_string}\n}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def vcl_backend(vcl_backend_string)
|
29
|
+
@varnish_string << " -vcl+backend {\n#{vcl_backend_string}\n}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def start
|
33
|
+
@varnish_string << " -start"
|
34
|
+
end
|
35
|
+
|
36
|
+
def varnish_source
|
37
|
+
if @varnish_string.end_with?(" -start")
|
38
|
+
@varnish_string
|
39
|
+
else
|
40
|
+
start
|
41
|
+
@varnish_string
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/vcl.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Urushiol
|
2
|
+
class Vcl
|
3
|
+
|
4
|
+
def initialize (vcl_file_ref)
|
5
|
+
@vcl = ""
|
6
|
+
parse(vcl_file_ref)
|
7
|
+
end
|
8
|
+
|
9
|
+
def parse (vcl_file_ref)
|
10
|
+
file = File.open(vcl_file_ref)
|
11
|
+
file.each do |line|
|
12
|
+
@vcl << line
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_conf
|
17
|
+
@vcl
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/lib/vtc.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module Urushiol
|
2
|
+
class Vtc
|
3
|
+
|
4
|
+
def initialize(name)
|
5
|
+
@vtc = ""
|
6
|
+
end
|
7
|
+
|
8
|
+
def mock_backends(vcl)
|
9
|
+
if vcl != nil
|
10
|
+
backends = vcl.scan(/^backend (\w*)\W+/).flatten
|
11
|
+
|
12
|
+
#Mock servers in vtc-file
|
13
|
+
backends.each do |backend|
|
14
|
+
index = backends.index(backend)
|
15
|
+
@vtc << "server s#{index} -repeat 10000 {
|
16
|
+
# Receive a request
|
17
|
+
rxreq
|
18
|
+
# Send a standard response
|
19
|
+
txresp -hdr \"Connection: close\" -body \"#{backend}\"
|
20
|
+
} -start \n"
|
21
|
+
|
22
|
+
#mock backends in vcl-file
|
23
|
+
vcl.gsub!(/(?m)backend #{backend} \{(.*?)\}/,
|
24
|
+
"backend #{backend} {\n .host = \"${s#{index}_addr}\";\n .port = \"${s#{index}_port}\";\n}")
|
25
|
+
end
|
26
|
+
@test_vcl = vcl
|
27
|
+
else
|
28
|
+
puts("no vcl-file given")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def append_config(tests)
|
33
|
+
@vtc << tests
|
34
|
+
end
|
35
|
+
|
36
|
+
def mock_varnish
|
37
|
+
@vtc << "# Start a varnish instance called \"v1\" from vcl and override backends with mock server adresses
|
38
|
+
varnish v1 -arg \"-f /tmp/test.vcl\" -start \n"
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_spec
|
42
|
+
@vtc
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/meta/live_top.uru
ADDED
data/meta/mock_top.uru
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#Reference the vclfile to be tested
|
2
|
+
vcl_file_ref = ';file_ref;'
|
3
|
+
|
4
|
+
#Automock backends stated in the vclfile
|
5
|
+
vcl_file = testcase.mock_backends(vcl_file_ref)
|
6
|
+
|
7
|
+
#Mock varnish and give it a vcl file to run
|
8
|
+
testcase.mock_varnish("v1") do |varnish|
|
9
|
+
varnish.vcl vcl_file
|
10
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Welcome to Urushio! We have created a testcase instance for you.
|
2
|
+
# Access it by calling methods on the variable "testcase", like so:
|
3
|
+
#
|
4
|
+
# testcase.mock_server "s1" do |server|
|
5
|
+
# server.rxreq
|
6
|
+
# server.txresp
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
# testcase.run
|
10
|
+
#
|
11
|
+
# If you have any questions regarding functionality we redirect you to our Github page:
|
12
|
+
# https://github.com/TV4/Urushiol
|
13
|
+
#
|
14
|
+
# Run this test by running `urushiol NameOfTestHere.uru` in your shell.
|
15
|
+
|
16
|
+
testcase.mock_server("s1") do |server|
|
17
|
+
server.rxreq
|
18
|
+
server.txresp
|
19
|
+
end
|
20
|
+
|
21
|
+
testcase.client_testcase("c1","-connect ${s1_sock} ") do |test|
|
22
|
+
test.txreq
|
23
|
+
test.rxresp
|
24
|
+
end
|
25
|
+
|
26
|
+
testcase.server do |server|
|
27
|
+
server.wait("s1")
|
28
|
+
end
|
29
|
+
|
30
|
+
testcase.run
|
data/test/01test.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
testcase.mock_server("s1") do |server|
|
2
|
+
server.rxreq
|
3
|
+
server.txresp
|
4
|
+
end
|
5
|
+
|
6
|
+
testcase.client_testcase("c1","-connect ${s1_sock} ") do |test|
|
7
|
+
test.txreq
|
8
|
+
test.rxresp
|
9
|
+
end
|
10
|
+
|
11
|
+
testcase.server do |server|
|
12
|
+
server.wait("s1")
|
13
|
+
end
|
14
|
+
|
15
|
+
testcase.run
|
data/test/02test.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
testcase.mock_server("s1") do |server|
|
2
|
+
server.rxreq
|
3
|
+
server.expect "req.proto == HTTP/1.1"
|
4
|
+
server.expect "req.url == \"/\""
|
5
|
+
server.txresp do |resp|
|
6
|
+
resp.message "Ok"
|
7
|
+
resp.status_code "200"
|
8
|
+
resp.protocol "HTTP/1.1"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
testcase.client_testcase("c1","-connect ${s1_sock} ") do |test|
|
13
|
+
test.txreq do |req|
|
14
|
+
req.url "/"
|
15
|
+
req.protocol "HTTP/1.1"
|
16
|
+
end
|
17
|
+
test.rxresp
|
18
|
+
test.expect "resp.proto == HTTP/1.1"
|
19
|
+
test.expect "resp.status == 200"
|
20
|
+
test.expect "resp.msg == Ok"
|
21
|
+
end
|
22
|
+
|
23
|
+
testcase.server do |server|
|
24
|
+
server.wait("s1")
|
25
|
+
end
|
26
|
+
|
27
|
+
testcase.run
|
data/test/03test.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
testcase.mock_server("s1") do |server|
|
2
|
+
server.rxreq
|
3
|
+
server.expect "req.request == PUT"
|
4
|
+
server.expect "req.proto == HTTP/1.0"
|
5
|
+
server.expect "req.url == \"/foo\""
|
6
|
+
server.txresp do |resp|
|
7
|
+
resp.message "Foo"
|
8
|
+
resp.status_code "201"
|
9
|
+
resp.protocol "HTTP/1.2"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
testcase.client_testcase("c1","-connect ${s1_sock} ") do |test|
|
14
|
+
test.txreq do |req|
|
15
|
+
req.url "/foo"
|
16
|
+
req.protocol "HTTP/1.0"
|
17
|
+
req.request "PUT"
|
18
|
+
end
|
19
|
+
test.rxresp
|
20
|
+
test.expect "resp.proto == HTTP/1.2"
|
21
|
+
test.expect "resp.status == 201"
|
22
|
+
test.expect "resp.msg == Foo"
|
23
|
+
end
|
24
|
+
|
25
|
+
testcase.server do |server|
|
26
|
+
server.wait("s1")
|
27
|
+
end
|
28
|
+
|
29
|
+
testcase.run
|
data/test/04test.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
testcase.mock_server "s1" do |server|
|
2
|
+
server.rxreq
|
3
|
+
server.txresp do |resp|
|
4
|
+
resp.header "Connection: close"
|
5
|
+
resp.body "012345"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
testcase.mock_varnish "v1" do |varnish|
|
10
|
+
varnish.vcl_backend 'backend b2 {
|
11
|
+
.host = "${s1_addr}";
|
12
|
+
.port = "${s1_port}";
|
13
|
+
}
|
14
|
+
sub vcl_recv {
|
15
|
+
set req.backend = b2;
|
16
|
+
return(lookup);
|
17
|
+
}'
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
testcase.client_testcase do |test|
|
22
|
+
test.txreq do |req|
|
23
|
+
req.url "/"
|
24
|
+
end
|
25
|
+
test.rxresp
|
26
|
+
test.expect "resp.status == 200"
|
27
|
+
test.expect "resp.body == \"012345\""
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
testcase.run
|
data/urushiol.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path('../lib/', __FILE__)
|
3
|
+
$:.unshift lib unless $:.include?(lib)
|
4
|
+
require 'meta/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'urushiol'
|
8
|
+
s.version = Urushiol::VERSION
|
9
|
+
s.date = Date.today.to_s
|
10
|
+
s.required_ruby_version = ">=1.9.2"
|
11
|
+
s.authors = ["TV4","Karl Litterfeldt"]
|
12
|
+
s.email = "karl.litterfeldt@tv4.se"
|
13
|
+
s.license = "MIT-LICENSE"
|
14
|
+
|
15
|
+
s.homepage = "http://www.tv4.se"
|
16
|
+
s.summary = %q{Test framework written in Ruby to test varnish-cache routing and caching logic.}
|
17
|
+
s.description = %q{Urushiol was born out of necessity. When we decided to migrate our reverse proxy routing from Apache to Varnish we noticed that this would not be a trivial matter. As the migration came to a halt due to unexpected routing errors we decided that, having a proper way of testing our configs would not be a bad idea. Urushiol is the product of that idea.}
|
18
|
+
|
19
|
+
s.files = Dir.glob("lib/**/*") + Dir.glob("test/**/*") + Dir.glob("meta/**/*") + Dir.glob("bin/**/*") + %w(urushiol.gemspec LICENSE README.md)
|
20
|
+
s.executables = %w(urushiol)
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: urushiol
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TV4
|
8
|
+
- Karl Litterfeldt
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-30 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Urushiol was born out of necessity. When we decided to migrate our reverse
|
15
|
+
proxy routing from Apache to Varnish we noticed that this would not be a trivial
|
16
|
+
matter. As the migration came to a halt due to unexpected routing errors we decided
|
17
|
+
that, having a proper way of testing our configs would not be a bad idea. Urushiol
|
18
|
+
is the product of that idea.
|
19
|
+
email: karl.litterfeldt@tv4.se
|
20
|
+
executables:
|
21
|
+
- urushiol
|
22
|
+
extensions: []
|
23
|
+
extra_rdoc_files: []
|
24
|
+
files:
|
25
|
+
- lib/client_test_base.rb
|
26
|
+
- lib/meta/help.rb
|
27
|
+
- lib/meta/version.rb
|
28
|
+
- lib/server.rb
|
29
|
+
- lib/test_case.rb
|
30
|
+
- lib/txreq.rb
|
31
|
+
- lib/txresp.rb
|
32
|
+
- lib/urushiol.rb
|
33
|
+
- lib/varnish.rb
|
34
|
+
- lib/vcl.rb
|
35
|
+
- lib/vtc.rb
|
36
|
+
- test/01test.rb
|
37
|
+
- test/02test.rb
|
38
|
+
- test/03test.rb
|
39
|
+
- test/04test.rb
|
40
|
+
- meta/live_top.uru
|
41
|
+
- meta/mock_top.uru
|
42
|
+
- meta/test_skeleton.uru
|
43
|
+
- bin/urushiol
|
44
|
+
- urushiol.gemspec
|
45
|
+
- LICENSE
|
46
|
+
- README.md
|
47
|
+
homepage: http://www.tv4.se
|
48
|
+
licenses:
|
49
|
+
- MIT-LICENSE
|
50
|
+
metadata: {}
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 1.9.2
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 2.0.4
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: Test framework written in Ruby to test varnish-cache routing and caching
|
71
|
+
logic.
|
72
|
+
test_files: []
|
73
|
+
has_rdoc:
|