com.amazonaws.services.ec2.model.InstanceState Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.InstanceState. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
// Test terminating a valid but not matching instances is handled correctly
public void testTerminateInstanceNoMatchingInstance() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    String instanceId="foo";
    TerminateInstancesResult terminateInstancesResult = new TerminateInstancesResult();
    client.setTerminateInstancesResult(terminateInstancesResult);
    InstanceStateChange stateChange = new InstanceStateChange();
    stateChange.withInstanceId("notMatching");
    stateChange.setCurrentState(new InstanceState().withCode(8));
    terminateInstancesResult.setTerminatingInstances(Arrays.asList(stateChange));
    Properties properties = new Properties();
    String region = "east";
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);

    boolean success = manageEC2.terminateInstance(instanceId);
    TerminateInstancesRequest request = client.getTerminateInstancesRequest();
    Assert.assertEquals("Instance id size should match", 1, request.getInstanceIds().size());
    Assert.assertEquals("Instance ids should match", instanceId, request.getInstanceIds().get(0));
    Assert.assertFalse("Termination call should have not been successful", success);
}
 
Example #2
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
// Tests terminating an invalid instance is handled correctly
public void testTerminateInstanceInvalidRunningCode() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    String instanceId="foo";
    TerminateInstancesResult terminateInstancesResult = new TerminateInstancesResult();
    client.setTerminateInstancesResult(terminateInstancesResult);
    InstanceStateChange stateChange = new InstanceStateChange();
    stateChange.withInstanceId(instanceId);
    stateChange.setCurrentState(new InstanceState().withCode(8));
    terminateInstancesResult.setTerminatingInstances(Arrays.asList(stateChange));
    Properties properties = new Properties();
    String region = "east";
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);

    boolean success = manageEC2.terminateInstance(instanceId);
    TerminateInstancesRequest request = client.getTerminateInstancesRequest();
    Assert.assertEquals("Instance id size should match", 1, request.getInstanceIds().size());
    Assert.assertEquals("Instance ids should match", instanceId, request.getInstanceIds().get(0));
    Assert.assertFalse("Termination call should have not been successful", success);
}
 
Example #3
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
// Test terminating instances works correctly
public void testTerminateInstance() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    String instanceId="foo";
    TerminateInstancesResult terminateInstancesResult = new TerminateInstancesResult();
    client.setTerminateInstancesResult(terminateInstancesResult);
    InstanceStateChange stateChange = new InstanceStateChange();
    stateChange.withInstanceId(instanceId);
    stateChange.setCurrentState(new InstanceState().withCode(32));
    terminateInstancesResult.setTerminatingInstances(Arrays.asList(stateChange));
    Properties properties = new Properties();
    String region = "east";
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);

    boolean success = manageEC2.terminateInstance(instanceId);
    TerminateInstancesRequest request = client.getTerminateInstancesRequest();
    Assert.assertEquals("Instance id size should match", 1, request.getInstanceIds().size());
    Assert.assertEquals("Instance ids should match", instanceId, request.getInstanceIds().get(0));
    Assert.assertTrue("Termination call should have been successful", success);
}
 
Example #4
Source File: InstanceStateChangeTerminateConverter.java    From primecloud-controller with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected InstanceStateChange convertObject(TerminatingInstanceDescription from) {
    InstanceStateChange to = new InstanceStateChange();

    to.setInstanceId(from.getInstanceId());

    InstanceState previousState = new InstanceState();
    previousState.setCode(from.getPreviousStateCode());
    previousState.setName(from.getPreviousState());
    to.setPreviousState(previousState);

    InstanceState currentState = new InstanceState();
    currentState.setCode(from.getShutdownStateCode());
    currentState.setName(from.getShutdownState());
    to.setCurrentState(currentState);

    return to;
}
 
