|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.action.ingest; |
| 10 | + |
| 11 | +import org.elasticsearch.action.ActionListener; |
| 12 | +import org.elasticsearch.action.support.ActionFilters; |
| 13 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
| 14 | +import org.elasticsearch.cluster.ClusterName; |
| 15 | +import org.elasticsearch.cluster.ClusterState; |
| 16 | +import org.elasticsearch.cluster.metadata.Metadata; |
| 17 | +import org.elasticsearch.common.bytes.BytesArray; |
| 18 | +import org.elasticsearch.common.util.concurrent.EsExecutors; |
| 19 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 20 | +import org.elasticsearch.common.xcontent.XContentType; |
| 21 | +import org.elasticsearch.core.Tuple; |
| 22 | +import org.elasticsearch.ingest.IngestMetadata; |
| 23 | +import org.elasticsearch.ingest.IngestService; |
| 24 | +import org.elasticsearch.ingest.PipelineConfiguration; |
| 25 | +import org.elasticsearch.test.ESTestCase; |
| 26 | +import org.elasticsearch.test.client.NoOpNodeClient; |
| 27 | +import org.elasticsearch.threadpool.ThreadPool; |
| 28 | +import org.elasticsearch.transport.TransportService; |
| 29 | + |
| 30 | +import java.io.OutputStream; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.concurrent.CountDownLatch; |
| 33 | +import java.util.concurrent.atomic.AtomicLong; |
| 34 | + |
| 35 | +import static org.elasticsearch.core.Tuple.tuple; |
| 36 | +import static org.hamcrest.Matchers.equalTo; |
| 37 | +import static org.mockito.Matchers.anyString; |
| 38 | +import static org.mockito.Mockito.mock; |
| 39 | +import static org.mockito.Mockito.when; |
| 40 | + |
| 41 | +public class PutPipelineTransportActionTests extends ESTestCase { |
| 42 | + |
| 43 | + public void testUpdatingRandomPipelineWithoutChangesIsNoOp() throws Exception { |
| 44 | + var randomMap = randomMap(10, 50, PutPipelineTransportActionTests::randomMapEntry); |
| 45 | + |
| 46 | + XContentBuilder x = XContentBuilder.builder(XContentType.JSON.xContent()) |
| 47 | + .startObject() |
| 48 | + .field("processors", randomMap) |
| 49 | + .endObject(); |
| 50 | + |
| 51 | + OutputStream os = x.getOutputStream(); |
| 52 | + x.generator().close(); |
| 53 | + testUpdatingPipeline(os.toString()); |
| 54 | + } |
| 55 | + |
| 56 | + public void testUpdatingPipelineWithoutChangesIsNoOp() throws Exception { |
| 57 | + var value = randomAlphaOfLength(5); |
| 58 | + var pipelineString = "{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"" + value + "\"}}]}"; |
| 59 | + testUpdatingPipeline(pipelineString); |
| 60 | + } |
| 61 | + |
| 62 | + private void testUpdatingPipeline(String pipelineString) throws Exception { |
| 63 | + var threadPool = mock(ThreadPool.class); |
| 64 | + when(threadPool.generic()).thenReturn(EsExecutors.DIRECT_EXECUTOR_SERVICE); |
| 65 | + when(threadPool.executor(anyString())).thenReturn(EsExecutors.DIRECT_EXECUTOR_SERVICE); |
| 66 | + var client = new NoOpNodeClient(threadPool); |
| 67 | + var action = new PutPipelineTransportAction( |
| 68 | + threadPool, |
| 69 | + mock(TransportService.class), |
| 70 | + mock(ActionFilters.class), |
| 71 | + null, |
| 72 | + mock(IngestService.class), |
| 73 | + client |
| 74 | + ); |
| 75 | + |
| 76 | + var pipelineId = randomAlphaOfLength(5); |
| 77 | + var value = randomAlphaOfLength(5); |
| 78 | + var existingPipeline = new PipelineConfiguration(pipelineId, new BytesArray(pipelineString), XContentType.JSON); |
| 79 | + var clusterState = ClusterState.builder(new ClusterName("test")) |
| 80 | + .metadata(Metadata.builder().putCustom( |
| 81 | + IngestMetadata.TYPE, |
| 82 | + new IngestMetadata(Map.of(pipelineId, existingPipeline)) |
| 83 | + ).build() |
| 84 | + ).build(); |
| 85 | + |
| 86 | + CountDownLatch latch = new CountDownLatch(1); |
| 87 | + var listener = new ActionListener<AcknowledgedResponse>() { |
| 88 | + final AtomicLong successCount = new AtomicLong(0); |
| 89 | + final AtomicLong failureCount = new AtomicLong(0); |
| 90 | + |
| 91 | + @Override |
| 92 | + public void onResponse(AcknowledgedResponse acknowledgedResponse) { |
| 93 | + successCount.incrementAndGet(); |
| 94 | + latch.countDown(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onFailure(Exception e) { |
| 99 | + failureCount.incrementAndGet(); |
| 100 | + latch.countDown(); |
| 101 | + } |
| 102 | + |
| 103 | + public long getSuccessCount() { |
| 104 | + return successCount.get(); |
| 105 | + } |
| 106 | + |
| 107 | + public long getFailureCount() { |
| 108 | + return failureCount.get(); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + var request = new PutPipelineRequest(pipelineId, new BytesArray(pipelineString), XContentType.JSON); |
| 113 | + action.masterOperation(null, request, clusterState, listener); |
| 114 | + latch.await(); |
| 115 | + |
| 116 | + assertThat(client.getExecutionCount(), equalTo(0L)); |
| 117 | + assertThat(listener.getSuccessCount(), equalTo(1L)); |
| 118 | + assertThat(listener.getFailureCount(), equalTo(0L)); |
| 119 | + } |
| 120 | + |
| 121 | + private static Tuple<String, Object> randomMapEntry() { |
| 122 | + return tuple(randomAlphaOfLength(5), randomObject()); |
| 123 | + } |
| 124 | + |
| 125 | + private static Object randomObject() { |
| 126 | + return randomFrom( |
| 127 | + random(), |
| 128 | + ESTestCase::randomLong, |
| 129 | + () -> generateRandomStringArray(10, 5, true), |
| 130 | + () -> randomMap(3, 5, PutPipelineTransportActionTests::randomMapEntry), |
| 131 | + () -> randomAlphaOfLength(5), |
| 132 | + ESTestCase::randomTimeValue, |
| 133 | + ESTestCase::randomDouble |
| 134 | + ); |
| 135 | + } |
| 136 | +} |
0 commit comments