aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient/src/perl/test/Yahoo/Vespa/Bin/SetNodeStateTest.pl
blob: adb8024e19b6ec7f974f6f6527268aa97803b088 (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
# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

use Test::More;
use strict;
use warnings;

BEGIN { use_ok( 'Yahoo::Vespa::Bin::SetNodeState' ); }
require_ok( 'Yahoo::Vespa::Bin::SetNodeState' );

use TestUtils::VespaTest;
use Yahoo::Vespa::Mocks::ClusterControllerMock;
use Yahoo::Vespa::Mocks::VespaModelMock;

# Set which application is called on assertRun / assertRunMatches calls
setApplication( \&setNodeState ); 

&testSimple();
&testSyntaxPage();
&testHelp();
&testDownState();
&testDownFailure();
&testDefaultMaintenanceFails();
&testForcedMaintenanceSucceeds();

done_testing();

exit(0);

sub testSimple {
    my $stdout = <<EOS;
Set user state for books/storage/0 to 'up' with reason ''
Set user state for music/distributor/0 to 'up' with reason ''
EOS
    assertRun("Default - Min arguments", "up", 0, $stdout, "");
}

sub testSyntaxPage {
    my $stdout = <<EOS;
EOS
    my $pat = qr/^Set the user state of a node.*Usage:.*SetNodeState.*Arguments:.*Options:.*--help.*/s;
    assertRunMatches("Syntax page", "--help", 1, $pat, qr/^$/);
}

sub testHelp {
    my $stdout = <<EOS;
Set the user state of a node. This will set the generated state to the user
state if the user state is "better" than the generated state that would have
been created if the user state was up. For instance, a node that is currently
in initializing state can be forced into down state, while a node that is
currently down can not be forced into retired state, but can be forced into
maintenance state.

Usage: SetNodeStateTest.pl [Options] <Wanted State> [Description]

Arguments:
 Wanted State : User state to set. This must be one of up, down, maintenance or
                retired.
 Description  : Give a reason for why you are altering the user state, which
                will show up in various admin tools. (Use double quotes to give
                a reason with whitespace in it)

Options:
 -h --help                : Show this help page.
 -v                       : Create more verbose output.
 -s                       : Create less verbose output.
 --show-hidden            : Also show hidden undocumented debug options.

Node selection options. By default, nodes running locally will be selected:
 -c --cluster             : Cluster name of cluster to query. If unspecified,
                            and vespa is installed on current node, information
                            will be attempted auto-extracted
 -f --force               : Force the execution of a dangerous command.
 -t --type                : Node type to query. This can either be 'storage' or
                            'distributor'. If not specified, the operation will
                            show state for all types.
 -i --index               : The node index to show state for. If not specified,
                            all nodes found running on this host will be shown.

Config retrieval options:
 --config-server          : Host name of config server to query
 --config-server-port     : Port to connect to config server on
 --config-request-timeout : Timeout of config request
EOS

    assertRun("Help text", "-h", 1, $stdout, "");
}

sub testDownState {
    my $stdout = <<EOS;
Set user state for books/storage/0 to 'down' with reason 'testing'
Set user state for music/distributor/0 to 'down' with reason 'testing'
EOS
    assertRun("Down state", "down testing", 0, $stdout, "");
}

sub testDownFailure {
    $Yahoo::Vespa::Mocks::ClusterControllerMock::forceInternalServerError = 1;

    my $stderr = <<EOS;
Failed to set node state for node books/storage/0: 500 Internal Server Error
(forced)
EOS

    assertRun("Down failure", "--nocolors down testing", 1, "", $stderr);

    $Yahoo::Vespa::Mocks::ClusterControllerMock::forceInternalServerError = 0;
}

sub testDefaultMaintenanceFails {
    my $stderr = <<EOS;
Setting the distributor to maintenance mode may have severe consequences for
feeding!
Please specify -t storage to only set the storage node to maintenance mode, or
-f to override this error.
EOS

    assertRun("Default maintenance fails", "--nocolors maintenance testing",
              1, "", $stderr);
}

sub testForcedMaintenanceSucceeds {
    my $stdout = <<EOS;
Set user state for books/storage/0 to 'maintenance' with reason 'testing'
Set user state for music/distributor/0 to 'maintenance' with reason 'testing'
EOS

    assertRun("Forced maintenance succeeds", "-f maintenance testing",
              0, $stdout, "");
}