aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient/src/perl/test/Yahoo/Vespa/Mocks/ClusterControllerMock.pm
blob: fc46c7471a6bcebcef67d45892e4b27e01269957 (plain) (blame)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# 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 = <<EOS;
{
    "cluster" : {
        "books" : {
            "link" : "/cluster/v2/books"
        },
        "music" : {
            "link" : "/cluster/v2/music"
        }
    }
}
EOS
our $musicClusterJson = <<EOS;
{
  "state" : {
    "generated" : {
      "state" : "up",
      "reason" : ""
    }
  },
  "service" : {
    "distributor" : {
      "node" : {
        "0" : {
          "attributes" : { "hierarchical-group" : "top" },
          "state" : {
            "generated" : { "state" : "down", "reason" : "Setting it down" },
            "unit" : { "state" : "up", "reason" : "Now reporting state U" },
            "user" : { "state" : "down", "reason" : "Setting it down" }
          }
        },
        "1" : {
          "attributes" : { "hierarchical-group" : "top" },
          "state" : {
            "generated" : { "state" : "up", "reason" : "Setting it up" },
            "unit" : { "state" : "up", "reason" : "Now reporting state U" },
            "user" : { "state" : "up", "reason" : ""
            }
          }
        }
      }
    },
    "storage" : {
      "node" : {
        "0" : {
          "attributes" : { "hierarchical-group" : "top" },
          "state" : {
            "generated" : { "state" : "retired", "reason" : "Stop using" },
            "unit" : { "state" : "up", "reason" : "Now reporting state U" },
            "user" : { "state" : "retired", "reason" : "Stop using" }
          },
          "partition" : {
            "0" : {
              "metrics" : {
                "bucket-count" : 5,
                "unique-document-count" : 10,
                "unique-document-total-size" : 1000
              }
            }
          }
        },
        "1" : {
          "attributes" : { "hierarchical-group" : "top" },
          "state" : {
            "generated" : { "state" : "up", "reason" : "Setting it up" },
            "unit" : { "state" : "up", "reason" : "Now reporting state U" },
            "user" : { "state" : "up", "reason" : ""
            }
          },
          "partition" : {
            "0" : {
              "metrics" : {
                "bucket-count" : 50,
                "unique-document-count" : 100,
                "unique-document-total-size" : 10000
              }
            }
          }
        }
      }
    }
  }
}
EOS
our $booksClusterJson = <<EOS;
{
  "state" : {
    "generated" : {
      "state" : "up",
      "reason" : ""
    }
  },
  "service" : {
    "distributor" : {
      "node" : {
        "0" : {
          "attributes" : { "hierarchical-group" : "top.g1" },
          "state" : {
            "generated" : { "state" : "down", "reason" : "Setting it down" },
            "unit" : { "state" : "up", "reason" : "Now reporting state U" },
            "user" : { "state" : "down", "reason" : "Setting it down" }
          }
        },
        "1" : {
          "attributes" : { "hierarchical-group" : "top.g2" },
          "state" : {
            "generated" : { "state" : "up", "reason" : "Setting it up" },
            "unit" : { "state" : "up", "reason" : "Now reporting state U" },
            "user" : { "state" : "up", "reason" : ""
            }
          }
        }
      }
    },
    "storage" : {
      "node" : {
        "0" : {
          "attributes" : { "hierarchical-group" : "top.g1" },
          "state" : {
            "generated" : { "state" : "down", "reason" : "Not seen" },
            "unit" : { "state" : "down", "reason" : "Not in slobrok" },
            "user" : { "state" : "down", "reason" : "default" }
          }
        },
        "1" : {
          "attributes" : { "hierarchical-group" : "top.g2" },
          "state" : {
            "generated" : { "state" : "up", "reason" : "Setting it up" },
            "unit" : { "state" : "up", "reason" : "Now reporting state U" },
            "user" : { "state" : "up", "reason" : ""
            }
          }
        }
      }
    }
  }
}
EOS

return &init();

sub init {
    #print "Verifying that cluster list json is parsable.\n";
    my $json = Json::parse($clusterListJson);
    #print "Verifying that music json is parsable\n";
    $json = Json::parse($musicClusterJson);
    #print "Verifying that books json is parsable\n";
    $json = Json::parse($booksClusterJson);
    #print "All seems parsable.\n";
    return 1;
}

sub setClusterDown {
    $musicClusterJson =~ s/"up"/"down"/;
    $musicClusterJson =~ s/""/"Not enough nodes up"/;
    #print "Cluster state: $musicClusterJson\n";
    #print "Verifying that music json is parsable\n";
    my $json = Json::parse($musicClusterJson);
}

sub handleCCRequest { # (Type, Host, Port, Path, ParameterMap, Content, Headers)
    my ($type, $host, $port, $path, $params, $content, $headers) = @_;
    my %paramHash;
    if (defined $params) {
        %paramHash = @$params;
    }
    if ($forceInternalServerError) {
        printDebug "Forcing internal server error response\n";
        return (
            'code' => 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;
}