Example #5
Source File: EC2Mockup.java    From development with Apache License 2.0 6 votes vote down vote up
public void addDescribeInstanceStatusResult(String instanceId,
        String stateName, String instanceStatusName,
        String systemStatusName) {
    InstanceState state = new InstanceState().withName(stateName);
    InstanceStatusSummary instanceSummary = new InstanceStatusSummary()
            .withStatus(instanceStatusName);
    InstanceStatusSummary systemSummary = new InstanceStatusSummary()
            .withStatus(systemStatusName);
    com.amazonaws.services.ec2.model.InstanceStatus instanceStatus = new com.amazonaws.services.ec2.model.InstanceStatus()
            .withInstanceId(instanceId).withInstanceState(state)
            .withInstanceStatus(instanceSummary)
            .withSystemStatus(systemSummary);
    DescribeInstanceStatusResult instanceStatusResult = new DescribeInstanceStatusResult()
            .withInstanceStatuses(instanceStatus);
    answerDescribeInstanceStatus.add(instanceStatusResult);
}
 
Example #6
Source File: EC2Mockup.java    From development with Apache License 2.0 6 votes vote down vote up
public void createDescribeInstanceStatusResult(String instanceId,
        String stateName, String instanceStatusName,
        String systemStatusName) {
    InstanceState state = new InstanceState().withName(stateName);
    InstanceStatusSummary instanceSummary = new InstanceStatusSummary()
            .withStatus(instanceStatusName);
    InstanceStatusSummary systemSummary = new InstanceStatusSummary()
            .withStatus(systemStatusName);
    InstanceStatus instanceStatus = new com.amazonaws.services.ec2.model.InstanceStatus()
            .withInstanceId(instanceId).withInstanceState(state)
            .withInstanceStatus(instanceSummary)
            .withSystemStatus(systemSummary);
    DescribeInstanceStatusResult instanceStatusResult = new DescribeInstanceStatusResult()
            .withInstanceStatuses(instanceStatus);
    doReturn(instanceStatusResult).when(ec2).describeInstanceStatus(
            any(DescribeInstanceStatusRequest.class));
}
 
Example #7
Source File: EC2Mockup.java    From development with Apache License 2.0 5 votes vote down vote up
public void addDescribeInstancesResult(String instanceId, String stateName,
        String publicDnsName) {
    InstanceState state = new InstanceState().withName(stateName);
    Instance instance = new Instance().withInstanceId(instanceId)
            .withState(state).withPublicDnsName(publicDnsName);
    Reservation reservation = new Reservation().withInstances(instance);
    DescribeInstancesResult instancesResult = new DescribeInstancesResult()
            .withReservations(reservation);
    answerDescribeInstances.add(instancesResult);
}
 
Example #8
Source File: AwsVmManager.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Terminates the specified instance.
 *
 * @param  instanceId  Id of the instance to terminate
 */
public boolean terminateInstance(final String instanceId) {
    TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest();
    terminateRequest.withInstanceIds(instanceId);

    if(client == null){
        throw new RuntimeException("The client is not initialized");
    }
    TerminateInstancesResult result = client.terminateInstances(terminateRequest);
    List<InstanceStateChange> stateChanges = result.getTerminatingInstances();
    boolean terminatedInstance = false;
    for (InstanceStateChange stateChange : stateChanges) {
        if (instanceId.equals(stateChange.getInstanceId())) {
            terminatedInstance = true;

            InstanceState currentState = stateChange.getCurrentState();
            if (currentState.getCode() != 32 && currentState.getCode() != 48) {
                log.error(String.format(
                        "Machine state for id %s should be terminated (48) or shutting down (32) but was %s instead",
                        instanceId, currentState.getCode()));
                return false;
            }
        }
    }

    if (!terminatedInstance) {
        log.error("Matching terminated instance was not found for instance " + instanceId);
        return false;
    }

    return true;
}
 
