|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.plugin.wlm; |
| 10 | + |
| 11 | +import org.opensearch.action.ActionRequest; |
| 12 | +import org.opensearch.cluster.metadata.IndexNameExpressionResolver; |
| 13 | +import org.opensearch.cluster.node.DiscoveryNodes; |
| 14 | +import org.opensearch.cluster.service.ClusterService; |
| 15 | +import org.opensearch.common.settings.Settings; |
| 16 | +import org.opensearch.core.action.ActionResponse; |
| 17 | +import org.opensearch.core.common.io.stream.NamedWriteableRegistry; |
| 18 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 19 | +import org.opensearch.env.Environment; |
| 20 | +import org.opensearch.indices.SystemIndexDescriptor; |
| 21 | +import org.opensearch.plugin.wlm.action.CreateWorkloadGroupAction; |
| 22 | +import org.opensearch.plugin.wlm.rest.RestCreateWorkloadGroupAction; |
| 23 | +import org.opensearch.plugin.wlm.rest.RestDeleteWorkloadGroupAction; |
| 24 | +import org.opensearch.plugin.wlm.rest.RestGetWorkloadGroupAction; |
| 25 | +import org.opensearch.plugin.wlm.rest.RestUpdateWorkloadGroupAction; |
| 26 | +import org.opensearch.plugin.wlm.service.WorkloadGroupPersistenceService; |
| 27 | +import org.opensearch.plugins.ActionPlugin; |
| 28 | +import org.opensearch.repositories.RepositoriesService; |
| 29 | +import org.opensearch.rest.RestController; |
| 30 | +import org.opensearch.rest.RestHandler; |
| 31 | +import org.opensearch.rule.RulePersistenceService; |
| 32 | +import org.opensearch.rule.autotagging.FeatureType; |
| 33 | +import org.opensearch.rule.service.IndexStoredRulePersistenceService; |
| 34 | +import org.opensearch.script.ScriptService; |
| 35 | +import org.opensearch.test.OpenSearchTestCase; |
| 36 | +import org.opensearch.threadpool.ThreadPool; |
| 37 | +import org.opensearch.transport.client.Client; |
| 38 | +import org.opensearch.watcher.ResourceWatcherService; |
| 39 | + |
| 40 | +import java.util.Collection; |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +import static org.mockito.Mockito.mock; |
| 44 | + |
| 45 | +public class WorkloadManagementPluginTests extends OpenSearchTestCase { |
| 46 | + WorkloadManagementPlugin plugin = new WorkloadManagementPlugin(); |
| 47 | + |
| 48 | + public void testGetActionsReturnsHandlers() { |
| 49 | + List<ActionPlugin.ActionHandler<? extends ActionRequest, ? extends ActionResponse>> actions = plugin.getActions(); |
| 50 | + assertEquals(4, actions.size()); |
| 51 | + assertEquals(CreateWorkloadGroupAction.INSTANCE.name(), actions.get(0).getAction().name()); |
| 52 | + } |
| 53 | + |
| 54 | + public void testGetRestHandlersReturnsHandlers() { |
| 55 | + List<RestHandler> handlers = plugin.getRestHandlers( |
| 56 | + Settings.EMPTY, |
| 57 | + mock(RestController.class), |
| 58 | + null, |
| 59 | + null, |
| 60 | + null, |
| 61 | + null, |
| 62 | + () -> mock(DiscoveryNodes.class) |
| 63 | + ); |
| 64 | + |
| 65 | + assertEquals(4, handlers.size()); |
| 66 | + assertTrue(handlers.stream().anyMatch(h -> h instanceof RestCreateWorkloadGroupAction)); |
| 67 | + assertTrue(handlers.stream().anyMatch(h -> h instanceof RestGetWorkloadGroupAction)); |
| 68 | + assertTrue(handlers.stream().anyMatch(h -> h instanceof RestDeleteWorkloadGroupAction)); |
| 69 | + assertTrue(handlers.stream().anyMatch(h -> h instanceof RestUpdateWorkloadGroupAction)); |
| 70 | + } |
| 71 | + |
| 72 | + public void testCreateComponentsInitializesRulePersistenceService() { |
| 73 | + Client mockClient = mock(Client.class); |
| 74 | + plugin.createComponents( |
| 75 | + mockClient, |
| 76 | + mock(ClusterService.class), |
| 77 | + mock(ThreadPool.class), |
| 78 | + mock(ResourceWatcherService.class), |
| 79 | + mock(ScriptService.class), |
| 80 | + mock(NamedXContentRegistry.class), |
| 81 | + mock(Environment.class), |
| 82 | + null, |
| 83 | + mock(NamedWriteableRegistry.class), |
| 84 | + mock(IndexNameExpressionResolver.class), |
| 85 | + () -> mock(RepositoriesService.class) |
| 86 | + ); |
| 87 | + RulePersistenceService service = plugin.getRulePersistenceServiceSupplier().get(); |
| 88 | + assertNotNull(service); |
| 89 | + assertTrue(service instanceof IndexStoredRulePersistenceService); |
| 90 | + } |
| 91 | + |
| 92 | + public void testGetSystemIndexDescriptorsReturnsCorrectDescriptor() { |
| 93 | + Collection<SystemIndexDescriptor> descriptors = plugin.getSystemIndexDescriptors(Settings.EMPTY); |
| 94 | + assertEquals(1, descriptors.size()); |
| 95 | + SystemIndexDescriptor descriptor = descriptors.iterator().next(); |
| 96 | + assertEquals(".wlm_rules", descriptor.getIndexPattern()); |
| 97 | + } |
| 98 | + |
| 99 | + public void testGetFeatureTypeReturnsWorkloadGroupFeatureType() { |
| 100 | + FeatureType featureType = plugin.getFeatureType(); |
| 101 | + assertEquals("workload_group", featureType.getName()); |
| 102 | + } |
| 103 | + |
| 104 | + public void testGetSettingsIncludesMaxQueryGroupCount() { |
| 105 | + List<?> settings = plugin.getSettings(); |
| 106 | + assertTrue(settings.contains(WorkloadGroupPersistenceService.MAX_QUERY_GROUP_COUNT)); |
| 107 | + } |
| 108 | + |
| 109 | + public void testCreateGuiceModulesReturnsModule() { |
| 110 | + Collection<?> modules = plugin.createGuiceModules(); |
| 111 | + assertEquals(1, modules.size()); |
| 112 | + assertTrue(modules.iterator().next() instanceof WorkloadManagementPluginModule); |
| 113 | + } |
| 114 | +} |
0 commit comments