|
| 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; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.shutdown; |
| 9 | + |
| 10 | +import org.elasticsearch.cluster.metadata.SingleNodeShutdownMetadata; |
| 11 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 12 | +import org.elasticsearch.common.io.stream.Writeable; |
| 13 | +import org.elasticsearch.core.TimeValue; |
| 14 | +import org.elasticsearch.tasks.TaskId; |
| 15 | +import org.elasticsearch.test.AbstractWireSerializingTestCase; |
| 16 | +import org.elasticsearch.test.ESTestCase; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | + |
| 20 | +public class PutShutdownRequestTests extends AbstractWireSerializingTestCase<PutShutdownRequestTests.RequestWrapper> { |
| 21 | + |
| 22 | + /** |
| 23 | + * Wraps a {@link org.elasticsearch.xpack.shutdown.PutShutdownNodeAction.Request} to add proper equality checks |
| 24 | + */ |
| 25 | + record RequestWrapper( |
| 26 | + String nodeId, |
| 27 | + SingleNodeShutdownMetadata.Type type, |
| 28 | + String reason, |
| 29 | + TimeValue allocationDelay, |
| 30 | + String targetNodeName, |
| 31 | + TimeValue gracePeriod, |
| 32 | + TaskId parentTask, |
| 33 | + TimeValue masterNodeTimeout, |
| 34 | + TimeValue ackTimeout |
| 35 | + ) implements Writeable { |
| 36 | + @Override |
| 37 | + public void writeTo(StreamOutput out) throws IOException { |
| 38 | + final var request = new PutShutdownNodeAction.Request(nodeId, type, reason, allocationDelay, targetNodeName, gracePeriod); |
| 39 | + request.setParentTask(parentTask); |
| 40 | + request.timeout(ackTimeout); |
| 41 | + request.masterNodeTimeout(masterNodeTimeout); |
| 42 | + request.writeTo(out); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected Writeable.Reader<RequestWrapper> instanceReader() { |
| 48 | + return in -> { |
| 49 | + final var request = new PutShutdownNodeAction.Request(in); |
| 50 | + return new RequestWrapper( |
| 51 | + request.getNodeId(), |
| 52 | + request.getType(), |
| 53 | + request.getReason(), |
| 54 | + request.getAllocationDelay(), |
| 55 | + request.getTargetNodeName(), |
| 56 | + request.getGracePeriod(), |
| 57 | + request.getParentTask(), |
| 58 | + request.masterNodeTimeout(), |
| 59 | + request.ackTimeout() |
| 60 | + ); |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected RequestWrapper createTestInstance() { |
| 66 | + return new RequestWrapper( |
| 67 | + randomIdentifier(), |
| 68 | + randomFrom(SingleNodeShutdownMetadata.Type.values()), |
| 69 | + randomIdentifier(), |
| 70 | + randomOptionalTimeValue(), |
| 71 | + randomOptionalIdentifier(), |
| 72 | + randomOptionalTimeValue(), |
| 73 | + randomTaskId(), |
| 74 | + TimeValue.parseTimeValue(randomTimeValue(), getTestName()), |
| 75 | + TimeValue.parseTimeValue(randomTimeValue(), getTestName()) |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + private static String randomOptionalIdentifier() { |
| 80 | + return randomBoolean() ? null : randomIdentifier(); |
| 81 | + } |
| 82 | + |
| 83 | + private TimeValue randomOptionalTimeValue() { |
| 84 | + return randomBoolean() ? null : TimeValue.parseTimeValue(randomTimeValue(), getTestName()); |
| 85 | + } |
| 86 | + |
| 87 | + private static TaskId randomTaskId() { |
| 88 | + return randomBoolean() ? TaskId.EMPTY_TASK_ID : new TaskId(randomIdentifier(), randomNonNegativeLong()); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected RequestWrapper mutateInstance(RequestWrapper instance) { |
| 93 | + return switch (between(1, 9)) { |
| 94 | + case 1 -> new RequestWrapper( |
| 95 | + randomValueOtherThan(instance.nodeId, ESTestCase::randomIdentifier), |
| 96 | + instance.type, |
| 97 | + instance.reason, |
| 98 | + instance.allocationDelay, |
| 99 | + instance.targetNodeName, |
| 100 | + instance.gracePeriod, |
| 101 | + instance.parentTask, |
| 102 | + instance.ackTimeout, |
| 103 | + instance.masterNodeTimeout |
| 104 | + ); |
| 105 | + case 2 -> new RequestWrapper( |
| 106 | + instance.nodeId, |
| 107 | + randomValueOtherThan(instance.type, () -> randomFrom(SingleNodeShutdownMetadata.Type.values())), |
| 108 | + instance.reason, |
| 109 | + instance.allocationDelay, |
| 110 | + instance.targetNodeName, |
| 111 | + instance.gracePeriod, |
| 112 | + instance.parentTask, |
| 113 | + instance.ackTimeout, |
| 114 | + instance.masterNodeTimeout |
| 115 | + ); |
| 116 | + case 3 -> new RequestWrapper( |
| 117 | + instance.nodeId, |
| 118 | + instance.type, |
| 119 | + randomValueOtherThan(instance.reason, ESTestCase::randomIdentifier), |
| 120 | + instance.allocationDelay, |
| 121 | + instance.targetNodeName, |
| 122 | + instance.gracePeriod, |
| 123 | + instance.parentTask, |
| 124 | + instance.ackTimeout, |
| 125 | + instance.masterNodeTimeout |
| 126 | + ); |
| 127 | + case 4 -> new RequestWrapper( |
| 128 | + instance.nodeId, |
| 129 | + instance.type, |
| 130 | + instance.reason, |
| 131 | + randomValueOtherThan(instance.allocationDelay, this::randomOptionalTimeValue), |
| 132 | + instance.targetNodeName, |
| 133 | + instance.gracePeriod, |
| 134 | + instance.parentTask, |
| 135 | + instance.ackTimeout, |
| 136 | + instance.masterNodeTimeout |
| 137 | + ); |
| 138 | + case 5 -> new RequestWrapper( |
| 139 | + instance.nodeId, |
| 140 | + instance.type, |
| 141 | + instance.reason, |
| 142 | + instance.allocationDelay, |
| 143 | + randomValueOtherThan(instance.targetNodeName, PutShutdownRequestTests::randomOptionalIdentifier), |
| 144 | + instance.gracePeriod, |
| 145 | + instance.parentTask, |
| 146 | + instance.ackTimeout, |
| 147 | + instance.masterNodeTimeout |
| 148 | + ); |
| 149 | + case 6 -> new RequestWrapper( |
| 150 | + instance.nodeId, |
| 151 | + instance.type, |
| 152 | + instance.reason, |
| 153 | + instance.allocationDelay, |
| 154 | + instance.targetNodeName, |
| 155 | + randomValueOtherThan(instance.gracePeriod, this::randomOptionalTimeValue), |
| 156 | + instance.parentTask, |
| 157 | + instance.ackTimeout, |
| 158 | + instance.masterNodeTimeout |
| 159 | + ); |
| 160 | + case 7 -> new RequestWrapper( |
| 161 | + instance.nodeId, |
| 162 | + instance.type, |
| 163 | + instance.reason, |
| 164 | + instance.allocationDelay, |
| 165 | + instance.targetNodeName, |
| 166 | + instance.gracePeriod, |
| 167 | + randomValueOtherThan(instance.parentTask, PutShutdownRequestTests::randomTaskId), |
| 168 | + instance.ackTimeout, |
| 169 | + instance.masterNodeTimeout |
| 170 | + ); |
| 171 | + case 8 -> new RequestWrapper( |
| 172 | + instance.nodeId, |
| 173 | + instance.type, |
| 174 | + instance.reason, |
| 175 | + instance.allocationDelay, |
| 176 | + instance.targetNodeName, |
| 177 | + instance.gracePeriod, |
| 178 | + instance.parentTask, |
| 179 | + randomValueOtherThan(instance.ackTimeout, () -> TimeValue.parseTimeValue(randomTimeValue(), getTestName())), |
| 180 | + instance.masterNodeTimeout |
| 181 | + ); |
| 182 | + case 9 -> new RequestWrapper( |
| 183 | + instance.nodeId, |
| 184 | + instance.type, |
| 185 | + instance.reason, |
| 186 | + instance.allocationDelay, |
| 187 | + instance.targetNodeName, |
| 188 | + instance.gracePeriod, |
| 189 | + instance.parentTask, |
| 190 | + instance.ackTimeout, |
| 191 | + randomValueOtherThan(instance.masterNodeTimeout, () -> TimeValue.parseTimeValue(randomTimeValue(), getTestName())) |
| 192 | + ); |
| 193 | + default -> throw new AssertionError("impossible"); |
| 194 | + }; |
| 195 | + } |
| 196 | +} |
0 commit comments