Example #9
Source File: EC2ConnectorTest.java    From jenkins-deployment-dashboard-plugin with MIT License 5 votes vote down vote up
@Test
public void testGetEnvironmentFromInstance() throws Exception {
    final Date launchTime = new Date();
    final Instance instance = new Instance().withInstanceId("instance").withInstanceType(InstanceType.C1Xlarge).withTags(new Tag(EC2Connector.DEFAULT_INSTANCE_NAME_TAG, "unknown"))
            .withState(new InstanceState().withName(InstanceStateName.Running)).withLaunchTime(launchTime).withPublicIpAddress("127.0.0.1");
    ServerEnvironment serverEnv = Whitebox.<ServerEnvironment> invokeMethod(env, "getEnvironmentFromInstance", instance);
    assertThat(serverEnv, notNullValue());
    assertThat(serverEnv.getInstanceId(), is("instance"));
    assertThat(serverEnv.getInstanceType(), is(InstanceType.C1Xlarge.toString()));
    assertThat(serverEnv.getType(), is(ENVIRONMENT_TYPES.TEST));
    assertThat(serverEnv.getEnvironmentTag(), is("unknown"));
    assertThat(serverEnv.getState().getName(), is(InstanceStateName.Running.toString()));
    assertThat(serverEnv.getLaunchTime(), is(launchTime));
    assertThat(serverEnv.getPublicIpAddress(), is("127.0.0.1"));
}
 
Example #10
Source File: AwsInstanceStatusMapper.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public static InstanceStatus getInstanceStatusByAwsStateAndReason(InstanceState state, StateReason stateReason) {
    switch (state.getName().toLowerCase()) {
        case "stopped":
            return InstanceStatus.STOPPED;
        case "running":
            return InstanceStatus.STARTED;
        case "terminated":
            return stateReason != null && "Server.SpotInstanceTermination".equals(stateReason.getCode())
                    ? InstanceStatus.TERMINATED_BY_PROVIDER
                    : InstanceStatus.TERMINATED;
        default:
            return InstanceStatus.IN_PROGRESS;
    }
}
 
Example #11
Source File: EC2Mockup.java    From development with Apache License 2.0 5 votes vote down vote up
public void createDescribeInstancesResult(String instanceId,
        String stateName, String publicDnsName) {
    InstanceState state = new InstanceState().withName(stateName);
    Instance instance = new Instance().withInstanceId(instanceId)
            .withState(state).withPublicDnsName(publicDnsName);
    Reservation reservation = new Reservation().withInstances(instance);
    DescribeInstancesResult instancesResult = new DescribeInstancesResult()
            .withReservations(reservation);
    doReturn(instancesResult).when(ec2)
            .describeInstances(any(DescribeInstancesRequest.class));
}
 
Example #12
Source File: AutoResubmitIntegrationTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
@Before
public void before() {
    EC2Api ec2Api = spy(EC2Api.class);
    Registry.setEc2Api(ec2Api);

    AmazonEC2 amazonEC2 = mock(AmazonEC2.class);
    when(ec2Api.connect(anyString(), anyString(), Mockito.nullable(String.class))).thenReturn(amazonEC2);

    final Instance instance = new Instance()
            .withState(new InstanceState().withName(InstanceStateName.Running))
            .withPublicIpAddress("public-io")
            .withInstanceId("i-1");

    when(amazonEC2.describeInstances(any(DescribeInstancesRequest.class))).thenReturn(
            new DescribeInstancesResult().withReservations(
                    new Reservation().withInstances(
                            instance
                    )));

    when(amazonEC2.describeSpotFleetInstances(any(DescribeSpotFleetInstancesRequest.class)))
            .thenReturn(new DescribeSpotFleetInstancesResult()
                    .withActiveInstances(new ActiveInstance().withInstanceId("i-1")));

    DescribeSpotFleetRequestsResult describeSpotFleetRequestsResult = new DescribeSpotFleetRequestsResult();
    describeSpotFleetRequestsResult.setSpotFleetRequestConfigs(Arrays.asList(
            new SpotFleetRequestConfig()
                    .withSpotFleetRequestState("active")
                    .withSpotFleetRequestConfig(
                            new SpotFleetRequestConfigData().withTargetCapacity(1))));
    when(amazonEC2.describeSpotFleetRequests(any(DescribeSpotFleetRequestsRequest.class)))
            .thenReturn(describeSpotFleetRequestsResult);
}
 
