|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.bookkeeper.tools.cli.commands.bookie; |
| 20 | + |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 23 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 24 | +import static org.mockito.Mockito.doAnswer; |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.when; |
| 27 | + |
| 28 | +import org.apache.bookkeeper.bookie.EntryLogMetadata; |
| 29 | +import org.apache.bookkeeper.bookie.ReadOnlyDefaultEntryLogger; |
| 30 | +import org.apache.bookkeeper.client.BKException; |
| 31 | +import org.apache.bookkeeper.meta.LedgerManager; |
| 32 | +import org.apache.bookkeeper.meta.LedgerManagerFactory; |
| 33 | +import org.apache.bookkeeper.proto.BookkeeperInternalCallbacks; |
| 34 | +import org.apache.bookkeeper.tools.cli.helpers.BookieCommandTestBase; |
| 35 | +import org.apache.bookkeeper.util.LedgerIdFormatter; |
| 36 | +import org.apache.zookeeper.AsyncCallback; |
| 37 | +import org.junit.Assert; |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | +/** |
| 41 | + * Unit test for {@link ListActiveLedgersCommand}. |
| 42 | + */ |
| 43 | +public class ListActiveLedgersCommandTest extends BookieCommandTestBase { |
| 44 | + private EntryLogMetadata entryLogMetadata; |
| 45 | + |
| 46 | + public ListActiveLedgersCommandTest() { |
| 47 | + super(3, 3); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void setup() throws Exception { |
| 52 | + super.setup(); |
| 53 | + |
| 54 | + mockServerConfigurationConstruction(); |
| 55 | + |
| 56 | + LedgerManagerFactory mFactory = mock(LedgerManagerFactory.class); |
| 57 | + mockMetadataDriversWithLedgerManagerFactory(mFactory); |
| 58 | + |
| 59 | + mockStatic(LedgerIdFormatter.class) |
| 60 | + .when(() -> LedgerIdFormatter.newLedgerIdFormatter(any())) |
| 61 | + .thenReturn(new LedgerIdFormatter.LongLedgerIdFormatter()); |
| 62 | + |
| 63 | + LedgerManager ledgerManager = mock(LedgerManager.class); |
| 64 | + when(mFactory.newLedgerManager()).thenReturn(ledgerManager); |
| 65 | + |
| 66 | + doAnswer(invocation -> { |
| 67 | + BookkeeperInternalCallbacks.Processor<Long> processor = invocation.getArgument(0); |
| 68 | + AsyncCallback.VoidCallback cb = mock(AsyncCallback.VoidCallback.class); |
| 69 | + processor.process(101L, cb); // only legerId-101 on metadata |
| 70 | + |
| 71 | + AsyncCallback.VoidCallback callback = invocation.getArgument(1); |
| 72 | + callback.processResult(BKException.Code.OK, "", null); |
| 73 | + return true; |
| 74 | + }).when(ledgerManager).asyncProcessLedgers(any(BookkeeperInternalCallbacks.Processor.class), |
| 75 | + any(AsyncCallback.VoidCallback.class), any(), anyInt(), anyInt()); |
| 76 | + |
| 77 | + entryLogMetadata = createEntryLogMeta(); |
| 78 | + mockConstruction(ReadOnlyDefaultEntryLogger.class, (entryLogger, context) -> { |
| 79 | + when(entryLogger.getEntryLogMetadata(anyLong())).thenReturn(entryLogMetadata); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testCommand() { |
| 85 | + ListActiveLedgersCommand command = new ListActiveLedgersCommand(); |
| 86 | + Assert.assertTrue(command.apply(bkFlags, new String[] {"-l", "0", "-t", "1000000"})); |
| 87 | + |
| 88 | + EntryLogMetadata entryLogMetadataToPrint = createEntryLogMeta(); |
| 89 | + entryLogMetadataToPrint.removeLedgerIf(lId -> lId == 100L); |
| 90 | + |
| 91 | + Assert.assertEquals(entryLogMetadataToPrint.getTotalSize(), entryLogMetadata.getTotalSize()); |
| 92 | + Assert.assertEquals(entryLogMetadataToPrint.getRemainingSize(), entryLogMetadata.getRemainingSize()); |
| 93 | + Assert.assertEquals(entryLogMetadataToPrint.getUsage(), entryLogMetadata.getUsage(), 0.0); |
| 94 | + } |
| 95 | + |
| 96 | + private EntryLogMetadata createEntryLogMeta() { |
| 97 | + EntryLogMetadata entryLogMetadata = new EntryLogMetadata(0); |
| 98 | + entryLogMetadata.addLedgerSize(100, 10); |
| 99 | + entryLogMetadata.addLedgerSize(101, 20); |
| 100 | + return entryLogMetadata; |
| 101 | + } |
| 102 | +} |
0 commit comments