Skip to main content

Varnish integration unit test

Project description

cache-unit-test

Unit test Python library for testing cache systems like varnish

Based on the work of @onyxfish (https://gist.github.com/onyxfish)

  • cachetest.py : productive
  • wordpresscachetest.py : not usable yet
user@localhost> python3.3 exampletest 

Python test launcher

With the unittest python system, you may have a unittest launcher, and many tests on different folders

#!/usr/bin/env python

import sys, getopt
import json
import re
import random
import unittest
import argparse
import cacheunittest
from cacheunittest import CacheUnitTest
import unittest
import os
import sys


if __name__ == "__main__":

	os.environ['MOZ_HEADLESS'] = '1'

	argparse=argparse.ArgumentParser(description="Proxy directives tests app", add_help=True)
	argparse.add_argument("--host", default="127.0.0.1", help="define a target varnish host")
	argparse.add_argument("--httpport", type=int, default=80, help="override the default http port 80")
	argparse.add_argument("--httpsport", type=int, default=443, help="override the default https port 443")
	argparse.add_argument("--verbosity", type=int, default=2, help="verbosity level")
	argparse.add_argument("-p", "--path", default=".", help="code path")
	args = argparse.parse_args()

	verbosity = args.verbosity
	codePath = args.path

	cacheunittest.proxyhost = args.host
	cacheunittest.proxyhttpport  = args.httpport
	cacheunittest.proxyhttpsport = args.httpsport



	print("")
	print("")
	print("Discovering ", codePath, " for artetest*.py tests")
	print("")
	tests = unittest.TestSuite()
	loader = unittest.TestLoader()
	tests_auto = loader.discover(codePath, pattern="artetest*.py", top_level_dir=None)
	result = unittest.TextTestRunner(verbosity=verbosity).run(tests_auto)
	print(result)
	if result.wasSuccessful():
		print("")
		print("")
		print("")
		print("")
		print("    All tests were successfully passed")
		print("        GGGGGOOOOOOOODDDDDD JOB !!!!")
		print("")
		print("")
		print("")
		print("")
		sys.exit(0)
	else:
		print("")
		print("    OOps something wrong happened")
		print("")
		print("###########################")
		print("###########################")
		print("###########################")
		print("#     KEEP THE FAITH!     #")
		print("###########################")
		print("###########################")
		print("###########################")
		sys.exit(2)

cache unit test

Varnish configuration

The tests are only valid if the varnish configuration contains the following setup Based on:

It's probably a good idea to put some security around it via a test on client.ip for example

Example VCL (docs/example.vcl):

acl dev {
    # ACL we'll use later to allow purges
    "localhost";
    "127.0.0.1";
    "::1";
}

sub vcl_recv  {

	# To avoid any hack based on these headers
	unset req.http.x-cache;
    unset req.http.X-Back;

    if (req.url ~ "^/static/") {
        set req.http.X-Back = "STATIC";
    } else {
        set req.http.X-Back = "WORDPRESS";
    }

    if (req.method == "PURGE") {
        if (!client.ip ~ dev) {
            return(synth(405,"Not allowed."));
        }
        return (purge);
    }
}

sub vcl_hit {
	set req.http.x-cache = "HIT";
	if (obj.ttl <= 0 and obj.grace > 0s) {
		set req.http.x-cache = "HIT_GRACED";
	}
}

sub vcl_miss {
	set req.http.x-cache = "MISS";
}

sub vcl_pass {
	set req.http.x-cache = "PASS";
}

sub vcl_pipe {
	set req.http.x-cache = "PIPE";
}

sub vcl_synth {
    if (!client.ip ~ dev) {
        set resp.http.X-Back = "VARNISH";
        set resp.http.x-cache = "SYNTH";
    }
}

sub vcl_deliver {
    if (!client.ip ~ dev) {
        if (obj.hits > 0) {
            set resp.http.X-NumberOfHits = obj.hits;
        }
        set resp.http.X-Varnish-Server = server.identity;
        set resp.http.X-Back = req.http.X-Back;
        set resp.http.X-Cache = req.http.X-Cache;
    }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cacheunittest-1.3.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

cacheunittest-1.3.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file cacheunittest-1.3.0.tar.gz.

File metadata

  • Download URL: cacheunittest-1.3.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.7

File hashes

Hashes for cacheunittest-1.3.0.tar.gz
Algorithm Hash digest
SHA256 762ee979b1ec7a77bff900a8031d6c03b4a91c3556f5fca1abac6a21ad85d141
MD5 37dbd03e6f270d2ad69f5b20d7cf035c
BLAKE2b-256 fbf813fe56998d335b4737d14ffce17dc14b33ccdd6e1fccd894d9e48b15638c

See more details on using hashes here.

File details

Details for the file cacheunittest-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: cacheunittest-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.7

File hashes

Hashes for cacheunittest-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5490147ad9c659b0cde3d178df701d4f7761ff43efe4bf0408a10871e08ad462
MD5 2a9c7d787a3b3894a543c2e7ae148387
BLAKE2b-256 3df73503fb532d7ed1b5ae183cd38c6c9ede4f2f992073dd22f351f7d5d90270

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page