Skip to main content

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;
    }
}

Metadata

Release files for cacheunittest 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cacheunittest 1.3.0
File Size Uploaded
cacheunittest-1.3.0.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cacheunittest 1.3.0
File Interpreter ABI Platform
cacheunittest-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.8 kB

Release files / cacheunittest-1.3.0.tar.gz

Download URL cacheunittest-1.3.0.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
762ee979b1ec7a77bff900a8031d6c03b4a91c3556f5fca1abac6a21ad85d141
BLAKE2b-256 checksum
How to use checksums
fbf813fe56998d335b4737d14ffce17dc14b33ccdd6e1fccd894d9e48b15638c
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / cacheunittest-1.3.0-py3-none-any.whl

Download URL cacheunittest-1.3.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5490147ad9c659b0cde3d178df701d4f7761ff43efe4bf0408a10871e08ad462
BLAKE2b-256 checksum
How to use checksums
3df73503fb532d7ed1b5ae183cd38c6c9ede4f2f992073dd22f351f7d5d90270
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0.0

1 release file

0.1.14

1 release file

0.1.13

1 release file

0.1.12

1 release file

0.1.11

1 release file

0.1.10

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page