|
| 1 | +package org.opensearch.ml.engine.tools; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.any; |
| 4 | +import static org.mockito.ArgumentMatchers.isA; |
| 5 | +import static org.mockito.Mockito.doAnswer; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Iterator; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.Test; |
| 18 | +import org.mockito.ArgumentCaptor; |
| 19 | +import org.mockito.Mock; |
| 20 | +import org.mockito.MockitoAnnotations; |
| 21 | +import org.opensearch.Version; |
| 22 | +import org.opensearch.action.admin.cluster.health.ClusterHealthRequest; |
| 23 | +import org.opensearch.action.admin.cluster.health.ClusterHealthResponse; |
| 24 | +import org.opensearch.action.admin.cluster.state.ClusterStateRequest; |
| 25 | +import org.opensearch.action.admin.cluster.state.ClusterStateResponse; |
| 26 | +import org.opensearch.action.admin.indices.settings.get.GetSettingsRequest; |
| 27 | +import org.opensearch.action.admin.indices.settings.get.GetSettingsResponse; |
| 28 | +import org.opensearch.action.admin.indices.stats.CommonStats; |
| 29 | +import org.opensearch.action.admin.indices.stats.IndexStats; |
| 30 | +import org.opensearch.action.admin.indices.stats.IndicesStatsRequest; |
| 31 | +import org.opensearch.action.admin.indices.stats.IndicesStatsResponse; |
| 32 | +import org.opensearch.cluster.ClusterState; |
| 33 | +import org.opensearch.cluster.health.ClusterIndexHealth; |
| 34 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 35 | +import org.opensearch.cluster.metadata.Metadata; |
| 36 | +import org.opensearch.cluster.routing.IndexRoutingTable; |
| 37 | +import org.opensearch.cluster.routing.IndexShardRoutingTable; |
| 38 | +import org.opensearch.cluster.service.ClusterService; |
| 39 | +import org.opensearch.common.UUIDs; |
| 40 | +import org.opensearch.common.settings.Settings; |
| 41 | +import org.opensearch.core.action.ActionListener; |
| 42 | +import org.opensearch.core.common.unit.ByteSizeValue; |
| 43 | +import org.opensearch.core.index.Index; |
| 44 | +import org.opensearch.index.shard.DocsStats; |
| 45 | +import org.opensearch.index.store.StoreStats; |
| 46 | +import org.opensearch.ml.common.spi.tools.Tool; |
| 47 | +import org.opensearch.transport.client.AdminClient; |
| 48 | +import org.opensearch.transport.client.Client; |
| 49 | +import org.opensearch.transport.client.ClusterAdminClient; |
| 50 | +import org.opensearch.transport.client.IndicesAdminClient; |
| 51 | + |
| 52 | +import com.google.common.collect.ImmutableMap; |
| 53 | + |
| 54 | +public class ListIndexToolTests { |
| 55 | + @Mock |
| 56 | + private Client client; |
| 57 | + @Mock |
| 58 | + private AdminClient adminClient; |
| 59 | + @Mock |
| 60 | + private IndicesAdminClient indicesAdminClient; |
| 61 | + @Mock |
| 62 | + private ClusterAdminClient clusterAdminClient; |
| 63 | + @Mock |
| 64 | + private ClusterService clusterService; |
| 65 | + @Mock |
| 66 | + private ClusterState clusterState; |
| 67 | + @Mock |
| 68 | + private Metadata metadata; |
| 69 | + @Mock |
| 70 | + private IndexMetadata indexMetadata; |
| 71 | + @Mock |
| 72 | + private IndexRoutingTable indexRoutingTable; |
| 73 | + @Mock |
| 74 | + private Index index; |
| 75 | + |
| 76 | + @Before |
| 77 | + public void setup() { |
| 78 | + MockitoAnnotations.openMocks(this); |
| 79 | + |
| 80 | + when(adminClient.indices()).thenReturn(indicesAdminClient); |
| 81 | + when(adminClient.cluster()).thenReturn(clusterAdminClient); |
| 82 | + when(client.admin()).thenReturn(adminClient); |
| 83 | + |
| 84 | + when(indexMetadata.getState()).thenReturn(IndexMetadata.State.OPEN); |
| 85 | + when(indexMetadata.getCreationVersion()).thenReturn(Version.CURRENT); |
| 86 | + |
| 87 | + when(metadata.index(any(String.class))).thenReturn(indexMetadata); |
| 88 | + when(indexMetadata.getIndex()).thenReturn(index); |
| 89 | + when(indexMetadata.getIndexUUID()).thenReturn(UUIDs.base64UUID()); |
| 90 | + when(index.getName()).thenReturn("index-1"); |
| 91 | + when(clusterState.metadata()).thenReturn(metadata); |
| 92 | + when(clusterState.getMetadata()).thenReturn(metadata); |
| 93 | + when(clusterService.state()).thenReturn(clusterState); |
| 94 | + |
| 95 | + ListIndexTool.Factory.getInstance().init(client, clusterService); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void test_getType() { |
| 100 | + Tool tool = ListIndexTool.Factory.getInstance().create(Collections.emptyMap()); |
| 101 | + assert (tool.getType().equals("ListIndexTool")); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void test_run_successful_1() { |
| 106 | + mockUp(); |
| 107 | + Tool tool = ListIndexTool.Factory.getInstance().create(Collections.emptyMap()); |
| 108 | + verifyResult(tool, createParameters("[\"index-1\"]", "true", "10", "true")); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void test_run_successful_2() { |
| 113 | + mockUp(); |
| 114 | + Tool tool = ListIndexTool.Factory.getInstance().create(Collections.emptyMap()); |
| 115 | + verifyResult(tool, createParameters(null, null, null, null)); |
| 116 | + } |
| 117 | + |
| 118 | + private Map<String, String> createParameters(String indices, String local, String pageSize, String includeUnloadedSegments) { |
| 119 | + Map<String, String> parameters = new HashMap<>(); |
| 120 | + if (indices != null) { |
| 121 | + parameters.put("indices", indices); |
| 122 | + } |
| 123 | + if (local != null) { |
| 124 | + parameters.put("local", local); |
| 125 | + } |
| 126 | + if (pageSize != null) { |
| 127 | + parameters.put("page_size", pageSize); |
| 128 | + } |
| 129 | + if (includeUnloadedSegments != null) { |
| 130 | + parameters.put("include_unloaded_segments", includeUnloadedSegments); |
| 131 | + } |
| 132 | + return parameters; |
| 133 | + } |
| 134 | + |
| 135 | + private void verifyResult(Tool tool, Map<String, String> parameters) { |
| 136 | + ActionListener<String> listener = mock(ActionListener.class); |
| 137 | + ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
| 138 | + tool.run(parameters, listener); |
| 139 | + verify(listener).onResponse(captor.capture()); |
| 140 | + System.out.println(captor.getValue()); |
| 141 | + assert captor.getValue().contains("1,red,open,index-1"); |
| 142 | + assert captor.getValue().contains("5,1,100,10,100kb,100kb"); |
| 143 | + } |
| 144 | + |
| 145 | + private void mockUp() { |
| 146 | + doAnswer(invocation -> { |
| 147 | + ActionListener<GetSettingsResponse> actionListener = invocation.getArgument(1); |
| 148 | + GetSettingsResponse response = mock(GetSettingsResponse.class); |
| 149 | + Map<String, Settings> indexToSettings = new HashMap<>(); |
| 150 | + indexToSettings.put("index-1", Settings.EMPTY); |
| 151 | + indexToSettings.put("index-2", Settings.EMPTY); |
| 152 | + when(response.getIndexToSettings()).thenReturn(indexToSettings); |
| 153 | + actionListener.onResponse(response); |
| 154 | + return null; |
| 155 | + }).when(indicesAdminClient).getSettings(any(GetSettingsRequest.class), isA(ActionListener.class)); |
| 156 | + |
| 157 | + // clusterStateResponse.getState().getMetadata().spliterator() |
| 158 | + doAnswer(invocation -> { |
| 159 | + ActionListener<ClusterStateResponse> actionListener = invocation.getArgument(1); |
| 160 | + ClusterStateResponse response = mock(ClusterStateResponse.class); |
| 161 | + when(response.getState()).thenReturn(clusterState); |
| 162 | + actionListener.onResponse(response); |
| 163 | + return null; |
| 164 | + }).when(clusterAdminClient).state(any(ClusterStateRequest.class), isA(ActionListener.class)); |
| 165 | + |
| 166 | + doAnswer(invocation -> { |
| 167 | + ActionListener<IndicesStatsResponse> actionListener = invocation.getArgument(1); |
| 168 | + IndicesStatsResponse response = mock(IndicesStatsResponse.class); |
| 169 | + Map<String, IndexStats> indicesStats = new HashMap<>(); |
| 170 | + IndexStats indexStats = mock(IndexStats.class); |
| 171 | + // mock primary stats |
| 172 | + CommonStats primaryStats = mock(CommonStats.class); |
| 173 | + DocsStats docsStats = mock(DocsStats.class); |
| 174 | + when(docsStats.getCount()).thenReturn(100L); |
| 175 | + when(docsStats.getDeleted()).thenReturn(10L); |
| 176 | + when(primaryStats.getDocs()).thenReturn(docsStats); |
| 177 | + StoreStats primaryStoreStats = mock(StoreStats.class); |
| 178 | + when(primaryStoreStats.size()).thenReturn(ByteSizeValue.parseBytesSizeValue("100k", "mock_setting_name")); |
| 179 | + when(primaryStats.getStore()).thenReturn(primaryStoreStats); |
| 180 | + // end mock primary stats |
| 181 | + |
| 182 | + // mock total stats |
| 183 | + CommonStats totalStats = mock(CommonStats.class); |
| 184 | + DocsStats totalDocsStats = mock(DocsStats.class); |
| 185 | + when(totalDocsStats.getCount()).thenReturn(100L); |
| 186 | + when(totalDocsStats.getDeleted()).thenReturn(10L); |
| 187 | + StoreStats totalStoreStats = mock(StoreStats.class); |
| 188 | + when(totalStoreStats.size()).thenReturn(ByteSizeValue.parseBytesSizeValue("100k", "mock_setting_name")); |
| 189 | + when(totalStats.getStore()).thenReturn(totalStoreStats); |
| 190 | + // end mock common stats |
| 191 | + |
| 192 | + when(indexStats.getPrimaries()).thenReturn(primaryStats); |
| 193 | + when(indexStats.getTotal()).thenReturn(totalStats); |
| 194 | + indicesStats.put("index-1", indexStats); |
| 195 | + when(response.getIndices()).thenReturn(indicesStats); |
| 196 | + actionListener.onResponse(response); |
| 197 | + return null; |
| 198 | + }).when(indicesAdminClient).stats(any(IndicesStatsRequest.class), isA(ActionListener.class)); |
| 199 | + |
| 200 | + doAnswer(invocation -> { |
| 201 | + ActionListener<ClusterHealthResponse> actionListener = invocation.getArgument(1); |
| 202 | + ClusterHealthResponse response = mock(ClusterHealthResponse.class); |
| 203 | + Map<String, ClusterIndexHealth> clusterIndexHealthMap = new HashMap<>(); |
| 204 | + when(indexMetadata.getNumberOfShards()).thenReturn(5); |
| 205 | + when(indexMetadata.getNumberOfReplicas()).thenReturn(1); |
| 206 | + when(metadata.spliterator()).thenReturn(Arrays.spliterator(new IndexMetadata[] { indexMetadata })); |
| 207 | + Iterator<IndexShardRoutingTable> iterator = (Iterator<IndexShardRoutingTable>) mock(Iterator.class); |
| 208 | + when(iterator.hasNext()).thenReturn(false); |
| 209 | + when(indexRoutingTable.iterator()).thenReturn(iterator); |
| 210 | + ClusterIndexHealth health = new ClusterIndexHealth(indexMetadata, indexRoutingTable); |
| 211 | + clusterIndexHealthMap.put("index-1", health); |
| 212 | + when(response.getIndices()).thenReturn(clusterIndexHealthMap); |
| 213 | + actionListener.onResponse(response); |
| 214 | + return null; |
| 215 | + }).when(clusterAdminClient).health(any(ClusterHealthRequest.class), isA(ActionListener.class)); |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + public void test_run_withEmptyTableResult() { |
| 220 | + Map<String, String> parameters = createParameters("[\"index-1\"]", "true", "10", "true"); |
| 221 | + Tool tool = ListIndexTool.Factory.getInstance().create(Collections.emptyMap()); |
| 222 | + doAnswer(invocation -> { |
| 223 | + ActionListener<GetSettingsResponse> actionListener = invocation.getArgument(1); |
| 224 | + GetSettingsResponse response = mock(GetSettingsResponse.class); |
| 225 | + Map<String, Settings> indexToSettings = new HashMap<>(); |
| 226 | + indexToSettings.put("index-1", Settings.EMPTY); |
| 227 | + indexToSettings.put("index-2", Settings.EMPTY); |
| 228 | + when(response.getIndexToSettings()).thenReturn(indexToSettings); |
| 229 | + actionListener.onResponse(response); |
| 230 | + return null; |
| 231 | + }).when(indicesAdminClient).getSettings(any(GetSettingsRequest.class), isA(ActionListener.class)); |
| 232 | + |
| 233 | + // clusterStateResponse.getState().getMetadata().spliterator() |
| 234 | + doAnswer(invocation -> { |
| 235 | + ActionListener<ClusterStateResponse> actionListener = invocation.getArgument(1); |
| 236 | + ClusterStateResponse response = mock(ClusterStateResponse.class); |
| 237 | + when(response.getState()).thenReturn(clusterState); |
| 238 | + actionListener.onResponse(response); |
| 239 | + return null; |
| 240 | + }).when(clusterAdminClient).state(any(ClusterStateRequest.class), isA(ActionListener.class)); |
| 241 | + |
| 242 | + doAnswer(invocation -> { |
| 243 | + ActionListener<IndicesStatsResponse> actionListener = invocation.getArgument(1); |
| 244 | + actionListener.onResponse(null); |
| 245 | + return null; |
| 246 | + }).when(indicesAdminClient).stats(any(IndicesStatsRequest.class), isA(ActionListener.class)); |
| 247 | + |
| 248 | + doAnswer(invocation -> { |
| 249 | + ActionListener<ClusterHealthResponse> actionListener = invocation.getArgument(1); |
| 250 | + actionListener.onResponse(null); |
| 251 | + return null; |
| 252 | + }).when(clusterAdminClient).health(any(ClusterHealthRequest.class), isA(ActionListener.class)); |
| 253 | + |
| 254 | + ActionListener<String> listener = mock(ActionListener.class); |
| 255 | + ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
| 256 | + tool.run(parameters, listener); |
| 257 | + verify(listener).onResponse(captor.capture()); |
| 258 | + System.out.println(captor.getValue()); |
| 259 | + assert captor.getValue().contains("There were no results searching the indices parameter"); |
| 260 | + } |
| 261 | + |
| 262 | + @Test |
| 263 | + public void test_run_failed() { |
| 264 | + Map<String, String> parameters = new HashMap<>(); |
| 265 | + parameters.put("indices", "[\"index-1\"]"); |
| 266 | + parameters.put("page_size", "10"); |
| 267 | + |
| 268 | + doAnswer(invocation -> { |
| 269 | + ActionListener<GetSettingsResponse> actionListener = invocation.getArgument(1); |
| 270 | + actionListener.onFailure(new RuntimeException("failed to get settings")); |
| 271 | + return null; |
| 272 | + }).when(indicesAdminClient).getSettings(any(GetSettingsRequest.class), isA(ActionListener.class)); |
| 273 | + |
| 274 | + Tool tool = ListIndexTool.Factory.getInstance().create(Collections.emptyMap()); |
| 275 | + ActionListener<String> listener = mock(ActionListener.class); |
| 276 | + ArgumentCaptor<RuntimeException> captor = ArgumentCaptor.forClass(RuntimeException.class); |
| 277 | + tool.run(parameters, listener); |
| 278 | + verify(listener).onFailure(captor.capture()); |
| 279 | + System.out.println(captor.getValue().getMessage()); |
| 280 | + assert (captor.getValue().getMessage().contains("failed to get settings")); |
| 281 | + } |
| 282 | + |
| 283 | + @Test |
| 284 | + public void test_validate() { |
| 285 | + Tool tool = ListIndexTool.Factory.getInstance().create(Collections.emptyMap()); |
| 286 | + assert tool.validate(ImmutableMap.of("runtimeParameter", "value1")); |
| 287 | + assert !tool.validate(null); |
| 288 | + assert !tool.validate(Collections.emptyMap()); |
| 289 | + } |
| 290 | + |
| 291 | + @Test |
| 292 | + public void test_getDefaultDescription() { |
| 293 | + Tool.Factory<ListIndexTool> factory = ListIndexTool.Factory.getInstance(); |
| 294 | + System.out.println(factory.getDefaultDescription()); |
| 295 | + assert (factory |
| 296 | + .getDefaultDescription() |
| 297 | + .equals( |
| 298 | + "This tool gets index information from the OpenSearch cluster. It takes 2 optional arguments named `index` which is a comma-delimited list of one or more indices to get information from (default is an empty list meaning all indices), and `local` which means whether to return information from the local node only instead of the cluster manager node (default is false). The tool returns the indices information, including `health`, `status`, `index`, `uuid`, `pri`, `rep`, `docs.count`, `docs.deleted`, `store.size`, `pri.store. size `, `pri.store.size`, `pri.store`." |
| 299 | + )); |
| 300 | + } |
| 301 | + |
| 302 | + @Test |
| 303 | + public void test_getDefaultType() { |
| 304 | + Tool.Factory<ListIndexTool> factory = ListIndexTool.Factory.getInstance(); |
| 305 | + System.out.println(factory.getDefaultType()); |
| 306 | + assert (factory.getDefaultType().equals("ListIndexTool")); |
| 307 | + } |
| 308 | + |
| 309 | + @Test |
| 310 | + public void test_getDefaultVersion() { |
| 311 | + Tool.Factory<ListIndexTool> factory = ListIndexTool.Factory.getInstance(); |
| 312 | + assert factory.getDefaultVersion() == null; |
| 313 | + } |
| 314 | +} |
0 commit comments