|
34 | 34 |
|
35 | 35 | package com.starrocks.plugin; |
36 | 36 |
|
| 37 | +import com.google.common.collect.Maps; |
37 | 38 | import com.starrocks.common.Config; |
38 | 39 | import com.starrocks.common.util.DigitalVersion; |
| 40 | +import com.starrocks.persist.EditLog; |
| 41 | +import com.starrocks.persist.UninstallPluginLog; |
| 42 | +import com.starrocks.persist.WALApplier; |
39 | 43 | import com.starrocks.plugin.PluginInfo.PluginType; |
40 | 44 | import com.starrocks.server.GlobalStateMgr; |
| 45 | +import com.starrocks.sql.ast.InstallPluginStmt; |
| 46 | +import com.starrocks.sql.ast.UninstallPluginStmt; |
| 47 | +import com.starrocks.sql.parser.NodePosition; |
41 | 48 | import com.starrocks.utframe.UtFrameUtils; |
| 49 | +import mockit.Mock; |
| 50 | +import mockit.MockUp; |
42 | 51 | import org.apache.commons.io.FileUtils; |
43 | 52 | import org.junit.jupiter.api.BeforeAll; |
44 | 53 | import org.junit.jupiter.api.BeforeEach; |
45 | 54 | import org.junit.jupiter.api.Test; |
46 | 55 |
|
47 | 56 | import java.io.IOException; |
48 | 57 | import java.nio.file.Files; |
| 58 | +import java.util.Map; |
49 | 59 |
|
| 60 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
50 | 61 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 62 | +import static org.junit.jupiter.api.Assertions.assertNull; |
51 | 63 | import static org.junit.jupiter.api.Assertions.assertTrue; |
52 | 64 |
|
53 | 65 | public class PluginMgrTest { |
@@ -89,4 +101,53 @@ public void testLoadPluginFail() { |
89 | 101 | assert false; |
90 | 102 | } |
91 | 103 | } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testInstallPluginIfNotExistsIdempotent() throws Exception { |
| 107 | + String pluginName = "idempotent_install_plugin"; |
| 108 | + PluginInfo pluginInfo = new PluginInfo(pluginName, PluginType.AUDIT, "test"); |
| 109 | + |
| 110 | + PluginMgr pluginMgr = new PluginMgr(); |
| 111 | + pluginMgr.replayLoadDynamicPlugin(pluginInfo); |
| 112 | + |
| 113 | + new MockUp<DynamicPluginLoader>() { |
| 114 | + @Mock |
| 115 | + public PluginInfo getPluginInfo() throws IOException { |
| 116 | + return pluginInfo; |
| 117 | + } |
| 118 | + }; |
| 119 | + |
| 120 | + Map<String, String> props = Maps.newHashMap(); |
| 121 | + InstallPluginStmt stmt = new InstallPluginStmt("http://dummy/test.zip", props, true, NodePosition.ZERO); |
| 122 | + PluginInfo result = pluginMgr.installPlugin(stmt); |
| 123 | + assertNull(result); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testUninstallPluginIfExistsIdempotent() { |
| 128 | + PluginMgr pluginMgr = new PluginMgr(); |
| 129 | + UninstallPluginStmt stmt = new UninstallPluginStmt("nonexistent_plugin", true, NodePosition.ZERO); |
| 130 | + assertDoesNotThrow(() -> pluginMgr.uninstallPluginFromStmt(stmt)); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testUninstallPluginFromStmtNormal() throws Exception { |
| 135 | + String pluginName = "normal_uninstall_plugin"; |
| 136 | + PluginInfo pluginInfo = new PluginInfo(pluginName, PluginType.AUDIT, "test"); |
| 137 | + |
| 138 | + PluginMgr pluginMgr = new PluginMgr(); |
| 139 | + pluginMgr.replayLoadDynamicPlugin(pluginInfo); |
| 140 | + |
| 141 | + new MockUp<EditLog>() { |
| 142 | + @Mock |
| 143 | + public void logUninstallPlugin(UninstallPluginLog log, WALApplier walApplier) { |
| 144 | + walApplier.apply(log); |
| 145 | + } |
| 146 | + }; |
| 147 | + |
| 148 | + UninstallPluginStmt stmt = new UninstallPluginStmt(pluginName); |
| 149 | + pluginMgr.uninstallPluginFromStmt(stmt); |
| 150 | + assertFalse(pluginMgr.getAllDynamicPluginInfo() |
| 151 | + .stream().anyMatch(p -> p.getName().equals(pluginName))); |
| 152 | + } |
92 | 153 | } |
0 commit comments