File: integrationwrap.pl

package info (click to toggle)
gcli 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,596 kB
  • sloc: ansic: 26,273; sh: 601; makefile: 538; perl: 374; yacc: 261; lex: 59
file content (141 lines) | stat: -rwxr-xr-x 3,172 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env perl
#
# This is the test runner for integration tests.
# It reads a given file and based on it takes actions.
#
# This file is part of gcli.
use Cwd qw(realpath getcwd);
use File::Basename;
use Data::Dumper;

BEGIN { push(@INC, realpath(dirname($0))); }
use server;
use testparser;

# Get a value from the test props
if ($ARGV[0] eq "-g") {
	my %testprops = testparser::parsetest $ARGV[2];

	if (!defined($testprops{$ARGV[1]})) {
		die(sprintf "%s not defined by test %s", $ARGV[1], $ARGV[2]);
	}

	print $testprops{$ARGV[1]};
	exit 0;
}

# Otherwise just continue running the test
my %testprops = testparser::parsetest $ARGV[0];

# Build the actual response body
my @responses = ();

sub build_response {
	my ($status, $headers, $body) = @_;

	$status = '200 OK' if !defined($status);
	$headers = '' if !defined($headers);

	$body =~ s/\n$/\r\n/;
	$headers =~ s/\n$/\r\n/;

	return sprintf("HTTP/1.1 %s\r\n%s\r\n%s", $status, $headers, $body);
}

if (ref($testprops{'ServerResponseBody'}) eq "ARRAY") {
	my $n_reqs = scalar(@{$testprops{'ServerResponseBody'}});
	for (my $i = 0; $i < $n_reqs; ++$i) {
		my $status = $testprops{'ServerResponseStatus'}[$i];
		my $headers = $testprops{'ServerResponseHeaders'}[$i];
		my $body = $testprops{'ServerResponseBody'}[$i];
		my $rsp = build_response($status, $headers, $body);

		push(@responses, $rsp);
	}
} else {
	my $status = $testprops{'ServerResponseStatus'};
	my $headers = $testprops{'ServerResponseHeaders'};
	my $body = $testprops{'ServerResponseBody'};
	my $rsp = build_response($status, $headers, $body);

	push(@responses, $rsp);
}

my $srv = server::new(
	id => $testprops{'ID'},
	responses => \@responses,
);

my %results = $srv->run_gcli($testprops{'ClientArgs'});

my $check_id = 1;

sub ok {
	my ($result, $description) = @_;

	if ($result) {
		printf "ok %d - %s\n", $check_id++, $description;
	} else {
		printf "not ok %d - %s\n", $check_id++, $description;
	}
}

sub is {
	my ($have, $want, $description) = @_;

	if ($have eq $want) {
		printf "ok %d - %s\n", $check_id++, $description;
	} else {
		# Hacky way to eliminate newlines
		$have =~ s/\n/\\n/g;
		$want =~ s/\n/\\n/g;

		printf "not ok %d - %s\n", $check_id++, $description;
		printf "  ---\n";
		printf "  data:\n";
		printf "    got: \"%s\"\n", $have;
		printf "    expect: \"%s\"\n", $want;
		printf "  ...\n";
	}
}

print "TAP version 14\n";

# Client Verification
my @client_verify = (
	'VerifyClientExitCode',
	'VerifyClientOutput',
);

foreach (@client_verify) {
	if (defined($testprops{$_})) {
		ok(defined($results{$_}), "Client check: $_ not undefined");
		is($results{$_}, $testprops{$_}, "Client check: $_");
	}
}

# Server Verification
my @server_verify = (
	'VerifyRequestMethod',
	'VerifyRequestPath',
	'VerifyRequestVersion',
	'VerifyRequestHeaders',
	'VerifyRequestBody',
);

for (my $i = 0; $i < $#responses; ++$i) {
	my $rq = $srv->get_request($i);

	foreach (@server_verify) {
		if (defined($testprops{$_}[$i])) {
			ok(defined($rq->{$_}), "Server check: $_ not undefined");
			is($rq->{$_}, $testprops{$_}[$i], "Server check: $_");
		}
	}
}

$srv->kill;

printf "1..%d\n", $check_id - 1;

# kak: filetype=perl