PageRenderTime 38ms CodeModel.GetById 25ms app.highlight 10ms RepoModel.GetById 0ms app.codeStats 0ms

/compute/src/test/java/org/jclouds/compute/callables/RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilCompleteTest.java

https://github.com/machak/jclouds
Java | 245 lines | 161 code | 52 blank | 32 comment | 0 complexity | 277eabd2669203d0ce3c060fc6e88b7d MD5 | raw file
  1/**
  2 * Licensed to jclouds, Inc. (jclouds) under one or more
  3 * contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  jclouds 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 */
 19package org.jclouds.compute.callables;
 20
 21import static org.easymock.EasyMock.createMock;
 22import static org.easymock.EasyMock.expect;
 23import static org.easymock.EasyMock.replay;
 24import static org.easymock.EasyMock.verify;
 25import static org.jclouds.scriptbuilder.domain.Statements.exec;
 26import static org.testng.Assert.assertEquals;
 27
 28import org.jclouds.Constants;
 29import org.jclouds.compute.domain.ExecResponse;
 30import org.jclouds.compute.domain.NodeMetadata;
 31import org.jclouds.compute.domain.NodeMetadataBuilder;
 32import org.jclouds.compute.domain.NodeState;
 33import org.jclouds.compute.options.RunScriptOptions;
 34import org.jclouds.compute.reference.ComputeServiceConstants;
 35import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts;
 36import org.jclouds.concurrent.MoreExecutors;
 37import org.jclouds.concurrent.config.ExecutorServiceModule;
 38import org.jclouds.domain.Credentials;
 39import org.jclouds.scriptbuilder.InitBuilder;
 40import org.jclouds.scriptbuilder.domain.OsFamily;
 41import org.jclouds.scriptbuilder.domain.Statement;
 42import org.jclouds.ssh.SshClient;
 43import org.testng.annotations.Test;
 44
 45import com.google.common.base.Functions;
 46import com.google.common.collect.ImmutableMap;
 47import com.google.common.collect.ImmutableSet;
 48import com.google.inject.AbstractModule;
 49import com.google.inject.Guice;
 50import com.google.inject.assistedinject.FactoryModuleBuilder;
 51import com.google.inject.name.Names;
 52
 53/**
 54 * @author Adrian Cole
 55 */
 56@Test(groups = "unit", singleThreaded = true, testName = "RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilCompleteTest")
 57public class RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilCompleteTest {
 58
 59   BlockUntilInitScriptStatusIsZeroThenReturnOutput.Factory statusFactory = Guice.createInjector(
 60            new ExecutorServiceModule(MoreExecutors.sameThreadExecutor(), MoreExecutors.sameThreadExecutor()),
 61            new AbstractModule() {
 62
 63               @Override
 64               protected void configure() {
 65                  bindConstant().annotatedWith(Names.named(Constants.PROPERTY_USER_THREADS)).to(1);
 66                  bindConstant().annotatedWith(Names.named(Constants.PROPERTY_IO_WORKER_THREADS)).to(1);
 67                  bindConstant().annotatedWith(Names.named(ComputeServiceConstants.PROPERTY_TIMEOUT_SCRIPT_COMPLETE))
 68                           .to(100);
 69                  install(new FactoryModuleBuilder()
 70                           .build(BlockUntilInitScriptStatusIsZeroThenReturnOutput.Factory.class));
 71               }
 72            }).getInstance(BlockUntilInitScriptStatusIsZeroThenReturnOutput.Factory.class);
 73
 74   // fail faster than normal
 75   Timeouts timeouts = Guice.createInjector(new AbstractModule() {
 76
 77      @Override
 78      protected void configure() {
 79         bindConstant().annotatedWith(Names.named(ComputeServiceConstants.PROPERTY_TIMEOUT_SCRIPT_COMPLETE)).to(100l);
 80      }
 81   }).getInstance(Timeouts.class);
 82
 83   @Test(expectedExceptions = IllegalStateException.class)
 84   public void testWithoutInitThrowsIllegalStateException() {
 85      Statement command = exec("doFoo");
 86      NodeMetadata node = new NodeMetadataBuilder().ids("id").state(NodeState.RUNNING).credentials(
 87               new Credentials("tester", "notalot")).build();
 88
 89      SshClient sshClient = createMock(SshClient.class);
 90
 91      replay(sshClient);
 92
 93      RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete testMe = new RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete(
 94               statusFactory, timeouts, Functions.forMap(ImmutableMap.of(node, sshClient)),
 95               InitScriptConfigurationForTasks.create().appendIncrementingNumberToAnonymousTaskNames(), node, command,
 96               new RunScriptOptions());
 97
 98      testMe.call();
 99   }
100
101   public void testDefault() {
102      Statement command = exec("doFoo");
103      NodeMetadata node = new NodeMetadataBuilder().ids("id").state(NodeState.RUNNING).credentials(
104               new Credentials("tester", "notalot")).build();
105
106      SshClient sshClient = createMock(SshClient.class);
107
108      InitBuilder init = new InitBuilder("jclouds-script-0", "/tmp/jclouds-script-0", "/tmp/jclouds-script-0",
109               ImmutableMap.<String, String> of(), ImmutableSet.of(command));
110
111      sshClient.connect();
112      sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX));
113      expect(sshClient.getUsername()).andReturn("tester").atLeastOnce();
114      expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce();
115
116      // setup script as default user
117      expect(sshClient.exec("chmod 755 /tmp/init-jclouds-script-0")).andReturn(new ExecResponse("", "", 0));
118      expect(sshClient.exec("ln -fs /tmp/init-jclouds-script-0 jclouds-script-0")).andReturn(
119               new ExecResponse("", "", 0));
120      expect(sshClient.exec("./jclouds-script-0 init")).andReturn(new ExecResponse("", "", 0));
121
122      // start script as root via sudo, note that since there's no adminPassword we do a straight
123      // sudo
124      expect(sshClient.exec("sudo ./jclouds-script-0 start")).andReturn(new ExecResponse("", "", 0));
125
126      // signal the command completed
127      expect(sshClient.exec("./jclouds-script-0 status")).andReturn(new ExecResponse("", "", 1));
128      expect(sshClient.exec("./jclouds-script-0 tail")).andReturn(new ExecResponse("out", "", 0));
129      expect(sshClient.exec("./jclouds-script-0 tailerr")).andReturn(new ExecResponse("err", "", 0));
130
131      sshClient.disconnect();
132      replay(sshClient);
133
134      RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete testMe = new RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete(
135               statusFactory, timeouts, Functions.forMap(ImmutableMap.of(node, sshClient)),
136               InitScriptConfigurationForTasks.create().appendIncrementingNumberToAnonymousTaskNames(), node, command,
137               new RunScriptOptions());
138
139      assertEquals(testMe.getInitFile(), "/tmp/init-jclouds-script-0");
140      assertEquals(testMe.getNode(), node);
141      assertEquals(testMe.getStatement(), init);
142
143      testMe.init();
144      
145      assertEquals(testMe.call(), new ExecResponse("out", "err", 0));
146
147      verify(sshClient);
148   }
149
150   public void testWithSudoPassword() {
151      Statement command = exec("doFoo");
152      NodeMetadata node = new NodeMetadataBuilder().ids("id").state(NodeState.RUNNING).credentials(
153               new Credentials("tester", "notalot")).adminPassword("rootme").build();
154
155      SshClient sshClient = createMock(SshClient.class);
156
157      InitBuilder init = new InitBuilder("jclouds-script-0", "/tmp/jclouds-script-0", "/tmp/jclouds-script-0",
158               ImmutableMap.<String, String> of(), ImmutableSet.of(command));
159
160      sshClient.connect();
161      sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX));
162      expect(sshClient.getUsername()).andReturn("tester").atLeastOnce();
163      expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce();
164
165      // setup script as default user
166      expect(sshClient.exec("chmod 755 /tmp/init-jclouds-script-0")).andReturn(new ExecResponse("", "", 0));
167      expect(sshClient.exec("ln -fs /tmp/init-jclouds-script-0 jclouds-script-0")).andReturn(
168               new ExecResponse("", "", 0));
169      expect(sshClient.exec("./jclouds-script-0 init")).andReturn(new ExecResponse("", "", 0));
170
171      // since there's an adminPassword we must pass this in
172      expect(sshClient.exec("echo 'rootme'|sudo -S ./jclouds-script-0 start")).andReturn(new ExecResponse("", "", 0));
173
174      // signal the command completed
175      expect(sshClient.exec("./jclouds-script-0 status")).andReturn(new ExecResponse("", "", 1));
176      expect(sshClient.exec("./jclouds-script-0 tail")).andReturn(new ExecResponse("out", "", 0));
177      expect(sshClient.exec("./jclouds-script-0 tailerr")).andReturn(new ExecResponse("err", "", 0));
178
179      sshClient.disconnect();
180      replay(sshClient);
181
182      RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete testMe = new RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete(
183               statusFactory, timeouts, Functions.forMap(ImmutableMap.of(node, sshClient)),
184               InitScriptConfigurationForTasks.create().appendIncrementingNumberToAnonymousTaskNames(), node, command,
185               new RunScriptOptions());
186
187      assertEquals(testMe.getInitFile(), "/tmp/init-jclouds-script-0");
188      assertEquals(testMe.getNode(), node);
189      assertEquals(testMe.getStatement(), init);
190
191      testMe.init();
192      
193      assertEquals(testMe.call(), new ExecResponse("out", "err", 0));
194      
195      verify(sshClient);
196   }
197
198   public void testNotRoot() {
199      Statement command = exec("doFoo");
200      NodeMetadata node = new NodeMetadataBuilder().ids("id").state(NodeState.RUNNING).credentials(
201               new Credentials("tester", "notalot")).adminPassword("rootme").build();
202
203      SshClient sshClient = createMock(SshClient.class);
204
205      InitBuilder init = new InitBuilder("jclouds-script-0", "/tmp/jclouds-script-0", "/tmp/jclouds-script-0",
206               ImmutableMap.<String, String> of(), ImmutableSet.of(command));
207
208      sshClient.connect();
209      sshClient.put("/tmp/init-jclouds-script-0", init.render(OsFamily.UNIX));
210      expect(sshClient.getUsername()).andReturn("tester").atLeastOnce();
211      expect(sshClient.getHostAddress()).andReturn("somewhere.example.com").atLeastOnce();
212
213      // setup script as default user
214      expect(sshClient.exec("chmod 755 /tmp/init-jclouds-script-0")).andReturn(new ExecResponse("", "", 0));
215      expect(sshClient.exec("ln -fs /tmp/init-jclouds-script-0 jclouds-script-0")).andReturn(
216               new ExecResponse("", "", 0));
217      expect(sshClient.exec("./jclouds-script-0 init")).andReturn(new ExecResponse("", "", 0));
218
219      // kick off as current user
220      expect(sshClient.exec("./jclouds-script-0 start")).andReturn(new ExecResponse("", "", 0));
221
222      // signal the command completed
223      expect(sshClient.exec("./jclouds-script-0 status")).andReturn(new ExecResponse("", "", 1));
224      expect(sshClient.exec("./jclouds-script-0 tail")).andReturn(new ExecResponse("out", "", 0));
225      expect(sshClient.exec("./jclouds-script-0 tailerr")).andReturn(new ExecResponse("err", "", 0));
226
227      sshClient.disconnect();
228      replay(sshClient);
229
230      RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete testMe = new RunScriptOnNodeAsInitScriptUsingSshAndBlockUntilComplete(
231               statusFactory, timeouts, Functions.forMap(ImmutableMap.of(node, sshClient)),
232               InitScriptConfigurationForTasks.create().appendIncrementingNumberToAnonymousTaskNames(), node, command,
233               new RunScriptOptions().runAsRoot(false));
234
235      assertEquals(testMe.getInitFile(), "/tmp/init-jclouds-script-0");
236      assertEquals(testMe.getNode(), node);
237      assertEquals(testMe.getStatement(), init);
238
239      testMe.init();
240      
241      assertEquals(testMe.call(), new ExecResponse("out", "err", 0));
242
243      verify(sshClient);
244   }
245}