mladjan-gadzic commented on code in PR #5724: URL: https://github.com/apache/ozone/pull/5724#discussion_r1485307063
########## hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/debug/TestContainerKeyScanner.java: ########## @@ -0,0 +1,252 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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 org.apache.hadoop.ozone.debug; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import org.apache.hadoop.hdds.client.BlockID; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.protocol.proto.HddsProtos; +import org.apache.hadoop.hdds.utils.db.DBStore; +import org.apache.hadoop.hdds.utils.db.DBStoreBuilder; +import org.apache.hadoop.hdds.utils.db.Table; +import org.apache.hadoop.ozone.ClientVersion; +import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo; +import org.apache.hadoop.ozone.om.helpers.OmKeyInfo; +import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo; +import org.apache.hadoop.ozone.om.request.OMRequestTestUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import picocli.CommandLine; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; +import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.DIRECTORY_TABLE; +import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.FILE_TABLE; +import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.KEY_TABLE; + +/** + * This class tests `ozone debug ckscanner` CLI that reads from RocksDB + * and gets keys for container ids. + */ +public class TestContainerKeyScanner { + private DBStore dbStore; + @TempDir + private File tempDir; + private StringWriter stdout, stderr; + private PrintWriter pstdout, pstderr; + private CommandLine cmd; + private static final Gson GSON = + new GsonBuilder().setPrettyPrinting().create(); + private static final ContainerKeyInfo KEY_ONE = + new ContainerKeyInfo(1L, "vol1", -123L, "bucket1", -456L, "dir1/key1", + -789L); + private static final ContainerKeyInfo KEY_TWO = + new ContainerKeyInfo(2L, "vol1", 0L, "bucket1", 0L, "key2", 0L); + private static final ContainerKeyInfo KEY_THREE = + new ContainerKeyInfo(3L, "vol1", 0L, "bucket1", 0L, "key3", 0L); + + private static final Map<Long, List<ContainerKeyInfo>> CONTAINER_KEYS = + new HashMap<>(); + + static { + List<ContainerKeyInfo> list1 = new ArrayList<>(); + list1.add(KEY_ONE); + List<ContainerKeyInfo> list2 = new ArrayList<>(); + list2.add(KEY_TWO); + List<ContainerKeyInfo> list3 = new ArrayList<>(); + list3.add(KEY_THREE); + CONTAINER_KEYS.put(1L, list1); + CONTAINER_KEYS.put(2L, list2); + CONTAINER_KEYS.put(3L, list3); + } + + private static final ContainerKeyInfoResponse KEYS_FOUND_OUTPUT = + new ContainerKeyInfoResponse(3, CONTAINER_KEYS); + + private static final String KEYS_NOT_FOUND_OUTPUT = + "No keys were found for container IDs: [1, 2, 3]\n" + + "Keys processed: 3\n"; + + @BeforeEach + public void setup() throws IOException { + OzoneConfiguration conf = new OzoneConfiguration(); + stdout = new StringWriter(); + pstdout = new PrintWriter(stdout); + stderr = new StringWriter(); + pstderr = new PrintWriter(stderr); + + cmd = new CommandLine(new OzoneDebug()) + .addSubcommand(new ContainerKeyScanner()) + .setOut(pstdout) + .setErr(pstderr); + + dbStore = DBStoreBuilder.newBuilder(conf).setName("om.db") + .setPath(tempDir.toPath()).addTable(KEY_TABLE).addTable(FILE_TABLE) + .addTable(DIRECTORY_TABLE) + .build(); + } + + @AfterEach + public void shutdown() throws IOException { + closeDbStore(); + pstderr.close(); + stderr.close(); + pstdout.close(); + stdout.close(); + } + + @Test + void testWhenThereAreKeysForContainerIds() throws IOException { + + // create keys for tables + long volumeId = -123L; + long bucketId = -456L; + long dirObjectId = -789L; + createDirectory(volumeId, bucketId, bucketId, dirObjectId, "dir1"); + createFile(volumeId, bucketId, "key1", -987L, dirObjectId, 1L); + createKey("key2", 2L); + createKey("key3", 3L); + + String[] cmdArgs = + {"ckscanner", "--om-db", dbStore.getDbLocation().getAbsolutePath(), + "--container-ids", "1 2 3"}; + + closeDbStore(); + + int exitCode = cmd.execute(cmdArgs); + Assertions.assertEquals(0, exitCode); Review Comment: Done. Furthermore, I am going to use assertj instead of junit assertions, because I do not like to mix those in a single test class. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org For additional commands, e-mail: issues-h...@ozone.apache.org