PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/compute/src/test/java/org/jclouds/compute/config/PersistNodeCredentialsTest.java

https://github.com/regularfry/jclouds
Java | 147 lines | 90 code | 33 blank | 24 comment | 0 complexity | 95f8dcc25b78185f281803c0b78fb69e 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. */
  19. package org.jclouds.compute.config;
  20. import static org.easymock.EasyMock.expect;
  21. import static org.easymock.classextension.EasyMock.createMock;
  22. import static org.easymock.classextension.EasyMock.replay;
  23. import static org.easymock.classextension.EasyMock.verify;
  24. import static org.testng.Assert.assertEquals;
  25. import java.util.Map;
  26. import org.jclouds.compute.config.PersistNodeCredentialsModule.RefreshCredentialsForNode;
  27. import org.jclouds.compute.config.PersistNodeCredentialsModule.RefreshCredentialsForNodeIfRanAdminAccess;
  28. import org.jclouds.compute.domain.NodeMetadata;
  29. import org.jclouds.compute.domain.NodeMetadataBuilder;
  30. import org.jclouds.compute.domain.NodeState;
  31. import org.jclouds.compute.internal.PersistNodeCredentials;
  32. import org.jclouds.domain.Credentials;
  33. import org.jclouds.scriptbuilder.statements.login.AdminAccess;
  34. import org.testng.annotations.Test;
  35. import com.google.common.collect.ImmutableMap;
  36. import com.google.inject.AbstractModule;
  37. import com.google.inject.Guice;
  38. import com.google.inject.TypeLiteral;
  39. /**
  40. *
  41. * @author Adrian Cole
  42. */
  43. @Test(groups = "unit")
  44. public class PersistNodeCredentialsTest {
  45. public void testReturnsCorrectFunction() {
  46. PersistNodeCredentials persistNodeCredentials = Guice.createInjector(new PersistNodeCredentialsModule(),
  47. new AbstractModule() {
  48. @Override
  49. protected void configure() {
  50. bind(new TypeLiteral<Map<String, Credentials>>() {
  51. }).toInstance(ImmutableMap.<String, Credentials> of());
  52. }
  53. }).getInstance(PersistNodeCredentials.class);
  54. assertEquals(persistNodeCredentials.always(null).getClass(),
  55. PersistNodeCredentialsModule.RefreshCredentialsForNode.class);
  56. assertEquals(persistNodeCredentials.ifAdminAccess(null).getClass(),
  57. PersistNodeCredentialsModule.RefreshCredentialsForNodeIfRanAdminAccess.class);
  58. }
  59. public void testRefreshCredentialsForNodeIfRanAdminAccessWhenStatementIsNullSameCredentialsAndNoCaching() {
  60. @SuppressWarnings("unchecked")
  61. Map<String, Credentials> credstore = createMock(Map.class);
  62. replay(credstore);
  63. NodeMetadata node = new NodeMetadataBuilder().ids("id").state(NodeState.RUNNING).build();
  64. RefreshCredentialsForNodeIfRanAdminAccess fn = new PersistNodeCredentialsModule.RefreshCredentialsForNodeIfRanAdminAccess(
  65. credstore, null);
  66. assertEquals(node, fn.apply(node));
  67. verify(credstore);
  68. }
  69. public void testRefreshCredentialsForNodeWhenStatementIsNullSameCredentialsAndDoesCache() {
  70. @SuppressWarnings("unchecked")
  71. Map<String, Credentials> credstore = createMock(Map.class);
  72. Credentials credentials = createMock(Credentials.class);
  73. expect(credstore.put("node#id", credentials)).andReturn(null);
  74. replay(credstore);
  75. NodeMetadata node = new NodeMetadataBuilder().ids("id").state(NodeState.RUNNING).credentials(credentials).build();
  76. RefreshCredentialsForNode fn = new PersistNodeCredentialsModule.RefreshCredentialsForNode(credstore, null);
  77. assertEquals(node, fn.apply(node));
  78. verify(credstore);
  79. }
  80. public void testRefreshCredentialsForNodeIfRanAdminAccessWhenStatementIsAdminAccessNewCredentialsAndDoesCache() {
  81. @SuppressWarnings("unchecked")
  82. Map<String, Credentials> credstore = createMock(Map.class);
  83. AdminAccess statement = createMock(AdminAccess.class);
  84. Credentials credentials = createMock(Credentials.class);
  85. expect(statement.getAdminCredentials()).andReturn(credentials).atLeastOnce();
  86. expect(credstore.put("node#id", credentials)).andReturn(null);
  87. replay(statement);
  88. replay(credstore);
  89. NodeMetadata node = new NodeMetadataBuilder().ids("id").state(NodeState.RUNNING).build();
  90. RefreshCredentialsForNodeIfRanAdminAccess fn = new PersistNodeCredentialsModule.RefreshCredentialsForNodeIfRanAdminAccess(
  91. credstore, statement);
  92. assertEquals(fn.apply(node).getCredentials(), credentials);
  93. verify(statement);
  94. verify(credstore);
  95. }
  96. public void testRefreshCredentialsForNodeWhenStatementIsAdminAccessNewCredentialsAndDoesCache() {
  97. @SuppressWarnings("unchecked")
  98. Map<String, Credentials> credstore = createMock(Map.class);
  99. AdminAccess statement = createMock(AdminAccess.class);
  100. Credentials credentials = createMock(Credentials.class);
  101. expect(statement.getAdminCredentials()).andReturn(credentials).atLeastOnce();
  102. expect(credstore.put("node#id", credentials)).andReturn(null);
  103. expect(credstore.put("node#id", credentials)).andReturn(null); // TODO
  104. // optimize
  105. // this
  106. replay(statement);
  107. replay(credstore);
  108. NodeMetadata node = new NodeMetadataBuilder().ids("id").state(NodeState.RUNNING).build();
  109. RefreshCredentialsForNode fn = new PersistNodeCredentialsModule.RefreshCredentialsForNode(credstore, statement);
  110. assertEquals(fn.apply(node).getCredentials(), credentials);
  111. verify(statement);
  112. verify(credstore);
  113. }
  114. }