add UT for control plugin (#11611)

* add ut for control plugin

* control plugin fix LocalDiskRuleStorage mkdirs
This commit is contained in:
hth 2024-01-11 17:11:47 +08:00 committed by GitHub
parent b4a380f15b
commit 207505735c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 738 additions and 0 deletions

View File

@ -0,0 +1,100 @@
/*
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.plugin.control;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.connection.request.ConnectionCheckRequest;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckCode;
import com.alibaba.nacos.plugin.control.connection.response.ConnectionCheckResponse;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import com.alibaba.nacos.plugin.control.spi.ControlManagerBuilder;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.plugin.control.tps.barrier.TpsBarrier;
import com.alibaba.nacos.plugin.control.tps.request.TpsCheckRequest;
import com.alibaba.nacos.plugin.control.tps.response.TpsCheckResponse;
import com.alibaba.nacos.plugin.control.tps.response.TpsResultCode;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
import java.util.Map;
public class ControlManagerBuilderTest implements ControlManagerBuilder {
@Override
public String getName() {
return "test";
}
@Override
public ConnectionControlManager buildConnectionControlManager() {
return new ConnectionControlManager() {
@Override
public String getName() {
return "testConnection";
}
@Override
public void applyConnectionLimitRule(ConnectionControlRule connectionControlRule) {
}
@Override
public ConnectionCheckResponse check(ConnectionCheckRequest connectionCheckRequest) {
ConnectionCheckResponse connectionCheckResponse = new ConnectionCheckResponse();
connectionCheckResponse.setSuccess(true);
connectionCheckResponse.setCode(ConnectionCheckCode.PASS_BY_TOTAL);
return connectionCheckResponse;
}
};
}
@Override
public TpsControlManager buildTpsControlManager() {
return new TpsControlManager() {
@Override
public void registerTpsPoint(String pointName) {
}
@Override
public Map<String, TpsBarrier> getPoints() {
return null;
}
@Override
public Map<String, TpsControlRule> getRules() {
return null;
}
@Override
public void applyTpsRule(String pointName, TpsControlRule rule) {
}
@Override
public TpsCheckResponse check(TpsCheckRequest tpsRequest) {
return new TpsCheckResponse(true, TpsResultCode.CHECK_SKIP, "skip");
}
@Override
public String getName() {
return "testTps";
}
};
}
}

View File

@ -0,0 +1,241 @@
/*
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.plugin.control;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.plugin.control.configs.ControlConfigs;
import com.alibaba.nacos.plugin.control.connection.ConnectionControlManager;
import com.alibaba.nacos.plugin.control.connection.rule.ConnectionControlRule;
import com.alibaba.nacos.plugin.control.rule.storage.RuleStorageProxy;
import com.alibaba.nacos.plugin.control.tps.MonitorType;
import com.alibaba.nacos.plugin.control.tps.TpsControlManager;
import com.alibaba.nacos.plugin.control.tps.rule.RuleDetail;
import com.alibaba.nacos.plugin.control.tps.rule.TpsControlRule;
import com.alibaba.nacos.plugin.control.utils.EnvUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.concurrent.TimeUnit;
public class ControlManagerCenterTest {
@Before
public void initInstance() throws NoSuchFieldException, IllegalAccessException {
//reset instance for reload spi
Field instanceControlConfigs = ControlConfigs.class.getDeclaredField("instance");
instanceControlConfigs.setAccessible(true);
instanceControlConfigs.set(null, null);
Field instanceControlManagerCenter = ControlManagerCenter.class.getDeclaredField("instance");
instanceControlManagerCenter.setAccessible(true);
instanceControlManagerCenter.set(null, null);
}
private void resetRuleStorageProxy() {
try {
//reset instance for reload spi
Field instanceRuleStorageProxy = RuleStorageProxy.class.getDeclaredField("INSTANCE");
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(instanceRuleStorageProxy, instanceRuleStorageProxy.getModifiers() & ~Modifier.FINAL);
instanceRuleStorageProxy.setAccessible(true);
Constructor<RuleStorageProxy> constructor = RuleStorageProxy.class.getDeclaredConstructor();
constructor.setAccessible(true);
Field modifiersFieldConstructor = Constructor.class.getDeclaredField("modifiers");
modifiersFieldConstructor.setAccessible(true);
modifiersFieldConstructor.setInt(constructor, constructor.getModifiers() & ~Modifier.PRIVATE);
instanceRuleStorageProxy.set(null, constructor.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testGetInstance() {
ControlConfigs.getInstance().setControlManagerType("test");
ControlManagerCenter controlManagerCenter = ControlManagerCenter.getInstance();
ConnectionControlManager connectionControlManager = controlManagerCenter.getConnectionControlManager();
Assert.assertEquals("testConnection", connectionControlManager.getName());
TpsControlManager tpsControlManager = controlManagerCenter.getTpsControlManager();
Assert.assertEquals("testTps", tpsControlManager.getName());
Assert.assertNotNull(controlManagerCenter.getRuleStorageProxy());
}
@Test
public void testGetInstanceWithDefault() {
ControlManagerCenter controlManagerCenter = ControlManagerCenter.getInstance();
ConnectionControlManager connectionControlManager = controlManagerCenter.getConnectionControlManager();
Assert.assertEquals("noLimit", connectionControlManager.getName());
TpsControlManager tpsControlManager = controlManagerCenter.getTpsControlManager();
Assert.assertEquals("noLimit", tpsControlManager.getName());
}
@Test
public void testReloadTpsControlRule() throws Exception {
String localRuleStorageBaseDir =
EnvUtils.getNacosHome() + File.separator + "tmpTps" + File.separator + "tps" + File.separator;
ControlConfigs.getInstance().setLocalRuleStorageBaseDir(localRuleStorageBaseDir);
resetRuleStorageProxy();
final ControlManagerCenter controlManagerCenter = ControlManagerCenter.getInstance();
TpsControlRule tpsControlRule = new TpsControlRule();
tpsControlRule.setPointName("test");
RuleDetail ruleDetail = new RuleDetail();
ruleDetail.setMaxCount(100);
ruleDetail.setRuleName("test");
ruleDetail.setMonitorType(MonitorType.INTERCEPT.getType());
ruleDetail.setPeriod(TimeUnit.SECONDS);
tpsControlRule.setPointRule(ruleDetail);
String ruleContent = JacksonUtils.toJson(tpsControlRule);
controlManagerCenter.getRuleStorageProxy().getLocalDiskStorage().saveTpsRule("test", ruleContent);
controlManagerCenter.getTpsControlManager().applyTpsRule("test", tpsControlRule);
TpsControlRule testTpsControlRule = controlManagerCenter.getTpsControlManager().getRules().get("test");
Assert.assertEquals(100, testTpsControlRule.getPointRule().getMaxCount());
Assert.assertEquals("test", testTpsControlRule.getPointRule().getRuleName());
TpsControlRule tpsControlRule2 = new TpsControlRule();
tpsControlRule2.setPointName("test");
RuleDetail ruleDetail2 = new RuleDetail();
ruleDetail2.setMaxCount(200);
ruleDetail2.setRuleName("test2");
ruleDetail2.setMonitorType(MonitorType.INTERCEPT.getType());
ruleDetail2.setPeriod(TimeUnit.SECONDS);
tpsControlRule2.setPointRule(ruleDetail2);
String ruleContent2 = JacksonUtils.toJson(tpsControlRule2);
controlManagerCenter.getRuleStorageProxy().getLocalDiskStorage().saveTpsRule("test", ruleContent2);
controlManagerCenter.reloadTpsControlRule("test", false);
//wait event
TimeUnit.SECONDS.sleep(1);
TpsControlRule testTpsControlRule2 = controlManagerCenter.getTpsControlManager().getRules().get("test");
Assert.assertEquals(200, testTpsControlRule2.getPointRule().getMaxCount());
Assert.assertEquals("test2", testTpsControlRule2.getPointRule().getRuleName());
}
@Test
public void testReloadTpsControlRuleExternal() throws Exception {
String localRuleStorageBaseDir =
EnvUtils.getNacosHome() + File.separator + "tmpTps" + File.separator + "tpsExternal" + File.separator;
ControlConfigs.getInstance().setLocalRuleStorageBaseDir(localRuleStorageBaseDir);
ControlConfigs.getInstance().setRuleExternalStorage("test");
resetRuleStorageProxy();
final ControlManagerCenter controlManagerCenter = ControlManagerCenter.getInstance();
TpsControlRule tpsControlRule = new TpsControlRule();
tpsControlRule.setPointName("test");
RuleDetail ruleDetail = new RuleDetail();
ruleDetail.setMaxCount(100);
ruleDetail.setRuleName("test");
ruleDetail.setMonitorType(MonitorType.INTERCEPT.getType());
ruleDetail.setPeriod(TimeUnit.SECONDS);
tpsControlRule.setPointRule(ruleDetail);
String ruleContent = JacksonUtils.toJson(tpsControlRule);
controlManagerCenter.getRuleStorageProxy().getExternalStorage().saveTpsRule("test", ruleContent);
controlManagerCenter.getTpsControlManager().applyTpsRule("test", tpsControlRule);
TpsControlRule testTpsControlRule = controlManagerCenter.getTpsControlManager().getRules().get("test");
Assert.assertEquals(100, testTpsControlRule.getPointRule().getMaxCount());
Assert.assertEquals("test", testTpsControlRule.getPointRule().getRuleName());
TpsControlRule tpsControlRule2 = new TpsControlRule();
tpsControlRule2.setPointName("test");
RuleDetail ruleDetail2 = new RuleDetail();
ruleDetail2.setMaxCount(200);
ruleDetail2.setRuleName("test2");
ruleDetail2.setMonitorType(MonitorType.INTERCEPT.getType());
ruleDetail2.setPeriod(TimeUnit.SECONDS);
tpsControlRule2.setPointRule(ruleDetail2);
String ruleContent2 = JacksonUtils.toJson(tpsControlRule2);
controlManagerCenter.getRuleStorageProxy().getExternalStorage().saveTpsRule("test", ruleContent2);
controlManagerCenter.reloadTpsControlRule("test", true);
//wait event
TimeUnit.SECONDS.sleep(1);
TpsControlRule testTpsControlRule2 = controlManagerCenter.getTpsControlManager().getRules().get("test");
Assert.assertEquals(200, testTpsControlRule2.getPointRule().getMaxCount());
Assert.assertEquals("test2", testTpsControlRule2.getPointRule().getRuleName());
}
@Test
public void testReloadConnectionControlRule() throws Exception {
String localRuleStorageBaseDir =
EnvUtils.getNacosHome() + File.separator + "tmpConnection" + File.separator + "connection"
+ File.separator;
ControlConfigs.getInstance().setLocalRuleStorageBaseDir(localRuleStorageBaseDir);
resetRuleStorageProxy();
ConnectionControlRule connectionLimitRule = new ConnectionControlRule();
connectionLimitRule.setCountLimit(100);
String ruleContent = JacksonUtils.toJson(connectionLimitRule);
ControlManagerCenter controlManagerCenter = ControlManagerCenter.getInstance();
controlManagerCenter.getRuleStorageProxy().getLocalDiskStorage().saveConnectionRule(ruleContent);
ConnectionControlManager connectionControlManager = controlManagerCenter.getConnectionControlManager();
//apply rule
connectionControlManager.applyConnectionLimitRule(connectionLimitRule);
ConnectionControlRule connectionLimitRule1 = connectionControlManager.getConnectionLimitRule();
Assert.assertEquals(100, connectionLimitRule1.getCountLimit());
ConnectionControlRule connectionLimitRule2 = new ConnectionControlRule();
connectionLimitRule2.setCountLimit(200);
String ruleContent2 = JacksonUtils.toJson(connectionLimitRule2);
controlManagerCenter.getRuleStorageProxy().getLocalDiskStorage().saveConnectionRule(ruleContent2);
//reload new rule
controlManagerCenter.reloadConnectionControlRule(false);
//wait event
TimeUnit.SECONDS.sleep(1);
ConnectionControlRule connectionLimitRule3 = connectionControlManager.getConnectionLimitRule();
Assert.assertEquals(200, connectionLimitRule3.getCountLimit());
}
@Test
public void testReloadConnectionControlRuleExternal() throws Exception {
String localRuleStorageBaseDir =
EnvUtils.getNacosHome() + File.separator + "tmpConnection" + File.separator + "connectionExternal"
+ File.separator;
ControlConfigs.getInstance().setLocalRuleStorageBaseDir(localRuleStorageBaseDir);
ControlConfigs.getInstance().setRuleExternalStorage("test");
resetRuleStorageProxy();
ConnectionControlRule connectionLimitRule = new ConnectionControlRule();
connectionLimitRule.setCountLimit(100);
String ruleContent = JacksonUtils.toJson(connectionLimitRule);
ControlManagerCenter controlManagerCenter = ControlManagerCenter.getInstance();
controlManagerCenter.getRuleStorageProxy().getExternalStorage().saveConnectionRule(ruleContent);
ConnectionControlManager connectionControlManager = controlManagerCenter.getConnectionControlManager();
//apply rule
connectionControlManager.applyConnectionLimitRule(connectionLimitRule);
ConnectionControlRule connectionLimitRule1 = connectionControlManager.getConnectionLimitRule();
Assert.assertEquals(100, connectionLimitRule1.getCountLimit());
ConnectionControlRule connectionLimitRule2 = new ConnectionControlRule();
connectionLimitRule2.setCountLimit(200);
String ruleContent2 = JacksonUtils.toJson(connectionLimitRule2);
controlManagerCenter.getRuleStorageProxy().getExternalStorage().saveConnectionRule(ruleContent2);
//reload new rule
controlManagerCenter.reloadConnectionControlRule(true);
//wait event
TimeUnit.SECONDS.sleep(1);
ConnectionControlRule connectionLimitRule3 = connectionControlManager.getConnectionLimitRule();
Assert.assertEquals(200, connectionLimitRule3.getCountLimit());
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.plugin.control.spi;
import com.alibaba.nacos.plugin.control.rule.storage.ExternalRuleStorage;
public class ExternalRuleStorageBuilderTest implements ExternalRuleStorageBuilder {
@Override
public String getName() {
return "test";
}
@Override
public ExternalRuleStorage buildExternalRuleStorage() {
return new ExternalRuleStorageTest();
}
}

View File

@ -0,0 +1,54 @@
/*
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.plugin.control.spi;
import com.alibaba.nacos.plugin.control.rule.storage.ExternalRuleStorage;
import java.util.HashMap;
import java.util.Map;
public class ExternalRuleStorageTest implements ExternalRuleStorage {
private String ruleContent;
private Map<String, String> tpsRuleMap = new HashMap<>(1);
@Override
public String getName() {
return "testExternal";
}
@Override
public void saveConnectionRule(String ruleContent) throws Exception {
this.ruleContent = ruleContent;
}
@Override
public String getConnectionRule() {
return ruleContent;
}
@Override
public void saveTpsRule(String pointName, String ruleContent) throws Exception {
tpsRuleMap.put(pointName, ruleContent);
}
@Override
public String getTpsRule(String pointName) {
return tpsRuleMap.get(pointName);
}
}

View File

@ -0,0 +1,242 @@
/*
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.plugin.control.utils;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
public class DiskUtilsTest {
private static File testFile;
private static final String TMP_PATH = EnvUtils.getNacosHome() + File.separator + "data" + File.separator + "tmp" + File.separator;
@BeforeClass
public static void setup() throws IOException {
testFile = DiskUtils.createTmpFile("nacostmp", ".ut");
}
@AfterClass
public static void tearDown() throws IOException {
testFile.deleteOnExit();
}
@Test
public void testTouch() throws IOException {
File file = Paths.get(TMP_PATH, "touch.ut").toFile();
Assert.assertFalse(file.exists());
DiskUtils.touch(file);
Assert.assertTrue(file.exists());
file.deleteOnExit();
}
@Test
public void testTouchWithFileName() throws IOException {
File file = Paths.get(TMP_PATH, UUID.randomUUID().toString()).toFile();
Assert.assertFalse(file.exists());
DiskUtils.touch(file.getParent(), file.getName());
Assert.assertTrue(file.exists());
file.deleteOnExit();
}
@Test
public void testCreateTmpFile() throws IOException {
File tmpFile = null;
try {
tmpFile = DiskUtils.createTmpFile("nacos1", ".ut");
Assert.assertTrue(tmpFile.getName().startsWith("nacos1"));
Assert.assertTrue(tmpFile.getName().endsWith(".ut"));
} finally {
if (tmpFile != null) {
tmpFile.deleteOnExit();
}
}
}
@Test
public void testCreateTmpFileWithPath() throws IOException {
File tmpFile = null;
try {
tmpFile = DiskUtils.createTmpFile(TMP_PATH, "nacos1", ".ut");
Assert.assertEquals(TMP_PATH, tmpFile.getParent() + File.separator);
Assert.assertTrue(tmpFile.getName().startsWith("nacos1"));
Assert.assertTrue(tmpFile.getName().endsWith(".ut"));
} finally {
if (tmpFile != null) {
tmpFile.deleteOnExit();
}
}
}
@Test
public void testReadFile() {
Assert.assertNotNull(DiskUtils.readFile(testFile));
}
@Test
public void testReadFileWithInputStream() throws FileNotFoundException {
Assert.assertNotNull(DiskUtils.readFile(new FileInputStream(testFile)));
}
@Test
public void testReadFileWithPath() {
Assert.assertNotNull(DiskUtils.readFile(testFile.getParent(), testFile.getName()));
}
@Test
public void testReadFileBytes() {
Assert.assertNotNull(DiskUtils.readFileBytes(testFile));
}
@Test
public void testReadFileBytesWithPath() {
Assert.assertNotNull(DiskUtils.readFileBytes(testFile.getParent(), testFile.getName()));
}
@Test
public void writeFile() {
Assert.assertTrue(DiskUtils.writeFile(testFile, "unit test".getBytes(StandardCharsets.UTF_8), false));
Assert.assertEquals("unit test", DiskUtils.readFile(testFile));
}
@Test
public void deleteQuietly() throws IOException {
File tmpFile = DiskUtils.createTmpFile(UUID.randomUUID().toString(), ".ut");
DiskUtils.deleteQuietly(tmpFile);
Assert.assertFalse(tmpFile.exists());
}
@Test
public void testDeleteQuietlyWithPath() throws IOException {
String dir = TMP_PATH + "/" + "diskutils";
DiskUtils.forceMkdir(dir);
DiskUtils.createTmpFile(dir, "nacos", ".ut");
Path path = Paths.get(dir);
DiskUtils.deleteQuietly(path);
Assert.assertFalse(path.toFile().exists());
}
@Test
public void testDeleteFile() throws IOException {
File tmpFile = DiskUtils.createTmpFile(UUID.randomUUID().toString(), ".ut");
Assert.assertTrue(DiskUtils.deleteFile(tmpFile.getParent(), tmpFile.getName()));
Assert.assertFalse(DiskUtils.deleteFile(tmpFile.getParent(), tmpFile.getName()));
}
@Test
public void deleteDirectory() throws IOException {
Path diskutils = Paths.get(TMP_PATH, "diskutils");
File file = diskutils.toFile();
if (!file.exists()) {
file.mkdir();
}
Assert.assertTrue(file.exists());
DiskUtils.deleteDirectory(diskutils.toString());
Assert.assertFalse(file.exists());
}
@Test
public void testForceMkdir() throws IOException {
File dir = Paths.get(TMP_PATH, UUID.randomUUID().toString(), UUID.randomUUID().toString())
.toFile();
DiskUtils.forceMkdir(dir);
Assert.assertTrue(dir.exists());
dir.deleteOnExit();
}
@Test
public void testForceMkdirWithPath() throws IOException {
Path path = Paths.get(TMP_PATH, UUID.randomUUID().toString(), UUID.randomUUID().toString());
DiskUtils.forceMkdir(path.toString());
File file = path.toFile();
Assert.assertTrue(file.exists());
file.deleteOnExit();
}
@Test
public void deleteDirThenMkdir() throws IOException {
Path path = Paths.get(TMP_PATH, UUID.randomUUID().toString());
DiskUtils.forceMkdir(path.toString());
DiskUtils.createTmpFile(path.toString(), UUID.randomUUID().toString(), ".ut");
DiskUtils.createTmpFile(path.toString(), UUID.randomUUID().toString(), ".ut");
DiskUtils.deleteDirThenMkdir(path.toString());
File file = path.toFile();
Assert.assertTrue(file.exists());
Assert.assertTrue(file.isDirectory());
Assert.assertTrue(file.list() == null || file.list().length == 0);
file.deleteOnExit();
}
@Test
public void testCopyDirectory() throws IOException {
Path srcPath = Paths.get(TMP_PATH, UUID.randomUUID().toString());
DiskUtils.forceMkdir(srcPath.toString());
File nacos = DiskUtils.createTmpFile(srcPath.toString(), "nacos", ".ut");
Path destPath = Paths.get(TMP_PATH, UUID.randomUUID().toString());
DiskUtils.copyDirectory(srcPath.toFile(), destPath.toFile());
File file = Paths.get(destPath.toString(), nacos.getName()).toFile();
Assert.assertTrue(file.exists());
DiskUtils.deleteDirectory(srcPath.toString());
DiskUtils.deleteDirectory(destPath.toString());
}
@Test
public void testCopyFile() throws IOException {
File nacos = DiskUtils.createTmpFile("nacos", ".ut");
DiskUtils.copyFile(testFile, nacos);
Assert.assertEquals(DiskUtils.readFile(testFile), DiskUtils.readFile(nacos));
nacos.deleteOnExit();
}
@Test
public void openFile() {
File file = DiskUtils.openFile(testFile.getParent(), testFile.getName());
Assert.assertNotNull(file);
Assert.assertEquals(testFile.getPath(), file.getPath());
Assert.assertEquals(testFile.getName(), file.getName());
}
@Test
public void testOpenFileWithPath() {
File file = DiskUtils.openFile(testFile.getParent(), testFile.getName(), false);
Assert.assertNotNull(file);
Assert.assertEquals(testFile.getPath(), file.getPath());
Assert.assertEquals(testFile.getName(), file.getName());
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 1999-2024 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.plugin.control.utils;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
public class EnvUtilsTest {
@Test
public void test() {
String nacosHome = EnvUtils.getNacosHome();
Assert.assertEquals(System.getProperty("user.home") + File.separator + "nacos", nacosHome);
System.setProperty("nacos.home", "test");
String testHome = EnvUtils.getNacosHome();
Assert.assertEquals("test", testHome);
}
}

View File

@ -0,0 +1,17 @@
#
# Copyright 1999-2024 Alibaba Group Holding Ltd.
#
# Licensed 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.
#
com.alibaba.nacos.plugin.control.ControlManagerBuilderTest

View File

@ -0,0 +1,17 @@
#
# Copyright 1999-2024 Alibaba Group Holding Ltd.
#
# Licensed 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.
#
com.alibaba.nacos.plugin.control.spi.ExternalRuleStorageBuilderTest