PageRenderTime 815ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/chef/src/test/java/org/jclouds/chef/functions/GroupToBootScriptTest.java

http://github.com/jclouds/jclouds
Java | 277 lines | 210 code | 51 blank | 16 comment | 0 complexity | 6e86f45b8d1690396b089a67b8197031 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  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, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.chef.functions;
  18. import static org.easymock.EasyMock.createMock;
  19. import static org.easymock.EasyMock.expect;
  20. import static org.easymock.EasyMock.replay;
  21. import static org.easymock.EasyMock.verify;
  22. import static org.jclouds.chef.config.ChefProperties.CHEF_UPDATE_GEMS;
  23. import static org.jclouds.chef.config.ChefProperties.CHEF_UPDATE_GEM_SYSTEM;
  24. import static org.jclouds.chef.config.ChefProperties.CHEF_USE_OMNIBUS;
  25. import static org.testng.Assert.assertEquals;
  26. import java.io.IOException;
  27. import java.net.URI;
  28. import java.security.PrivateKey;
  29. import org.jclouds.chef.ChefApiMetadata;
  30. import org.jclouds.chef.config.ChefBootstrapModule;
  31. import org.jclouds.chef.config.ChefParserModule;
  32. import org.jclouds.chef.config.InstallChef;
  33. import org.jclouds.chef.domain.BootstrapConfig;
  34. import org.jclouds.chef.domain.BootstrapConfig.SSLVerifyMode;
  35. import org.jclouds.chef.util.RunListBuilder;
  36. import org.jclouds.crypto.PemsTest;
  37. import org.jclouds.domain.JsonBall;
  38. import org.jclouds.json.config.GsonModule;
  39. import org.jclouds.rest.annotations.ApiVersion;
  40. import org.jclouds.scriptbuilder.domain.OsFamily;
  41. import org.jclouds.scriptbuilder.domain.ShellToken;
  42. import org.jclouds.scriptbuilder.domain.Statement;
  43. import org.testng.annotations.BeforeClass;
  44. import org.testng.annotations.Test;
  45. import com.google.common.base.Charsets;
  46. import com.google.common.base.Functions;
  47. import com.google.common.base.Optional;
  48. import com.google.common.base.Suppliers;
  49. import com.google.common.cache.CacheLoader;
  50. import com.google.common.collect.ImmutableMap;
  51. import com.google.common.io.Resources;
  52. import com.google.inject.AbstractModule;
  53. import com.google.inject.Guice;
  54. import com.google.inject.Injector;
  55. import com.google.inject.Key;
  56. import com.google.inject.name.Names;
  57. @Test(groups = "unit", testName = "GroupToBootScriptTest")
  58. public class GroupToBootScriptTest {
  59. private Statement installChefGems;
  60. private Statement installChefOmnibus;
  61. private Optional<String> validatorName;
  62. @BeforeClass
  63. public void setup() {
  64. Injector injectorGems = Guice.createInjector(new AbstractModule() {
  65. @Override
  66. protected void configure() {
  67. bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefApiMetadata.DEFAULT_API_VERSION);
  68. bind(String.class).annotatedWith(Names.named(CHEF_UPDATE_GEM_SYSTEM)).toInstance("true");
  69. bind(String.class).annotatedWith(Names.named(CHEF_UPDATE_GEMS)).toInstance("true");
  70. bind(String.class).annotatedWith(Names.named(CHEF_USE_OMNIBUS)).toInstance("false");
  71. }
  72. }, new ChefParserModule(), new GsonModule(), new ChefBootstrapModule());
  73. Injector injectorOmnibus = Guice.createInjector(new AbstractModule() {
  74. @Override
  75. protected void configure() {
  76. bind(String.class).annotatedWith(ApiVersion.class).toInstance(ChefApiMetadata.DEFAULT_API_VERSION);
  77. bind(String.class).annotatedWith(Names.named(CHEF_UPDATE_GEM_SYSTEM)).toInstance("true");
  78. bind(String.class).annotatedWith(Names.named(CHEF_UPDATE_GEMS)).toInstance("true");
  79. bind(String.class).annotatedWith(Names.named(CHEF_USE_OMNIBUS)).toInstance("true");
  80. }
  81. }, new ChefParserModule(), new GsonModule(), new ChefBootstrapModule());
  82. installChefGems = injectorGems.getInstance(Key.get(Statement.class, InstallChef.class));
  83. installChefOmnibus = injectorOmnibus.getInstance(Key.get(Statement.class, InstallChef.class));
  84. validatorName = Optional.<String> of("chef-validator");
  85. }
  86. @Test(expectedExceptions = IllegalStateException.class)
  87. public void testMustHaveValidatorName() {
  88. Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
  89. GroupToBootScript fn = new GroupToBootScript(Suppliers.ofInstance(URI.create("http://localhost:4000")),
  90. CacheLoader.from(Functions.forMap(ImmutableMap.of("foo", BootstrapConfig.builder().build()))),
  91. installChefOmnibus, Optional.<String> absent(), validatorCredential);
  92. fn.apply("foo", null);
  93. }
  94. @Test(expectedExceptions = IllegalStateException.class)
  95. public void testMustHaveValidatorCredential() {
  96. GroupToBootScript fn = new GroupToBootScript(Suppliers.ofInstance(URI.create("http://localhost:4000")),
  97. CacheLoader.from(Functions.forMap(ImmutableMap.of("foo", BootstrapConfig.builder().build()))),
  98. installChefOmnibus, validatorName, Optional.<PrivateKey> absent());
  99. fn.apply("foo", null);
  100. }
  101. @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Key 'foo' not present in map")
  102. public void testMustHaveRunScriptsName() {
  103. Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
  104. GroupToBootScript fn = new GroupToBootScript(Suppliers.ofInstance(URI.create("http://localhost:4000")),
  105. CacheLoader.from(Functions.forMap(ImmutableMap.<String, BootstrapConfig> of())), installChefOmnibus,
  106. validatorName, validatorCredential);
  107. fn.apply("foo", null);
  108. }
  109. @Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "null value in entry: foo=null")
  110. public void testMustHaveRunScriptsValue() {
  111. Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
  112. GroupToBootScript fn = new GroupToBootScript(Suppliers.ofInstance(URI.create("http://localhost:4000")),
  113. CacheLoader.from(Functions.forMap(ImmutableMap.of("foo", (BootstrapConfig) null))), installChefOmnibus,
  114. validatorName, validatorCredential);
  115. fn.apply("foo", null);
  116. }
  117. public void testOneRecipe() throws IOException {
  118. Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
  119. BootstrapConfig config = BootstrapConfig.builder().attributes(new JsonBall("{\"tomcat6\":{\"ssl_port\":8433}}"))
  120. .runList(new RunListBuilder().addRecipe("apache2").addRole("webserver").build()).build();
  121. GroupToBootScript fn = groupToBootScriptFor(config, validatorCredential, false);
  122. PrivateKey validatorKey = validatorCredential.get();
  123. expect(validatorKey.getEncoded()).andReturn(PemsTest.PRIVATE_KEY.getBytes());
  124. replay(validatorKey);
  125. assertEquals(
  126. fn.apply("foo", null).render(OsFamily.UNIX),
  127. exitInsteadOfReturn(OsFamily.UNIX, readContent("test_install_ruby.sh")
  128. + readContent("test_install_rubygems.sh"))
  129. + "gem install chef --no-rdoc --no-ri\n" + readContent("bootstrap.sh"));
  130. verify(validatorKey);
  131. }
  132. public void testOneRecipeAndEnvironment() throws IOException {
  133. Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
  134. BootstrapConfig config = BootstrapConfig.builder().attributes(new JsonBall("{\"tomcat6\":{\"ssl_port\":8433}}"))
  135. .environment("env").runList(new RunListBuilder().addRecipe("apache2").addRole("webserver").build()).build();
  136. GroupToBootScript fn = groupToBootScriptFor(config, validatorCredential, false);
  137. PrivateKey validatorKey = validatorCredential.get();
  138. expect(validatorKey.getEncoded()).andReturn(PemsTest.PRIVATE_KEY.getBytes());
  139. replay(validatorKey);
  140. assertEquals(
  141. fn.apply("foo", null).render(OsFamily.UNIX),
  142. exitInsteadOfReturn(OsFamily.UNIX, readContent("test_install_ruby.sh")
  143. + readContent("test_install_rubygems.sh") + "gem install chef --no-rdoc --no-ri\n"
  144. + readContent("bootstrap-env.sh")));
  145. verify(validatorKey);
  146. }
  147. public void testOneRecipeOmnibus() throws IOException {
  148. Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
  149. BootstrapConfig config = BootstrapConfig.builder().attributes(new JsonBall("{\"tomcat6\":{\"ssl_port\":8433}}"))
  150. .runList(new RunListBuilder().addRecipe("apache2").addRole("webserver").build()).build();
  151. GroupToBootScript fn = groupToBootScriptFor(config, validatorCredential, true);
  152. PrivateKey validatorKey = validatorCredential.get();
  153. expect(validatorKey.getEncoded()).andReturn(PemsTest.PRIVATE_KEY.getBytes());
  154. replay(validatorKey);
  155. assertEquals(fn.apply("foo", null).render(OsFamily.UNIX),
  156. "setupPublicCurl || exit 1\ncurl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 "
  157. + "-X GET https://www.opscode.com/chef/install.sh |(bash)\n" + readContent("bootstrap.sh"));
  158. verify(validatorKey);
  159. }
  160. public void testOneRecipeAndEnvironmentOmnibus() throws IOException {
  161. Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
  162. BootstrapConfig config = BootstrapConfig.builder().attributes(new JsonBall("{\"tomcat6\":{\"ssl_port\":8433}}"))
  163. .environment("env").runList(new RunListBuilder().addRecipe("apache2").addRole("webserver").build()).build();
  164. GroupToBootScript fn = groupToBootScriptFor(config, validatorCredential, true);
  165. PrivateKey validatorKey = validatorCredential.get();
  166. expect(validatorKey.getEncoded()).andReturn(PemsTest.PRIVATE_KEY.getBytes());
  167. replay(validatorKey);
  168. assertEquals(fn.apply("foo", null).render(OsFamily.UNIX),
  169. "setupPublicCurl || exit 1\ncurl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 "
  170. + "-X GET https://www.opscode.com/chef/install.sh |(bash)\n" + readContent("bootstrap-env.sh"));
  171. verify(validatorKey);
  172. }
  173. public void testNoAttribtues() throws IOException {
  174. Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
  175. BootstrapConfig config = BootstrapConfig.builder()
  176. .environment("env").runList(new RunListBuilder().addRecipe("apache2").addRole("webserver").build()).build();
  177. GroupToBootScript fn = groupToBootScriptFor(config, validatorCredential, true);
  178. PrivateKey validatorKey = validatorCredential.get();
  179. expect(validatorKey.getEncoded()).andReturn(PemsTest.PRIVATE_KEY.getBytes());
  180. replay(validatorKey);
  181. assertEquals(fn.apply("foo", null).render(OsFamily.UNIX),
  182. "setupPublicCurl || exit 1\ncurl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 "
  183. + "-X GET https://www.opscode.com/chef/install.sh |(bash)\n" + readContent("bootstrap-env-noattrs.sh"));
  184. verify(validatorKey);
  185. }
  186. public void testCustomNodeName() throws IOException {
  187. Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
  188. BootstrapConfig config = BootstrapConfig.builder().attributes(new JsonBall("{\"tomcat6\":{\"ssl_port\":8433}}"))
  189. .environment("env").runList(new RunListBuilder().addRecipe("apache2").addRole("webserver").build()).build();
  190. GroupToBootScript fn = groupToBootScriptFor(config, validatorCredential, true);
  191. PrivateKey validatorKey = validatorCredential.get();
  192. expect(validatorKey.getEncoded()).andReturn(PemsTest.PRIVATE_KEY.getBytes());
  193. replay(validatorKey);
  194. assertEquals(fn.apply("foo", "bar").render(OsFamily.UNIX),
  195. "setupPublicCurl || exit 1\ncurl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 "
  196. + "-X GET https://www.opscode.com/chef/install.sh |(bash)\n" + readContent("bootstrap-node-env.sh"));
  197. verify(validatorKey);
  198. }
  199. public void testCustomSecurityOptions() throws IOException {
  200. Optional<PrivateKey> validatorCredential = Optional.of(createMock(PrivateKey.class));
  201. BootstrapConfig config = BootstrapConfig.builder().attributes(new JsonBall("{\"tomcat6\":{\"ssl_port\":8433}}"))
  202. .runList(new RunListBuilder().addRecipe("apache2").addRole("webserver").build())
  203. .sslCAFile(readContent("chef.crt")).sslCAPath("/etc/chef").sslVerifyMode(SSLVerifyMode.PEER)
  204. .verifyApiCert(true).build();
  205. GroupToBootScript fn = groupToBootScriptFor(config, validatorCredential, true);
  206. PrivateKey validatorKey = validatorCredential.get();
  207. expect(validatorKey.getEncoded()).andReturn(PemsTest.PRIVATE_KEY.getBytes());
  208. replay(validatorKey);
  209. assertEquals(fn.apply("foo", null).render(OsFamily.UNIX),
  210. "setupPublicCurl || exit 1\ncurl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 "
  211. + "-X GET https://www.opscode.com/chef/install.sh |(bash)\n" + readContent("bootstrap-ssl.sh"));
  212. verify(validatorKey);
  213. }
  214. private static String exitInsteadOfReturn(OsFamily family, String input) {
  215. return input.replaceAll(ShellToken.RETURN.to(family), ShellToken.EXIT.to(family));
  216. }
  217. private GroupToBootScript groupToBootScriptFor(BootstrapConfig config, Optional<PrivateKey> validatorCredential,
  218. boolean useOmnibus) {
  219. return new GroupToBootScript(Suppliers.ofInstance(URI.create("http://localhost:4000")),
  220. CacheLoader.from(Functions.forMap(ImmutableMap.of("foo", config))), useOmnibus ? installChefOmnibus
  221. : installChefGems, validatorName, validatorCredential);
  222. }
  223. private static String readContent(String resource) throws IOException {
  224. return Resources.toString(Resources.getResource(resource), Charsets.UTF_8);
  225. }
  226. }