@@ -51,3 +51,117 @@ def test_get_nodes_status(self):
51
51
result = Cluster (mock_conn ).get_nodes_status ()
52
52
self .assertListEqual (result , expected_resp .get ("nodes" ))
53
53
mock_conn .get .assert_called_with (path = "/nodes" )
54
+
55
+ def test_get_cluster_statistics (self ):
56
+ # error messages
57
+ unexpected_err_msg = "Cluster statistics"
58
+ empty_response_err_msg = "Cluster statistics response returned empty"
59
+ connection_err_msg = "Get cluster statistics failed due to connection error"
60
+
61
+ # expected failure
62
+ mock_conn = mock_connection_func ("get" , status_code = 500 )
63
+ with self .assertRaises (UnexpectedStatusCodeException ) as error :
64
+ Cluster (mock_conn ).get_cluster_statistics ()
65
+ check_startswith_error_message (self , error , unexpected_err_msg )
66
+
67
+ mock_conn = mock_connection_func ("get" , status_code = 200 , return_json = None )
68
+ with self .assertRaises (EmptyResponseException ) as error :
69
+ Cluster (mock_conn ).get_cluster_statistics ()
70
+ check_error_message (self , error , empty_response_err_msg )
71
+
72
+ mock_conn = mock_connection_func ("get" , side_effect = RequestsConnectionError )
73
+ with self .assertRaises (RequestsConnectionError ) as error :
74
+ Cluster (mock_conn ).get_cluster_statistics ()
75
+ check_error_message (self , error , connection_err_msg )
76
+
77
+ # expected success
78
+ expected_resp = {
79
+ "statistics" : [
80
+ {
81
+ "candidates" : {
82
+ "weaviate-0" : "10.244.2.3:8300" ,
83
+ "weaviate-1" : "10.244.1.3:8300" ,
84
+ },
85
+ "dbLoaded" : True ,
86
+ "isVoter" : True ,
87
+ "leaderAddress" : "10.244.3.3:8300" ,
88
+ "leaderId" : "weaviate-2" ,
89
+ "name" : "weaviate-0" ,
90
+ "open_" : True ,
91
+ "raft" : {
92
+ "appliedIndex" : "3" ,
93
+ "commitIndex" : "3" ,
94
+ "fsmPending" : "0" ,
95
+ "lastContact" : "29.130625ms" ,
96
+ "lastLogIndex" : "3" ,
97
+ "lastLogTerm" : "2" ,
98
+ "lastSnapshotIndex" : "0" ,
99
+ "lastSnapshotTerm" : "0" ,
100
+ "latestConfiguration" : [
101
+ {"address" : "10.244.1.3:8300" , "id_" : "weaviate-1" , "suffrage" : 0 },
102
+ {"address" : "10.244.3.3:8300" , "id_" : "weaviate-2" , "suffrage" : 0 },
103
+ {"address" : "10.244.2.3:8300" , "id_" : "weaviate-0" , "suffrage" : 0 },
104
+ ],
105
+ "latestConfigurationIndex" : "0" ,
106
+ "numPeers" : "2" ,
107
+ "protocolVersion" : "3" ,
108
+ "protocolVersionMax" : "3" ,
109
+ "protocolVersionMin" : "0" ,
110
+ "snapshotVersionMax" : "1" ,
111
+ "snapshotVersionMin" : "0" ,
112
+ "state" : "Follower" ,
113
+ "term" : "2" ,
114
+ },
115
+ "ready" : True ,
116
+ "status" : "HEALTHY" ,
117
+ },
118
+ {
119
+ "bootstrapped" : True ,
120
+ "candidates" : {},
121
+ "dbLoaded" : True ,
122
+ "isVoter" : True ,
123
+ "leaderAddress" : "10.244.3.3:8300" ,
124
+ "leaderId" : "weaviate-2" ,
125
+ "name" : "weaviate-1" ,
126
+ "open_" : True ,
127
+ "raft" : {
128
+ "appliedIndex" : "3" ,
129
+ "commitIndex" : "3" ,
130
+ "fsmPending" : "0" ,
131
+ "lastContact" : "41.289833ms" ,
132
+ "lastLogIndex" : "3" ,
133
+ "lastLogTerm" : "2" ,
134
+ "lastSnapshotIndex" : "0" ,
135
+ "lastSnapshotTerm" : "0" ,
136
+ "latestConfiguration" : [
137
+ {"address" : "10.244.1.3:8300" , "id_" : "weaviate-1" , "suffrage" : 0 },
138
+ {"address" : "10.244.3.3:8300" , "id_" : "weaviate-2" , "suffrage" : 0 },
139
+ {"address" : "10.244.2.3:8300" , "id_" : "weaviate-0" , "suffrage" : 0 },
140
+ ],
141
+ "latestConfigurationIndex" : "0" ,
142
+ "numPeers" : "2" ,
143
+ "protocolVersion" : "3" ,
144
+ "protocolVersionMax" : "3" ,
145
+ "protocolVersionMin" : "0" ,
146
+ "snapshotVersionMax" : "1" ,
147
+ "snapshotVersionMin" : "0" ,
148
+ "state" : "Follower" ,
149
+ "term" : "2" ,
150
+ },
151
+ "ready" : True ,
152
+ "status" : "HEALTHY" ,
153
+ },
154
+ ],
155
+ "synchronized" : True ,
156
+ }
157
+ mock_conn = mock_connection_func ("get" , status_code = 200 , return_json = expected_resp )
158
+ result = Cluster (mock_conn ).get_cluster_statistics ()
159
+
160
+ # Convert the response to match our type definitions with renamed fields
161
+ for node in result ["statistics" ]:
162
+ node ["open_" ] = node .pop ("open_" )
163
+ for peer in node ["raft" ]["latestConfiguration" ]:
164
+ peer ["id_" ] = peer .pop ("id_" )
165
+
166
+ self .assertEqual (result , expected_resp )
167
+ mock_conn .get .assert_called_with (path = "/cluster/statistics" )
0 commit comments