Search in sources :

Example 46 with TestMonkeyCalendar

use of com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar in project SimianArmy by Netflix.

the class TestJanitorContext method testJanitorWithOptedOutResources.

@Test
public static void testJanitorWithOptedOutResources() {
    Collection<Resource> crawledResources = new ArrayList<Resource>();
    int n = 10;
    for (Resource r : generateTestingResources(n)) {
        crawledResources.add(r);
    }
    TestJanitorCrawler crawler = new TestJanitorCrawler(crawledResources);
    // set some resources in the tracker as opted out
    Date now = new Date(DateTime.now().minusDays(1).getMillis());
    Map<String, Resource> trackedResources = new HashMap<String, Resource>();
    for (Resource r : generateTestingResources(n)) {
        int id = Integer.parseInt(r.getId());
        if (id % 4 == 1 || id % 4 == 2) {
            r.setOptOutOfJanitor(true);
            r.setState(CleanupState.MARKED);
            r.setExpectedTerminationTime(now);
            r.setMarkTime(now);
        }
        trackedResources.put(r.getId(), r);
    }
    TestJanitorResourceTracker resourceTracker = new TestJanitorResourceTracker(trackedResources);
    TestAbstractJanitor janitor = new TestAbstractJanitor(new TestJanitorContext(TEST_REGION, new BasicJanitorRuleEngine().addRule(new IsEvenRule()), crawler, resourceTracker, new TestMonkeyCalendar()), TestResourceType.TEST_RESOURCE_TYPE);
    janitor.setLeashed(false);
    Assert.assertEquals(crawler.resources(TestResourceType.TEST_RESOURCE_TYPE).size(), 10);
    Assert.assertEquals(resourceTracker.getResources(TestResourceType.TEST_RESOURCE_TYPE, CleanupState.MARKED, TEST_REGION).size(), // 1, 2, 5, 6, 9, 10 are marked
    6);
    Assert.assertEquals(janitor.markedResourceIds.size(), 0);
    janitor.markResources();
    Assert.assertEquals(resourceTracker.getResources(TestResourceType.TEST_RESOURCE_TYPE, CleanupState.MARKED, TEST_REGION).size(), // 1, 3, 5, 7, 9 are marked
    5);
    // 3, 7 are newly marked.
    Assert.assertEquals(janitor.getMarkedResources().size(), 2);
    Assert.assertEquals(janitor.markedResourceIds.size(), 2);
    Assert.assertEquals(janitor.cleanedResourceIds.size(), 0);
    Assert.assertEquals(resourceTracker.getResources(TestResourceType.TEST_RESOURCE_TYPE, CleanupState.MARKED, TEST_REGION).size(), // 1, 3, 5, 7, 9 are marked
    5);
    // 2, 6, 10 got unmarked
    Assert.assertEquals(janitor.getUnmarkedResources().size(), 3);
    Assert.assertEquals(resourceTracker.getResources(TestResourceType.TEST_RESOURCE_TYPE, CleanupState.UNMARKED, TEST_REGION).size(), 3);
    janitor.cleanupResources();
    // 3, 7 are cleaned
    Assert.assertEquals(janitor.getCleanedResources().size(), 2);
    Assert.assertEquals(janitor.getFailedToCleanResources().size(), 0);
    Assert.assertEquals(resourceTracker.getResources(TestResourceType.TEST_RESOURCE_TYPE, CleanupState.JANITOR_TERMINATED, TEST_REGION).size(), 2);
    Assert.assertEquals(janitor.getResourcesCleanedCount(), janitor.cleanedResourceIds.size());
    Assert.assertEquals(janitor.getMarkedResourcesCount(), janitor.markedResourceIds.size());
    Assert.assertEquals(janitor.getFailedToCleanResourcesCount(), 0);
    Assert.assertEquals(janitor.getUnmarkedResourcesCount(), 3);
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) BasicJanitorRuleEngine(com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine) Test(org.testng.annotations.Test)

Example 47 with TestMonkeyCalendar

use of com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar in project SimianArmy by Netflix.

the class TestJanitorContext method testJanitorWithoutNotification.