Example #13
Source File: EC2ApiTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * NotFound exception example data
 * <p>
 * <code>
 * Single instance
 * requestId = "0fd56c54-e11a-4928-843c-9a80a24bedd1"
 * errorCode = "InvalidInstanceID.NotFound"
 * errorType = {AmazonServiceException$ErrorType@11247} "Unknown"
 * errorMessage = "The instance ID 'i-1233f' does not exist"
 * </code>
 * <p>
 * Multiple instances
 * <code>
 * ex = {AmazonEC2Exception@11233} "com.amazonaws.services.ec2.model.AmazonEC2Exception: The instance IDs 'i-1233f, i-ffffff' do not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidInstanceID.NotFound; Request ID:)"
 * requestId = "1a353313-ef52-4626-b87b-fd828db6343f"
 * errorCode = "InvalidInstanceID.NotFound"
 * errorType = {AmazonServiceException$ErrorType@11251} "Unknown"
 * errorMessage = "The instance IDs 'i-1233f, i-ffffff' do not exist"
 * </code>
 */
@Test
public void describeInstances_shouldHandleAmazonEc2NotFoundErrorAsTerminatedInstancesAndRetry() {
    // given
    Set<String> instanceIds = new HashSet<>();
    instanceIds.add("i-1");
    instanceIds.add("i-f");
    instanceIds.add("i-3");

    AmazonEC2Exception notFoundException = new AmazonEC2Exception(
            "The instance IDs 'i-1, i-f' do not exist");
    notFoundException.setErrorCode("InvalidInstanceID.NotFound");

    final Instance instance3 = new Instance().withInstanceId("i-3")
            .withState(new InstanceState().withName(InstanceStateName.Running));
    DescribeInstancesResult describeInstancesResult2 = new DescribeInstancesResult()
            .withReservations(new Reservation().withInstances(
                    instance3));

    when(amazonEC2.describeInstances(any(DescribeInstancesRequest.class)))
            .thenThrow(notFoundException)
            .thenReturn(describeInstancesResult2);

    // when
    final Map<String, Instance> described = new EC2Api().describeInstances(amazonEC2, instanceIds);

    // then
    Assert.assertEquals(ImmutableMap.of("i-3", instance3), described);
    verify(amazonEC2).describeInstances(new DescribeInstancesRequest().withInstanceIds(Arrays.asList("i-1", "i-3", "i-f")));
    verify(amazonEC2).describeInstances(new DescribeInstancesRequest().withInstanceIds(Arrays.asList("i-3")));
    verifyNoMoreInteractions(amazonEC2);
}
 
Example #14
Source File: EC2ApiTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void describeInstances_shouldSendInOneCallNoMoreThenBatchSizeOfInstance() {
    // given
    Set<String> instanceIds = new HashSet<>();
    instanceIds.add("i1");
    instanceIds.add("i2");
    instanceIds.add("i3");

    DescribeInstancesResult describeInstancesResult1 =
            new DescribeInstancesResult()
                    .withReservations(
                            new Reservation().withInstances(new Instance()
                                            .withInstanceId("stopped")
                                            .withState(new InstanceState().withName(InstanceStateName.Running)),
                                    new Instance()
                                            .withInstanceId("stopping")
                                            .withState(new InstanceState().withName(InstanceStateName.Running))
                            ));

    DescribeInstancesResult describeInstancesResult2 = new DescribeInstancesResult();

    when(amazonEC2.describeInstances(any(DescribeInstancesRequest.class)))
            .thenReturn(describeInstancesResult1)
            .thenReturn(describeInstancesResult2);

    // when
    new EC2Api().describeInstances(amazonEC2, instanceIds, 2);

    // then
    verify(amazonEC2).describeInstances(new DescribeInstancesRequest().withInstanceIds(Arrays.asList("i1", "i2")));
    verify(amazonEC2).describeInstances(new DescribeInstancesRequest().withInstanceIds(Arrays.asList("i3")));
    verifyNoMoreInteractions(amazonEC2);
}
 
