Search in sources :

Example 51 with TestMonkeyCalendar

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

the class TestOldDetachedVolumeRule method testOldDetachedVolumeBeforeDaylightSavingsCutover.

/** This test exists to check logic on a utility method.
     * The tagging rule for resource expiry uses a variable nubmer of days.
     * However, JodaTime date arithmetic for DAYS uses calendar days.  It does NOT
     * treat a day as 24 hours in this case (HOUR arithmetic, however, does).
     * Therefore, a termination policy of 4 days (96 hours) will actually occur in
     * 95 hours if the resource is tagged with that rule within 4 days of the DST
     * cutover.
     *
     * We experienced test case failures around the 2014 spring DST cutover that
     * prevented us from getting green builds.  So, the assertion logic was loosened
     * to check that we were within a day of the expected date.  For the test case,
     * all but 4 days of the year this problem never shows up.  To verify that our
     * fix was correct, this test case explicitly sets the date.  The other tests
     * that use a DateTime of "DateTime.now()" are not true unit tests, because the
     * test does not isolate the date.  They are actually a partial integration test,
     * as they leave the date up to the system where the test executes.
     *
     * We have to mock the call to MonkeyCalendar.now() because the constructor
     * for that class uses Calendar.getInstance() internally.
     *
     */
@Test
public void testOldDetachedVolumeBeforeDaylightSavingsCutover() {
    int ageThreshold = 5;
    //here we set the create date to a few days before a known DST cutover, where
    //we observed DST failures
    DateTime closeToSpringAheadDst = new DateTime(2014, 3, 7, 0, 0, DateTimeZone.forID("America/Los_Angeles"));
    Resource resource = new AWSResource().withId("vol-12345678901234567").withResourceType(AWSResourceType.EBS_VOLUME).withLaunchTime(new Date(closeToSpringAheadDst.minusDays(ageThreshold + 1).getMillis()));
    ((AWSResource) resource).setAWSResourceState("available");
    Date lastDetachTime = new Date(closeToSpringAheadDst.minusDays(ageThreshold + 1).getMillis());
    String metaTag = VolumeTaggingMonkey.makeMetaTag(null, null, lastDetachTime);
    resource.setTag(JanitorMonkey.JANITOR_META_TAG, metaTag);
    int retentionDays = 4;
    //set the "now" to the fixed execution date for this rule and create a partial mock
    Calendar fixed = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
    fixed.setTimeInMillis(closeToSpringAheadDst.getMillis());
    MonkeyCalendar monkeyCalendar = new TestMonkeyCalendar();
    MonkeyCalendar spyCalendar = spy(monkeyCalendar);
    when(spyCalendar.now()).thenReturn(fixed);
    //use the partial mock for the OldDetachedVolumeRule
    OldDetachedVolumeRule rule = new OldDetachedVolumeRule(spyCalendar, ageThreshold, retentionDays);
    //this volume should be seen as invalid
    Assert.assertFalse(rule.isValid(resource));
    //3.26.2014. Commenting out DST cutover verification.  A line in
    //OldDetachedVolumeRule.isValid actually creates a new date from the Tag value.
    //while this unit test tries its best to use invariants, the tag does not contain timezone
    //information, so a time set in the Los Angeles timezone and tagged, then parsed as
    //UTC (if that's how the VM running the test is set) will fail.
    /////////////////////////////
    //Leaving the code in place to be uncommnented later if that class is refactored
    //to support a design that promotes more complete testing.
    //now verify that the difference between "now" and the cutoff is slightly under the intended
    //retention limit, as the DST cutover makes us lose one hour
    //verifyDSTCutoverHappened(resource, retentionDays, closeToSpringAheadDst);
    /////////////////////////////
    //now verify that our projected termination time is within one day of what was asked for
    TestUtils.verifyTerminationTimeRough(resource, retentionDays, closeToSpringAheadDst);
}
Also used : TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) AWSResource(com.netflix.simianarmy.aws.AWSResource) TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) MonkeyCalendar(com.netflix.simianarmy.MonkeyCalendar) Calendar(java.util.Calendar) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) MonkeyCalendar(com.netflix.simianarmy.MonkeyCalendar) DateTime(org.joda.time.DateTime) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 52 with TestMonkeyCalendar

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

