NovaLauncher extends Launcher { @Override public List<MachineRuntime> forkMachines(JsonCluster definition, ClusterRuntime runtime, String name) throws KaramelException { Nova nova = (Nova) UserClusterDataExtractor.getGroupProvider(definition,name); JsonGroup definedGroup = UserClusterDataExtractor.findGroup(definition, name); GroupRuntime groupRuntime = UserClusterDataExtractor.findGroup(runtime,name); Set<String> groupIds = new HashSet<>(); groupIds.add(groupRuntime.getId()); String keypairName = NovaSetting.NOVA_KEYPAIR_NAME(runtime.getName(), novaContext.getNovaCredentials().getRegion()); if(!keys.contains(keypairName)) { uploadSshPublicKey(keypairName,nova,true); keys.add(keypairName); } return requestNodes(keypairName,groupRuntime,groupIds,Integer.valueOf(definedGroup.getSize()),nova); } NovaLauncher(NovaContext novaContext, SshKeyPair sshKeyPair); static NovaContext validateCredentials(NovaCredentials novaCredentials, ContextBuilder builder); static NovaCredentials readCredentials(Confs confs); String createSecurityGroup(String clusterName, String groupName, Nova nova, Set<String> ports); boolean uploadSshPublicKey(String keyPairName, Nova nova, boolean removeOld); boolean cleanupFailedNodes(Map<NodeMetadata, Throwable> failedNodes); @Override void cleanup(JsonCluster definition, ClusterRuntime runtime); void cleanup(String clusterName, Set<String> vmIds, Set<String> vmNames, Map<String, String> groupRegion); @Override String forkGroup(JsonCluster definition, ClusterRuntime runtime, String name); @Override List<MachineRuntime> forkMachines(JsonCluster definition, ClusterRuntime runtime, String name); static Predicate<NodeMetadata> withPredicate(final Set<String> ids, final Set<String> names,
                                                      final Set<String> groupNames);  }
@Ignore @Test public void testForkMachines() throws KaramelException, RunNodesException { NovaLauncher novaLauncher = new NovaLauncher(novaContext, sshKeyPair); String keypairName = "pepeKeyPair"; KeyPair pair = mock(KeyPair.class); List<KeyPair> keyPairList = new ArrayList<>(); keyPairList.add(pair); FluentIterable<KeyPair> keys = FluentIterable.from(keyPairList); when(keyPairApi.list()).thenReturn(keys); when(keyPairApi.delete(keypairName)).thenReturn(true); when(keyPairApi.createWithPublicKey(keypairName, sshKeyPair.getPublicKey())).thenReturn(pair); JsonCluster cluster = mock(JsonCluster.class); ClusterRuntime clusterRuntime = mock(ClusterRuntime.class); when(clusterRuntime.getName()).thenReturn(clusterName); List<JsonGroup> groups = new ArrayList<>(); JsonGroup group = mock(JsonGroup.class); groups.add(group); when(group.getName()).thenReturn(groupName); when(group.getProvider()).thenReturn(nova); when(group.getSize()).thenReturn(1); when(cluster.getGroups()).thenReturn(groups); when(cluster.getProvider()).thenReturn(nova); when(cluster.getName()).thenReturn(clusterName); List<GroupRuntime> groupRuntimes = new ArrayList<>(); GroupRuntime groupRuntime = mock(GroupRuntime.class); when(groupRuntime.getName()).thenReturn(groupName); when(groupRuntime.getId()).thenReturn("10"); when(groupRuntime.getCluster()).thenReturn(clusterRuntime); groupRuntimes.add(groupRuntime); when(clusterRuntime.getGroups()).thenReturn(groupRuntimes); NovaTemplateOptions novaTemplateOptions = mock(NovaTemplateOptions.class); TemplateBuilder templateBuilder = mock(TemplateBuilder.class); TemplateOptions templateOptions = mock(TemplateOptions.class); when(novaContext.getComputeService()).thenReturn(novaComputeService); when(novaComputeService.templateOptions()).thenReturn(novaTemplateOptions); when(novaTemplateOptions.securityGroups(Matchers.anyCollection())).thenReturn(novaTemplateOptions); when(templateOptions.as(NovaTemplateOptions.class)).thenReturn(novaTemplateOptions); when(novaComputeService.templateBuilder()).thenReturn(templateBuilder); when(novaTemplateOptions.keyPairName(keypairName)).thenReturn(novaTemplateOptions); when(novaTemplateOptions.autoAssignFloatingIp(true)).thenReturn(novaTemplateOptions); when(novaTemplateOptions.nodeNames(Matchers.anyCollection())).thenReturn(novaTemplateOptions); Set<NodeMetadata> succeededNodes = new HashSet<>(); NodeMetadata succeededNode = mock(NodeMetadata.class); succeededNodes.add(succeededNode); doReturn(succeededNodes).when(novaComputeService) .createNodesInGroup(Matchers.anyString(), eq(1), Matchers.any(Template.class)); LoginCredentials loginCredentials = mock(LoginCredentials.class); Set<String> ipAddresses = new HashSet<>(); ipAddresses.add("127.0.0.1"); when(succeededNode.getPublicAddresses()).thenReturn(ipAddresses); when(succeededNode.getPrivateAddresses()).thenReturn(ipAddresses); when(succeededNode.getLoginPort()).thenReturn(22); when(succeededNode.getCredentials()).thenReturn(loginCredentials); when(loginCredentials.getUser()).thenReturn("ubuntu"); List<MachineRuntime> forkedMachines =novaLauncher.forkMachines(cluster,clusterRuntime,groupName); assertNotNull(forkedMachines); assertFalse(forkedMachines.isEmpty()); }