Example #15
Source File: EC2ApiTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void describeInstances_shouldNotDescribeMissedInResultInstanceOrTerminatedOrStoppedOrStoppingOrShuttingDownAs() {
    // given
    Set<String> instanceIds = new HashSet<>();
    instanceIds.add("missed");
    instanceIds.add("stopped");
    instanceIds.add("terminated");
    instanceIds.add("stopping");
    instanceIds.add("shutting-down");

    DescribeInstancesResult describeInstancesResult1 =
            new DescribeInstancesResult()
                    .withReservations(
                            new Reservation().withInstances(new Instance()
                                            .withInstanceId("stopped")
                                            .withState(new InstanceState().withName(InstanceStateName.Stopped)),
                                    new Instance()
                                            .withInstanceId("stopping")
                                            .withState(new InstanceState().withName(InstanceStateName.Stopping)),
                                    new Instance()
                                            .withInstanceId("shutting-down")
                                            .withState(new InstanceState().withName(InstanceStateName.ShuttingDown)),
                                    new Instance()
                                            .withInstanceId("terminated")
                                            .withState(new InstanceState().withName(InstanceStateName.Terminated))
                            ));


    when(amazonEC2.describeInstances(any(DescribeInstancesRequest.class)))
            .thenReturn(describeInstancesResult1);

    // when
    Map<String, Instance> described = new EC2Api().describeInstances(amazonEC2, instanceIds);

    // then
    Assert.assertEquals(Collections.<String, Instance>emptyMap(), described);
    verify(amazonEC2, times(1))
            .describeInstances(any(DescribeInstancesRequest.class));
}
 
Example #16
Source File: ReconcileWithAwsJob.java    From soundwave with Apache License 2.0 5 votes vote down vote up
private boolean isRunningOrPending(InstanceState state) {
  boolean ret = false;
  if (state != null) {
    ret = state.getCode() == 0 || state.getCode() == 16; //0 - pending. 16 -- running
  }
  return ret;
}
 
Example #17
Source File: EC2ApiTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void describeInstances_shouldReturnAllInstancesIfStillActive() {
    // given
    Set<String> instanceIds = new HashSet<>();
    instanceIds.add("i-1");
    instanceIds.add("i-2");

    DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult();
    Reservation reservation = new Reservation();
    Instance instance1 = new Instance()
            .withInstanceId("i-1")
            .withState(new InstanceState().withName(InstanceStateName.Running));
    Instance instance2 = new Instance()
            .withInstanceId("i-2")
            .withState(new InstanceState().withName(InstanceStateName.Running));
    reservation.setInstances(Arrays.asList(instance1, instance2));
    describeInstancesResult.setReservations(Arrays.asList(reservation));

    when(amazonEC2.describeInstances(any(DescribeInstancesRequest.class))).thenReturn(describeInstancesResult);

    // when
    Map<String, Instance> described = new EC2Api().describeInstances(amazonEC2, instanceIds);

    // then
    Assert.assertEquals(ImmutableMap.of("i-1", instance1, "i-2", instance2), described);
    verify(amazonEC2, times(1))
            .describeInstances(any(DescribeInstancesRequest.class));
}
 
