|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Graylog, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + */ |
| 17 | +package org.graylog.datanode.bootstrap.preflight; |
| 18 | + |
| 19 | +import org.assertj.core.api.Assertions; |
| 20 | +import org.graylog.datanode.DatanodeTestUtils; |
| 21 | +import org.graylog.datanode.OpensearchDistribution; |
| 22 | +import org.graylog.datanode.configuration.DatanodeConfiguration; |
| 23 | +import org.graylog.datanode.configuration.DatanodeDirectories; |
| 24 | +import org.graylog.datanode.filesystem.index.IncompatibleIndexVersionException; |
| 25 | +import org.graylog.datanode.filesystem.index.IndicesDirectoryParser; |
| 26 | +import org.graylog.datanode.filesystem.index.dto.IndexerDirectoryInformation; |
| 27 | +import org.graylog.datanode.filesystem.index.indexreader.ShardStatsParserImpl; |
| 28 | +import org.graylog.datanode.filesystem.index.statefile.StateFileParserImpl; |
| 29 | +import org.graylog2.bootstrap.preflight.PreflightCheckException; |
| 30 | +import org.graylog2.security.jwt.IndexerJwtAuthToken; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.io.TempDir; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.nio.charset.StandardCharsets; |
| 36 | +import java.nio.file.Files; |
| 37 | +import java.nio.file.Path; |
| 38 | + |
| 39 | +class OpensearchDataDirCompatibilityCheckTest { |
| 40 | + |
| 41 | + private static final String OPENSEARCH_VERSION = "2.19.0"; |
| 42 | + |
| 43 | + @Test |
| 44 | + void testCompatibilityCheckSkipping(@TempDir Path tempDir) throws IOException { |
| 45 | + writeCheckFile(tempDir, OPENSEARCH_VERSION); |
| 46 | + |
| 47 | + final IndicesDirectoryParser parser = new IndicesDirectoryParser(null, null) { |
| 48 | + @Override |
| 49 | + public IndexerDirectoryInformation parse(Path path) { |
| 50 | + throw new AssertionError("Should not be called"); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + final OpensearchDataDirCompatibilityCheck check = |
| 55 | + new OpensearchDataDirCompatibilityCheck(configFor(tempDir, OPENSEARCH_VERSION), parser); |
| 56 | + |
| 57 | + Assertions.assertThatCode(check::runCheck).doesNotThrowAnyException(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testCompatibilityCheckInitialRun(@TempDir Path tempDir) throws IOException { |
| 62 | + final OpensearchDataDirCompatibilityCheck check = |
| 63 | + new OpensearchDataDirCompatibilityCheck(configFor(tempDir, OPENSEARCH_VERSION), realParser()); |
| 64 | + |
| 65 | + Assertions.assertThatCode(check::runCheck).doesNotThrowAnyException(); |
| 66 | + Assertions.assertThat(readCheckFile(tempDir)).isEqualTo(OPENSEARCH_VERSION); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testCompatibilityCheckSkipsOnMinorVersionChange(@TempDir Path dataDir) throws IOException { |
| 71 | + writeCheckFile(dataDir, "2.18.0"); |
| 72 | + |
| 73 | + final IndicesDirectoryParser parser = new IndicesDirectoryParser(null, null) { |
| 74 | + @Override |
| 75 | + public IndexerDirectoryInformation parse(Path path) { |
| 76 | + throw new AssertionError("Should not be called on minor version change"); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + final OpensearchDataDirCompatibilityCheck check = |
| 81 | + new OpensearchDataDirCompatibilityCheck(configFor(dataDir, OPENSEARCH_VERSION), parser); |
| 82 | + |
| 83 | + Assertions.assertThatCode(check::runCheck).doesNotThrowAnyException(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testCompatibilityCheckRerunsForMajorVersionChange(@TempDir Path dataDir) throws IOException { |
| 88 | + writeCheckFile(dataDir, "2.19.0"); |
| 89 | + final String nextMajorVersion = "3.0.0"; |
| 90 | + |
| 91 | + final OpensearchDataDirCompatibilityCheck check = |
| 92 | + new OpensearchDataDirCompatibilityCheck(configFor(dataDir, nextMajorVersion), realParser()); |
| 93 | + |
| 94 | + Assertions.assertThatCode(check::runCheck).doesNotThrowAnyException(); |
| 95 | + Assertions.assertThat(readCheckFile(dataDir)).isEqualTo(nextMajorVersion); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testCompatibilityCheckFailsForNonExistentDirectory() { |
| 100 | + final Path nonExistentDir = Path.of("/nonexistent/opensearch/data"); |
| 101 | + |
| 102 | + final OpensearchDataDirCompatibilityCheck check = |
| 103 | + new OpensearchDataDirCompatibilityCheck(configFor(nonExistentDir, OPENSEARCH_VERSION), realParser()); |
| 104 | + |
| 105 | + Assertions.assertThatThrownBy(check::runCheck) |
| 106 | + .isInstanceOf(PreflightCheckException.class) |
| 107 | + .hasMessageContaining("nonexistent"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void testCompatibilityCheckFailsForIncompatibleIndexVersion(@TempDir Path dataDir) { |
| 112 | + final IndicesDirectoryParser parser = new IndicesDirectoryParser(null, null) { |
| 113 | + @Override |
| 114 | + public IndexerDirectoryInformation parse(Path path) { |
| 115 | + throw new IncompatibleIndexVersionException("Index data version is not compatible"); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + final OpensearchDataDirCompatibilityCheck check = |
| 120 | + new OpensearchDataDirCompatibilityCheck(configFor(dataDir, OPENSEARCH_VERSION), parser); |
| 121 | + |
| 122 | + Assertions.assertThatThrownBy(check::runCheck) |
| 123 | + .isInstanceOf(PreflightCheckException.class) |
| 124 | + .hasMessageContaining("is not compatible with current version " + OPENSEARCH_VERSION); |
| 125 | + } |
| 126 | + |
| 127 | + private DatanodeConfiguration configFor(Path dataDir, String opensearchVersion) { |
| 128 | + final DatanodeDirectories directories = DatanodeTestUtils.tempDirectories(dataDir); |
| 129 | + return new DatanodeConfiguration(() -> new OpensearchDistribution(Path.of("/opensearch"), opensearchVersion), directories, 0, IndexerJwtAuthToken.disabled()); |
| 130 | + } |
| 131 | + |
| 132 | + private IndicesDirectoryParser realParser() { |
| 133 | + return new IndicesDirectoryParser(new StateFileParserImpl(), new ShardStatsParserImpl()); |
| 134 | + } |
| 135 | + |
| 136 | + private void writeCheckFile(Path dataDir, String version) throws IOException { |
| 137 | + Files.writeString(dataDir.resolve(OpensearchDataDirCompatibilityCheck.COMPATIBILITY_CHECK_FILENAME), version, StandardCharsets.UTF_8); |
| 138 | + } |
| 139 | + |
| 140 | + private String readCheckFile(Path dataDir) throws IOException { |
| 141 | + return Files.readString(dataDir.resolve(OpensearchDataDirCompatibilityCheck.COMPATIBILITY_CHECK_FILENAME), StandardCharsets.UTF_8); |
| 142 | + } |
| 143 | +} |
0 commit comments