|
| 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 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.searchablesnapshots; |
| 8 | + |
| 9 | +import org.elasticsearch.ElasticsearchException; |
| 10 | +import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; |
| 11 | +import org.elasticsearch.client.Client; |
| 12 | +import org.elasticsearch.client.OriginSettingClient; |
| 13 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 14 | +import org.elasticsearch.common.Strings; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
| 16 | +import org.elasticsearch.indices.SystemIndexDescriptor; |
| 17 | +import org.elasticsearch.plugins.Plugin; |
| 18 | +import org.elasticsearch.plugins.SystemIndexPlugin; |
| 19 | +import org.elasticsearch.xpack.core.ClientHelper; |
| 20 | +import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotAction; |
| 21 | +import org.elasticsearch.xpack.core.searchablesnapshots.MountSearchableSnapshotRequest; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Locale; |
| 27 | + |
| 28 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 29 | +import static org.hamcrest.Matchers.containsString; |
| 30 | +import static org.hamcrest.Matchers.equalTo; |
| 31 | + |
| 32 | +public class SearchableSnapshotsSystemIndicesIntegTests extends BaseSearchableSnapshotsIntegTestCase { |
| 33 | + |
| 34 | + @Override |
| 35 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 36 | + final List<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins()); |
| 37 | + plugins.add(TestSystemIndexPlugin.class); |
| 38 | + return plugins; |
| 39 | + } |
| 40 | + |
| 41 | + public void testCannotMountSystemIndex() throws Exception { |
| 42 | + executeTest(TestSystemIndexPlugin.INDEX_NAME, new OriginSettingClient(client(), ClientHelper.SEARCHABLE_SNAPSHOTS_ORIGIN)); |
| 43 | + } |
| 44 | + |
| 45 | + public void testCannotMountSnapshotBlobCacheIndex() throws Exception { |
| 46 | + executeTest(SearchableSnapshotsConstants.SNAPSHOT_BLOB_CACHE_INDEX, client()); |
| 47 | + } |
| 48 | + |
| 49 | + private void executeTest(final String indexName, final Client client) throws Exception { |
| 50 | + final boolean isHidden = randomBoolean(); |
| 51 | + createAndPopulateIndex(indexName, Settings.builder().put(IndexMetadata.SETTING_INDEX_HIDDEN, isHidden)); |
| 52 | + |
| 53 | + final String repositoryName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); |
| 54 | + createRepo(repositoryName); |
| 55 | + |
| 56 | + final String snapshotName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); |
| 57 | + final CreateSnapshotResponse snapshotResponse = client.admin() |
| 58 | + .cluster() |
| 59 | + .prepareCreateSnapshot(repositoryName, snapshotName) |
| 60 | + .setIndices(indexName) |
| 61 | + .setWaitForCompletion(true) |
| 62 | + .get(); |
| 63 | + |
| 64 | + final int numPrimaries = getNumShards(indexName).numPrimaries; |
| 65 | + assertThat(snapshotResponse.getSnapshotInfo().successfulShards(), equalTo(numPrimaries)); |
| 66 | + assertThat(snapshotResponse.getSnapshotInfo().failedShards(), equalTo(0)); |
| 67 | + |
| 68 | + if (randomBoolean()) { |
| 69 | + assertAcked(client.admin().indices().prepareClose(indexName)); |
| 70 | + } else { |
| 71 | + assertAcked(client.admin().indices().prepareDelete(indexName)); |
| 72 | + } |
| 73 | + |
| 74 | + final MountSearchableSnapshotRequest mountRequest = new MountSearchableSnapshotRequest( |
| 75 | + indexName, |
| 76 | + repositoryName, |
| 77 | + snapshotName, |
| 78 | + indexName, |
| 79 | + Settings.builder().put(IndexMetadata.SETTING_INDEX_HIDDEN, randomBoolean()).build(), |
| 80 | + Strings.EMPTY_ARRAY, |
| 81 | + true |
| 82 | + ); |
| 83 | + |
| 84 | + final ElasticsearchException exception = expectThrows( |
| 85 | + ElasticsearchException.class, |
| 86 | + () -> client.execute(MountSearchableSnapshotAction.INSTANCE, mountRequest).actionGet() |
| 87 | + ); |
| 88 | + assertThat(exception.getMessage(), containsString("system index [" + indexName + "] cannot be mounted as searchable snapshots")); |
| 89 | + } |
| 90 | + |
| 91 | + public static class TestSystemIndexPlugin extends Plugin implements SystemIndexPlugin { |
| 92 | + |
| 93 | + static final String INDEX_NAME = ".test-system-index"; |
| 94 | + |
| 95 | + @Override |
| 96 | + public Collection<SystemIndexDescriptor> getSystemIndexDescriptors(Settings settings) { |
| 97 | + return List.of(new SystemIndexDescriptor(INDEX_NAME, "System index for [" + getTestClass().getName() + ']')); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments