Search in sources :

Example 96 with Resource

use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.

the class TestRDSJanitorResourceTracker method testInsertNewResource.

@SuppressWarnings("unchecked")
@Test
public void testInsertNewResource() {
    // mock the select query that is issued to see if the record already exists
    ArrayList<AWSResource> resources = new ArrayList<>();
    TestRDSJanitorResourceTracker tracker = new TestRDSJanitorResourceTracker();
    when(tracker.getJdbcTemplate().query(Matchers.anyString(), Matchers.any(Object[].class), Matchers.any(RowMapper.class))).thenReturn(resources);
    String id = "i-12345678901234567";
    AWSResourceType resourceType = AWSResourceType.INSTANCE;
    Resource.CleanupState state = Resource.CleanupState.MARKED;
    String description = "This is a test resource.";
    String ownerEmail = "owner@test.com";
    String region = "us-east-1";
    String terminationReason = "This is a test termination reason.";
    DateTime now = DateTime.now();
    Date expectedTerminationTime = new Date(now.plusDays(10).getMillis());
    Date markTime = new Date(now.getMillis());
    String fieldName = "fieldName123";
    String fieldValue = "fieldValue456";
    Resource resource = new AWSResource().withId(id).withResourceType(resourceType).withDescription(description).withOwnerEmail(ownerEmail).withRegion(region).withState(state).withTerminationReason(terminationReason).withExpectedTerminationTime(expectedTerminationTime).withMarkTime(markTime).withOptOutOfJanitor(false).setAdditionalField(fieldName, fieldValue);
    ArgumentCaptor<Object> objCap = ArgumentCaptor.forClass(Object.class);
    ArgumentCaptor<String> sqlCap = ArgumentCaptor.forClass(String.class);
    when(tracker.getJdbcTemplate().update(sqlCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture())).thenReturn(1);
    tracker.addOrUpdate(resource);
    List<Object> args = objCap.getAllValues();
    Assert.assertEquals(sqlCap.getValue(), "insert into janitortable (resourceId,resourceType,region,ownerEmail,description,state,terminationReason,expectedTerminationTime,actualTerminationTime,notificationTime,launchTime,markTime,optOutOfJanitor,additionalFields) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    Assert.assertEquals(args.size(), 14);
    Assert.assertEquals(args.get(0).toString(), id);
    Assert.assertEquals(args.get(1).toString(), AWSResourceType.INSTANCE.toString());
    Assert.assertEquals(args.get(2).toString(), region);
    Assert.assertEquals(args.get(3).toString(), ownerEmail);
    Assert.assertEquals(args.get(4).toString(), description);
    Assert.assertEquals(args.get(5).toString(), state.toString());
    Assert.assertEquals(args.get(6).toString(), terminationReason);
    Assert.assertEquals(args.get(7).toString(), expectedTerminationTime.getTime() + "");
    Assert.assertEquals(args.get(8).toString(), "0");
    Assert.assertEquals(args.get(9).toString(), "0");
    Assert.assertEquals(args.get(10).toString(), "0");
    Assert.assertEquals(args.get(11).toString(), markTime.getTime() + "");
    Assert.assertEquals(args.get(12).toString(), "false");
    Assert.assertEquals(args.get(13).toString(), "{\"fieldName123\":\"fieldValue456\"}");
}
Also used : Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.testng.annotations.Test)

Example 97 with Resource

use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.

the class TestRDSJanitorResourceTracker method testUpdateResource.

@SuppressWarnings("unchecked")
@Test
public void testUpdateResource() {
    String id = "i-12345678901234567";
    AWSResourceType resourceType = AWSResourceType.INSTANCE;
    Resource.CleanupState state = Resource.CleanupState.MARKED;
    String description = "This is a test resource.";
    String ownerEmail = "owner@test.com";
    String region = "us-east-1";
    String terminationReason = "This is a test termination reason.";
    DateTime now = DateTime.now();
    Date expectedTerminationTime = new Date(now.plusDays(10).getMillis());
    Date markTime = new Date(now.getMillis());
    String fieldName = "fieldName123";
    String fieldValue = "fieldValue456";
    Resource resource = new AWSResource().withId(id).withResourceType(resourceType).withDescription(description).withOwnerEmail(ownerEmail).withRegion(region).withState(state).withTerminationReason(terminationReason).withExpectedTerminationTime(expectedTerminationTime).withMarkTime(markTime).withOptOutOfJanitor(false).setAdditionalField(fieldName, fieldValue);
    // mock the select query that is issued to see if the record already exists
    ArrayList<Resource> resources = new ArrayList<>();
    resources.add(resource);
    TestRDSJanitorResourceTracker tracker = new TestRDSJanitorResourceTracker();
    when(tracker.getJdbcTemplate().query(Matchers.anyString(), Matchers.any(Object[].class), Matchers.any(RowMapper.class))).thenReturn(resources);
    // update the ownerEmail
    ownerEmail = "owner2@test.com";
    Resource newResource = new AWSResource().withId(id).withResourceType(resourceType).withDescription(description).withOwnerEmail(ownerEmail).withRegion(region).withState(state).withTerminationReason(terminationReason).withExpectedTerminationTime(expectedTerminationTime).withMarkTime(markTime).withOptOutOfJanitor(false).setAdditionalField(fieldName, fieldValue);
    ArgumentCaptor<Object> objCap = ArgumentCaptor.forClass(Object.class);
    ArgumentCaptor<String> sqlCap = ArgumentCaptor.forClass(String.class);
    when(tracker.getJdbcTemplate().update(sqlCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture(), objCap.capture())).thenReturn(1);
    tracker.addOrUpdate(newResource);
    List<Object> args = objCap.getAllValues();
    Assert.assertEquals(sqlCap.getValue(), "update janitortable set resourceType=?,region=?,ownerEmail=?,description=?,state=?,terminationReason=?,expectedTerminationTime=?,actualTerminationTime=?,notificationTime=?,launchTime=?,markTime=?,optOutOfJanitor=?,additionalFields=? where resourceId=? and region=?");
    Assert.assertEquals(args.size(), 15);
    Assert.assertEquals(args.get(0).toString(), AWSResourceType.INSTANCE.toString());
    Assert.assertEquals(args.get(1).toString(), region);
    Assert.assertEquals(args.get(2).toString(), ownerEmail);
    Assert.assertEquals(args.get(3).toString(), description);
    Assert.assertEquals(args.get(4).toString(), state.toString());
    Assert.assertEquals(args.get(5).toString(), terminationReason);
    Assert.assertEquals(args.get(6).toString(), expectedTerminationTime.getTime() + "");
    Assert.assertEquals(args.get(7).toString(), "0");
    Assert.assertEquals(args.get(8).toString(), "0");
    Assert.assertEquals(args.get(9).toString(), "0");
    Assert.assertEquals(args.get(10).toString(), markTime.getTime() + "");
    Assert.assertEquals(args.get(11).toString(), "false");
    Assert.assertEquals(args.get(12).toString(), "{\"fieldName123\":\"fieldValue456\"}");
    Assert.assertEquals(args.get(13).toString(), id);
    Assert.assertEquals(args.get(14).toString(), region);
}
Also used : Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) AWSResource(com.netflix.simianarmy.aws.AWSResource) RowMapper(org.springframework.jdbc.core.RowMapper) Test(org.testng.annotations.Test)

Example 98 with Resource

use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.

the class TestSimpleDBJanitorResourceTracker method testGetResources.

@Test
public void testGetResources() {
    String id1 = "id-1";
    String id2 = "id-2";
    AWSResourceType resourceType = AWSResourceType.INSTANCE;
    Resource.CleanupState state = Resource.CleanupState.MARKED;
    String description = "This is a test resource.";
    String ownerEmail = "owner@test.com";
    String region = "us-east-1";
    String terminationReason = "This is a test termination reason.";
    DateTime now = DateTime.now();
    Date expectedTerminationTime = new Date(now.plusDays(10).getMillis());
    Date markTime = new Date(now.getMillis());
    String fieldName = "fieldName123";
    String fieldValue = "fieldValue456";
    SelectResult result1 = mkSelectResult(id1, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, false, fieldName, fieldValue);
    result1.setNextToken("nextToken");
    SelectResult result2 = mkSelectResult(id2, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, true, fieldName, fieldValue);
    ArgumentCaptor<SelectRequest> arg = ArgumentCaptor.forClass(SelectRequest.class);
    TestSimpleDBJanitorResourceTracker tracker = new TestSimpleDBJanitorResourceTracker();
    when(tracker.sdbMock.select(any(SelectRequest.class))).thenReturn(result1).thenReturn(result2);
    verifyResources(tracker.getResources(resourceType, state, region), id1, id2, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, fieldName, fieldValue);
    verify(tracker.sdbMock, times(2)).select(arg.capture());
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) DateTime(org.joda.time.DateTime) Date(java.util.Date) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest) Test(org.testng.annotations.Test)

Example 99 with Resource

use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.

the class TestSimpleDBJanitorResourceTracker method testAddResource.

