Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
buth committed Jun 1, 2015
1 parent 9b7e9b6 commit f4bd160
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 49 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.git
test-nginx
Makefile
test.rb
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ RUN apt-get update \

COPY varnish.vcl /etc/varnish.vcl

CMD ["varnishd","-f","/etc/varnish.vcl"]

CMD ["varnishd", "-F", "-f", "/etc/varnish.vcl"]
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test:
ruby test.rb


test-up:
cd test-nginx && docker build -t docker-varnish-test-nginx . && docker run -d --name docker-varnish-test-nginx docker-varnish-test-nginx
docker build -t docker-varnish-test . && docker run -d --name docker-varnish-test --link docker-varnish-test-nginx:backend -p 8080:80 docker-varnish-test

test-destroy:
docker kill docker-varnish-test docker-varnish-test-nginx || true
docker rm docker-varnish-test docker-varnish-test-nginx || true
docker rmi docker-varnish-test docker-varnish-test-nginx || true

clean: test-destroy

.PHONY: test test-up test-destroy
2 changes: 2 additions & 0 deletions test-nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
46 changes: 46 additions & 0 deletions test-nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
user nginx;
worker_processes auto;

error_log /dev/stdout info;

events {
worker_connections 1024;
multi_accept on;
use epoll;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

server_tokens off;
access_log off;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
chunked_transfer_encoding on;

keepalive_timeout 30;

client_max_body_size 2m;

server {
listen 80 default_server;
server_name _;

location /cache/1s {
add_header "cache-control" "max-age=1";
return 200 'ok';
}

location /cache/forever {
add_header "cache-control" "max-age=31536000";
return 200 'ok';
}
}
}
44 changes: 44 additions & 0 deletions test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@


require 'open-uri'


base = "http://#{`boot2docker ip`.chomp}:8080"





# Test 1 second TTL.

open("#{base}/cache/1s") do |f|
raise "wrong max-age header \"#{f.meta['cache-control']}\"" if f.meta['cache-control'] != 'max-age=1'
xvarnish = f.meta['x-varnish'].split(' ')
raise 'varnish cache miss on cached content' if xvarnish.length != 1
end

sleep(1)

open("#{base}/cache/1s") do |f|
raise "wrong max-age header \"#{f.meta['cache-control']}\"" if f.meta['cache-control'] != 'max-age=1'
xvarnish = f.meta['x-varnish'].split(' ')
raise 'varnish cache hit on content that should have expired' if xvarnish.length != 2
end


# Test max TTL.

open("#{base}/cache/forever") do |f|
raise "wrong max-age header \"#{f.meta['cache-control']}\"" if f.meta['cache-control'] != 'max-age=1'
xvarnish = f.meta['x-varnish'].split(' ')
raise 'varnish cache miss on cached content' if xvarnish.length != 1
end

sleep(1)

open("#{base}/cache/forever") do |f|
raise "wrong max-age header \"#{f.meta['cache-control']}\"" if f.meta['cache-control'] != 'max-age=1'
xvarnish = f.meta['x-varnish'].split(' ')
raise 'varnish cache hit on content that should have expired' if xvarnish.length != 2
end

99 changes: 52 additions & 47 deletions varnish.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,34 @@ vcl 4.0;
# Client side

sub vcl_recv {
if (req.method == "PRI") {
/* We do not support SPDY or HTTP/2.0 */
return (synth(405));
}
if (req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}

if (req.method != "GET" && req.method != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
return (hash);
if (req.method == "PRI") {
/* We do not support SPDY or HTTP/2.0 */
return (synth(405));
}

if (req.method != "GET" &&
req.method != "HEAD" &&
req.method != "PUT" &&
req.method != "POST" &&
req.method != "TRACE" &&
req.method != "OPTIONS" &&
req.method != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}

if (req.method != "GET" && req.method != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}

if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}

return (hash);
}

sub vcl_pipe {
Expand Down Expand Up @@ -148,23 +152,32 @@ sub vcl_synth {
# Backend Fetch

sub vcl_backend_fetch {
return (fetch);
return (fetch);
}

# CUSTOM
sub vcl_backend_response {
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Surrogate-control ~ "no-store" ||
(!beresp.http.Surrogate-Control &&
beresp.http.Cache-Control ~ "no-cache|no-store|private") ||
beresp.http.Vary == "*") {
/*
* Mark as "Hit-For-Pass" for the next 2 minutes
*/
set beresp.ttl = 120s;
set beresp.uncacheable = true;
}
return (deliver);

# Handle TTLs that are two high.
if (beresp.ttl > 1s) {
set beresp.ttl = 1s;
set beresp.http.cache-control = "max-age=1";
}

# Handle uncacheable responses.
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Surrogate-control ~ "no-store" ||
(!beresp.http.Surrogate-Control &&
beresp.http.Cache-Control ~ "no-cache|no-store|private") ||
beresp.http.Vary == "*") {
/*
* Mark as "Hit-For-Pass" for the next 2 minutes
*/
set beresp.ttl = 120s;
set beresp.uncacheable = true;
}
return (deliver);
}

sub vcl_backend_error {
Expand Down Expand Up @@ -200,15 +213,7 @@ sub vcl_fini {
}

backend ap1 {
.host = "127.0.0.1";
.port = "8080";
.host = "backend";
.port = "80";
.first_byte_timeout = 200s;
.probe = {
.url = "/";
.interval = 5s;
.timeout = 1 s;
.window = 5;
.threshold = 3;
.initial = 1;
}
}

0 comments on commit f4bd160

Please sign in to comment.