Search in sources :

Example 1 with PowerState

use of com.azure.resourcemanager.compute.models.PowerState 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 PowerState

use of com.azure.resourcemanager.compute.models.PowerState 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 3 with PowerState

use of com.azure.resourcemanager.compute.models.PowerState in project azure-vm-agents-plugin by jenkinsci.

the class ITAzureVMManagementServiceDelegate method restartVMTest.

@Test
public void restartVMTest() throws IOException, AzureCloudException {
    final String vmName = "vmrestart";
    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.restartVirtualMachine(agentMock);
    PowerState state = azureClient.virtualMachines().getByResourceGroup(testEnv.azureResourceGroup, vmName).powerState();
    Assert.assertTrue(state.equals(PowerState.RUNNING) || state.equals(PowerState.STARTING));
    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"));
    try {
        // restart throws exception when the VM is already stopped
        delegate.restartVirtualMachine(agentMock);
        Assert.fail("Expect throwing AzureCloudException but not");
    } catch (AzureCloudException ex) {
    // Expect exception
    }
}
Also used : AzureVMAgent(com.microsoft.azure.vmagent.AzureVMAgent) AzureCloudException(com.microsoft.azure.vmagent.exceptions.AzureCloudException) PowerState(com.azure.resourcemanager.compute.models.PowerState) VirtualMachine(com.azure.resourcemanager.compute.models.VirtualMachine) Test(org.junit.Test)

Aggregations

PowerState (com.azure.resourcemanager.compute.models.PowerState)3 VirtualMachine (com.azure.resourcemanager.compute.models.VirtualMachine)3 AzureVMAgent (com.microsoft.azure.vmagent.AzureVMAgent)3 AzureCloudException (com.microsoft.azure.vmagent.exceptions.AzureCloudException)3 Test (org.junit.Test)3 ManagementException (com.azure.core.management.exception.ManagementException)2 IOException (java.io.IOException)2