# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package Yahoo::Vespa::Mocks::ClusterControllerMock; use strict; use warnings; use URI::Escape; use Yahoo::Vespa::ConsoleOutput; use Yahoo::Vespa::Mocks::HttpClientMock; use Yahoo::Vespa::Utils; BEGIN { use base 'Exporter'; our @EXPORT = qw( ); } our $forceInternalServerError = 0; # Register a handler in the Http Client mock registerHttpClientHandler(\&handleCCRequest); our $clusterListJson = < 500, 'status' => 'Internal Server Error (forced)' ); } if ($path eq "/cluster/v2/") { printDebug "Handling cluster list request\n"; return ( 'code' => 200, 'status' => 'OK', 'content' => $clusterListJson ); } if ($path eq "/cluster/v2/music/" && (exists $paramHash{'recursive'} && $paramHash{'recursive'} eq 'true')) { printDebug "Handling cluster music state request\n"; return ( 'code' => 200, 'status' => 'OK', 'content' => $musicClusterJson ); } if ($path eq "/cluster/v2/books/" && (exists $paramHash{'recursive'} && $paramHash{'recursive'} eq 'true')) { printDebug "Handling cluster books state request\n"; return ( 'code' => 200, 'status' => 'OK', 'content' => $booksClusterJson ); } if ($path =~ /^\/cluster\/v2\/(books|music)\/(storage|distributor)\/(\d+)$/) { my ($cluster, $service, $index) = ($1, $2, $3); my $json = Json::parse($content); my $state = $json->{'state'}->{'user'}->{'state'}; my $description = $json->{'state'}->{'user'}->{'reason'}; if (!defined $description && $state eq 'up') { $description = ""; } if ($state !~ /^(?:up|down|maintenance|retired)$/) { return ( 'code' => 500, 'status' => "Unknown state '$state' specified" ); } if (!defined $state || !defined $description) { return ( 'code' => 500, 'status' => "Invalid form data or failed parsing: '$content'" ); } printDebug "Handling set user state request $cluster/$service/$index"; return ( 'code' => 200, 'status' => "Set user state for $cluster/$service/$index to " . "'$state' with reason '$description'" ); } printDebug "Request to '$path' not matched. Params:\n"; foreach my $key (keys %paramHash) { printDebug " $key => '$paramHash{$key}'\n"; } return; }