Skip to content

Instantly share code, notes, and snippets.

@wolstena
Created January 29, 2015 21:18
Show Gist options
  • Save wolstena/ef1934cd086f260e2c42 to your computer and use it in GitHub Desktop.
Save wolstena/ef1934cd086f260e2c42 to your computer and use it in GitHub Desktop.
Varnish integgration tests using varnishtest
varnishtest -v v_purge12.vtc
# or
python /usr/local/bin/vtctrans.py -v v_purge12.vtc
# Ansible managed: /home/wolstenp/ops/vagrant/music/roles/varnish_tests/files/tests/v_purge10.vtc modified on 2015-01-29 11:45:14 by wolstenp on vr3uxwolstenp
varnishtest "Test PURGE method"
server ssearchSEARCH03 {
rxreq
expect req.url == "/artists/artist/_search"
expect req.http.host == "search.internal.cbcr3.com"
txresp -hdr "X-Server-Name:searchSEARCH03"
rxreq
# Not implemented?
#expect req.method = PURGE
expect req.url == "/artists/artist/_search"
expect req.http.host == "search.internal.cbcr3.com"
txresp -hdr "X-Server-Name:searchSEARCH03"
rxreq
expect req.url == "/artists/artist/_search"
expect req.http.host == "search.internal.cbcr3.com"
txresp -hdr "X-Server-Name:searchSEARCH03"
rxreq
expect req.url == "/artists/artist/_search"
expect req.http.host == "search.internal.cbcr3.com"
txresp -hdr "X-Server-Name:searchSEARCH03"
} -start
varnish v1 -vcl+backend {
import ${vmod_directors};
backend searchSEARCH03 {
.host = "${ssearchSEARCH03_addr}"; .port = "${ssearchSEARCH03_port}";
}
acl purge {
"127.0.0.1";
"localhost";
}
sub vcl_init {## search_director
new search_director = directors.round_robin();
search_director.add_backend(searchSEARCH03);
}
sub vcl_recv {
if (req.method == "PURGE") {
# Check ACL
if (!client.ip ~ purge) {
return(synth(405, "PURGE Not Allowed"));
}
return(purge);
}
if (! req.http.host ) {
return(synth(404, "File or directory not found"));
}
elseif (req.http.host ~ "(?i)^search\." ) {
set req.backend_hint = search_director.backend();
set req.http.host = "search.internal.cbcr3.com";
}
else {
return(synth(404, "File or directory not found"));
}
}
sub vcl_backend_response {
set beresp.http.X-Server-Name = bereq.backend;
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
}
else
{
set resp.http.X-Cache = "MISS";
}
}
} -start
client c1 {
timeout 3
txreq -req "GET" -url "/artists/artist/_search" -hdr "Host: search.internal.cbcr3.com"
rxresp
expect resp.status == 200
expect resp.http.x-cache == MISS
txreq -req "PURGE" -url "/artists/artist/_search" -hdr "Host: search.internal.cbcr3.com"
rxresp
expect resp.status == 200
txreq -req "GET" -url "/artists/artist/_search" -hdr "Host: search.internal.cbcr3.com"
rxresp
expect resp.status == 200
expect resp.http.x-cache == MISS
txreq -req "GET" -url "/artists/artist/_search" -hdr "Host: search.internal.cbcr3.com"
rxresp
expect resp.status == 200
expect resp.http.x-cache == HIT
} -run
varnish v1 -expect client_req == 4
varnish v1 -expect s_pipe == 0
varnish v1 -expect s_pass == 0
varnish v1 -expect n_purges == 1
varnish v1 -expect n_obj_purged > 0
varnishtest "Test PURGE using BANNED method"
server ssearchSEARCH03 {
rxreq
expect req.url == "/artists/artist/_search"
expect req.http.host == "search.internal.cbcr3.com"
txresp -hdr "X-Server-Name:searchSEARCH03"
rxreq
# Not implemented?
#expect req.method = PURGE
expect req.url == "/artists/artist/_search"
expect req.http.host == "search.internal.cbcr3.com"
txresp -hdr "X-Server-Name:searchSEARCH03"
rxreq
#expect req.method = PURGE
expect req.url == "/artists/artist/_search"
expect req.http.host == "search.internal.cbcr3.com"
txresp -hdr "X-Server-Name:searchSEARCH03"
rxreq
expect req.url == "/artists/artist/_search"
expect req.http.host == "search.internal.cbcr3.com"
txresp -hdr "X-Server-Name:searchSEARCH03"
rxreq
expect req.url == "/artists/artist/_search"
expect req.http.host == "search.internal.cbcr3.com"
txresp -hdr "X-Server-Name:searchSEARCH03"
} -start
varnish v1 -vcl+backend {
import ${vmod_directors};
backend searchSEARCH03 {
.host = "${ssearchSEARCH03_addr}"; .port = "${ssearchSEARCH03_port}";
}
acl purge {
"127.0.0.1";
"localhost";
}
sub vcl_init {## search_director
new search_director = directors.round_robin();
search_director.add_backend(searchSEARCH03);
}
sub vcl_recv {
# Alternative to calling purge, using banned from hit and missed
# so that we know if object is hit or miss
if (req.method == "PURGE") {
# Check ACL
if (!client.ip ~ purge) {
return(synth(405, "PURGE Not Allowed"));
}
return(hash);
}
if (! req.http.host ) {
return(synth(404, "File or directory not found"));
}
elseif (req.http.host ~ "(?i)^search\." ) {
set req.backend_hint = search_director.backend();
set req.http.host = "search.internal.cbcr3.com";
}
else {
return(synth(404, "File or directory not found"));
}
}
sub vcl_backend_response {
set beresp.http.X-Server-Name = bereq.backend;
set beresp.http.x-url = bereq.url;
set beresp.http.x-host = bereq.http.host;
}
# Called if the cache has a copy of the page.
sub vcl_hit {
if (req.method == "PURGE") {
# Lurker friendly alternative
ban("obj.http.x-host ~ " + req.http.host + " && obj.http.x-url ~ " + req.url);
return(synth(200, "Purged from Cache"));
}
}
# Called if the cache does not have a copy of the page.
sub vcl_miss {
if (req.method == "PURGE") {
return(synth(205, "Not in cache"));
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
}
else
{
set resp.http.X-Cache = "MISS";
}
}
} -start
client c1 {
timeout 3
txreq -req "GET" -url "/artists/artist/_search" -hdr "Host: search.internal.cbcr3.com"
rxresp
expect resp.status == 200
txreq -req "PURGE" -url "/artists/artist/_search" -hdr "Host: search.internal.cbcr3.com"
rxresp
expect resp.status == 200
txreq -req "PURGE" -url "/artists/artist/_search" -hdr "Host: search.internal.cbcr3.com"
rxresp
# This would be a 205
expect resp.status == 205
txreq -req "GET" -url "/artists/artist/_search" -hdr "Host: search.internal.cbcr3.com"
rxresp
expect resp.status == 200
# This would be a MISS
expect resp.http.x-cache == MISS
txreq -req "GET" -url "/artists/artist/_search" -hdr "Host: search.internal.cbcr3.com"
rxresp
expect resp.status == 200
expect resp.http.x-cache == HIT
} -run
varnish v1 -expect client_req == 5
varnish v1 -expect s_pipe == 0
varnish v1 -expect s_pass == 0
varnish v1 -expect bans > 0
varnish v1 -expect bans_obj >= 0
varnish v1 -expect bans_req >= 0
varnish v1 -expect bans_completed > 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment