ndimiduk commented on a change in pull request #1583: URL: https://github.com/apache/hbase/pull/1583#discussion_r417625196
########## File path: hbase-it/src/test/java/org/apache/hadoop/hbase/TestShellExecCoproc.java ########## @@ -0,0 +1,130 @@ +/* + * 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 + * + * 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 org.apache.hadoop.hbase; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.client.AsyncAdmin; +import org.apache.hadoop.hbase.client.AsyncConnection; +import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecRequest; +import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecResponse; +import org.apache.hadoop.hbase.coprocessor.protobuf.generated.ShellExecEndpoint.ShellExecService; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +/** + * Test for the {@link ShellExecCoproc}. + */ +@Category(MediumTests.class) +public class TestShellExecCoproc { + + @ClassRule + public static final HBaseClassTestRule testRule = + HBaseClassTestRule.forClass(TestShellExecCoproc.class); + + @ClassRule + public static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder() + .setConfiguration(createConfiguration()) + .build(); + + @Rule + public final ConnectionRule connectionRule = + new ConnectionRule(miniClusterRule::createConnection); + + @Test + public void testShellExec() { + final AsyncConnection conn = connectionRule.getConnection(); + final AsyncAdmin admin = conn.getAdmin(); + + final String command = "echo -n \"hello world\""; + final ShellExecRequest req = ShellExecRequest.newBuilder() + .setCommand(command) + .build(); + final ShellExecResponse resp = admin + .<ShellExecService.Stub, ShellExecResponse>coprocessorService( + ShellExecService::newStub, + (stub, controller, callback) -> stub.shellExec(controller, req, callback)) + .join(); + assertEquals(0, resp.getExitCode()); + assertEquals("hello world", resp.getStdout()); + } + + @Test + public void testShellExecAsync() throws IOException { + final AsyncConnection conn = connectionRule.getConnection(); + final AsyncAdmin admin = conn.getAdmin(); + + final File testDataDir = ensureTestDataDirExists(miniClusterRule.getTestingUtility()); + final File testFile = new File(testDataDir, "shell_exec_async.txt"); + assertTrue(testFile.createNewFile()); + assertEquals(0, testFile.length()); + + final String command = "echo \"hello world\" >> " + testFile.getAbsolutePath(); + final ShellExecRequest req = ShellExecRequest.newBuilder() + .setCommand(command) + .build(); + admin.<ShellExecService.Stub, ShellExecResponse>coprocessorService( + ShellExecService::newStub, + (stub, controller, callback) -> stub.shellExecAsync(controller, req, callback)) + .join(); + + Waiter.waitFor(conn.getConfiguration(), 5_000, () -> testFile.length() > 0); Review comment: > This seems a good idea. Just concerned both `testFile` and `cat` can be empty, so... This could be caught by the final `assertEquals` at the end of the test. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
