Return-Path: X-Original-To: apmail-provisionr-commits-archive@minotaur.apache.org Delivered-To: apmail-provisionr-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id ED2BCF185 for ; Mon, 1 Apr 2013 08:52:57 +0000 (UTC) Received: (qmail 5000 invoked by uid 500); 1 Apr 2013 08:52:57 -0000 Delivered-To: apmail-provisionr-commits-archive@provisionr.apache.org Received: (qmail 4979 invoked by uid 500); 1 Apr 2013 08:52:57 -0000 Mailing-List: contact commits-help@provisionr.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@provisionr.apache.org Delivered-To: mailing list commits@provisionr.apache.org Received: (qmail 4965 invoked by uid 99); 1 Apr 2013 08:52:57 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Apr 2013 08:52:57 +0000 X-ASF-Spam-Status: No, hits=-2002.3 required=5.0 tests=ALL_TRUSTED,RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 01 Apr 2013 08:52:45 +0000 Received: (qmail 4169 invoked by uid 99); 1 Apr 2013 08:52:25 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 Apr 2013 08:52:25 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 5BDC2834F79; Mon, 1 Apr 2013 08:52:24 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: asavu@apache.org To: commits@provisionr.incubator.apache.org Date: Mon, 01 Apr 2013 08:52:25 -0000 Message-Id: <50d0fd1c55304ad885e45ce8d6e5f48c@git.apache.org> In-Reply-To: <87783f7120954fc28ba08d7ec2dced3b@git.apache.org> References: <87783f7120954fc28ba08d7ec2dced3b@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [02/21] PROVISIONR-20. Change groupId from com.axemblr.provisionr to org.apache.provisionr X-Virus-Checked: Checked by ClamAV on apache.org http://git-wip-us.apache.org/repos/asf/incubator-provisionr/blob/6ba40c4b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/CloudStackActivityLiveTest.java ---------------------------------------------------------------------- diff --git a/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/CloudStackActivityLiveTest.java b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/CloudStackActivityLiveTest.java new file mode 100644 index 0000000..2cd33d2 --- /dev/null +++ b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/CloudStackActivityLiveTest.java @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2012 S.C. Axemblr Software Solutions S.R.L + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.provisionr.cloudstack.activities; + +import org.apache.provisionr.api.provider.Provider; +import org.apache.provisionr.cloudstack.CloudStackProvisionr; +import org.apache.provisionr.cloudstack.ProviderOptions; +import org.apache.provisionr.cloudstack.core.SecurityGroups; +import org.apache.provisionr.test.Generics; +import org.apache.provisionr.test.ProvisionrLiveTestSupport; +import com.google.common.base.Throwables; +import java.util.Set; +import java.util.UUID; +import org.jclouds.cloudstack.CloudStackAsyncClient; +import org.jclouds.cloudstack.CloudStackClient; +import org.jclouds.cloudstack.domain.Network; +import org.jclouds.cloudstack.domain.SecurityGroup; +import org.jclouds.cloudstack.domain.SshKeyPair; +import org.jclouds.cloudstack.domain.VirtualMachine; +import org.jclouds.rest.RestContext; +import org.junit.After; +import org.junit.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Helper base class for CloudStack Live tests. + */ +public class CloudStackActivityLiveTest extends ProvisionrLiveTestSupport { + + public CloudStackActivityLiveTest() { + super(CloudStackProvisionr.ID); + } + + private static final Logger LOG = LoggerFactory.getLogger(CloudStackActivityLiveTest.class); + + protected final String BUSINESS_KEY = "j-" + UUID.randomUUID().toString(); + /** + * Cloud provider credentials collected from system properties. + */ + protected Provider provider; + /** + * Instance of CloudStackActivity being tested. Automatically created from the + * generic type class argument. + */ + protected CloudStackActivity activity; + + protected RestContext context; + + public CloudStackActivityLiveTest(String provisionrId) { + super(provisionrId); + } + + protected T createCloudStackActivitiInstance() { + try { + return getCloudStackActivityClass().newInstance(); + } catch (Exception e) { + throw Throwables.propagate(e); + } + } + + protected Class getCloudStackActivityClass() { + return Generics.getTypeParameter(getClass(), CloudStackActivity.class); + } + + @Before + public void setUp() throws Exception { + provider = collectProviderCredentialsFromSystemProperties() + .option(ProviderOptions.ZONE_ID, getProviderProperty(ProviderOptions.ZONE_ID)) + .option(ProviderOptions.NETWORK_OFFERING_ID, getProviderProperty(ProviderOptions.NETWORK_OFFERING_ID)) + .createProvider(); + LOG.info("Using provider {}", provider); + + activity = createCloudStackActivitiInstance(); + context = activity.newCloudStackClient(provider); + } + + + @After + public void tearDown() throws Exception { + context.close(); + } + + protected void logSecurityGroupDetails() { + Set securityGroups = SecurityGroups.getAll(context.getApi()); + LOG.info("Security Group count is {}", securityGroups.size()); + for (SecurityGroup securityGroup : securityGroups) { + LOG.info("\tSecurity Group {}", securityGroup.getName()); + } + } + + protected void logKeyPairs() { + Set keys = context.getApi().getSSHKeyPairClient().listSSHKeyPairs(); + LOG.info("Access Key count is {}", keys.size()); + for (SshKeyPair keyPair : keys) { + LOG.info("\tKey {}", keyPair.getName()); + } + } + + protected void logVirtualMachines() { + Set vms = context.getApi().getVirtualMachineClient().listVirtualMachines(); + LOG.info("Virtual machines count is {}", vms.size()); + for (VirtualMachine vm : vms) { + LOG.info("\tVirtual machine {}", vm.toString()); + } + } + + protected void logNetworks() { + Set networks = context.getApi().getNetworkClient().listNetworks(); + LOG.info("Networks count is {}", networks.size()); + for (Network network : networks) { + LOG.info("{}\n", network.toString()); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-provisionr/blob/6ba40c4b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/DeleteKeyPairLiveTest.java ---------------------------------------------------------------------- diff --git a/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/DeleteKeyPairLiveTest.java b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/DeleteKeyPairLiveTest.java new file mode 100644 index 0000000..70604f0 --- /dev/null +++ b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/DeleteKeyPairLiveTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2012 S.C. Axemblr Software Solutions S.R.L + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.provisionr.cloudstack.activities; + +import org.activiti.engine.delegate.DelegateExecution; +import org.apache.provisionr.api.access.AdminAccess; +import org.apache.provisionr.api.pool.Pool; +import org.apache.provisionr.cloudstack.core.KeyPairs; +import org.apache.provisionr.core.CoreProcessVariables; +import static org.fest.assertions.api.Assertions.assertThat; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class DeleteKeyPairLiveTest extends CloudStackActivityLiveTest { + + private final String KEYPAIR_NAME = KeyPairs.formatNameFromBusinessKey(BUSINESS_KEY); + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + logKeyPairs(); + context.getApi().getSSHKeyPairClient().registerSSHKeyPair(KEYPAIR_NAME, getResourceAsString("keys/test.pub")); + } + + @Override + @After + public void tearDown() throws Exception { + context.getApi().getSSHKeyPairClient().deleteSSHKeyPair(KEYPAIR_NAME); + logKeyPairs(); + super.tearDown(); + } + + @Test + public void testDeleteKeyPair() throws Exception { + final AdminAccess adminAccess = AdminAccess.builder() + .username("admin") + .publicKey(getResourceAsString("keys/test.pub")) + .privateKey(getResourceAsString("keys/test")) + .createAdminAccess(); + + DelegateExecution execution = mock(DelegateExecution.class); + Pool pool = mock(Pool.class); + + when(pool.getAdminAccess()).thenReturn(adminAccess); + when(pool.getProvider()).thenReturn(provider); + + when(execution.getProcessBusinessKey()).thenReturn(BUSINESS_KEY); + when(execution.getVariable(CoreProcessVariables.POOL)).thenReturn(pool); + + activity.execute(execution); + assertKeyNotFound(KEYPAIR_NAME); + /* the second call should just do nothing */ + activity.execute(execution); + } + + private void assertKeyNotFound(String keyName) { + assertThat(context.getApi().getSSHKeyPairClient().getSSHKeyPair(keyName)).isNull(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-provisionr/blob/6ba40c4b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/DeleteSecurityGroupLiveTest.java ---------------------------------------------------------------------- diff --git a/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/DeleteSecurityGroupLiveTest.java b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/DeleteSecurityGroupLiveTest.java new file mode 100644 index 0000000..7662abe --- /dev/null +++ b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/DeleteSecurityGroupLiveTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2012 S.C. Axemblr Software Solutions S.R.L + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.provisionr.cloudstack.activities; + +import java.util.NoSuchElementException; +import org.activiti.engine.delegate.DelegateExecution; +import org.apache.provisionr.api.network.Network; +import org.apache.provisionr.api.pool.Pool; +import org.apache.provisionr.cloudstack.core.SecurityGroups; +import org.apache.provisionr.core.CoreProcessVariables; +import org.junit.After; +import static org.junit.Assert.fail; +import org.junit.Before; +import org.junit.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DeleteSecurityGroupLiveTest extends CloudStackActivityLiveTest { + + private final Logger LOG = LoggerFactory.getLogger(DeleteSecurityGroupLiveTest.class); + private final String SECURITY_GROUP_NAME = SecurityGroups.formatNameFromBusinessKey(BUSINESS_KEY); + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + logSecurityGroupDetails(); + } + + @Override + @After + public void tearDown() throws Exception { + logSecurityGroupDetails(); + super.tearDown(); + } + + @Test + public void testDeleteSecurityGroup() throws Exception { + DelegateExecution execution = mock(DelegateExecution.class); + final Network network = Network.builder().createNetwork(); + + Pool pool = mock(Pool.class); + + when(pool.getProvider()).thenReturn(provider); + when(pool.getNetwork()).thenReturn(network); + + when(execution.getVariable(CoreProcessVariables.POOL)).thenReturn(pool); + when(execution.getProcessBusinessKey()).thenReturn(BUSINESS_KEY); + + SecurityGroups.createSecurityGroup(context.getApi(), SECURITY_GROUP_NAME); + + activity.execute(execution); + + try { + SecurityGroups.getByName(context.getApi(), SECURITY_GROUP_NAME); + fail("Does not throw Exception as it should have"); + } catch (NoSuchElementException e) { + LOG.info("Exception thrown. Test passed."); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-provisionr/blob/6ba40c4b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureKeyPairExistsLiveTest.java ---------------------------------------------------------------------- diff --git a/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureKeyPairExistsLiveTest.java b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureKeyPairExistsLiveTest.java new file mode 100644 index 0000000..12fbb67 --- /dev/null +++ b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureKeyPairExistsLiveTest.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2012 S.C. Axemblr Software Solutions S.R.L + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.provisionr.cloudstack.activities; + +import java.io.IOException; +import org.activiti.engine.delegate.DelegateExecution; +import org.apache.provisionr.api.access.AdminAccess; +import org.apache.provisionr.api.pool.Pool; +import org.apache.provisionr.cloudstack.core.KeyPairs; +import org.apache.provisionr.core.CoreProcessVariables; +import static org.fest.assertions.api.Assertions.assertThat; +import org.jclouds.cloudstack.domain.SshKeyPair; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class EnsureKeyPairExistsLiveTest extends CloudStackActivityLiveTest { + + public static final String TEST_KEY_FINGERPRINT = "15:0b:a4:43:dd:58:19:9e:84:ca:db:31:a8:6b:b6:c3"; + private final String KEYPAIR_NAME = KeyPairs.formatNameFromBusinessKey(BUSINESS_KEY); + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + logKeyPairs(); + } + + @Override + @After + public void tearDown() throws Exception { + context.getApi().getSSHKeyPairClient().deleteSSHKeyPair(KEYPAIR_NAME); + logKeyPairs(); + super.tearDown(); + } + + @Test + public void testEnsureKeyPairExists() throws Exception { + final AdminAccess adminAccess = AdminAccess.builder() + .username("admin") + .publicKey(getResourceAsString("keys/test.pub")) + .privateKey(getResourceAsString("keys/test")) + .createAdminAccess(); + + DelegateExecution execution = mock(DelegateExecution.class); + Pool pool = mock(Pool.class); + + when(pool.getProvider()).thenReturn(provider); + when(pool.getAdminAccess()).thenReturn(adminAccess); + + when(execution.getProcessBusinessKey()).thenReturn(BUSINESS_KEY); + when(execution.getVariable(CoreProcessVariables.POOL)).thenReturn(pool); + + activity.execute(execution); + assertKeyPairWasImportedAsExpected(); + + /* the second call should just re-import the key */ + activity.execute(execution); + assertKeyPairWasImportedAsExpected(); + } + + private void assertKeyPairWasImportedAsExpected() throws IOException { + SshKeyPair pair = context.getApi().getSSHKeyPairClient().getSSHKeyPair(KEYPAIR_NAME); + assertThat(pair.getFingerprint()).isEqualTo(TEST_KEY_FINGERPRINT); + } +} http://git-wip-us.apache.org/repos/asf/incubator-provisionr/blob/6ba40c4b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureNetworkExistsLiveTest.java ---------------------------------------------------------------------- diff --git a/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureNetworkExistsLiveTest.java b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureNetworkExistsLiveTest.java new file mode 100644 index 0000000..b9329e0 --- /dev/null +++ b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureNetworkExistsLiveTest.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2013 S.C. Axemblr Software Solutions S.R.L + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.provisionr.cloudstack.activities; + +import java.util.NoSuchElementException; +import org.activiti.engine.delegate.DelegateExecution; +import org.apache.provisionr.api.pool.Pool; +import org.apache.provisionr.cloudstack.NetworkOptions; +import org.apache.provisionr.cloudstack.ProcessVariables; +import org.apache.provisionr.cloudstack.core.Networks; +import org.apache.provisionr.core.CoreProcessVariables; +import org.apache.provisionr.test.ProcessVariablesCollector; +import static org.fest.assertions.api.Assertions.assertThat; +import org.jclouds.cloudstack.CloudStackClient; +import org.jclouds.cloudstack.domain.Network; +import org.jclouds.cloudstack.features.NetworkClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EnsureNetworkExistsLiveTest extends CloudStackActivityLiveTest { + + private final static Logger LOG = LoggerFactory.getLogger(EnsureNetworkExistsLiveTest.class); + private final String networkName = Networks.formatNameFromBusinessKey(BUSINESS_KEY); + private DelegateExecution execution; + private Pool pool; + private ProcessVariablesCollector collector; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + logNetworks(); + initMocks(); + } + + private void initMocks() { + execution = mock(DelegateExecution.class); + pool = mock(Pool.class); + + when(pool.getProvider()).thenReturn(provider); + when(execution.getVariable(CoreProcessVariables.POOL)).thenReturn(pool); + when(execution.getProcessBusinessKey()).thenReturn(BUSINESS_KEY); + when(pool.getNetwork()).thenReturn(org.apache.provisionr.api.network.Network.builder() + .type("provided") + .createNetwork()); + collector = new ProcessVariablesCollector(); + collector.install(execution); + } + + @Override + @After + public void tearDown() throws Exception { + deleteNetworkIfExists(); + logNetworks(); + super.tearDown(); + } + + private void deleteNetworkIfExists() { + try { + Network network = Networks.getByName(context.getApi(), networkName); + // this will not wait for the network to be deleted. operation will fail if network is used. + context.getApi().getNetworkClient().deleteNetwork(network.getId()); + } catch (NoSuchElementException e) { + LOG.info("Network {} does not exist", networkName); + } + } + + @Test + public void testEnsureNetworkExistsByCreatingTheNetwork() throws Exception { + activity.execute(execution); + + assertThat(collector.getVariable(ProcessVariables.NETWORK_ID)).isNotNull(); + + Network network = Networks.getByName(context.getApi(), networkName); + assertThat(network.getName()).isEqualToIgnoringCase(networkName); + String networkId = network.getId(); + // second run should not create a new network + activity.execute(execution); + + when(execution.getProcessBusinessKey()).thenReturn(BUSINESS_KEY); + network = Networks.getByName(context.getApi(), networkName); + assertThat(network.getId()).isEqualTo(networkId); + } + + @Test + public void testEnsureNetworkExistsWithProvidedExistingNetwork() throws Exception { + final String networkId = "network-id-0123"; + final CloudStackClient mockClient = mock(CloudStackClient.class); + final NetworkClient mockNetworkClient = mock(NetworkClient.class); + final Network mockNetwork = mock(Network.class); + final org.apache.provisionr.api.network.Network network = org.apache.provisionr.api.network.Network.builder() + .option(NetworkOptions.EXISTING_NETWORK_ID, networkId).createNetwork(); + + execution.setVariable(ProcessVariables.NETWORK_ID, networkId); + + when(pool.getNetwork()).thenReturn(network); + when(mockClient.getNetworkClient()).thenReturn(mockNetworkClient); + when(mockNetworkClient.getNetwork(networkId)).thenReturn(mockNetwork); + when(mockNetwork.getId()).thenReturn(networkId); + + activity.execute(mockClient, pool, execution); + assertThat(collector.getVariable(ProcessVariables.NETWORK_ID)).isEqualTo(networkId); + } +} http://git-wip-us.apache.org/repos/asf/incubator-provisionr/blob/6ba40c4b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureSecurityGroupExistsLiveTest.java ---------------------------------------------------------------------- diff --git a/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureSecurityGroupExistsLiveTest.java b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureSecurityGroupExistsLiveTest.java new file mode 100644 index 0000000..569b84a --- /dev/null +++ b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/EnsureSecurityGroupExistsLiveTest.java @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2012 S.C. Axemblr Software Solutions S.R.L + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.provisionr.cloudstack.activities; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import java.util.NoSuchElementException; +import org.activiti.engine.delegate.DelegateExecution; +import org.apache.provisionr.api.network.Network; +import org.apache.provisionr.api.network.Rule; +import org.apache.provisionr.api.pool.Pool; +import org.apache.provisionr.cloudstack.core.ConvertIngressRuleToRule; +import org.apache.provisionr.cloudstack.core.SecurityGroups; +import org.apache.provisionr.core.CoreProcessVariables; +import static org.fest.assertions.api.Assertions.assertThat; +import org.jclouds.cloudstack.domain.SecurityGroup; +import static org.jclouds.cloudstack.options.ListSecurityGroupsOptions.Builder.named; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EnsureSecurityGroupExistsLiveTest extends CloudStackActivityLiveTest { + + private static final Logger LOG = LoggerFactory.getLogger(EnsureSecurityGroupExistsLiveTest.class); + + private final String SECURITY_GROUP_NAME = "network-" + BUSINESS_KEY; + + private final ImmutableSet ingressRules = ImmutableSet.of( + Rule.builder().anySource().icmp().createRule(), + Rule.builder().anySource().tcp().port(22).createRule(), + Rule.builder().anySource().udp().port(53).createRule()); + + private final Network network = Network.builder().ingress(ingressRules).createNetwork(); + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + logSecurityGroupDetails(); + deleteSecurityGroupIfExists(); + } + + @Override + @After + public void tearDown() throws Exception { + deleteSecurityGroupIfExists(); + logSecurityGroupDetails(); + super.tearDown(); + } + + private void deleteSecurityGroupIfExists() { + try { + SecurityGroup securityGroup = Iterables.getOnlyElement(context.getApi() + .getSecurityGroupClient() + .listSecurityGroups(named(SECURITY_GROUP_NAME))); + + context.getApi().getSecurityGroupClient().deleteSecurityGroup(securityGroup.getId()); + } catch (NoSuchElementException e) { + LOG.info("Security group {} was not found", SECURITY_GROUP_NAME); + } catch (Exception e2) { + LOG.error("Exception deleting security group {}", e2); + } + } + + @Test + public void testCreateSecurityGroup() throws Exception { + DelegateExecution execution = mock(DelegateExecution.class); + Pool pool = mock(Pool.class); + + when(pool.getProvider()).thenReturn(provider); + when(pool.getNetwork()).thenReturn(network); + + when(execution.getVariable(CoreProcessVariables.POOL)).thenReturn(pool); + when(execution.getProcessBusinessKey()).thenReturn(BUSINESS_KEY); + + activity.execute(execution); + assertSecurityGroupExistsWithRules(SecurityGroups.getByName( + context.getApi(), SECURITY_GROUP_NAME), ingressRules); + } + + @Test + public void testCreateSecurityGroupWithExistingSecurityGroup() throws Exception { + DelegateExecution execution = mock(DelegateExecution.class); + Pool pool = mock(Pool.class); + + when(pool.getProvider()).thenReturn(provider); + + when(execution.getVariable(CoreProcessVariables.POOL)).thenReturn(pool); + when(execution.getProcessBusinessKey()).thenReturn(BUSINESS_KEY); + + // create the SecurityGroup with an extra Network Rule, then call the activity + when(pool.getNetwork()).thenReturn(network.toBuilder().addRules( + Rule.builder().anySource().tcp().port(80).createRule()).createNetwork()); + + activity.execute(execution); + // call the process again with the old network rules and check the rules + when(pool.getNetwork()).thenReturn(network); + + activity.execute(execution); + + assertSecurityGroupExistsWithRules(SecurityGroups.getByName(context.getApi(), + SECURITY_GROUP_NAME), ingressRules); + } + + private void assertSecurityGroupExistsWithRules(SecurityGroup securityGroup, ImmutableSet ingressRules) { + assertThat(ingressRules).containsAll(Iterables.transform(securityGroup.getIngressRules(), + ConvertIngressRuleToRule.FUNCTION)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-provisionr/blob/6ba40c4b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/RunInstancesLiveTest.java ---------------------------------------------------------------------- diff --git a/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/RunInstancesLiveTest.java b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/RunInstancesLiveTest.java new file mode 100644 index 0000000..514a038 --- /dev/null +++ b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/activities/RunInstancesLiveTest.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2012 S.C. Axemblr Software Solutions S.R.L + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.provisionr.cloudstack.activities; + +import com.google.common.collect.ImmutableMap; +import java.util.Map; +import org.activiti.engine.delegate.DelegateExecution; +import org.apache.provisionr.api.access.AdminAccess; +import org.apache.provisionr.api.hardware.Hardware; +import org.apache.provisionr.api.network.Network; +import org.apache.provisionr.api.network.Rule; +import org.apache.provisionr.api.pool.Pool; +import org.apache.provisionr.api.software.Software; +import org.apache.provisionr.cloudstack.ProviderOptions; +import org.apache.provisionr.cloudstack.core.VirtualMachines; +import org.apache.provisionr.core.CoreProcessVariables; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RunInstancesLiveTest extends CloudStackActivityLiveTest { + + private static final Logger LOG = LoggerFactory.getLogger(RunInstancesLiveTest.class); + + private DelegateExecution execution; + private Pool pool; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + logSecurityGroupDetails(); + logKeyPairs(); + logVirtualMachines(); + execution = mock(DelegateExecution.class); + pool = mock(Pool.class); + + final AdminAccess adminAccess = AdminAccess.builder() + .username("admin") + .publicKey(getResourceAsString("keys/test.pub")) + .privateKey(getResourceAsString("keys/test")) + .createAdminAccess(); + + final Network network = Network.builder().addRules( + Rule.builder().anySource().tcp().port(22).createRule()).createNetwork(); + + final Hardware hardware = Hardware.builder().type(getProviderProperty("serviceOffering")).createHardware(); + final Software software = Software.builder() + .imageId(getProviderProperty("templateId")) + .createSoftware(); + + Map options = ImmutableMap.of(ProviderOptions.ZONE_ID, + getProviderProperty("zoneId")); + + when(pool.getProvider()).thenReturn(provider); + when(pool.getAdminAccess()).thenReturn(adminAccess); + when(pool.getNetwork()).thenReturn(network); + when(pool.getHardware()).thenReturn(hardware); + when(pool.getSoftware()).thenReturn(software); + when(pool.getOptions()).thenReturn(options); + + when(execution.getProcessBusinessKey()).thenReturn(BUSINESS_KEY); + when(execution.getVariable(CoreProcessVariables.POOL)).thenReturn(pool); + + new EnsureSecurityGroupExists().execute(execution); + new EnsureKeyPairExists().execute(execution); + } + + @Override + @After + public void tearDown() throws Exception { + new DeleteKeyPair().execute(execution); + new DeleteSecurityGroup().execute(execution); + + logSecurityGroupDetails(); + logKeyPairs(); + logVirtualMachines(); + VirtualMachines.destroyAllVirtualMachineByName(context.getApi(), BUSINESS_KEY); + logVirtualMachines(); + super.tearDown(); + } + + @Test + @Ignore + public void test() throws Exception { + activity.execute(execution); + } +} http://git-wip-us.apache.org/repos/asf/incubator-provisionr/blob/6ba40c4b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/CommandTestSupport.java ---------------------------------------------------------------------- diff --git a/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/CommandTestSupport.java b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/CommandTestSupport.java new file mode 100644 index 0000000..d8d44d2 --- /dev/null +++ b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/CommandTestSupport.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2012 S.C. Axemblr Software Solutions S.R.L + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.provisionr.cloudstack.commands; + +import org.apache.provisionr.api.provider.Provider; +import org.apache.provisionr.cloudstack.DefaultProviderConfig; +import com.google.common.base.Optional; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.jclouds.cloudstack.CloudStackClient; +import org.junit.Before; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Support class for CloudStack Karaf Command tests. + */ +public class CommandTestSupport { + + protected DefaultProviderConfig defaultProviderConfig; + protected ByteArrayOutputStream byteArrayOutputStream; + protected PrintStream out; + protected CloudStackClient client; + + @Before + public void setUp() throws Exception { + defaultProviderConfig = mock(DefaultProviderConfig.class); + byteArrayOutputStream = new ByteArrayOutputStream(); + out = new PrintStream(byteArrayOutputStream); + client = mock(CloudStackClient.class); + when(defaultProviderConfig.createProvider()).thenReturn(Optional.of(mock(Provider.class))); + } +} http://git-wip-us.apache.org/repos/asf/incubator-provisionr/blob/6ba40c4b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/OfferingsCommandTest.java ---------------------------------------------------------------------- diff --git a/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/OfferingsCommandTest.java b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/OfferingsCommandTest.java new file mode 100644 index 0000000..bec3041 --- /dev/null +++ b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/OfferingsCommandTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2012 S.C. Axemblr Software Solutions S.R.L + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.provisionr.cloudstack.commands; + +import com.google.common.collect.Sets; +import java.util.Set; +import static org.fest.assertions.api.Assertions.assertThat; +import org.jclouds.cloudstack.domain.DiskOffering; +import org.jclouds.cloudstack.domain.NetworkOffering; +import org.jclouds.cloudstack.domain.ServiceOffering; +import org.jclouds.cloudstack.features.OfferingClient; +import org.junit.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class OfferingsCommandTest extends CommandTestSupport { + + final Set serviceOfferings = Sets.newHashSet(ServiceOffering.builder() + .id("service-1") + .name("service-one") + .build()); + + final Set diskOfferings = Sets.newHashSet(DiskOffering.builder() + .id("disk-1") + .name("disk-one") + .build()); + + final Set networkOfferings = Sets.newHashSet(NetworkOffering.builder() + .id("network-1") + .name("network-one") + .build()); + + @Test + public void testOfferingCommandWithNoOptionSpecified() throws Exception { + final OfferingsCommand offeringsCommand = new OfferingsCommand(defaultProviderConfig); + offeringsCommand.doExecuteWithContext(client, out); + out.close(); + assertThat(byteArrayOutputStream.toString()).contains("No option specified"); + } + + @Test + public void testOfferingCommandsPrintsServiceOfferings() throws Exception { + final OfferingClient offeringClient = mock(OfferingClient.class); + when(client.getOfferingClient()).thenReturn(offeringClient); + when(offeringClient.listServiceOfferings()).thenReturn(serviceOfferings); + + final OfferingsCommand offeringsCommand = new OfferingsCommand(defaultProviderConfig); + offeringsCommand.setServiceOffering(true); + offeringsCommand.doExecuteWithContext(client, out); + out.close(); + + assertThat(byteArrayOutputStream.toString()) + .contains("service-1") + .contains("service-one") + .doesNotContain("No option"); + } + + @Test + public void testOfferingsCommandWithAllOptions() throws Exception { + final OfferingClient offeringClient = mock(OfferingClient.class); + + when(client.getOfferingClient()).thenReturn(offeringClient); + when(offeringClient.listServiceOfferings()).thenReturn(serviceOfferings); + when(offeringClient.listDiskOfferings()).thenReturn(diskOfferings); + when(offeringClient.listNetworkOfferings()).thenReturn(networkOfferings); + + final OfferingsCommand offeringsCommand = new OfferingsCommand(defaultProviderConfig); + + offeringsCommand.setServiceOffering(true); + offeringsCommand.setDiskOffering(true); + offeringsCommand.setNetworkOffering(true); + + offeringsCommand.doExecuteWithContext(client, out); + + out.close(); + + assertThat(byteArrayOutputStream.toString()) + .contains("service-1") + .contains("service-one") + .contains("disk-1") + .contains("network-1") + .doesNotContain("No option"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-provisionr/blob/6ba40c4b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/TemplatesCommandTest.java ---------------------------------------------------------------------- diff --git a/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/TemplatesCommandTest.java b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/TemplatesCommandTest.java new file mode 100644 index 0000000..9375853 --- /dev/null +++ b/providers/cloudstack/src/test/java/org/apache/provisionr/cloudstack/commands/TemplatesCommandTest.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2012 S.C. Axemblr Software Solutions S.R.L + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.provisionr.cloudstack.commands; + +import com.google.common.collect.Sets; +import java.util.Set; +import static org.fest.assertions.api.Assertions.assertThat; +import org.jclouds.cloudstack.domain.Template; +import org.jclouds.cloudstack.features.TemplateClient; +import org.junit.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class TemplatesCommandTest extends CommandTestSupport { + + final Set