Java Code Examples for com.amazonaws.services.ec2.model.Reservation#setInstances()

The following examples show how to use com.amazonaws.services.ec2.model.Reservation#setInstances() . 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: Ec2LookupServiceTest.java    From Gatekeeper with Apache License 2.0 6 votes vote down vote up
private Reservation fakeInstance(String instanceID, String instanceIP, String instanceName, String instanceApplication, String platform) {
    Reservation container = new Reservation();
    List<Instance> instances = new ArrayList<>();
    Instance i = new Instance();
    List<Tag> tags = new ArrayList<>();
    i.setInstanceId(instanceID);
    i.setPrivateIpAddress(instanceIP);
    Tag nameTag = new Tag();
    nameTag.setKey("Name");
    nameTag.setValue(instanceName);
    Tag applicationTag = new Tag();
    applicationTag.setKey("Application");
    applicationTag.setValue(instanceApplication);
    tags.add(applicationTag);
    tags.add(nameTag);
    i.setTags(tags);
    i.setPlatform(platform);
    instances.add(i);
    container.setInstances(instances);

    return container;
}
 
Example 2
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
// Happy path test flow for launching nodes
public void testLaunchNodes() throws NodesCouldNotBeStartedException{
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    RunInstancesResult runInstancesResult = new RunInstancesResult();
    Reservation reservation = new Reservation();
    reservation.setInstances(Arrays.asList(new Instance()));
    runInstancesResult.setReservation(reservation);
    client.setRunInstances(runInstancesResult);
    Properties properties = new Properties();
    String region = "east", uuid="uuid",browser="chrome",os="windows";
    Integer threadCount = 5,maxSessions=5;
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);
    String userData = "userData";
    manageEC2.setUserData(userData);
    List<Instance> instances = manageEC2.launchNodes(uuid,os,browser,null,threadCount,maxSessions);
    RunInstancesRequest request = client.getRunInstancesRequest();
    Assert.assertEquals("Min count should match thread count requested", threadCount, request.getMinCount());
    Assert.assertEquals("Max count should match thread count requested", threadCount, request.getMaxCount());
    Assert.assertEquals("User data should match", userData, request.getUserData());
    Assert.assertTrue("No security group should be set", request.getSecurityGroupIds().isEmpty());
    Assert.assertNull("No subnet should be set", request.getSubnetId());
    Assert.assertNull("No key name should be set", request.getKeyName());
}
 
Example 3
Source File: AwsTagReporterTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test()
public void testThreadTimesOut() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    Collection<Instance> instances = Arrays.asList(new Instance());
    DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult();
    Reservation reservation = new Reservation();
    describeInstancesResult.setReservations(Arrays.asList(reservation));
    // Make count mismatch
    reservation.setInstances(Arrays.asList(new Instance(),new Instance()));
    client.setDescribeInstances(describeInstancesResult);
    Properties properties = new Properties();
    properties.setProperty("accounting_tag","foo");
    properties.setProperty("function_tag","foo2");
    properties.setProperty("product_tag","foo3");
    AwsTagReporter reporter = new AwsTagReporter("testUuid",client,instances,properties);
    AwsTagReporter.TIMEOUT_IN_SECONDS = 1;
    try{
        reporter.run();
    } catch(RuntimeException e) {
        Assert.assertEquals("Error waiting for instances to exist to add tags",e.getMessage());
        return;
    }
    Assert.fail("Exception should have been thrown since tags were never filed");
}
 
Example 4
Source File: AwsTagReporterTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSleepThrowsErrors() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    client.setDescribeInstancesToThrowError();
    Collection<Instance> instances = Arrays.asList(new Instance());
    DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult();
    Reservation reservation = new Reservation();
    describeInstancesResult.setReservations(Arrays.asList(reservation));
    reservation.setInstances(instances);
    client.setDescribeInstances(describeInstancesResult);
    Properties properties = new Properties();
    properties.setProperty("accounting_tag","foo");
    properties.setProperty("function_tag","foo2");
    properties.setProperty("product_tag","foo3");
    AwsTagReporter reporter = new AwsTagReporter("testUuid",client,instances,properties) {
        @Override
        void sleep() throws InterruptedException {
            throw new InterruptedException();
        }
    };
    reporter.run();
}
 
Example 5
Source File: AwsTagReporterTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testClientThrowsErrors() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    client.setDescribeInstancesToThrowError();
    Collection<Instance> instances = Arrays.asList(new Instance());
    DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult();
    Reservation reservation = new Reservation();
    describeInstancesResult.setReservations(Arrays.asList(reservation));
    reservation.setInstances(instances);
    client.setDescribeInstances(describeInstancesResult);
    Properties properties = new Properties();
    properties.setProperty("accounting_tag","foo");
    properties.setProperty("function_tag","foo2");
    properties.setProperty("product_tag","foo3");
    AwsTagReporter reporter = new AwsTagReporter("testUuid",client,instances,properties) {
        @Override
        void sleep() throws InterruptedException {
            // do nothing
        }
    };
    reporter.run();
}
 
Example 6
Source File: AwsTagReporterTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testExceptionCaught() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    Collection<Instance> instances = Arrays.asList(new Instance());
    DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult();
    Reservation reservation = new Reservation();
    describeInstancesResult.setReservations(Arrays.asList(reservation));
    reservation.setInstances(instances);
    client.setDescribeInstances(describeInstancesResult);
    Properties properties = new Properties();
    properties.setProperty("tagAccounting","key");
    properties.setProperty("function_tag","foo2");
    properties.setProperty("product_tag","foo3");
    AwsTagReporter reporter = new AwsTagReporter("testUuid",client,instances,properties);
    reporter.run();
}
 
Example 7
Source File: AwsTagReporterTest.java    From SeleniumGridScaler with GNU General Public License v2.0 6 votes vote down vote up
@Test
     public void testTagsAssociated() {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    Collection<Instance> instances = Arrays.asList(new Instance());
    DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult();
    Reservation reservation = new Reservation();
    describeInstancesResult.setReservations(Arrays.asList(reservation));
    reservation.setInstances(instances);
    client.setDescribeInstances(describeInstancesResult);
    Properties properties = new Properties();
    properties.setProperty("tagAccounting","key,value");
    properties.setProperty("function_tag","foo2");
    properties.setProperty("product_tag","foo3");
    AwsTagReporter reporter = new AwsTagReporter("testUuid",client,instances,properties);
    reporter.run();
}
 
Example 8
Source File: AutomationReaperTaskTest.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
@Test
// Tests that a node that is not old enough is not terminated
public void testNoShutdownTooRecent() {
    MockVmManager ec2 = new MockVmManager();
    Reservation reservation = new Reservation();
    Instance instance = new Instance();
    String instanceId = "foo";
    instance.setInstanceId(instanceId);
    instance.setLaunchTime(AutomationUtils.modifyDate(new Date(),-15,Calendar.MINUTE));
    reservation.setInstances(Arrays.asList(instance));
    ec2.setReservations(Arrays.asList(reservation));
    AutomationReaperTask task = new AutomationReaperTask(null,ec2);
    task.run();
    Assert.assertFalse("Node should NOT be terminated as it was not old", ec2.isTerminated());
}
 
Example 9
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch instances test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchInstancesTest() throws Exception {
    
    mockStatic(AmazonEC2ClientBuilder.class);
    AmazonEC2 ec2Client = PowerMockito.mock(AmazonEC2.class);
    AmazonEC2ClientBuilder amazonEC2ClientBuilder = PowerMockito.mock(AmazonEC2ClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonEC2ClientBuilder.standard()).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withCredentials(anyObject())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.withRegion(anyString())).thenReturn(amazonEC2ClientBuilder);
    when(amazonEC2ClientBuilder.build()).thenReturn(ec2Client);
    
    DescribeInstancesResult describeInstancesResult = new DescribeInstancesResult();
    List<Instance> instanceList = new ArrayList<>();
    instanceList.add(new Instance());
    List<Reservation> reservations = new ArrayList<>();
    Reservation reservation = new Reservation();
    reservation.setInstances(instanceList);
    reservations.add(reservation);
    describeInstancesResult.setReservations(reservations );
    when(ec2Client.describeInstances(anyObject())).thenReturn(describeInstancesResult);
    assertThat(inventoryUtil.fetchInstances(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName","").size(), is(1));
    
}
 
Example 10
Source File: AutomationReaperTaskTest.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testShutdown() {
    MockVmManager ec2 = new MockVmManager();
    Reservation reservation = new Reservation();
    Instance instance = new Instance();
    String instanceId = "foo";
    instance.setInstanceId(instanceId);
    instance.setLaunchTime(AutomationUtils.modifyDate(new Date(),-5,Calendar.HOUR));
    reservation.setInstances(Arrays.asList(instance));
    ec2.setReservations(Arrays.asList(reservation));
    AutomationReaperTask task = new AutomationReaperTask(null,ec2);
    task.run();
    Assert.assertTrue("Node should be terminated as it was empty", ec2.isTerminated());
}
 
Example 11
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
@Test
// Tests that the built in guard against an infinite loop in the fallback recursive logic has a working safeguard
public void testSubnetInfiniteLoop() throws NodesCouldNotBeStartedException {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    client.setThrowExceptionsInRunInstancesIndefinitely();
    AmazonServiceException exception = new AmazonServiceException("message");
    exception.setErrorCode("InsufficientInstanceCapacity");
    client.setThrowDescribeInstancesError(exception);
    RunInstancesResult runInstancesResult = new RunInstancesResult();
    Reservation reservation = new Reservation();
    reservation.setInstances(Arrays.asList(new Instance()));
    runInstancesResult.setReservation(reservation);
    client.setRunInstances(runInstancesResult);
    Properties properties = new Properties();
    String region = "east", uuid="uuid",browser="chrome",os="linux";
    Integer threadCount = 5,maxSessions=5;
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);
    String userData = "userData";
    String securityGroup="securityGroup",subnetId="subnetId",keyName="keyName",windowsImage="windowsImage";
    properties.setProperty(region + "_security_group",securityGroup);
    properties.setProperty(region + "_subnet_id", subnetId);
    properties.setProperty(region + "_subnet_fallback_id_1", "foo");
    properties.setProperty(region + "_subnet_fallback_id_2", "foo");
    properties.setProperty(region + "_subnet_fallback_id_3", "foo");
    properties.setProperty(region + "_subnet_fallback_id_4", "foo");
    properties.setProperty(region + "_subnet_fallback_id_5", "foo");
    properties.setProperty(region + "_subnet_fallback_id_6", "foo");
    properties.setProperty(region + "_key_name", keyName);
    properties.setProperty(region + "_windows_node_ami", windowsImage);
    manageEC2.setUserData(userData);
    try{
        manageEC2.launchNodes(uuid,os,browser,null,threadCount,maxSessions);
    } catch(NodesCouldNotBeStartedException e) {
        Assert.assertTrue("Failure message should be correct",e.getMessage().contains("Sufficient resources were not available in any of the availability zones"));
        return;
    }
    Assert.fail("Call should fail due to insufficient resources");
}
 
Example 12
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
@Test
// Tests that if the client fails for an error other than insufficient capacity, subnet fallback logic is not performed
public void testSubnetFallBackUnknownError() throws NodesCouldNotBeStartedException {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    AmazonServiceException exception = new AmazonServiceException("message");
    client.setThrowDescribeInstancesError(exception);
    RunInstancesResult runInstancesResult = new RunInstancesResult();
    Reservation reservation = new Reservation();
    reservation.setInstances(Arrays.asList(new Instance()));
    runInstancesResult.setReservation(reservation);
    client.setRunInstances(runInstancesResult);
    Properties properties = new Properties();
    String region = "east", uuid="uuid",browser="chrome",os="linux";
    Integer threadCount = 5,maxSessions=5;
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);
    String userData = "userData";
    String securityGroup="securityGroup",subnetId="subnetId",keyName="keyName",windowsImage="windowsImage";
    properties.setProperty(region + "_security_group",securityGroup);
    properties.setProperty(region + "_subnet_id", subnetId);
    properties.setProperty(region + "_key_name", keyName);
    properties.setProperty(region + "_windows_node_ami", windowsImage);
    manageEC2.setUserData(userData);
    try{
        manageEC2.launchNodes(uuid,os,browser,null,threadCount,maxSessions);
    } catch(AmazonServiceException e) {
        Assert.assertEquals("Exception should be the same",exception,e);
        return;
    }
    Assert.fail("Call should fail due to other AWS error");
}
 
Example 13
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
@Test
// Tests that if no fallback subnets are specified, the correct exception is thrown
public void testSubnetNoFallBack() throws NodesCouldNotBeStartedException {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    AmazonServiceException exception = new AmazonServiceException("message");
    exception.setErrorCode("InsufficientInstanceCapacity");
    client.setThrowDescribeInstancesError(exception);
    RunInstancesResult runInstancesResult = new RunInstancesResult();
    Reservation reservation = new Reservation();
    reservation.setInstances(Arrays.asList(new Instance()));
    runInstancesResult.setReservation(reservation);
    client.setRunInstances(runInstancesResult);
    Properties properties = new Properties();
    String region = "east", uuid="uuid",browser="chrome",os="linux";
    Integer threadCount = 5,maxSessions=5;
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);
    String userData = "userData";
    String securityGroup="securityGroup",subnetId="subnetId",keyName="keyName",windowsImage="windowsImage";
    properties.setProperty(region + "_security_group",securityGroup);
    properties.setProperty(region + "_subnet_id", subnetId);
    properties.setProperty(region + "_key_name", keyName);
    properties.setProperty(region + "_windows_node_ami", windowsImage);
    manageEC2.setUserData(userData);
    try{
        manageEC2.launchNodes(uuid,os,browser,null,threadCount,maxSessions);
    } catch(NodesCouldNotBeStartedException e) {
        Assert.assertTrue("Failure message should be correct",e.getMessage().contains("Sufficient resources were not available in any of the availability zones"));
        return;
    }
    Assert.fail("Call should fail due to insufficient resources");
}
 
