summaryrefslogtreecommitdiff
path: root/plugins/repository-azure/src/test/java/org/elasticsearch/cloud/azure/AbstractAzureRepositoryServiceTestCase.java
blob: 8a17f83d92d274e79dc4a1b28b02bb0d09ea0c1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.cloud.azure;

import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.LocationMode;
import org.elasticsearch.cloud.azure.storage.AzureStorageService;
import org.elasticsearch.cloud.azure.storage.AzureStorageService.Storage;
import org.elasticsearch.cloud.azure.storage.AzureStorageServiceMock;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugin.repository.azure.AzureRepositoryPlugin;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.repositories.RepositoryMissingException;
import org.elasticsearch.test.store.MockFSDirectoryService;
import org.junit.After;
import org.junit.Before;

import java.net.URISyntaxException;
import java.util.Collection;

public abstract class AbstractAzureRepositoryServiceTestCase extends AbstractAzureTestCase {

    public static class TestPlugin extends Plugin {
        @Override
        public String name() {
            return "mock-storage-service";
        }
        @Override
        public String description() {
            return "plugs in a mock storage service for testing";
        }
        public void onModule(AzureRepositoryModule azureRepositoryModule) {
            AzureRepositoryModule.storageServiceImpl = AzureStorageServiceMock.class;
        }
    }

    protected String basePath;

    public AbstractAzureRepositoryServiceTestCase(String basePath) {
        this.basePath = basePath;
    }

    /**
     * Deletes repositories, supports wildcard notation.
     */
    public static void wipeRepositories(String... repositories) {
        // if nothing is provided, delete all
        if (repositories.length == 0) {
            repositories = new String[]{"*"};
        }
        for (String repository : repositories) {
            try {
                client().admin().cluster().prepareDeleteRepository(repository).execute().actionGet();
            } catch (RepositoryMissingException ex) {
                // ignore
            }
        }
    }

    @Override
    protected Settings nodeSettings(int nodeOrdinal) {
        Settings.Builder builder = Settings.settingsBuilder()
                .put(Storage.CONTAINER, "snapshots");

        // We use sometime deprecated settings in tests
        builder.put(Storage.ACCOUNT, "mock_azure_account")
                .put(Storage.KEY, "mock_azure_key");

        return builder.build();
    }

    @Override
    protected Collection<Class<? extends Plugin>> nodePlugins() {
        return pluginList(AzureRepositoryPlugin.class, TestPlugin.class);
    }

    @Override
    public Settings indexSettings() {
        // During restore we frequently restore index to exactly the same state it was before, that might cause the same
        // checksum file to be written twice during restore operation
        return Settings.builder().put(super.indexSettings())
                .put(MockFSDirectoryService.RANDOM_PREVENT_DOUBLE_WRITE, false)
                .put(MockFSDirectoryService.RANDOM_NO_DELETE_OPEN_FILE, false)
                .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
                .build();
    }

    @Before @After
    public final void wipe() throws StorageException, URISyntaxException {
        wipeRepositories();
        cleanRepositoryFiles(basePath);
    }

    /**
     * Purge the test container
     */
    public void cleanRepositoryFiles(String path) throws StorageException, URISyntaxException {
        String container = internalCluster().getInstance(Settings.class).get("repositories.azure.container");
        logger.info("--> remove blobs in container [{}]", container);
        AzureStorageService client = internalCluster().getInstance(AzureStorageService.class);
        client.deleteFiles(null, LocationMode.PRIMARY_ONLY, container, path);
    }
}