/scriptbuilder/src/test/java/org/jclouds/scriptbuilder/statements/login/AdminAccessBuilderSpecTest.java

http://github.com/jclouds/jclouds · Java · 194 lines · 147 code · 20 blank · 27 comment · 1 complexity · 756dd97377cfb98855929af3efe3d0db 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.scriptbuilder.statements.login;
  18. import static java.lang.String.format;
  19. import static org.jclouds.scriptbuilder.statements.login.AdminAccessBuilderSpec.parse;
  20. import static org.testng.Assert.assertEquals;
  21. import static org.testng.Assert.assertNull;
  22. import static org.testng.Assert.fail;
  23. import java.io.File;
  24. import java.lang.reflect.Field;
  25. import javax.inject.Provider;
  26. import org.jclouds.scriptbuilder.statements.login.AdminAccess.Builder;
  27. import org.testng.annotations.Test;
  28. import com.google.gson.Gson;
  29. /**
  30. *
  31. * <p/>
  32. * inspired by guava {@code CacheBuilderSpecTest}
  33. */
  34. @Test(testName = "AdminAccessBuilderSpecTest")
  35. public class AdminAccessBuilderSpecTest {
  36. Provider<AdminAccess.Builder> adminAccessBuilders = new Provider<AdminAccess.Builder>() {
  37. @Override
  38. public Builder get() {
  39. return new AdminAccess.Builder();
  40. }
  41. };
  42. public void testParseEmpty() {
  43. AdminAccessBuilderSpec spec = parse("");
  44. assertNull(spec.adminHome);
  45. assertNull(spec.adminPassword);
  46. assertNull(spec.adminPrivateKeyFile);
  47. assertNull(spec.adminPublicKeyFile);
  48. assertNull(spec.adminUsername);
  49. assertNull(spec.authorizeAdminPublicKey);
  50. assertNull(spec.grantSudoToAdminUser);
  51. assertNull(spec.installAdminPrivateKey);
  52. assertNull(spec.lockSsh);
  53. assertNull(spec.loginPassword);
  54. assertNull(spec.resetLoginPassword);
  55. assertAdminAccessBuilderEquivalence(adminAccessBuilders.get(), adminAccessBuilders.get().from(spec));
  56. }
  57. public void testParseAdminUsername() {
  58. AdminAccessBuilderSpec spec = parse("adminUsername=superUser");
  59. assertEquals(spec.adminUsername, "superUser");
  60. assertNull(spec.adminHome);
  61. assertNull(spec.adminPassword);
  62. assertNull(spec.adminPrivateKeyFile);
  63. assertNull(spec.adminPublicKeyFile);
  64. assertNull(spec.authorizeAdminPublicKey);
  65. assertNull(spec.grantSudoToAdminUser);
  66. assertNull(spec.installAdminPrivateKey);
  67. assertNull(spec.lockSsh);
  68. assertNull(spec.loginPassword);
  69. assertNull(spec.resetLoginPassword);
  70. assertAdminAccessBuilderEquivalence(adminAccessBuilders.get().adminUsername("superUser"), adminAccessBuilders
  71. .get().from(spec));
  72. }
  73. public void testParseAdminHome() {
  74. AdminAccessBuilderSpec spec = parse("adminHome=/home/superUser");
  75. assertEquals(spec.getAdminHome(), "/home/superUser");
  76. assertNull(spec.adminUsername);
  77. assertNull(spec.adminPassword);
  78. assertNull(spec.adminPrivateKeyFile);
  79. assertNull(spec.adminPublicKeyFile);
  80. assertNull(spec.authorizeAdminPublicKey);
  81. assertNull(spec.grantSudoToAdminUser);
  82. assertNull(spec.installAdminPrivateKey);
  83. assertNull(spec.lockSsh);
  84. assertNull(spec.loginPassword);
  85. assertNull(spec.resetLoginPassword);
  86. assertAdminAccessBuilderEquivalence(adminAccessBuilders.get().adminHome("/home/superUser"), adminAccessBuilders
  87. .get().from(spec));
  88. }
  89. public void testParsePrivateKeyFile() {
  90. AdminAccessBuilderSpec spec = parse("adminPrivateKeyFile=target/test-classes/test");
  91. assertEquals(spec.getAdminPrivateKeyFile().getPath(), format("target%stest-classes%stest", File.separator, File.separator));
  92. assertNull(spec.adminHome);
  93. assertNull(spec.adminPassword);
  94. assertNull(spec.adminPublicKeyFile);
  95. assertNull(spec.authorizeAdminPublicKey);
  96. assertNull(spec.grantSudoToAdminUser);
  97. assertNull(spec.installAdminPrivateKey);
  98. assertNull(spec.lockSsh);
  99. assertNull(spec.loginPassword);
  100. assertNull(spec.resetLoginPassword);
  101. assertAdminAccessBuilderEquivalence(adminAccessBuilders.get().adminHome("/home/superUser"), adminAccessBuilders
  102. .get().from(spec));
  103. }
  104. public void testParseLockSSh() {
  105. AdminAccessBuilderSpec spec = parse("lockSsh=true");
  106. assertEquals(spec.getLockSsh(), Boolean.TRUE);
  107. assertNull(spec.adminUsername);
  108. assertNull(spec.adminHome);
  109. assertNull(spec.adminPassword);
  110. assertNull(spec.adminPrivateKeyFile);
  111. assertNull(spec.adminPublicKeyFile);
  112. assertNull(spec.authorizeAdminPublicKey);
  113. assertNull(spec.grantSudoToAdminUser);
  114. assertNull(spec.installAdminPrivateKey);
  115. assertNull(spec.loginPassword);
  116. assertNull(spec.resetLoginPassword);
  117. assertAdminAccessBuilderEquivalence(adminAccessBuilders.get().lockSsh(true), adminAccessBuilders.get().from(spec));
  118. }
  119. public void testParseAdminUsernameRepeated() {
  120. try {
  121. parse("adminUsername=superUser, adminUsername=notSoSuperUser");
  122. fail("Expected exception");
  123. } catch (IllegalArgumentException expected) {
  124. // expected
  125. }
  126. }
  127. public void testParseNonExistingFiles() {
  128. try {
  129. parse("adminPrivateKeyFile=nonExistingFile.txt");
  130. fail("Expected exception");
  131. } catch (IllegalArgumentException expected) {
  132. // expected
  133. }
  134. try {
  135. parse("adminPublicKeyFile=nonExistingFile.txt");
  136. fail("Expected exception");
  137. } catch (IllegalArgumentException expected) {
  138. // expected
  139. }
  140. }
  141. public void testNiceJson() {
  142. AdminAccessBuilderSpec spec = parse("adminUsername=nimda,adminPassword=dictionaryword");
  143. assertEquals(new Gson().toJson(spec), "{\"adminUsername\":\"nimda\",\"adminPassword\":\"dictionaryword\"}");
  144. assertEquals(new Gson().fromJson(new Gson().toJson(spec), AdminAccessBuilderSpec.class), spec);
  145. }
  146. public void testParse_unknownKey() {
  147. try {
  148. parse("foo=17");
  149. fail("Expected exception");
  150. } catch (IllegalArgumentException expected) {
  151. // expected
  152. }
  153. }
  154. public void testAdminAccessBuilderFromString() {
  155. AdminAccess.Builder fromString = adminAccessBuilders.get().from(
  156. "adminUsername=nimda,adminPassword=dictionaryword");
  157. AdminAccess.Builder expected = adminAccessBuilders.get().adminUsername("nimda").adminPassword("dictionaryword");
  158. assertAdminAccessBuilderEquivalence(expected, fromString);
  159. }
  160. private void assertAdminAccessBuilderEquivalence(AdminAccess.Builder a, AdminAccess.Builder b) {
  161. // Labs hack: dig into the TemplateBuilder instances, verifying all fields
  162. // are equal.
  163. for (Field f : AdminAccess.Builder.class.getFields()) {
  164. f.setAccessible(true);
  165. try {
  166. assertEquals(f.get(a), f.get(b), "Field " + f.getName() + " not equal");
  167. } catch (IllegalArgumentException e) {
  168. throw new AssertionError(e.getMessage());
  169. } catch (IllegalAccessException e) {
  170. throw new AssertionError(e.getMessage());
  171. }
  172. }
  173. }
  174. }