| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Test::APIcast::Blackbox; | 
| 2 | 1 |  |  | 1 |  | 13177 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 41 |  | 
| 3 | 1 |  |  | 1 |  | 7 | use warnings FATAL => 'all'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 63 |  | 
| 4 | 1 |  |  | 1 |  | 21 | use v5.10.1; | 
|  | 1 |  |  |  |  | 3 |  | 
| 5 | 1 |  |  | 1 |  | 691 | use JSON; | 
|  | 1 |  |  |  |  | 7449 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 172 | use Test::APIcast -Base; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 18 |  | 
| 8 | 1 |  |  | 1 |  | 11806 | use File::Copy "move"; | 
|  | 1 |  |  | 1 |  | 2 |  | 
|  | 1 |  |  | 1 |  | 30 |  | 
|  | 1 |  |  | 1 |  | 4 |  | 
|  | 1 |  |  | 1 |  | 1 |  | 
|  | 1 |  |  |  |  | 36 |  | 
|  | 1 |  |  |  |  | 5 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 15 |  | 
|  | 1 |  |  |  |  | 5 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 22 |  | 
|  | 1 |  |  |  |  | 540 |  | 
|  | 1 |  |  |  |  | 2218 |  | 
|  | 1 |  |  |  |  | 98 |  | 
| 9 | 1 |  |  | 1 |  | 7 | use File::Temp qw/ tempfile /; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 78 |  | 
| 10 | 1 |  |  | 1 |  | 5 | use File::Slurp qw(read_file); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 55 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | BEGIN { | 
| 13 | 1 |  |  | 1 |  | 13740 | $ENV{APICAST_OPENRESTY_BINARY} = $ENV{TEST_NGINX_BINARY}; | 
| 14 |  |  |  |  |  |  | } | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | our $ApicastBinary = $ENV{TEST_NGINX_APICAST_BINARY} || 'bin/apicast'; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | our %EnvToNginx = (); | 
| 19 |  |  |  |  |  |  | our %ResetEnv = (); | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | sub env_to_apicast (@) { | 
| 22 | 0 |  |  | 0 | 0 |  | my %env = (@_); | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | # merge two hashes, new %env takes precedence | 
| 25 | 0 |  |  |  |  |  | %EnvToNginx = (%EnvToNginx, %env); | 
| 26 |  |  |  |  |  |  | }; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | my $original_server_port_for_client; | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub set_server_port_for_client (@) { | 
| 31 | 0 |  |  | 0 | 0 |  | $original_server_port_for_client = $Test::Nginx::Util::ServerPortForClient; | 
| 32 | 0 |  |  |  |  |  | Test::Nginx::Util::server_port_for_client(shift); | 
| 33 | 0 | 0 |  |  |  |  | if ($Test::Nginx::Util::Verbose) { | 
| 34 | 0 |  |  |  |  |  | warn("changed ServerPortForClient from $original_server_port_for_client to $Test::Nginx::Util::ServerPortForClient"); | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | add_block_preprocessor(sub { | 
| 39 |  |  |  |  |  |  | my $block = shift; | 
| 40 |  |  |  |  |  |  | my $seq = $block->seq_num; | 
| 41 |  |  |  |  |  |  | my $name = $block->name; | 
| 42 |  |  |  |  |  |  | my $configuration = $block->configuration; | 
| 43 |  |  |  |  |  |  | my $backend = $block->backend; | 
| 44 |  |  |  |  |  |  | my $backend_name = $block->backend_name || 'test_backend'; | 
| 45 |  |  |  |  |  |  | my $upstream = $block->upstream; | 
| 46 |  |  |  |  |  |  | my $upstream_name = $block->upstream_name || 'test'; | 
| 47 |  |  |  |  |  |  | my $test = $block->test; | 
| 48 |  |  |  |  |  |  | my $sites_d = $block->sites_d || ''; | 
| 49 |  |  |  |  |  |  | my $ServerPort = $Test::Nginx::Util::ServerPort; | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | if (defined($test) && !defined($block->request) && !defined($block->raw_request) ) { | 
| 52 |  |  |  |  |  |  | my $test_port = Test::APIcast::get_random_port(); | 
| 53 |  |  |  |  |  |  | $sites_d .= <<_EOC_; | 
| 54 |  |  |  |  |  |  | server { | 
| 55 |  |  |  |  |  |  | listen $test_port; | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | server_name test default_server; | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | set \$apicast_port $ServerPort; | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | location / { | 
| 62 |  |  |  |  |  |  | $test | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | _EOC_ | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | set_server_port_for_client($test_port); | 
| 68 |  |  |  |  |  |  | $block->set_value('raw_request', "GET / HTTP/1.1\r\nHost: test\r\nConnection: close\r\n\r\n") | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | if (defined $backend) { | 
| 72 |  |  |  |  |  |  | $sites_d .= <<_EOC_; | 
| 73 |  |  |  |  |  |  | server { | 
| 74 |  |  |  |  |  |  | listen $ServerPort; | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | server_name $backend_name backend; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | $backend | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | upstream $backend_name { | 
| 82 |  |  |  |  |  |  | server 127.0.0.1:$ServerPort; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | _EOC_ | 
| 86 |  |  |  |  |  |  | $ENV{BACKEND_ENDPOINT_OVERRIDE} = "http://test_backend:$ServerPort"; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | if (defined $upstream) { | 
| 90 |  |  |  |  |  |  | $sites_d .= <<_EOC_; | 
| 91 |  |  |  |  |  |  | server { | 
| 92 |  |  |  |  |  |  | listen $ServerPort; | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | server_name $upstream_name; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | $upstream | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | upstream $upstream_name { | 
| 100 |  |  |  |  |  |  | server 127.0.0.1:$ServerPort; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  | _EOC_ | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | my $configuration_format = $block->configuration_format || 'json'; | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | if (defined $configuration) { | 
| 108 |  |  |  |  |  |  | $configuration = Test::Nginx::Util::expand_env_in_config($configuration); | 
| 109 |  |  |  |  |  |  | { | 
| 110 |  |  |  |  |  |  | local $SIG{__DIE__} = sub { | 
| 111 |  |  |  |  |  |  | Test::More::fail("$name - configuration block JSON") || Test::More::diag $_[0]; | 
| 112 |  |  |  |  |  |  | }; | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | if ($configuration_format eq 'json') { | 
| 115 |  |  |  |  |  |  | decode_json($configuration); | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  | $block->set_value("configuration", $configuration); | 
| 119 |  |  |  |  |  |  | $block->set_value("configuration_format", $configuration_format); | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | $block->set_value("config", "$name ($seq)"); | 
| 123 |  |  |  |  |  |  | $block->set_value('sites_d', $sites_d) | 
| 124 |  |  |  |  |  |  | }); | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | my $write_nginx_config = sub { | 
| 127 |  |  |  |  |  |  | my $block = shift; | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | my $FilterHttpConfig = $Test::Nginx::Util::FilterHttpConfig; | 
| 130 |  |  |  |  |  |  | my $ConfFile = $Test::Nginx::Util::ConfFile; | 
| 131 |  |  |  |  |  |  | my $Workers = $Test::Nginx::Util::Workers; | 
| 132 |  |  |  |  |  |  | my $MasterProcessEnabled = $Test::Nginx::Util::MasterProcessEnabled; | 
| 133 |  |  |  |  |  |  | my $DaemonEnabled = $Test::Nginx::Util::DaemonEnabled; | 
| 134 |  |  |  |  |  |  | my $err_log_file = $block->error_log_file || $Test::Nginx::Util::ErrLogFile; | 
| 135 |  |  |  |  |  |  | my $LogLevel = $Test::Nginx::Util::LogLevel; | 
| 136 |  |  |  |  |  |  | my $PidFile = $Test::Nginx::Util::PidFile; | 
| 137 |  |  |  |  |  |  | my $AccLogFile = $Test::Nginx::Util::AccLogFile; | 
| 138 |  |  |  |  |  |  | my $ServerPort = $Test::Nginx::Util::ServerPort; | 
| 139 |  |  |  |  |  |  | my $backend_port = Test::APIcast::get_random_port(); | 
| 140 |  |  |  |  |  |  | my $echo_port = Test::APIcast::get_random_port(); | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | my $management_server_name = $ENV{TEST_NGINX_MANAGEMENT_SERVER_NAME}; | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | my $management_port; | 
| 145 |  |  |  |  |  |  | if (defined $management_server_name) { | 
| 146 |  |  |  |  |  |  | $management_port = $ServerPort; | 
| 147 |  |  |  |  |  |  | $management_server_name = "'$management_server_name'" | 
| 148 |  |  |  |  |  |  | } else { | 
| 149 |  |  |  |  |  |  | $management_port = Test::APIcast::get_random_port(); | 
| 150 |  |  |  |  |  |  | $management_server_name = 'nil' | 
| 151 |  |  |  |  |  |  | } | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | my $environment = $block->environment; | 
| 154 |  |  |  |  |  |  | my @environments; | 
| 155 |  |  |  |  |  |  | my $environment_file = $block->environment_file; | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | if (defined $environment_file && ref $environment_file eq 'ARRAY') { | 
| 158 |  |  |  |  |  |  | @environments = @$environment_file; | 
| 159 |  |  |  |  |  |  | } elsif (defined($environment_file)) { | 
| 160 |  |  |  |  |  |  | @environments = $environment_file; | 
| 161 |  |  |  |  |  |  | } | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | unshift @environments, 'production'; | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | my $sites_d = $block->sites_d; | 
| 166 |  |  |  |  |  |  | my $apicast_cli = $block->apicast || $ApicastBinary; | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | my $configuration = $block->configuration; | 
| 169 |  |  |  |  |  |  | my $conf; | 
| 170 |  |  |  |  |  |  | my $configuration_file = $block->configuration_file; | 
| 171 |  |  |  |  |  |  | my $configuration_format = $block->configuration_format; | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | if (defined $configuration_file) { | 
| 174 |  |  |  |  |  |  | chomp($configuration_file); | 
| 175 |  |  |  |  |  |  | $configuration_file = "$configuration_file"; | 
| 176 |  |  |  |  |  |  | } else { | 
| 177 |  |  |  |  |  |  | if (defined $configuration) { | 
| 178 |  |  |  |  |  |  | ($conf, $configuration_file) = tempfile(SUFFIX => ".$configuration_format"); | 
| 179 |  |  |  |  |  |  | print $conf $configuration; | 
| 180 |  |  |  |  |  |  | close $conf; | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | $configuration_file = "$configuration_file"; | 
| 183 |  |  |  |  |  |  | } | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | my %env = (%EnvToNginx, $block->env); | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | # reset ENV to memorized state | 
| 189 |  |  |  |  |  |  | for my $key (keys %ResetEnv) { | 
| 190 |  |  |  |  |  |  | if (defined $ResetEnv{$key}) { | 
| 191 |  |  |  |  |  |  | $ENV{$key} = $ResetEnv{$key}; | 
| 192 |  |  |  |  |  |  | } else { | 
| 193 |  |  |  |  |  |  | delete $ENV{$key}; | 
| 194 |  |  |  |  |  |  | } | 
| 195 |  |  |  |  |  |  | delete $ResetEnv{$key}; | 
| 196 |  |  |  |  |  |  | } | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | for my $key (keys %env) { | 
| 199 |  |  |  |  |  |  | # memorize ENV before changing it | 
| 200 |  |  |  |  |  |  | $ResetEnv{$key} = $ENV{$key}; | 
| 201 |  |  |  |  |  |  | # change ENV to state desired by the test | 
| 202 |  |  |  |  |  |  | $ENV{$key} = $env{$key}; | 
| 203 |  |  |  |  |  |  | } | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | my ($env, $env_file) = tempfile(); | 
| 206 |  |  |  |  |  |  | push @environments, $env_file; | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | my $apicast_cmd = "APICAST_CONFIGURATION_LOADER='test' $apicast_cli start --test"; | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | if (defined $configuration_file && $configuration_file) { | 
| 211 |  |  |  |  |  |  | $apicast_cmd .= " --configuration $configuration_file" | 
| 212 |  |  |  |  |  |  | } else { | 
| 213 |  |  |  |  |  |  | $configuration_file = ""; | 
| 214 |  |  |  |  |  |  | } | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | foreach my $ef (@environments) { | 
| 217 |  |  |  |  |  |  | $apicast_cmd .= " --environment $ef" | 
| 218 |  |  |  |  |  |  | } | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | if (defined $environment) { | 
| 221 |  |  |  |  |  |  | print $env $environment; | 
| 222 |  |  |  |  |  |  | } else { | 
| 223 |  |  |  |  |  |  | print $env <<_EOC_; | 
| 224 |  |  |  |  |  |  | return { | 
| 225 |  |  |  |  |  |  | worker_processes = '$Workers', | 
| 226 |  |  |  |  |  |  | master_process = '$MasterProcessEnabled', | 
| 227 |  |  |  |  |  |  | daemon = '$DaemonEnabled', | 
| 228 |  |  |  |  |  |  | error_log = '$err_log_file', | 
| 229 |  |  |  |  |  |  | timer_resolution = false, | 
| 230 |  |  |  |  |  |  | log_level = '$LogLevel', | 
| 231 |  |  |  |  |  |  | pid = '$PidFile', | 
| 232 |  |  |  |  |  |  | lua_code_cache = 'on', | 
| 233 |  |  |  |  |  |  | access_log = '$AccLogFile', | 
| 234 |  |  |  |  |  |  | port = { | 
| 235 |  |  |  |  |  |  | apicast = '$ServerPort', | 
| 236 |  |  |  |  |  |  | management = '$management_port', | 
| 237 |  |  |  |  |  |  | backend = '$backend_port', | 
| 238 |  |  |  |  |  |  | echo = '$echo_port', | 
| 239 |  |  |  |  |  |  | metrics = '$ServerPort', | 
| 240 |  |  |  |  |  |  | }, | 
| 241 |  |  |  |  |  |  | env = { | 
| 242 |  |  |  |  |  |  | THREESCALE_CONFIG_FILE = [[$configuration_file]], | 
| 243 |  |  |  |  |  |  | APICAST_CONFIGURATION_LOADER = 'boot' | 
| 244 |  |  |  |  |  |  | }, | 
| 245 |  |  |  |  |  |  | server_name = { | 
| 246 |  |  |  |  |  |  | management = $management_server_name | 
| 247 |  |  |  |  |  |  | }, | 
| 248 |  |  |  |  |  |  | sites_d = [============================[$sites_d]============================], | 
| 249 |  |  |  |  |  |  | } | 
| 250 |  |  |  |  |  |  | _EOC_ | 
| 251 |  |  |  |  |  |  | } | 
| 252 |  |  |  |  |  |  | close $env; | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | if ($ENV{DEBUG}) { | 
| 255 |  |  |  |  |  |  | warn $apicast_cmd; | 
| 256 |  |  |  |  |  |  | } | 
| 257 |  |  |  |  |  |  |  | 
| 258 |  |  |  |  |  |  | my $apicast = `${apicast_cmd} 2>&1`; | 
| 259 |  |  |  |  |  |  | if ($apicast =~ /configuration file (?.+?) test is successful/) | 
| 260 |  |  |  |  |  |  | { | 
| 261 |  |  |  |  |  |  | open(my $fh, '+>', $ConfFile) or die "cannot open $ConfFile: $!"; | 
| 262 |  |  |  |  |  |  |  | 
| 263 | 1 |  |  | 1 |  | 513 | my $nginx_config = read_file($+{file}); | 
|  | 1 |  |  |  |  | 538 |  | 
|  | 1 |  |  |  |  | 206 |  | 
| 264 |  |  |  |  |  |  |  | 
| 265 |  |  |  |  |  |  | if ($FilterHttpConfig) { | 
| 266 |  |  |  |  |  |  | $nginx_config = $FilterHttpConfig->($nginx_config); | 
| 267 |  |  |  |  |  |  | } | 
| 268 |  |  |  |  |  |  |  | 
| 269 |  |  |  |  |  |  | print { $fh } $nginx_config; | 
| 270 |  |  |  |  |  |  | close($fh); | 
| 271 |  |  |  |  |  |  | } else { | 
| 272 |  |  |  |  |  |  | warn "Missing config file: $Test::Nginx::Util::ConfFile"; | 
| 273 |  |  |  |  |  |  | warn $apicast; | 
| 274 |  |  |  |  |  |  | } | 
| 275 |  |  |  |  |  |  |  | 
| 276 |  |  |  |  |  |  | if ($PidFile && -f $PidFile) { | 
| 277 |  |  |  |  |  |  | unlink $PidFile or warn "Couldn't remove $PidFile.\n"; | 
| 278 |  |  |  |  |  |  | } | 
| 279 |  |  |  |  |  |  |  | 
| 280 |  |  |  |  |  |  | $ENV{APICAST_LOADED_ENVIRONMENTS} = join('|',@environments); | 
| 281 |  |  |  |  |  |  | }; | 
| 282 |  |  |  |  |  |  |  | 
| 283 |  |  |  |  |  |  | add_block_preprocessor(sub { | 
| 284 |  |  |  |  |  |  | if (defined $original_server_port_for_client) { | 
| 285 |  |  |  |  |  |  | Test::Nginx::Util::server_port_for_client($original_server_port_for_client); | 
| 286 |  |  |  |  |  |  | undef $original_server_port_for_client; | 
| 287 |  |  |  |  |  |  | } | 
| 288 |  |  |  |  |  |  | }); | 
| 289 |  |  |  |  |  |  |  | 
| 290 | 0 |  |  |  |  |  | BEGIN { | 
| 291 | 1 |  |  | 1 |  | 7 | no warnings 'redefine'; | 
|  | 1 |  |  | 0 |  | 1 |  | 
|  | 1 |  |  |  |  | 113 |  | 
| 292 |  |  |  |  |  |  |  | 
| 293 |  |  |  |  |  |  | sub Test::Nginx::Util::write_config_file ($$) { | 
| 294 | 0 |  |  | 0 |  |  | my $block = shift; | 
| 295 | 0 |  |  |  |  |  | $write_nginx_config->($block); | 
| 296 |  |  |  |  |  |  |  | 
| 297 | 0 |  |  |  |  |  | Test::APIcast::close_random_ports(); | 
| 298 |  |  |  |  |  |  | } | 
| 299 |  |  |  |  |  |  | } | 
| 300 |  |  |  |  |  |  |  | 
| 301 |  |  |  |  |  |  | our @EXPORT = qw( | 
| 302 |  |  |  |  |  |  | env_to_apicast | 
| 303 |  |  |  |  |  |  | ); | 
| 304 |  |  |  |  |  |  |  | 
| 305 |  |  |  |  |  |  | 1; |