vtest "shard director error handling"

vinyl v1 -vcl {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new shard = directors.shard();
	new p = directors.shard_param();
	p.set(by=BLOB, key_blob=blob.decode(HEX, encoded=""));
    }

    sub vcl_recv {
	if (req.url == "/1") {
	    set req.backend_hint = shard.backend(
	      param=blob.decode(HEX, encoded=""));
	}
    }
} -start

logexpect l1 -v v1 -g raw -d 1 {
    expect 0 0    CLI       {^Rd vcl.load}
    expect 0 0    Error     {vmod_directors: shard p: shard_param.set.. by=BLOB but no or empty key_blob - using key 0}
} -start -wait

logexpect l2 -v v1 -g raw {
    expect * 1001 VCL_Error {vmod_directors: shard shard: .backend.key_blob. param invalid}
} -start

client c1 {
    txreq -url "/1"
    rxresp
    expect resp.status == 503
    expect_close
} -run


logexpect l2 -wait

vinyl v1 -errvcl {shard .associate param invalid} {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new shard = directors.shard();
	shard.associate(blob.decode(encoded=""));
    }
}

vinyl v1 -errvcl {missing key argument with by=KEY} {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new p = directors.shard_param();
	p.set(by=KEY);
    }
}

vinyl v1 -errvcl {invalid key argument -5 with by=KEY} {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new p = directors.shard_param();
	p.set(by=KEY, key=-5);
    }
}

vinyl v1 -errvcl {missing key_blob argument with by=BLOB} {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new p = directors.shard_param();
	p.set(by=BLOB);
    }
}

vinyl v1 -errvcl {key and key_blob arguments are invalid with by=URL} {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new p = directors.shard_param();
	p.set(by=URL, key=0);
    }
}

vinyl v1 -errvcl {key and key_blob arguments are invalid with by=HASH} {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new p = directors.shard_param();
	p.set(key=0);
    }
}

vinyl v1 -errvcl {invalid alt argument -1} {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new p = directors.shard_param();
	p.set(alt=-1);
    }
}

vinyl v1 -errvcl {invalid warmup argument -0.5} {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new p = directors.shard_param();
	p.set(warmup=-0.5);
    }
}

vinyl v1 -errvcl {invalid warmup argument 1.1} {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new p = directors.shard_param();
	p.set(warmup=1.1);
    }
}

vinyl v1 -errvcl {vmod_directors: shard shard: .backend(resolve=LAZY) with other parameters can only be used in backend/pipe context} {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new shard = directors.shard();
	new rr = directors.round_robin();
	rr.add_backend(shard.backend(resolve=LAZY, by=KEY));
    }
}

vinyl v1 -errvcl {vmod_directors: shard shard: .backend(resolve=NOW) cannot be used in vcl_init{}/vcl_fini{}} {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
	new shard = directors.shard();
	new rr = directors.round_robin();
	rr.add_backend(shard.backend(resolve=NOW));
    }
}

vinyl v1 -errvcl {vmod_directors: shard shard: None backend cannot be added}{
    import directors;

    backend dummy None;

    sub vcl_init {
	new shard = directors.shard();
	shard.add_backend(dummy);
    }
}

vinyl v1 -errvcl {vmod_directors: shard shard: .remove_backend(): either backend or ident are required}{
    import directors;

    backend dummy None;

    sub vcl_init {
	new shard = directors.shard();
	shard.remove_backend();
    }
}

vinyl v1 -errvcl "Not available in subroutine 'vcl_recv'" {
    import directors;
    import blob;

    backend dummy None;

    sub vcl_init {
        new shard = directors.shard();
	    new p = directors.shard_param();
	    p.set(by=BLOB, key_blob=blob.decode(HEX, encoded=""));
    }

    sub vcl_recv {
	    if (req.url == "/2") {
	        p.set(by=HASH);
	    }
    }
}