Example 14
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
@Test
// Test if a bad OS is specified, it is handled correctly
public void testLaunchNodesBadOs()  throws NodesCouldNotBeStartedException{
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    RunInstancesResult runInstancesResult = new RunInstancesResult();
    Reservation reservation = new Reservation();
    reservation.setInstances(Arrays.asList(new Instance()));
    runInstancesResult.setReservation(reservation);
    client.setRunInstances(runInstancesResult);
    Properties properties = new Properties();
    String region = "east", uuid="uuid",browser="chrome",os="badOs";
    Integer threadCount = 5,maxSessions=5;
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);
    String userData = "userData";
    String securityGroup="securityGroup",subnetId="subnetId",keyName="keyName",windowsImage="windowsImage";
    properties.setProperty(region + "_security_group",securityGroup);
    properties.setProperty(region + "_subnet_id", subnetId);
    properties.setProperty(region + "_key_name", keyName);
    properties.setProperty(region + "_windows_node_ami", windowsImage);
    manageEC2.setUserData(userData);
    try{
        manageEC2.launchNodes(uuid,os,browser,null,threadCount,maxSessions);
    } catch(RuntimeException e) {
        Assert.assertTrue("Failure message should be correct",e.getMessage().contains(os));
        return;
    }
    Assert.fail("Call should fail due to invalid OS");
}
 
Example 15
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
@Test
// Test launching an IE node works correctly
public void testLaunchNodesIe()  throws NodesCouldNotBeStartedException {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    RunInstancesResult runInstancesResult = new RunInstancesResult();
    Reservation reservation = new Reservation();
    reservation.setInstances(Arrays.asList(new Instance()));
    runInstancesResult.setReservation(reservation);
    client.setRunInstances(runInstancesResult);
    Properties properties = new Properties();
    String region = "east", uuid="uuid",browser="internet explorer",os=null;
    Integer threadCount = 5,maxSessions=5;
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);
    String userData = "userData";
    String securityGroup="securityGroup",subnetId="subnetId",keyName="keyName",windowsImage="windowsImage";
    properties.setProperty(region + "_security_group",securityGroup);
    properties.setProperty(region + "_subnet_id",subnetId);
    properties.setProperty(region + "_key_name", keyName);
    properties.setProperty(region + "_windows_node_ami", windowsImage);
    manageEC2.setUserData(userData);
    manageEC2.launchNodes(uuid,os,browser,null,threadCount,maxSessions);
    RunInstancesRequest request = client.getRunInstancesRequest();
    Assert.assertEquals("Min count should match thread count requested",threadCount,request.getMinCount());
    Assert.assertEquals("Max count should match thread count requested",threadCount,request.getMaxCount());
    Assert.assertEquals("User data should match",userData,request.getUserData());
    Assert.assertEquals("Image id should match",windowsImage,request.getImageId());
    List<String> securityGroups = request.getSecurityGroupIds();
    Assert.assertEquals("Only one security group should be set",1,securityGroups.size());
    Assert.assertEquals("Only one security group should be set", securityGroup, securityGroups.get(0));
    Assert.assertEquals("Subnet ids should match", subnetId, request.getSubnetId());
    Assert.assertEquals("Key names should match", keyName, request.getKeyName());
}
 
