Commit 9d62af99 authored by Geoff Simmons's avatar Geoff Simmons

Use a golden file to test VCL generation for Ingress rules.

parent 6217f87e
vcl 4.0;
import std;
import directors;
import re2;
backend notfound {
# 192.0.2.0/24 reserved for docs & examples (RFC5737).
.host = "192.0.2.255";
.port = "80";
}
backend coffee-svc_192_2e_0_2e_2_2e_4 {
.host = "192.0.2.4";
.port = "80";
}
backend coffee-svc_192_2e_0_2e_2_2e_5 {
.host = "192.0.2.5";
.port = "80";
}
backend tea-svc_192_2e_0_2e_2_2e_1 {
.host = "192.0.2.1";
.port = "80";
}
backend tea-svc_192_2e_0_2e_2_2e_2 {
.host = "192.0.2.2";
.port = "80";
}
backend tea-svc_192_2e_0_2e_2_2e_3 {
.host = "192.0.2.3";
.port = "80";
}
sub vcl_init {
new hosts = re2.set(posix_syntax=true, literal=true, anchor=both);
hosts.add("cafe.example.com");
hosts.compile();
new coffee-svc_director = directors.round_robin();
coffee-svc_director.add_backend(coffee-svc_192_2e_0_2e_2_2e_4);
coffee-svc_director.add_backend(coffee-svc_192_2e_0_2e_2_2e_5);
new tea-svc_director = directors.round_robin();
tea-svc_director.add_backend(tea-svc_192_2e_0_2e_2_2e_1);
tea-svc_director.add_backend(tea-svc_192_2e_0_2e_2_2e_2);
tea-svc_director.add_backend(tea-svc_192_2e_0_2e_2_2e_3);
new cafe_2e_example_2e_com_url = re2.set(posix_syntax=true, anchor=start);
cafe_2e_example_2e_com_url.add("/coffee",
backend=coffee-svc_director.backend());
cafe_2e_example_2e_com_url.add("/tea",
backend=tea-svc_director.backend());
cafe_2e_example_2e_com_url.compile();
}
sub set_backend {
set req.backend_hint = notfound;
if (hosts.match(req.http.Host)) {
if (hosts.nmatches() != 1) {
# Fail fast when the match was not unique.
return (fail);
}
if (0 != 0) {
#
}
elsif (hosts.which() == 1) {
if (cafe_2e_example_2e_com_url.match(req.url)) {
set req.backend_hint = cafe_2e_example_2e_com_url.backend(select=FIRST);
}
}
}
if (req.backend_hint == notfound) {
return (synth(404));
}
}
sub vcl_miss {
call set_backend;
}
sub vcl_pass {
call set_backend;
}
......@@ -30,6 +30,8 @@ package vcl
import (
"bytes"
"io/ioutil"
"path/filepath"
"testing"
)
......@@ -118,12 +120,25 @@ func TestTemplate(t *testing.T) {
if err := Tmpl.Execute(&buf, cafeSpec); err != nil {
t.Error("Execute():", err)
}
t.Log(string(buf.Bytes()))
goldpath := filepath.Join("testdata","ingressrule.golden")
gold, err := ioutil.ReadFile(goldpath)
if err != nil {
t.Fatalf("Error reading %s: %v", goldpath, err)
}
if !bytes.Equal(buf.Bytes(), gold) {
t.Errorf("Generated VCL for IngressSpec does not match gold "+
"file: %s", goldpath)
if testing.Verbose() {
t.Log("Generated VCL:", string(buf.Bytes()))
t.Log(goldpath, ":", string(gold))
}
}
}
func TestDeepHash(t *testing.T) {
t.Log("cafeSpec.DeepHash():", cafeSpec.DeepHash())
t.Log("cafeSpec2.DeepHash():", cafeSpec2.DeepHash())
t.Logf("cafeSpec.DeepHash(): %0x", cafeSpec.DeepHash())
t.Logf("cafeSpec2.DeepHash() %0x", cafeSpec2.DeepHash())
if cafeSpec.DeepHash() == cafeSpec2.DeepHash() {
t.Errorf("Distinct specs with different hashes: spec1=%+v "+
"spec2=%+v hash=%0x", cafeSpec, cafeSpec2,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment