Search in sources :

Example 1 with BasicJanitorRuleEngine

use of com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine in project SimianArmy by Netflix.

the class TestJanitorContext method testJanitorWithUnmarking.

@Test
public static void testJanitorWithUnmarking() {
    Collection<Resource> crawledResources = new ArrayList<Resource>();
    Map<String, Resource> trackedResources = new HashMap<String, Resource>();
    int n = 10;
    DateTime now = DateTime.now();
    Date markTime = new Date(now.minusDays(5).getMillis());
    Date notifyTime = new Date(now.minusDays(4).getMillis());
    Date terminationTime = new Date(now.minusDays(1).getMillis());
    for (Resource r : generateTestingResources(n)) {
        if (Integer.parseInt(r.getId()) % 3 == 0) {
            trackedResources.put(r.getId(), r);
            r.setState(CleanupState.MARKED);
            r.setMarkTime(markTime);
            r.setExpectedTerminationTime(terminationTime);
            r.setNotificationTime(notifyTime);
        }
    }
    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(crawler.resources(TestResourceType.TEST_RESOURCE_TYPE).size(), n);
    Assert.assertEquals(resourceTracker.getResources(TestResourceType.TEST_RESOURCE_TYPE, CleanupState.MARKED, TEST_REGION).size(), n / 3);
    janitor.markResources();
    // (n/3-n/6) resources were already marked, so in the last run the marked resources
    // should be n/2 - n/3 + n/6.
    Assert.assertEquals(janitor.getMarkedResources().size(), n / 2 - n / 3 + n / 6);
    Assert.assertEquals(janitor.getUnmarkedResources().size(), n / 6);
    janitor.cleanupResources();
    Assert.assertEquals(janitor.getCleanedResources().size(), n / 2);
    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);
    Assert.assertEquals(janitor.getUnmarkedResourcesCount(), n / 6);
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) BasicJanitorRuleEngine(com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine) Test(org.testng.annotations.Test)

Example 2 with BasicJanitorRuleEngine

use of com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine in project SimianArmy by Netflix.

the class TestJanitorContext method testJanitorWithUnmarkingUserTerminated.

@Test
public static void testJanitorWithUnmarkingUserTerminated() {
    Collection<Resource> crawledResources = new ArrayList<Resource>();
    Map<String, Resource> trackedResources = new HashMap<String, Resource>();
    int n = 10;
    DateTime now = DateTime.now();
    Date markTime = new Date(now.minusDays(5).getMillis());
    Date notifyTime = new Date(now.minusDays(4).getMillis());
    Date terminationTime = new Date(now.minusDays(1).getMillis());
    for (Resource r : generateTestingResources(n)) {
        if (Integer.parseInt(r.getId()) % 3 != 0) {
            crawledResources.add(r);
        } else {
            trackedResources.put(r.getId(), r);
            r.setState(CleanupState.MARKED);
            r.setMarkTime(markTime);
            r.setNotificationTime(notifyTime);
            r.setExpectedTerminationTime(terminationTime);
        }
    }
    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(crawler.resources(TestResourceType.TEST_RESOURCE_TYPE).size(), n - n / 3);
    Assert.assertEquals(resourceTracker.getResources(TestResourceType.TEST_RESOURCE_TYPE, CleanupState.MARKED, TEST_REGION).size(), n / 3);
    janitor.markResources();
    // n/3 resources should be considered user terminated
    Assert.assertEquals(janitor.getMarkedResources().size(), n / 2 - n / 3 + n / 6);
    Assert.assertEquals(janitor.getUnmarkedResources().size(), n / 3);
    janitor.cleanupResources();
    Assert.assertEquals(janitor.getCleanedResources().size(), n / 2 - n / 3 + n / 6);
    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);
    Assert.assertEquals(janitor.getUnmarkedResourcesCount(), n / 3);
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) BasicJanitorRuleEngine(com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine) Test(org.testng.annotations.Test)

Example 3 with BasicJanitorRuleEngine

use of com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine in project SimianArmy by Netflix.

the class TestJanitorContext method testLeashedJanitorForMarking.

@Test
public static void testLeashedJanitorForMarking() {
    Collection<Resource> crawledResources = new ArrayList<Resource>();
    int n = 10;
    for (Resource r : generateTestingResources(n)) {
        crawledResources.add(r);
    }
    TestJanitorCrawler crawler = new TestJanitorCrawler(crawledResources);
    TestJanitorResourceTracker resourceTracker = new TestJanitorResourceTracker(new HashMap<String, Resource>());
    TestAbstractJanitor janitor = new TestAbstractJanitor(new TestJanitorContext(TEST_REGION, new BasicJanitorRuleEngine().addRule(new IsEvenRule()), crawler, resourceTracker, new TestMonkeyCalendar()), TestResourceType.TEST_RESOURCE_TYPE);
    janitor.setLeashed(true);
    Assert.assertEquals(crawler.resources(TestResourceType.TEST_RESOURCE_TYPE).size(), n);
    janitor.markResources();
    Assert.assertEquals(janitor.getMarkedResources().size(), n / 2);
    Assert.assertEquals(janitor.getResourcesCleanedCount(), janitor.cleanedResourceIds.size());
    Assert.assertEquals(janitor.getMarkedResourcesCount(), n / 2);
    // No resource is really changed in tracker
    Assert.assertEquals(resourceTracker.getResources(TestResourceType.TEST_RESOURCE_TYPE, CleanupState.MARKED, TEST_REGION).size(), 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 4 with BasicJanitorRuleEngine

use of com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine in project SimianArmy by Netflix.

the class TestJanitorContext method testJanitorWithFutureTerminationTime.

@Test
public static void testJanitorWithFutureTerminationTime() {
    Collection<Resource> crawledResources = new ArrayList<Resource>();
    Map<String, Resource> trackedResources = new HashMap<String, Resource>();
    int n = 10;
    DateTime now = DateTime.now();
    Date markTime = new Date(now.minusDays(5).getMillis());
    Date notifyTime = new Date(now.minusDays(4).getMillis());
    Date terminationTime = new Date(now.plusDays(10).getMillis());
    for (Resource r : generateTestingResources(n)) {
        trackedResources.put(r.getId(), r);
        r.setState(CleanupState.MARKED);
        r.setNotificationTime(notifyTime);
        r.setMarkTime(markTime);
        r.setExpectedTerminationTime(terminationTime);
    }
    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) DateTime(org.joda.time.DateTime) TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) BasicJanitorRuleEngine(com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine) Test(org.testng.annotations.Test)

Example 5 with BasicJanitorRuleEngine

use of com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine 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)

Aggregations

BasicJanitorRuleEngine (com.netflix.simianarmy.basic.janitor.BasicJanitorRuleEngine)11 Test (org.testng.annotations.Test)11 AWSResource (com.netflix.simianarmy.aws.AWSResource)9 TestMonkeyCalendar (com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar)9 DateTime (org.joda.time.DateTime)4 UntaggedRule (com.netflix.simianarmy.aws.janitor.rule.generic.UntaggedRule)2