varnishtest "Code coverage of mgt_main, (VCL compiler and RSTdump etc)"

shell "varnishd -b None -C 2> ${tmpdir}/_.c"

shell {
	varnishd -n ${tmpdir}/no_keep -C -b None 2> no_keep.c
	test -s no_keep.c && ! test -d no_keep || test -d no_keep/*/vgc.so.dSYM
}

shell {
	varnishd -n ${tmpdir}/keep -p debug=+vcl_keep -C -b None 2> keep.c
	test -s keep.c && test -d keep
}

shell -err -expect {VCL version declaration missing} {
	echo 'bad vcl' > ${tmpdir}/bad.vcl
	varnishd -f ${tmpdir}/bad.vcl -n ${tmpdir}
}

shell -err -expect {-x must be the first argument} "varnishd -d -x foo "
shell -err -expect {-V must be the first argument} "varnishd -d -V foo "

shell -err -expect {Too many arguments for -x} "varnishd -x foo bar"
shell -err -expect {Invalid -x argument} "varnishd -x foo "

# This one is tricky, the getopt message on stderr is not standardized.
shell -err -expect {option} "varnishd -A "

shell -err -expect {Usage: varnishd [options]} "varnishd -? "
shell -err -expect {Too many arguments} "varnishd foo "

shell "varnishd -x parameter > ${tmpdir}/_.param"
shell "varnishd -x vsl > ${tmpdir}/_.vsl"
shell "varnishd -x cli > ${tmpdir}/_.cli"
shell "varnishd -x builtin > ${tmpdir}/_.builtin"
shell "varnishd -x optstring > ${tmpdir}/_.optstring"
shell "varnishd --optstring > ${tmpdir}/_.optstring2"

shell -err -expect {-C needs either -b <backend> or -f <vcl_file>} {
	varnishd -C
}

shell -err -expect {Cannot open -S file} {
	varnishd -S ${tmpdir}/nonexistent -n ${tmpdir}/v0 -f ''
}

shell -err -expect {Cannot create secret-file in} {
	mkdir ${tmpdir}/is_a_dir ${tmpdir}/is_a_dir/_.secret
	varnishd -n ${tmpdir}/is_a_dir -d -a :0
}

shell -err -expect {Unknown jail method "xyz"} "varnishd -jxyz -f '' "

shell -err -expect {Too many arguments for -V} "varnishd -V -V"
shell -expect {Copyright (c) 2006} "varnishd -V"

shell -err -expect {Only one of -d or -F can be specified} "varnishd -d -F "
shell -err -expect {Only one of -b or -f can be specified} "varnishd -b a -f b "
shell -err -expect {-d makes no sense with -C} "varnishd -C -b None -d "
shell -err -expect {-F makes no sense with -C} "varnishd -C -b None -F "
shell -err -expect {Neither -b nor -f given} { varnishd -n ${tmpdir}/v0  }

# Test -I and -l arguments (former a00016)

shell -err -expect {Only one -I allowed} {
	touch foo bar
	varnishd -f '' -I foo -I bar -n ${tmpdir}/v0 -a :0
}

shell -err -expect {Error: -I file CLI command failed (104)} {
	echo "vcl.list" > foo
	echo "-foobar" >> foo
	echo "vcl.load" >> foo
	varnishd -f '' -I foo -n ${tmpdir}/v0 -a :0 -l 2m
}

shell -err -expect "Can't open non-existent" {
	varnishd -f '' -I non-existent -n ${tmpdir}/v0 -a :0
}


# Code coverage of mgt_main, (VCL compiler and RSTdump etc) (former a00017)

# Test -F mode with no VCL loaded

shell {echo ping > ${tmpdir}/I_file}
process p1 "exec varnishd -n ${tmpdir}/v1 -F -f '' -a :0 -l2m -I ${tmpdir}/I_file 2>&1" -start

process p1 -expect-text 0 1 {PONG}
process p1 -expect-text 0 1 {END of -I file processing}

process p1 -screen_dump

shell {
	(
	echo 'vcl 4.0;'
	echo 'backend default {'
	echo '  .host="${bad_backend}";'
	echo '}'
	) > ${tmpdir}/vcl
}

shell -expect {VCL compiled.} {
	varnishadm -n ${tmpdir}/v1 vcl.load vcl1 ${tmpdir}/vcl
}

shell -expect {VCL 'vcl1' now active} {
	varnishadm -n ${tmpdir}/v1 vcl.use vcl1
}

shell -expect {active   auto    warm         -    vcl1} {
	varnishadm -n ${tmpdir}/v1 vcl.list
}

shell {varnishadm -n ${tmpdir}/v1 start}

shell {varnishadm -n ${tmpdir}/v1 debug.listen_address}

shell -exit 1 -expect "Command failed with error code 500" {
	varnishadm -n ${tmpdir}/v1 quit
}

shell -exit 1 -expect "Command failed with error code 102" {
	varnishadm -n ${tmpdir}/v1 debug.panic.master -j
}

shell -exit 1 -expect "Command failed with error code 101" {
	varnishadm -n ${tmpdir}/v1 123
}

shell "varnishadm -n ${tmpdir}/v1 param.set cli_limit 128"
shell -expect "[response was truncated]" "varnishadm -n ${tmpdir}/v1 help"

process p1 -expect-exit 64 -stop -wait

# Test multiple -f options

shell {
	cat >${tmpdir}/ok1 <<-EOF
	vcl 4.0;
	backend ok1 {
	  .host="${bad_backend}";
	}
	EOF

	cat >${tmpdir}/ok2 <<-EOF
	vcl 4.0;
	backend ok2 {
	  .host="${bad_backend}";
	}
	EOF
}

varnish v2 -arg "-f ${tmpdir}/ok1" -arg "-f ${tmpdir}/ok2" -start
varnish v2 -cliexpect {available   auto    warm      0   boot0} "vcl.list"
varnish v2 -cliexpect {active      auto    warm      0   boot1} "vcl.list"
varnish v2 -stop -wait

# Test multiple -f options with a bad VCL

shell -err -expect {Cannot read -f file} {
	exec varnishd -n ${tmpdir}/v0 -F -a :0 -l2m -f ${tmpdir}/ok1 \
	    -f ${tmpdir}/ok2 -f ${tmpdir}/bad
}

shell -err -expect {Cannot read -f file} {
	exec varnishd -n ${tmpdir}/v0 -F -a :0 -l2m -f ${tmpdir}/ok1 \
	    -f ${tmpdir}/bad -f ${tmpdir}/ok2
}

# varnishd -spersistent is tested in p00000.vtc
