Search code examples
varnishvarnish-vclvarnish-4

Varnish VTC include - override backend{} -


I am currently trying to add some unitests to my growing varnish config file.

i managed to initialize it, and i think i got the basics of varnishtest and vtc format.

i need to load my varnish.vcl - inside there, are a few backends. which have .host to some internal hosts, those are not resolveable from the CI machine.

how can i override a backend?

my idea is basically like: (api01 is defined in varnish.vcl with a internal dns.)

varnish v1 -vcl {
  # …some vcl to define backends… #
  include "${pwd}/varnish.vcl";
  backend api01 { .host = "127.0.0.1"; } 
} 
varnish v1 -start

it fails - that Backend host '"api_loadbalancer"' could not be resolved to an IP address

but it does not continue to define the backend above (with 127.0.0.1) - doing the backend before the include, results in a redefinition error.

what is the correct way to mock a backend?


Solution

  • We have split our VCL code into multiple files and then "include" all of them into a "main.vcl" which we then use to start Varnish. Here is a simplified example structure:

    main.vcl
    -- backends.vcl
    -- directors.vcl
    -- mainLogic.vcl
    

    This enables you to only include some of the vcl-files into your test case and allows you to specify the backends. For example if you want to use a real backend:

    varnish v1 -vcl {
      backend api01 { .host = "127.0.0.1"; } 
      include "${pwd}/mainLogic.vcl";
    } 
    

    or if you want to mock the backends (what we do):

    varnish v1 -vcl {
      backend api01 { 
        .host = "${s1_addr}";
        .port = "${s1_port}"; 
      } 
      include "${pwd}/mainLogic.vcl";
    }