@Test
public void testAddResource() {
    String id = "i-12345678901234567";
    AWSResourceType resourceType = AWSResourceType.INSTANCE;
    Resource.CleanupState state = Resource.CleanupState.MARKED;
    String description = "This is a test resource.";
    String ownerEmail = "owner@test.com";
    String region = "us-east-1";
    String terminationReason = "This is a test termination reason.";
    DateTime now = DateTime.now();
    Date expectedTerminationTime = new Date(now.plusDays(10).getMillis());
    Date markTime = new Date(now.getMillis());
    String fieldName = "fieldName123";
    String fieldValue = "fieldValue456";
    Resource resource = new AWSResource().withId(id).withResourceType(resourceType).withDescription(description).withOwnerEmail(ownerEmail).withRegion(region).withState(state).withTerminationReason(terminationReason).withExpectedTerminationTime(expectedTerminationTime).withMarkTime(markTime).withOptOutOfJanitor(false).setAdditionalField(fieldName, fieldValue);
    ArgumentCaptor<PutAttributesRequest> arg = ArgumentCaptor.forClass(PutAttributesRequest.class);
    TestSimpleDBJanitorResourceTracker tracker = new TestSimpleDBJanitorResourceTracker();
    tracker.addOrUpdate(resource);
    verify(tracker.sdbMock).putAttributes(arg.capture());
    PutAttributesRequest req = arg.getValue();
    Assert.assertEquals(req.getDomainName(), "DOMAIN");
    Assert.assertEquals(req.getItemName(), getSimpleDBItemName(resource));
    Map<String, String> map = new HashMap<String, String>();
    for (ReplaceableAttribute attr : req.getAttributes()) {
        map.put(attr.getName(), attr.getValue());
    }
    Assert.assertEquals(map.remove(AWSResource.FIELD_RESOURCE_ID), id);
    Assert.assertEquals(map.remove(AWSResource.FIELD_DESCRIPTION), description);
    Assert.assertEquals(map.remove(AWSResource.FIELD_EXPECTED_TERMINATION_TIME), AWSResource.DATE_FORMATTER.print(expectedTerminationTime.getTime()));
    Assert.assertEquals(map.remove(AWSResource.FIELD_MARK_TIME), AWSResource.DATE_FORMATTER.print(markTime.getTime()));
    Assert.assertEquals(map.remove(AWSResource.FIELD_REGION), region);
    Assert.assertEquals(map.remove(AWSResource.FIELD_OWNER_EMAIL), ownerEmail);
    Assert.assertEquals(map.remove(AWSResource.FIELD_RESOURCE_TYPE), resourceType.name());
    Assert.assertEquals(map.remove(AWSResource.FIELD_STATE), state.name());
    Assert.assertEquals(map.remove(AWSResource.FIELD_TERMINATION_REASON), terminationReason);
    Assert.assertEquals(map.remove(AWSResource.FIELD_OPT_OUT_OF_JANITOR), "false");
    Assert.assertEquals(map.remove(fieldName), fieldValue);
    Assert.assertEquals(map.size(), 0);
}
Also used : HashMap(java.util.HashMap) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) DateTime(org.joda.time.DateTime) Date(java.util.Date) ReplaceableAttribute(com.amazonaws.services.simpledb.model.ReplaceableAttribute) AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) AWSResource(com.netflix.simianarmy.aws.AWSResource) PutAttributesRequest(com.amazonaws.services.simpledb.model.PutAttributesRequest) Test(org.testng.annotations.Test)

Example 100 with Resource

use of com.netflix.simianarmy.Resource in project SimianArmy by Netflix.

the class TestASGJanitorCrawler method testInstancesWithNullNames.

@Test
public void testInstancesWithNullNames() {
    List<AutoScalingGroup> asgList = createASGList();
    AWSClient awsMock = createMockAWSClient(asgList);
    ASGJanitorCrawler crawler = new ASGJanitorCrawler(awsMock);
    List<Resource> resources = crawler.resources();
    verifyASGList(resources, asgList);
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Resource(com.netflix.simianarmy.Resource) AWSClient(com.netflix.simianarmy.client.aws.AWSClient) Test(org.testng.annotations.Test)

Aggregations

Resource (com.netflix.simianarmy.Resource)145 AWSResource (com.netflix.simianarmy.aws.AWSResource)132 Test (org.testng.annotations.Test)110 TestMonkeyCalendar (com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar)64 DateTime (org.joda.time.DateTime)60 Date (java.util.Date)45 MonkeyCalendar (com.netflix.simianarmy.MonkeyCalendar)21 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)18 JsonNode (org.codehaus.jackson.JsonNode)17 AWSResourceType (com.netflix.simianarmy.aws.AWSResourceType)11 BeforeTest (org.testng.annotations.BeforeTest)9 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)8 LoadBalancerDescription (com.amazonaws.services.elasticloadbalancing.model.LoadBalancerDescription)6 HashSet (java.util.HashSet)6 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)5 Instance (com.amazonaws.services.ec2.model.Instance)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 RowMapper (org.springframework.jdbc.core.RowMapper)5 Snapshot (com.amazonaws.services.ec2.model.Snapshot)4