the class TestOldDetachedVolumeRule method testNoMetaTag.

@Test
public void testNoMetaTag() {
    int ageThreshold = 5;
    DateTime now = DateTime.now();
    Resource resource = new AWSResource().withId("vol-12345678901234567").withResourceType(AWSResourceType.EBS_VOLUME).withLaunchTime(new Date(now.minusDays(ageThreshold + 1).getMillis()));
    ((AWSResource) resource).setAWSResourceState("available");
    int retentionDays = 4;
    OldDetachedVolumeRule rule = new OldDetachedVolumeRule(new TestMonkeyCalendar(), ageThreshold, retentionDays);
    resource.setTag(JanitorMonkey.JANITOR_TAG, "donotmark");
    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) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 53 with TestMonkeyCalendar

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

the class TestOldDetachedVolumeRule method testUserSpecifiedTerminationDate.

@Test
public void testUserSpecifiedTerminationDate() {
    int ageThreshold = 5;
    DateTime now = DateTime.now();
    Resource resource = new AWSResource().withId("vol-12345678901234567").withResourceType(AWSResourceType.EBS_VOLUME).withLaunchTime(new Date(now.minusDays(ageThreshold + 1).getMillis()));
    ((AWSResource) resource).setAWSResourceState("available");
    int retentionDays = 4;
    DateTime userDate = new DateTime(now.plusDays(3).withTimeAtStartOfDay());
    resource.setTag(JanitorMonkey.JANITOR_TAG, OldDetachedVolumeRule.TERMINATION_DATE_FORMATTER.print(userDate));
    OldDetachedVolumeRule rule = new OldDetachedVolumeRule(new TestMonkeyCalendar(), ageThreshold, retentionDays);
    Assert.assertFalse(rule.isValid(resource));
    Assert.assertEquals(resource.getExpectedTerminationTime().getTime(), userDate.getMillis());
}
Also used : TestMonkeyCalendar(com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) AWSResource(com.netflix.simianarmy.aws.AWSResource) DateTime(org.joda.time.DateTime) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 54 with TestMonkeyCalendar

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

the class TestOrphanedELBRule method testELBWithInstancesNoASGs.

@Test
public void testELBWithInstancesNoASGs() {
    DateTime now = DateTime.now();
    Resource resource = new AWSResource().withId("test-elb").withResourceType(AWSResourceType.ELB).withOwnerEmail("owner@foo.com");
    resource.setAdditionalField("referencedASGCount", "0");
    resource.setAdditionalField("instanceCount", "4");
    OrphanedELBRule rule = new OrphanedELBRule(new TestMonkeyCalendar(), 7);
    Assert.assertTrue(rule.isValid(resource));
}
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) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 55 with TestMonkeyCalendar

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

the class TestUntaggedRule method testResourceWithExpectedTerminationTimeSet.

@Test
public void testResourceWithExpectedTerminationTimeSet() {
    DateTime now = DateTime.now();
    Date oldTermDate = new Date(now.plusDays(10).getMillis());
    String oldTermReason = "Foo";
    Resource resource = new AWSResource().withId("i-12345678901234567").withResourceType(AWSResourceType.INSTANCE).withExpectedTerminationTime(oldTermDate).withTerminationReason(oldTermReason);
    ((AWSResource) resource).setAWSResourceState("running");
    Set<String> tags = new HashSet<String>();
    tags.add("tag1");
    tags.add("tag2");
    int retentionDaysWithOwner = 4;
    int retentionDaysWithoutOwner = 8;
    UntaggedRule rule = new UntaggedRule(new TestMonkeyCalendar(), tags, retentionDaysWithOwner, retentionDaysWithoutOwner);
    Assert.assertFalse(rule.isValid(resource));
    Assert.assertEquals(oldTermDate, resource.getExpectedTerminationTime());
    Assert.assertEquals(oldTermReason, resource.getTerminationReason());
}
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) DateTime(org.joda.time.DateTime) Date(java.util.Date) HashSet(java.util.HashSet) 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