|
| 1 | +/* |
| 2 | + * Licensed to Crate.io GmbH ("Crate") under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with this work for |
| 4 | + * additional information regarding copyright ownership. Crate licenses |
| 5 | + * this file to you under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. You may |
| 7 | + * obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + * |
| 17 | + * However, if you have executed another commercial license agreement |
| 18 | + * with Crate these terms will supersede the license and you may use the |
| 19 | + * software solely pursuant to the terms of the relevant commercial agreement. |
| 20 | + */ |
| 21 | + |
| 22 | +package io.crate.planner.node.ddl; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 26 | +import static org.mockito.ArgumentMatchers.any; |
| 27 | +import static org.mockito.Mockito.doThrow; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | + |
| 30 | +import java.util.Map; |
| 31 | +import java.util.concurrent.CompletableFuture; |
| 32 | + |
| 33 | +import org.elasticsearch.Version; |
| 34 | +import org.elasticsearch.action.admin.cluster.repositories.put.AlterRepositoryRequest; |
| 35 | +import org.elasticsearch.action.admin.cluster.repositories.put.TransportAlterRepository; |
| 36 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
| 37 | +import org.elasticsearch.client.Client; |
| 38 | +import org.elasticsearch.cluster.metadata.RepositoryMetadata; |
| 39 | +import org.elasticsearch.common.settings.Settings; |
| 40 | +import org.elasticsearch.repositories.RepositoryMissingException; |
| 41 | +import org.junit.Before; |
| 42 | +import org.junit.Test; |
| 43 | +import org.junit.runner.RunWith; |
| 44 | +import org.mockito.Answers; |
| 45 | +import org.mockito.Mock; |
| 46 | +import org.mockito.junit.MockitoJUnitRunner; |
| 47 | + |
| 48 | +import io.crate.analyze.AnalyzedAlterRepository; |
| 49 | +import io.crate.analyze.repositories.RepositoryParamValidator; |
| 50 | +import io.crate.data.Row; |
| 51 | +import io.crate.data.testing.TestingRowConsumer; |
| 52 | +import io.crate.execution.ddl.RepositoryService; |
| 53 | +import io.crate.expression.symbol.Literal; |
| 54 | +import io.crate.planner.DependencyCarrier; |
| 55 | +import io.crate.planner.PlannerContext; |
| 56 | +import io.crate.planner.operators.SubQueryResults; |
| 57 | +import io.crate.sql.tree.GenericProperties; |
| 58 | + |
| 59 | +@RunWith(MockitoJUnitRunner.class) |
| 60 | +public class AlterRepositoryPlanTest { |
| 61 | + @Mock |
| 62 | + RepositoryService repoService; |
| 63 | + @Mock |
| 64 | + RepositoryParamValidator repoParamValidator; |
| 65 | + @Mock |
| 66 | + Client client; |
| 67 | + @Mock(answer = Answers.RETURNS_DEEP_STUBS) |
| 68 | + PlannerContext plannerCtx; |
| 69 | + TestingRowConsumer rowConsumer; |
| 70 | + @Mock |
| 71 | + DependencyCarrier dependencyCarrier; |
| 72 | + |
| 73 | + @Before |
| 74 | + public void setUp() { |
| 75 | + rowConsumer = new TestingRowConsumer(); |
| 76 | + when(dependencyCarrier.repositoryService()).thenReturn(repoService); |
| 77 | + when(dependencyCarrier.repositoryParamValidator()).thenReturn(repoParamValidator); |
| 78 | + when(dependencyCarrier.client()).thenReturn(client); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void test_execute_or_fail() throws Exception { |
| 83 | + var underTest = new AlterRepositoryPlan( |
| 84 | + new AnalyzedAlterRepository( |
| 85 | + "repo-name", |
| 86 | + new GenericProperties<>(Map.of("location", Literal.of("/tmp/data"))) |
| 87 | + ) |
| 88 | + ); |
| 89 | + |
| 90 | + when(repoService.getRepository("repo-name")) |
| 91 | + .thenReturn(new RepositoryMetadata("repo-name", "dummy-type", Settings.EMPTY)); |
| 92 | + when(client.execute( |
| 93 | + TransportAlterRepository.ACTION, new AlterRepositoryRequest("repo-name", Settings.builder().put("location", "/tmp/data").build()) |
| 94 | + )).thenReturn(CompletableFuture.completedFuture(new AcknowledgedResponse(true))); |
| 95 | + |
| 96 | + underTest.executeOrFail(dependencyCarrier, plannerCtx, rowConsumer, Row.EMPTY, SubQueryResults.EMPTY); |
| 97 | + |
| 98 | + assertThat(rowConsumer.getResult()).hasSize(1); |
| 99 | + assertThat(rowConsumer.getResult().getFirst()).hasSize(1); |
| 100 | + // the number of rows changed |
| 101 | + assertThat(rowConsumer.getResult().getFirst()[0]).isEqualTo(1L); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void test_execute_or_fail_missing_repository() { |
| 106 | + var underTest = new AlterRepositoryPlan( |
| 107 | + new AnalyzedAlterRepository("repo-name", new GenericProperties<>(Map.of())) |
| 108 | + ); |
| 109 | + |
| 110 | + assertThatThrownBy(() -> |
| 111 | + underTest.executeOrFail(dependencyCarrier, plannerCtx, rowConsumer, Row.EMPTY, SubQueryResults.EMPTY) |
| 112 | + ).isExactlyInstanceOf(RepositoryMissingException.class) |
| 113 | + .hasMessageContaining("[repo-name]"); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void test_execute_or_fail_unsupported_property() { |
| 118 | + var underTest = new AlterRepositoryPlan( |
| 119 | + new AnalyzedAlterRepository("repo-name", new GenericProperties<>(Map.of())) |
| 120 | + ); |
| 121 | + |
| 122 | + when(repoService.getRepository("repo-name")) |
| 123 | + .thenReturn(new RepositoryMetadata("repo-name", "dummy-type", Settings.EMPTY)); |
| 124 | + doThrow(new IllegalArgumentException("invalid property")) |
| 125 | + .when(repoParamValidator).validateSupportedOnly(any(), any()); |
| 126 | + |
| 127 | + assertThatThrownBy(() -> |
| 128 | + underTest.executeOrFail(dependencyCarrier, plannerCtx, rowConsumer, Row.EMPTY, SubQueryResults.EMPTY) |
| 129 | + ).isExactlyInstanceOf(IllegalArgumentException.class) |
| 130 | + .hasMessageContaining("invalid property"); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void test_execute_or_fail_min_node_version() { |
| 135 | + var underTest = new AlterRepositoryPlan( |
| 136 | + new AnalyzedAlterRepository("repo-name", new GenericProperties<>(Map.of())) |
| 137 | + ); |
| 138 | + |
| 139 | + when(plannerCtx.clusterState().nodes().getMinNodeVersion()).thenReturn(Version.V_6_3_2); |
| 140 | + |
| 141 | + assertThatThrownBy(() -> |
| 142 | + underTest.executeOrFail(dependencyCarrier, plannerCtx, rowConsumer, Row.EMPTY, SubQueryResults.EMPTY) |
| 143 | + ).isExactlyInstanceOf(IllegalStateException.class) |
| 144 | + .hasMessageContaining("Cannot execute ALTER REPOSITORY while there are <6.4.0 nodes in the cluster"); |
| 145 | + } |
| 146 | +} |
0 commit comments