@Test
public static void testJanitorWithoutNotification() {
    Collection<Resource> crawledResources = new ArrayList<Resource>();
    Map<String, Resource> trackedResources = new HashMap<String, Resource>();
    int n = 10;
    for (Resource r : generateTestingResources(n)) {
        trackedResources.put(r.getId(), r);
        r.setState(CleanupState.MARKED);
        // The marking/cleanup is not notified so we the Janitor won't clean it up.
        // r.setNotificationTime(new Date());
        r.setMarkTime(new Date());
        r.setExpectedTerminationTime(new Date(DateTime.now().plusDays(10).getMillis()));
    }
    for (Resource r : generateTestingResources(n)) {
        crawledResources.add(r);
    }
    TestJanitorCrawler crawler = new TestJanitorCrawler(crawledResources);
    TestJanitorResourceTracker resourceTracker = new TestJanitorResourceTracker(trackedResources);
    TestAbstractJanitor janitor = new TestAbstractJanitor(new TestJanitorContext(TEST_REGION, new BasicJanitorRuleEngine().addRule(new IsEvenRule()), crawler, resourceTracker, new TestMonkeyCalendar()), TestResourceType.TEST_RESOURCE_TYPE);
    janitor.setLeashed(false);
    Assert.assertEquals(resourceTracker.getResources(TestResourceType.TEST_RESOURCE_TYPE, CleanupState.MARKED, TEST_REGION).size(), n);
    janitor.cleanupResources();
    Assert.assertEquals(janitor.getCleanedResources().size(), 0);
    Assert.assertEquals(janitor.getFailedToCleanResources().size(), 0);
    Assert.assertEquals(janitor.getResourcesCleanedCount(), janitor.cleanedResourceIds.size());
    Assert.assertEquals(janitor.getMarkedResourcesCount(), janitor.markedResourceIds.size());
    Assert.assertEquals(janitor.getFailedToCleanResourcesCount(), 0);
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) BasicJanitorRuleEngine(com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine) Test(org.testng.annotations.Test)

Example 48 with TestMonkeyCalendar

use of com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar in project SimianArmy by Netflix.

the class TestOldEmptyASGRule method testEmptyASGWithLaunchConfigWithoutCreateTime.

@Test
public void testEmptyASGWithLaunchConfigWithoutCreateTime() {
    Resource resource = new AWSResource().withId("asg1").withResourceType(AWSResourceType.ASG);
    resource.setAdditionalField(ASGJanitorCrawler.ASG_FIELD_LC_NAME, "launchConfig");
    resource.setAdditionalField(ASGJanitorCrawler.ASG_FIELD_MAX_SIZE, "0");
    int launchConfiguAgeThreshold = 60;
    MonkeyCalendar calendar = new TestMonkeyCalendar();
    int retentionDays = 3;
    OldEmptyASGRule rule = new OldEmptyASGRule(calendar, launchConfiguAgeThreshold, retentionDays, new DummyASGInstanceValidator());
    Assert.assertTrue(rule.isValid(resource));
    Assert.assertNull(resource.getExpectedTerminationTime());
}
Also used : TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) MonkeyCalendar(com.netflix.simianarmy.MonkeyCalendar) Test(org.testng.annotations.Test)

Example 49 with TestMonkeyCalendar

use of com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar in project SimianArmy by Netflix.

the class TestOldEmptyASGRule method testEmptyASGWithoutLaunchConfig.

@Test
public void testEmptyASGWithoutLaunchConfig() {
    Resource resource = new AWSResource().withId("asg1").withResourceType(AWSResourceType.ASG);
    resource.setAdditionalField(ASGJanitorCrawler.ASG_FIELD_MAX_SIZE, "0");
    int launchConfiguAgeThreshold = 60;
    MonkeyCalendar calendar = new TestMonkeyCalendar();
    DateTime now = new DateTime(calendar.now().getTimeInMillis());
    int retentionDays = 3;
    OldEmptyASGRule rule = new OldEmptyASGRule(calendar, launchConfiguAgeThreshold, retentionDays, new DummyASGInstanceValidator());
    Assert.assertFalse(rule.isValid(resource));
    TestUtils.verifyTerminationTimeRough(resource, retentionDays, now);
}
Also used : TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) MonkeyCalendar(com.netflix.simianarmy.MonkeyCalendar) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 50 with TestMonkeyCalendar

use of com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar in project SimianArmy by Netflix.

the class TestOldEmptyASGRule method testNullResource.

@Test(expectedExceptions = IllegalArgumentException.class)
public void testNullResource() {
    OldEmptyASGRule rule = new OldEmptyASGRule(new TestMonkeyCalendar(), 3, 60, new DummyASGInstanceValidator());
    rule.isValid(null);
}
Also used : TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) Test(org.testng.annotations.Test)

Aggregations

TestMonkeyCalendar (com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar)79 Test (org.testng.annotations.Test)79 AWSResource (com.netflix.simianarmy.aws.AWSResource)73 Resource (com.netflix.simianarmy.Resource)64 DateTime (org.joda.time.DateTime)51 Date (java.util.Date)28 MonkeyCalendar (com.netflix.simianarmy.MonkeyCalendar)21 BasicJanitorRuleEngine (com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine)9 HashSet (java.util.HashSet)5 Calendar (java.util.Calendar)1