Example #18
Source File: AwsLaunchTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
private void setupDescribeInstanceStatusResponse() {
    when(amazonEC2Client.describeInstanceStatus(any())).thenReturn(
            new DescribeInstanceStatusResult().withInstanceStatuses(
                    new com.amazonaws.services.ec2.model.InstanceStatus().withInstanceState(new InstanceState().withCode(INSTANCE_STATE_RUNNING)))
    );
}
 
Example #19
Source File: AwsInstanceConnectorTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
private Instance getAwsInstance(String s, String state, int code) {
    return new Instance().withState(new InstanceState().withName(state).withCode(code)).withInstanceId(s);
}
 
Example #20
Source File: AwsInstanceStatusMapper.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public static InstanceStatus getInstanceStatusByAwsStatus(String status) {
    return getInstanceStatusByAwsStateAndReason(new InstanceState().withName(status), null);
}
 
Example #21
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public InstanceState getState() {
    return (InstanceState) resource.getAttribute("State");
}
 
Example #22
Source File: InstanceConverter.java    From primecloud-controller with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected Instance convertObject(com.xerox.amazonws.ec2.ReservationDescription.Instance from) {
    Instance to = new Instance();

    to.setInstanceId(from.getInstanceId());
    to.setImageId(from.getImageId());

    InstanceState state = new InstanceState();
    state.setCode(from.getStateCode());
    state.setName(from.getState());
    to.setState(state);

    to.setPrivateDnsName(from.getPrivateDnsName());
    to.setPublicDnsName(from.getDnsName());
    to.setStateTransitionReason(from.getReason());
    to.setKeyName(from.getKeyName());
    to.setAmiLaunchIndex(null);
    to.setProductCodes(null);
    to.setInstanceType(from.getInstanceType().name());
    to.setLaunchTime(from.getLaunchTime().getTime());

    Placement placement = new Placement();
    placement.setAvailabilityZone(from.getAvailabilityZone());
    placement.setGroupName(null); // 未実装
    to.setPlacement(placement);

    to.setKernelId(from.getKernelId());
    to.setRamdiskId(from.getRamdiskId());
    to.setPlatform(from.getPlatform());

    Monitoring monitoring = new Monitoring();
    monitoring.setState(Boolean.toString(from.isMonitoring()));
    to.setMonitoring(monitoring);

    // 未実装
    to.setSubnetId(null);
    to.setVpcId(null);
    to.setPrivateIpAddress(null);
    to.setPublicIpAddress(null);
    to.setStateReason(null);
    //to.setArchitecture(null);
    to.setRootDeviceName(null);
    to.setRootDeviceName(null);
    to.setBlockDeviceMappings(null);
    //to.setVirtualizationType(null);
    //to.setInstanceLifecycle(null);
    to.setSpotInstanceRequestId(null);
    //to.setLicense(null);
    to.setClientToken(null);
    to.setTags(null);

    return to;
}
 
Example #23
Source File: ServerEnvironment.java    From jenkins-deployment-dashboard-plugin with MIT License 4 votes vote down vote up
public void setState(InstanceState state) {
    this.state = state;
}
 
Example #24
Source File: ServerEnvironment.java    From jenkins-deployment-dashboard-plugin with MIT License 4 votes vote down vote up
public InstanceState getState() {
    return state;
}
 