Example 16
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 5 votes vote down vote up
@Test
// Test the optional fields for launching a node are indeed optional
public void testLaunchNodesOptionalFieldsSet()  throws NodesCouldNotBeStartedException {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    RunInstancesResult runInstancesResult = new RunInstancesResult();
    Reservation reservation = new Reservation();
    reservation.setInstances(Arrays.asList(new Instance()));
    runInstancesResult.setReservation(reservation);
    client.setRunInstances(runInstancesResult);
    Properties properties = new Properties();
    String region = "east", uuid="uuid",browser="chrome",os=null;
    Integer threadCount = 5,maxSessions=5;
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);
    String userData = "userData";
    String securityGroup="securityGroup",subnetId="subnetId",keyName="keyName",linuxImage="linuxImage";
    properties.setProperty(region + "_security_group",securityGroup);
    properties.setProperty(region + "_subnet_id",subnetId);
    properties.setProperty(region + "_key_name", keyName);
    properties.setProperty(region + "_linux_node_ami", linuxImage);
    manageEC2.setUserData(userData);
    manageEC2.launchNodes(uuid,os,browser,null,threadCount,maxSessions);
    RunInstancesRequest request = client.getRunInstancesRequest();
    Assert.assertEquals("Min count should match thread count requested",threadCount,request.getMinCount());
    Assert.assertEquals("Max count should match thread count requested",threadCount,request.getMaxCount());
    Assert.assertEquals("User data should match",userData,request.getUserData());
    Assert.assertEquals("Image id should match",linuxImage,request.getImageId());
    List<String> securityGroups = request.getSecurityGroupIds();
    Assert.assertEquals("Only one security group should be set",1,securityGroups.size());
    Assert.assertEquals("Only one security group should be set", securityGroup, securityGroups.get(0));
    Assert.assertEquals("Subnet ids should match", subnetId, request.getSubnetId());
    Assert.assertEquals("Key names should match", keyName, request.getKeyName());
}
 
Example 17
Source File: ReservationConverter.java    From primecloud-controller with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Reservation convertObject(ReservationDescription from) {
    Reservation to = new Reservation();

    to.setReservationId(from.getReservationId());
    to.setOwnerId(from.getOwner());
    to.setRequesterId(from.getRequestId());
    to.setGroupNames(from.getGroups());
    to.setInstances(new InstanceConverter().convert(from.getInstances()));

    return to;
}
 
Example 18
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 19
Source File: AwsMetaDataCollectorTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
private Reservation getReservation(Instance... instance) {
    List<Instance> instances = Arrays.asList(instance);
    Reservation r = new Reservation();
    r.setInstances(instances);
    return r;
}
 
Example 20
Source File: VmManagerTest.java    From SeleniumGridScaler with GNU General Public License v2.0 4 votes vote down vote up
@Test
// Test if multiple security groups can be passed when launching a node
public void testLaunchNodesMultipleSecurityGroups()  throws NodesCouldNotBeStartedException {
    MockAmazonEc2Client client = new MockAmazonEc2Client(null);
    RunInstancesResult runInstancesResult = new RunInstancesResult();
    Reservation reservation = new Reservation();
    reservation.setInstances(Arrays.asList(new Instance()));
    runInstancesResult.setReservation(reservation);
    client.setRunInstances(runInstancesResult);
    Properties properties = new Properties();
    String region = "east", uuid="uuid",browser="chrome",os=null;
    Integer threadCount = 5,maxSessions=5;
    MockManageVm manageEC2 = new MockManageVm(client,properties,region);
    String userData = "userData";
    String securityGroup="securityGroup1,securityGroup2,securityGroup3",subnetId="subnetId",keyName="keyName",linuxImage="linuxImage";
    String[] splitSecurityGroupdIds = securityGroup.split(",");
    List securityGroupIdsAryLst = new ArrayList();

    if (securityGroup != null) {
        for (int i = 0; i < splitSecurityGroupdIds.length; i++) {
            securityGroupIdsAryLst.add(splitSecurityGroupdIds[i]);
        }
    }
    properties.setProperty(region + "_security_group",securityGroup);
    properties.setProperty(region + "_subnet_id",subnetId);
    properties.setProperty(region + "_key_name", keyName);
    properties.setProperty(region + "_linux_node_ami", linuxImage);
    manageEC2.setUserData(userData);
    manageEC2.launchNodes(uuid,os,browser,null,threadCount,maxSessions);
    RunInstancesRequest request = client.getRunInstancesRequest();
    request.setSecurityGroupIds(securityGroupIdsAryLst);
    List<String> securityGroups = request.getSecurityGroupIds();
    List<String> expectedSecurityGroups = Arrays.asList("securityGroup1,securityGroup2,securityGroup3".split(","));
    Assert.assertTrue("Security groups should match all given security groups", securityGroups.containsAll(expectedSecurityGroups));

    List<String> invalidSecurityGroups = Arrays.asList("securityGroup1,securityGroup2,securityGroup7".split(","));
    Assert.assertFalse("Security groups should match only the set security groups", securityGroups.containsAll(invalidSecurityGroups));

    Assert.assertFalse("Security group should not be empty", request.getSecurityGroupIds().isEmpty());
    Assert.assertEquals("More than 1 security group should be set",3,securityGroups.size());
}