Search in sources :

Example 21 with MonkeyCalendar

use of com.netflix.simianarmy.MonkeyCalendar 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)

Aggregations

MonkeyCalendar (com.netflix.simianarmy.MonkeyCalendar)21 Resource (com.netflix.simianarmy.Resource)21 AWSResource (com.netflix.simianarmy.aws.AWSResource)21 TestMonkeyCalendar (com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar)21 Test (org.testng.annotations.Test)21 DateTime (org.joda.time.DateTime)17 Date (java.util.Date)8 Calendar (java.util.Calendar)1