|
| 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 | +// 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 | +package com.cloud.utils; |
| 18 | + |
| 19 | +import org.apache.commons.lang.SystemUtils; |
| 20 | +import org.apache.log4j.Logger; |
| 21 | +import org.junit.Assert; |
| 22 | +import org.junit.Assume; |
| 23 | +import org.junit.Ignore; |
| 24 | +import org.junit.Test; |
| 25 | +import org.mockito.Mockito; |
| 26 | + |
| 27 | +import com.cloud.utils.script.OutputInterpreter; |
| 28 | +import com.cloud.utils.script.Script; |
| 29 | + |
| 30 | +public class ScriptTest { |
| 31 | + @Test |
| 32 | + public void testEcho() { |
| 33 | + Assume.assumeTrue(SystemUtils.IS_OS_LINUX); |
| 34 | + Script script = new Script("/bin/echo"); |
| 35 | + script.add("bar"); |
| 36 | + OutputInterpreter.AllLinesParser resultParser = new OutputInterpreter.AllLinesParser(); |
| 37 | + String result = script.execute(resultParser); |
| 38 | + // With allLinesParser, result is not comming from the return value |
| 39 | + Assert.assertNull(result); |
| 40 | + Assert.assertEquals("bar\n", resultParser.getLines()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testLogger() { |
| 45 | + Assume.assumeTrue(SystemUtils.IS_OS_LINUX); |
| 46 | + Logger mock = Mockito.mock(Logger.class); |
| 47 | + Mockito.doNothing().when(mock).debug(Mockito.any()); |
| 48 | + Script script = new Script("/bin/echo", mock); |
| 49 | + script.execute(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testToString() { |
| 54 | + Assume.assumeTrue(SystemUtils.IS_OS_LINUX); |
| 55 | + Script script = new Script("/bin/echo"); |
| 56 | + script.add("foo"); |
| 57 | + Assert.assertEquals("/bin/echo foo ", script.toString()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testSet() { |
| 62 | + Assume.assumeTrue(SystemUtils.IS_OS_LINUX); |
| 63 | + Script script = new Script("/bin/echo"); |
| 64 | + script.add("foo"); |
| 65 | + script.add("bar", "baz"); |
| 66 | + script.set("blah", "blah"); |
| 67 | + Assert.assertEquals("/bin/echo foo bar baz blah blah ", script.toString()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + @Ignore |
| 72 | + public void testExecute() { |
| 73 | + Assume.assumeTrue(SystemUtils.IS_OS_LINUX); |
| 74 | + Logger mock = Mockito.mock(Logger.class); |
| 75 | + Mockito.doNothing().when(mock).debug(Mockito.any()); |
| 76 | + for (int i = 0; i < 100000; i++) { |
| 77 | + Script script = new Script("/bin/false", mock); |
| 78 | + script.execute(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testRunSimpleBashScript() { |
| 84 | + Assume.assumeTrue(SystemUtils.IS_OS_LINUX); |
| 85 | + Assert.assertEquals("hello world!", Script.runSimpleBashScript("echo 'hello world!'")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testRunSimpleBashScriptWithTimeout() { |
| 90 | + Assume.assumeTrue(SystemUtils.IS_OS_LINUX); |
| 91 | + Assert.assertEquals("hello world!", Script.runSimpleBashScript("echo 'hello world!'", 1000)); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testFindScript() { |
| 96 | + Assume.assumeTrue(SystemUtils.IS_OS_LINUX); |
| 97 | + String script = Script.findScript("/usr/bin", "cat"); |
| 98 | + Assert.assertNotNull("/usr/bin/cat shoud be there on linux",script); |
| 99 | + } |
| 100 | +} |
0 commit comments