Example #25
Source File: ProvisionIntegrationTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
@Test
    public void should_not_allow_jenkins_to_provision_if_address_not_available() throws Exception {
        ComputerLauncher computerLauncher = mock(ComputerLauncher.class);
        ComputerConnector computerConnector = mock(ComputerConnector.class);
        when(computerConnector.launch(anyString(), any(TaskListener.class))).thenReturn(computerLauncher);

        EC2FleetCloud cloud = spy(new EC2FleetCloud(null, null, "credId", null, "region",
                null, "fId", "momo", null, computerConnector, false, false,
                0, 0, 10, 1, false, false,
                false, 0, 0, false,
                10, false));

        cloud.setStats(new FleetStateStats("", 0, "active",
                Collections.<String>emptySet(), Collections.<String, Double>emptyMap()));

        j.jenkins.clouds.add(cloud);

        EC2Api ec2Api = spy(EC2Api.class);
        Registry.setEc2Api(ec2Api);

        AmazonEC2 amazonEC2 = mock(AmazonEC2.class);
        when(ec2Api.connect(anyString(), anyString(), Mockito.nullable(String.class))).thenReturn(amazonEC2);

        when(amazonEC2.describeInstances(any(DescribeInstancesRequest.class))).thenReturn(
                new DescribeInstancesResult().withReservations(
                        new Reservation().withInstances(
                                new Instance()
                                        .withState(new InstanceState().withName(InstanceStateName.Running))
//                                        .withPublicIpAddress("public-io")
                                        .withInstanceId("i-1")
                        )));

        when(amazonEC2.describeSpotFleetInstances(any(DescribeSpotFleetInstancesRequest.class))).thenReturn(
                new DescribeSpotFleetInstancesResult().withActiveInstances(new ActiveInstance().withInstanceId("i-1")));

        DescribeSpotFleetRequestsResult describeSpotFleetRequestsResult = new DescribeSpotFleetRequestsResult();
        describeSpotFleetRequestsResult.setSpotFleetRequestConfigs(Arrays.asList(
                new SpotFleetRequestConfig()
                        .withSpotFleetRequestState("active")
                        .withSpotFleetRequestConfig(
                                new SpotFleetRequestConfigData().withTargetCapacity(1))));
        when(amazonEC2.describeSpotFleetRequests(any(DescribeSpotFleetRequestsRequest.class)))
                .thenReturn(describeSpotFleetRequestsResult);

        List<QueueTaskFuture> rs = getQueueTaskFutures(1);

        j.jenkins.getLabelAtom("momo").nodeProvisioner.suggestReviewNow();

        Assert.assertEquals(0, j.jenkins.getNodes().size());

        Thread.sleep(TimeUnit.MINUTES.toMillis(2));

        cancelTasks(rs);

        verify(cloud, times(1)).provision(any(Label.class), anyInt());
    }
 
Example #26
Source File: EC2ApiTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void describeInstances_shouldProcessAllPagesUntilNextTokenIsAvailable() {
    // given
    Set<String> instanceIds = new HashSet<>();
    instanceIds.add("i-1");
    instanceIds.add("i-2");
    instanceIds.add("i-3");

    final Instance instance1 = new Instance()
            .withInstanceId("i-1")
            .withState(new InstanceState().withName(InstanceStateName.Running));
    DescribeInstancesResult describeInstancesResult1 =
            new DescribeInstancesResult()
                    .withReservations(
                            new Reservation().withInstances(instance1))
                    .withNextToken("a");

    final Instance instance2 = new Instance()
            .withInstanceId("i-2")
            .withState(new InstanceState().withName(InstanceStateName.Running));
    DescribeInstancesResult describeInstancesResult2 =
            new DescribeInstancesResult()
                    .withReservations(new Reservation().withInstances(
                            instance2,
                            new Instance()
                                    .withInstanceId("i-3")
                                    .withState(new InstanceState().withName(InstanceStateName.Terminated))
                    ));

    when(amazonEC2.describeInstances(any(DescribeInstancesRequest.class)))
            .thenReturn(describeInstancesResult1)
            .thenReturn(describeInstancesResult2);

    // when
    Map<String, Instance> described = new EC2Api().describeInstances(amazonEC2, instanceIds);

    // then
    Assert.assertEquals(ImmutableMap.of("i-1", instance1, "i-2", instance2), described);
    verify(amazonEC2, times(2))
            .describeInstances(any(DescribeInstancesRequest.class));
}
 
Example #27
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the value of the State attribute. If this resource is not yet
 * loaded, a call to {@code load()} is made to retrieve the value of the
 * attribute.
 */
InstanceState getState();