|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.datasource.property.storage; |
| 19 | + |
| 20 | +import org.apache.doris.common.ExceptionChecker; |
| 21 | +import org.apache.doris.common.UserException; |
| 22 | +import org.apache.doris.common.util.LocationPath; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Assertions; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.function.Function; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +public class OzonePropertiesTest { |
| 35 | + private Map<String, String> origProps; |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + public void setup() { |
| 39 | + origProps = new HashMap<>(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testValidOzoneConfiguration() { |
| 44 | + origProps.put(StorageProperties.FS_OZONE_SUPPORT, "true"); |
| 45 | + origProps.put("ozone.endpoint", "http://ozone-s3g:9878"); |
| 46 | + origProps.put("ozone.access_key", "hadoop"); |
| 47 | + origProps.put("ozone.secret_key", "hadoop"); |
| 48 | + |
| 49 | + OzoneProperties ozoneProperties = (OzoneProperties) StorageProperties.createPrimary(origProps); |
| 50 | + Map<String, String> backendProps = ozoneProperties.getBackendConfigProperties(); |
| 51 | + |
| 52 | + Assertions.assertEquals(StorageProperties.Type.OZONE, ozoneProperties.getType()); |
| 53 | + Assertions.assertEquals("http://ozone-s3g:9878", ozoneProperties.getEndpoint()); |
| 54 | + Assertions.assertEquals("hadoop", ozoneProperties.getAccessKey()); |
| 55 | + Assertions.assertEquals("hadoop", ozoneProperties.getSecretKey()); |
| 56 | + Assertions.assertEquals("us-east-1", ozoneProperties.getRegion()); |
| 57 | + Assertions.assertEquals("true", ozoneProperties.getUsePathStyle()); |
| 58 | + |
| 59 | + Assertions.assertEquals("http://ozone-s3g:9878", backendProps.get("AWS_ENDPOINT")); |
| 60 | + Assertions.assertEquals("hadoop", backendProps.get("AWS_ACCESS_KEY")); |
| 61 | + Assertions.assertEquals("hadoop", backendProps.get("AWS_SECRET_KEY")); |
| 62 | + Assertions.assertEquals("us-east-1", backendProps.get("AWS_REGION")); |
| 63 | + Assertions.assertEquals("true", backendProps.get("use_path_style")); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testS3PropertiesBinding() { |
| 68 | + origProps.put(StorageProperties.FS_OZONE_SUPPORT, "true"); |
| 69 | + origProps.put("s3.endpoint", "http://ozone-s3g:9878"); |
| 70 | + origProps.put("s3.access_key", "hadoop"); |
| 71 | + origProps.put("s3.secret_key", "hadoop"); |
| 72 | + origProps.put("use_path_style", "true"); |
| 73 | + origProps.put("s3.region", "us-east-1"); |
| 74 | + |
| 75 | + OzoneProperties ozoneProperties = (OzoneProperties) StorageProperties.createPrimary(origProps); |
| 76 | + Map<String, String> backendProps = ozoneProperties.getBackendConfigProperties(); |
| 77 | + |
| 78 | + Assertions.assertEquals("http://ozone-s3g:9878", ozoneProperties.getEndpoint()); |
| 79 | + Assertions.assertEquals("hadoop", ozoneProperties.getAccessKey()); |
| 80 | + Assertions.assertEquals("hadoop", ozoneProperties.getSecretKey()); |
| 81 | + Assertions.assertEquals("true", ozoneProperties.getUsePathStyle()); |
| 82 | + |
| 83 | + Assertions.assertEquals("http://ozone-s3g:9878", backendProps.get("AWS_ENDPOINT")); |
| 84 | + Assertions.assertEquals("hadoop", backendProps.get("AWS_ACCESS_KEY")); |
| 85 | + Assertions.assertEquals("hadoop", backendProps.get("AWS_SECRET_KEY")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testFsS3aPropertiesAreNotSupported() { |
| 90 | + origProps.put(StorageProperties.FS_OZONE_SUPPORT, "true"); |
| 91 | + origProps.put("fs.s3a.endpoint", "http://ozone-s3g:9878"); |
| 92 | + origProps.put("fs.s3a.access.key", "hadoop"); |
| 93 | + origProps.put("fs.s3a.secret.key", "hadoop"); |
| 94 | + |
| 95 | + ExceptionChecker.expectThrowsWithMsg(IllegalArgumentException.class, |
| 96 | + "Property ozone.endpoint is required.", |
| 97 | + () -> StorageProperties.createPrimary(origProps)); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testCreateAllWithDefaultFs() throws UserException { |
| 102 | + origProps.put(StorageProperties.FS_OZONE_SUPPORT, "true"); |
| 103 | + origProps.put("fs.defaultFS", "s3a://dn-data/"); |
| 104 | + origProps.put("s3.endpoint", "http://ozone-s3g:9878"); |
| 105 | + origProps.put("s3.access_key", "hadoop"); |
| 106 | + origProps.put("s3.secret_key", "hadoop"); |
| 107 | + origProps.put("use_path_style", "true"); |
| 108 | + |
| 109 | + List<StorageProperties> properties = StorageProperties.createAll(origProps); |
| 110 | + Assertions.assertEquals(HdfsProperties.class, properties.get(0).getClass()); |
| 111 | + Assertions.assertEquals(OzoneProperties.class, properties.get(1).getClass()); |
| 112 | + |
| 113 | + Map<StorageProperties.Type, StorageProperties> propertiesMap = properties.stream() |
| 114 | + .collect(Collectors.toMap(StorageProperties::getType, Function.identity())); |
| 115 | + LocationPath locationPath = LocationPath.of("s3a://dn-data/warehouse/test_table", propertiesMap); |
| 116 | + Assertions.assertTrue(locationPath.getStorageProperties() instanceof OzoneProperties); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testCreateAllWithDefaultFsAndOzoneProperties() throws UserException { |
| 121 | + origProps.put(StorageProperties.FS_OZONE_SUPPORT, "true"); |
| 122 | + origProps.put("fs.defaultFS", "s3a://dn-data/"); |
| 123 | + origProps.put("ozone.endpoint", "http://ozone-s3g:9878"); |
| 124 | + origProps.put("ozone.access_key", "hadoop"); |
| 125 | + origProps.put("ozone.secret_key", "hadoop"); |
| 126 | + origProps.put("ozone.use_path_style", "true"); |
| 127 | + origProps.put("ozone.region", "us-east-1"); |
| 128 | + |
| 129 | + List<StorageProperties> properties = StorageProperties.createAll(origProps); |
| 130 | + Assertions.assertEquals(HdfsProperties.class, properties.get(0).getClass()); |
| 131 | + Assertions.assertEquals(OzoneProperties.class, properties.get(1).getClass()); |
| 132 | + |
| 133 | + OzoneProperties ozoneProperties = (OzoneProperties) properties.get(1); |
| 134 | + Assertions.assertEquals("hadoop", ozoneProperties.getHadoopStorageConfig().get("fs.s3a.access.key")); |
| 135 | + Assertions.assertEquals("hadoop", ozoneProperties.getHadoopStorageConfig().get("fs.s3a.secret.key")); |
| 136 | + Assertions.assertEquals("http://ozone-s3g:9878", ozoneProperties.getHadoopStorageConfig().get("fs.s3a.endpoint")); |
| 137 | + Assertions.assertEquals("us-east-1", ozoneProperties.getHadoopStorageConfig().get("fs.s3a.endpoint.region")); |
| 138 | + Assertions.assertEquals("true", ozoneProperties.getHadoopStorageConfig().get("fs.s3a.path.style.access")); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testMissingAccessKeyOrSecretKey() { |
| 143 | + origProps.put(StorageProperties.FS_OZONE_SUPPORT, "true"); |
| 144 | + origProps.put("ozone.endpoint", "http://ozone-s3g:9878"); |
| 145 | + origProps.put("ozone.access_key", "hadoop"); |
| 146 | + ExceptionChecker.expectThrowsWithMsg(IllegalArgumentException.class, |
| 147 | + "Both the access key and the secret key must be set.", |
| 148 | + () -> StorageProperties.createPrimary(origProps)); |
| 149 | + |
| 150 | + origProps.remove("ozone.access_key"); |
| 151 | + origProps.put("ozone.secret_key", "hadoop"); |
| 152 | + ExceptionChecker.expectThrowsWithMsg(IllegalArgumentException.class, |
| 153 | + "Both the access key and the secret key must be set.", |
| 154 | + () -> StorageProperties.createPrimary(origProps)); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + public void testMissingEndpoint() { |
| 159 | + origProps.put(StorageProperties.FS_OZONE_SUPPORT, "true"); |
| 160 | + origProps.put("ozone.access_key", "hadoop"); |
| 161 | + origProps.put("ozone.secret_key", "hadoop"); |
| 162 | + ExceptionChecker.expectThrowsWithMsg(IllegalArgumentException.class, |
| 163 | + "Property ozone.endpoint is required.", |
| 164 | + () -> StorageProperties.createPrimary(origProps)); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testRequireExplicitFsOzoneSupport() throws UserException { |
| 169 | + origProps.put("ozone.endpoint", "http://127.0.0.1:9878"); |
| 170 | + origProps.put("ozone.access_key", "hadoop"); |
| 171 | + origProps.put("ozone.secret_key", "hadoop"); |
| 172 | + |
| 173 | + List<StorageProperties> propertiesWithoutFlag = StorageProperties.createAll(origProps); |
| 174 | + Assertions.assertEquals(1, propertiesWithoutFlag.size()); |
| 175 | + Assertions.assertEquals(HdfsProperties.class, propertiesWithoutFlag.get(0).getClass()); |
| 176 | + |
| 177 | + origProps.put(StorageProperties.FS_OZONE_SUPPORT, "true"); |
| 178 | + List<StorageProperties> propertiesWithFlag = StorageProperties.createAll(origProps); |
| 179 | + Assertions.assertEquals(2, propertiesWithFlag.size()); |
| 180 | + Assertions.assertEquals(HdfsProperties.class, propertiesWithFlag.get(0).getClass()); |
| 181 | + Assertions.assertEquals(OzoneProperties.class, propertiesWithFlag.get(1).getClass()); |
| 182 | + } |
| 183 | +} |
0 commit comments