|
| 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.nereids.trees.plans.commands; |
| 19 | + |
| 20 | +import org.apache.doris.catalog.Env; |
| 21 | +import org.apache.doris.common.proc.TrashProcDir; |
| 22 | +import org.apache.doris.mysql.privilege.AccessControllerManager; |
| 23 | +import org.apache.doris.qe.ConnectContext; |
| 24 | +import org.apache.doris.qe.StmtExecutor; |
| 25 | +import org.apache.doris.system.Backend; |
| 26 | +import org.apache.doris.system.SystemInfoService; |
| 27 | + |
| 28 | +import com.google.common.collect.ImmutableMap; |
| 29 | +import com.google.common.collect.Lists; |
| 30 | +import org.junit.jupiter.api.AfterEach; |
| 31 | +import org.junit.jupiter.api.Assertions; |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import org.mockito.MockedStatic; |
| 35 | +import org.mockito.Mockito; |
| 36 | + |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +public class ShowTrashCommandTest { |
| 40 | + |
| 41 | + private MockedStatic<Env> mockedEnvStatic; |
| 42 | + private MockedStatic<ConnectContext> mockedConnectContextStatic; |
| 43 | + private MockedStatic<TrashProcDir> mockedTrashProcDirStatic; |
| 44 | + |
| 45 | + private SystemInfoService systemInfoService; |
| 46 | + private ConnectContext ctx; |
| 47 | + private StmtExecutor executor; |
| 48 | + private AccessControllerManager accessManager; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + public void setUp() { |
| 52 | + systemInfoService = Mockito.mock(SystemInfoService.class); |
| 53 | + ctx = Mockito.mock(ConnectContext.class); |
| 54 | + executor = Mockito.mock(StmtExecutor.class); |
| 55 | + accessManager = Mockito.mock(AccessControllerManager.class); |
| 56 | + mockedConnectContextStatic = Mockito.mockStatic(ConnectContext.class); |
| 57 | + mockedConnectContextStatic.when(ConnectContext::get).thenReturn(ctx); |
| 58 | + |
| 59 | + Env fakeEnv = Mockito.mock(Env.class); |
| 60 | + Mockito.when(fakeEnv.getAccessManager()).thenReturn(accessManager); |
| 61 | + |
| 62 | + Mockito.when(accessManager.checkGlobalPriv(Mockito.eq(ctx), Mockito.any())).thenReturn(true); |
| 63 | + |
| 64 | + mockedEnvStatic = Mockito.mockStatic(Env.class, Mockito.CALLS_REAL_METHODS); |
| 65 | + mockedEnvStatic.when(Env::getCurrentEnv).thenReturn(fakeEnv); |
| 66 | + mockedEnvStatic.when(Env::getCurrentSystemInfo).thenReturn(systemInfoService); |
| 67 | + |
| 68 | + mockedTrashProcDirStatic = Mockito.mockStatic(TrashProcDir.class); |
| 69 | + mockedTrashProcDirStatic.when(() -> TrashProcDir.getTrashInfo( |
| 70 | + Mockito.anyList(), |
| 71 | + Mockito.anyList() |
| 72 | + )).thenAnswer(invocation -> null); |
| 73 | + } |
| 74 | + |
| 75 | + @AfterEach |
| 76 | + public void tearDown() { |
| 77 | + if (mockedEnvStatic != null) { |
| 78 | + mockedEnvStatic.close(); |
| 79 | + } |
| 80 | + if (mockedConnectContextStatic != null) { |
| 81 | + mockedConnectContextStatic.close(); |
| 82 | + } |
| 83 | + if (mockedTrashProcDirStatic != null) { |
| 84 | + mockedTrashProcDirStatic.close(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testHandleShowTrashWithFilter() throws Exception { |
| 90 | + Backend be1 = new Backend(1001L, "192.168.1.1", 9050); |
| 91 | + Backend be2 = new Backend(1002L, "192.168.1.2", 9050); |
| 92 | + Backend be3 = new Backend(1003L, "192.168.1.3", 9050); |
| 93 | + ImmutableMap<Long, Backend> allBackendsMap = ImmutableMap.of( |
| 94 | + 1001L, be1, |
| 95 | + 1002L, be2, |
| 96 | + 1003L, be3 |
| 97 | + ); |
| 98 | + Mockito.when(systemInfoService.getAllBackendsByAllCluster()).thenReturn(allBackendsMap); |
| 99 | + ShowTrashCommand command = new ShowTrashCommand( |
| 100 | + Lists.newArrayList("192.168.1.1:9050", "192.168.1.3:9050") |
| 101 | + ); |
| 102 | + command.doRun(ctx, executor); |
| 103 | + List<Backend> resultBackends = command.getBackends(); |
| 104 | + Assertions.assertNotNull(resultBackends); |
| 105 | + Assertions.assertEquals(2, resultBackends.size()); |
| 106 | + List<Long> resultIds = Lists.newArrayList(resultBackends.get(0).getId(), resultBackends.get(1).getId()); |
| 107 | + Assertions.assertTrue(resultIds.contains(1001L)); |
| 108 | + Assertions.assertTrue(resultIds.contains(1003L)); |
| 109 | + Assertions.assertFalse(resultIds.contains(1002L)); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testHandleShowTrashWithNonExistentBackend() throws Exception { |
| 114 | + Backend be1 = new Backend(1001L, "192.168.1.1", 9050); |
| 115 | + ImmutableMap<Long, Backend> allBackendsMap = ImmutableMap.of(1001L, be1); |
| 116 | + Mockito.when(systemInfoService.getAllBackendsByAllCluster()).thenReturn(allBackendsMap); |
| 117 | + ShowTrashCommand command = new ShowTrashCommand( |
| 118 | + Lists.newArrayList("192.168.1.1:9050", "192.168.99.99:9999") |
| 119 | + ); |
| 120 | + command.doRun(ctx, executor); |
| 121 | + List<Backend> resultBackends = command.getBackends(); |
| 122 | + Assertions.assertNotNull(resultBackends); |
| 123 | + Assertions.assertEquals(1, resultBackends.size()); |
| 124 | + Assertions.assertEquals(1001L, resultBackends.get(0).getId()); |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments