Search in sources :

Example 1 with AzureVMAgent

use of com.microsoft.azure.vmagent.AzureVMAgent in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method shutdownVMTest.

@Test
public void shutdownVMTest() {
    try {
        final String vmName = "vmshutdown";
        VirtualMachine vm = createAzureVM(vmName);
        Assert.assertEquals(PowerState.RUNNING, vm.powerState());
        AzureVMAgent agentMock = mock(AzureVMAgent.class);
        when(agentMock.getNodeName()).thenReturn(vmName);
        when(agentMock.getResourceGroupName()).thenReturn(testEnv.azureResourceGroup);
        delegate.shutdownVirtualMachine(agentMock);
        PowerState state = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, vmName).powerState();
        assertThat(state, anyOf(is(PowerState.STOPPING), is(PowerState.STOPPED), is(PowerState.DEALLOCATING), is(PowerState.DEALLOCATED)));
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
Also used : AzureVMAgent(com.microsoft.azure.vmagent.AzureVMAgent) PowerState(com.azure.resourcemanager.compute.models.PowerState) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Example 2 with AzureVMAgent

use of com.microsoft.azure.vmagent.AzureVMAgent in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method isVMAliveOrHealthyTest.

@Test
public void isVMAliveOrHealthyTest() {
    try {
        final String vmName = "vmexists";
        VirtualMachine vm = createAzureVM(vmName);
        Assert.assertNotNull(vm);
        AzureVMAgent agentMock = mock(AzureVMAgent.class);
        when(agentMock.getNodeName()).thenReturn(vmName);
        when(agentMock.getResourceGroupName()).thenReturn(testEnv.azureResourceGroup);
        Assert.assertTrue(delegate.isVMAliveOrHealthy(agentMock));
        vm.powerOff();
        Assert.assertFalse(delegate.isVMAliveOrHealthy(agentMock));
        vm.deallocate();
        Assert.assertFalse(delegate.isVMAliveOrHealthy(agentMock));
        azureClient.virtualMachines().deleteByResourceGroup(testEnv.azureResourceGroup, vmName);
        try {
            delegate.isVMAliveOrHealthy(agentMock);
            Assert.fail("isVMAliveOrHealthy should have thrown an exception");
        } catch (Exception e) {
            Assert.assertTrue(true);
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
Also used : AzureVMAgent(com.microsoft.azure.vmagent.AzureVMAgent) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Example 3 with AzureVMAgent

use of com.microsoft.azure.vmagent.AzureVMAgent in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method startVMTest.

@Test
public void startVMTest() {
    try {
        final String vmName = "vmstart";
        VirtualMachine vm = createAzureVM(vmName);
        Assert.assertEquals(PowerState.RUNNING, vm.powerState());
        AzureVMAgent agentMock = mock(AzureVMAgent.class);
        when(agentMock.getNodeName()).thenReturn(vmName);
        when(agentMock.getResourceGroupName()).thenReturn(testEnv.azureResourceGroup);
        delegate.startVirtualMachine(agentMock);
        Assert.assertEquals(PowerState.RUNNING, azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, vmName).powerState());
        azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, vmName).powerOff();
        PowerState state2 = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, vmName).powerState();
        Assert.assertTrue(state2.toString(), state2.equals(PowerState.STOPPED) || state2.toString().equalsIgnoreCase("powerstate/stopping"));
        delegate.startVirtualMachine(agentMock);
        PowerState state = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, vmName).powerState();
        Assert.assertTrue(state.equals(PowerState.RUNNING) || state.equals(PowerState.STARTING));
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, null, e);
        Assert.fail(e.getMessage());
    }
}
Also used : AzureVMAgent(com.microsoft.azure.vmagent.AzureVMAgent) PowerState(com.azure.resourcemanager.compute.models.PowerState) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) IOException(java.io.IOException) ManagementException(com.azure.core.management.exception.ManagementException) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Example 4 with AzureVMAgent

use of com.microsoft.azure.vmagent.AzureVMAgent in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method virtualMachineExistsTest.

@Test
public void virtualMachineExistsTest() throws IOException, AzureCloudException {
    final String vmName = "vmexists";
    createAzureVM(vmName);
    AzureVMAgent agentMock = mock(AzureVMAgent.class);
    when(agentMock.getNodeName()).thenReturn(vmName);
    when(agentMock.getResourceGroupName()).thenReturn(testEnv.azureResourceGroup);
    when(agentMock.getServiceDelegate()).thenReturn(delegate);
    Assert.assertTrue("The deployed VM doesn't exist: " + vmName, AzureVMManagementServiceDelegate.virtualMachineExists(agentMock));
    // invalid VM name
    when(agentMock.getNodeName()).thenReturn(vmName + "a");
    Assert.assertFalse("The deployed VM exists: " + vmName, AzureVMManagementServiceDelegate.virtualMachineExists(agentMock));
}
Also used : AzureVMAgent(com.microsoft.azure.vmagent.AzureVMAgent) Test(org.junit.Test)

Example 5 with AzureVMAgent

use of com.microsoft.azure.vmagent.AzureVMAgent in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method setVirtualMachineDetailsCommonVerification.

private void setVirtualMachineDetailsCommonVerification(String vmName, String fqdn, String privateIP, String publicIp) throws Exception {
    AzureVMAgent agentMock = mock(AzureVMAgent.class);
    AzureVMAgentTemplate templateMock = mock(AzureVMAgentTemplate.class);
    AzureVMCloud cloudMock = mock(AzureVMCloud.class);
    when(templateMock.retrieveAzureCloudReference()).thenReturn(cloudMock);
    when(templateMock.getResourceGroupName()).thenReturn(testEnv.azureResourceGroup);
    when(agentMock.getNodeName()).thenReturn(vmName);
    delegate.setVirtualMachineDetails(agentMock, templateMock);
    verify(agentMock).setPublicDNSName(fqdn);
    verify(agentMock).setSshPort(Constants.DEFAULT_SSH_PORT);
    verify(agentMock).setPublicIP(publicIp);
    verify(agentMock).setPrivateIP(privateIP);
}
Also used : AzureVMAgent(com.microsoft.azure.vmagent.AzureVMAgent) AzureVMCloud(com.microsoft.azure.vmagent.AzureVMCloud) AzureVMAgentTemplate(com.microsoft.azure.vmagent.AzureVMAgentTemplate)

Aggregations

AzureVMAgent (com.microsoft.azure.vmagent.AzureVMAgent)10 Test (org.junit.Test)8 AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)7 ManagementException (com.azure.core.management.exception.ManagementException)5 IOException (java.io.IOException)5 VirtualMachine (com.azure.resourcemanager.compute.models.VirtualMachine)4 AzureVMAgentTemplate (com.microsoft.azure.vmagent.AzureVMAgentTemplate)4 AzureVMCloud (com.microsoft.azure.vmagent.AzureVMCloud)4 PowerState (com.azure.resourcemanager.compute.models.PowerState)3 AzureVMDeploymentInfo (com.microsoft.azure.vmagent.AzureVMDeploymentInfo)3 PublicIpAddress (com.azure.resourcemanager.network.models.PublicIpAddress)2 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 AzureVMComputer (com.microsoft.azure.vmagent.AzureVMComputer)1 Node (hudson.model.Node)1 TaskListener (hudson.model.TaskListener)1 Channel (hudson.remoting.Channel)1 Listener (hudson.remoting.Channel.Listener)1 SlaveComputer (hudson.slaves.SlaveComputer)1 ConnectException (java.net.ConnectException)1 UnknownHostException (java.net.UnknownHostException)1