PageRenderTime 738ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/providers/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/compute/functions/OrphanedGroupsFromDeadNodesTest.java

http://github.com/jclouds/jclouds
Java | 172 lines | 111 code | 45 blank | 16 comment | 0 complexity | 26f8fa171c318fbcdd200c6f7854425d 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.googlecomputeengine.compute.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.testng.Assert.assertSame;
  22. import static org.testng.Assert.assertTrue;
  23. import java.util.Set;
  24. import org.easymock.EasyMock;
  25. import org.jclouds.compute.ComputeService;
  26. import org.jclouds.compute.domain.ComputeMetadata;
  27. import org.jclouds.compute.domain.NodeMetadata;
  28. import org.jclouds.compute.domain.internal.NodeMetadataImpl;
  29. import org.jclouds.googlecomputeengine.compute.predicates.GroupIsEmpty;
  30. import org.testng.annotations.Test;
  31. import com.google.common.base.Predicate;
  32. import com.google.common.collect.ImmutableMap;
  33. import com.google.common.collect.ImmutableSet;
  34. import com.google.common.collect.Sets;
  35. import com.google.inject.AbstractModule;
  36. import com.google.inject.Guice;
  37. public class OrphanedGroupsFromDeadNodesTest {
  38. private static class IdAndGroupOnlyNodeMetadata extends NodeMetadataImpl {
  39. public IdAndGroupOnlyNodeMetadata(String id, String group, Status status) {
  40. super(null, null, id, null, null, ImmutableMap.<String, String>of(), ImmutableSet.<String>of(), group, null,
  41. null, null, status, null, 0, ImmutableSet.<String>of(), ImmutableSet.<String>of(), null, null);
  42. }
  43. }
  44. @Test
  45. public void testDetectsNoOrphanedGroupsWhenAllNodesArePresentAndTerminated() {
  46. Set<NodeMetadata> deadNodesGroup1 = ImmutableSet.<NodeMetadata>builder()
  47. .add(new IdAndGroupOnlyNodeMetadata("a", "1", NodeMetadata.Status.TERMINATED)).build();
  48. Set<NodeMetadata> deadNodesGroup2 = ImmutableSet.<NodeMetadata> builder()
  49. .add(new IdAndGroupOnlyNodeMetadata("b", "2", NodeMetadata.Status.SUSPENDED)).build();
  50. Set<NodeMetadata> allDeadNodes = Sets.union(deadNodesGroup1, deadNodesGroup2);
  51. ComputeService mock = createMock(ComputeService.class);
  52. expect(mock.listNodesDetailsMatching(EasyMock.<Predicate<ComputeMetadata>>anyObject()))
  53. .andReturn((Set) deadNodesGroup1).once();
  54. expect(mock.listNodesDetailsMatching(EasyMock.<Predicate<ComputeMetadata>>anyObject()))
  55. .andReturn((Set) deadNodesGroup2).once();
  56. replay(mock);
  57. OrphanedGroupsFromDeadNodes orphanedGroupsFromDeadNodes = new OrphanedGroupsFromDeadNodes(
  58. allNodesInGroupTerminated(mock));
  59. Set<String> orphanedGroups = orphanedGroupsFromDeadNodes.apply(allDeadNodes);
  60. assertTrue(orphanedGroups.isEmpty());
  61. }
  62. @Test
  63. public void testDetectsOneOrphanedGroupWhenSomeNodesTerminatedAndOtherMissing() {
  64. Set<NodeMetadata> deadNodesGroup1 = ImmutableSet.<NodeMetadata> builder()
  65. .add(new IdAndGroupOnlyNodeMetadata("a", "1", NodeMetadata.Status.TERMINATED)).build();
  66. Set<NodeMetadata> deadNodesGroup2 = ImmutableSet.<NodeMetadata> builder()
  67. .add(new IdAndGroupOnlyNodeMetadata("b", "2", NodeMetadata.Status.TERMINATED)).build();
  68. Set<NodeMetadata> allDeadNodes = Sets.union(deadNodesGroup1, deadNodesGroup2);
  69. ComputeService mock = createMock(ComputeService.class);
  70. expect(mock.listNodesDetailsMatching(EasyMock.<Predicate<ComputeMetadata>>anyObject()))
  71. .andReturn((Set) deadNodesGroup1).once();
  72. expect(mock.listNodesDetailsMatching(EasyMock.<Predicate<ComputeMetadata>>anyObject()))
  73. .andReturn((Set) ImmutableSet.of()).once();
  74. replay(mock);
  75. OrphanedGroupsFromDeadNodes orphanedGroupsFromDeadNodes = new OrphanedGroupsFromDeadNodes(
  76. allNodesInGroupTerminated(mock));
  77. Set<String> orphanedGroups = orphanedGroupsFromDeadNodes.apply(allDeadNodes);
  78. assertSame(orphanedGroups.size(), 1);
  79. assertTrue(orphanedGroups.contains("2"));
  80. }
  81. @Test
  82. public void testDetectsOneOrphanedGroupWhenSomeNodesAreAliveAndOtherMissing() {
  83. Set<NodeMetadata> deadNodesGroup1 = ImmutableSet.<NodeMetadata> builder()
  84. .add(new IdAndGroupOnlyNodeMetadata("a", "1", NodeMetadata.Status.RUNNING)).build();
  85. Set<NodeMetadata> deadNodesGroup2 = ImmutableSet.<NodeMetadata> builder()
  86. .add(new IdAndGroupOnlyNodeMetadata("b", "2", NodeMetadata.Status.TERMINATED)).build();
  87. Set<NodeMetadata> allDeadNodes = Sets.union(deadNodesGroup1, deadNodesGroup2);
  88. ComputeService mock = createMock(ComputeService.class);
  89. expect(mock.listNodesDetailsMatching(EasyMock.<Predicate<ComputeMetadata>>anyObject()))
  90. .andReturn((Set) deadNodesGroup1).once();
  91. expect(mock.listNodesDetailsMatching(EasyMock.<Predicate<ComputeMetadata>>anyObject()))
  92. .andReturn((Set) ImmutableSet.of()).once();
  93. replay(mock);
  94. OrphanedGroupsFromDeadNodes orphanedGroupsFromDeadNodes = new OrphanedGroupsFromDeadNodes(
  95. allNodesInGroupTerminated(mock));
  96. Set<String> orphanedGroups = orphanedGroupsFromDeadNodes.apply(allDeadNodes);
  97. assertSame(orphanedGroups.size(), 1);
  98. assertTrue(orphanedGroups.contains("2"));
  99. }
  100. @Test
  101. public void testDetectsAllOrphanedGroupsWhenAllNodesArerMissing() {
  102. Set<NodeMetadata> deadNodesGroup1 = ImmutableSet.<NodeMetadata> builder()
  103. .add(new IdAndGroupOnlyNodeMetadata("a", "1", NodeMetadata.Status.RUNNING)).build();
  104. Set<NodeMetadata> deadNodesGroup2 = ImmutableSet.<NodeMetadata> builder()
  105. .add(new IdAndGroupOnlyNodeMetadata("b", "2", NodeMetadata.Status.TERMINATED)).build();
  106. Set<NodeMetadata> allDeadNodes = Sets.union(deadNodesGroup1, deadNodesGroup2);
  107. ComputeService mock = createMock(ComputeService.class);
  108. expect(mock.listNodesDetailsMatching(EasyMock.<Predicate<ComputeMetadata>>anyObject()))
  109. .andReturn((Set) ImmutableSet.of()).once();
  110. expect(mock.listNodesDetailsMatching(EasyMock.<Predicate<ComputeMetadata>>anyObject()))
  111. .andReturn((Set) ImmutableSet.of()).once();
  112. replay(mock);
  113. OrphanedGroupsFromDeadNodes orphanedGroupsFromDeadNodes = new OrphanedGroupsFromDeadNodes(
  114. allNodesInGroupTerminated(mock));
  115. Set<String> orphanedGroups = orphanedGroupsFromDeadNodes.apply(allDeadNodes);
  116. assertSame(orphanedGroups.size(), 2);
  117. assertTrue(orphanedGroups.contains("1"));
  118. assertTrue(orphanedGroups.contains("2"));
  119. }
  120. private Predicate<String> allNodesInGroupTerminated(final ComputeService mock) {
  121. return Guice.createInjector(new AbstractModule() {
  122. @Override protected void configure() {
  123. bind(ComputeService.class).toInstance(mock);
  124. }
  125. }).getInstance(GroupIsEmpty.class); // rather than opening ctor.
  126. }
  127. }