Search in sources :

Example 81 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource 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 82 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource 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 83 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource 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 84 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class AlwaysInvalidRule method testWithExclusionRuleMatch1.

@Test
void testWithExclusionRuleMatch1() {
    Resource resource = new AWSResource().withId("id");
    DateTime now = DateTime.now();
    BasicJanitorRuleEngine engine = new BasicJanitorRuleEngine().addExclusionRule(new AlwaysValidRule()).addRule(new AlwaysInvalidRule(now, 1));
    Assert.assertTrue(engine.isValid(resource));
}
Also used : 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 85 with AWSResource

use of com.netflix.simianarmy.aws.AWSResource in project SimianArmy by Netflix.

the class AlwaysInvalidRule method testEmptyRuleSet.

@Test
public void testEmptyRuleSet() {
    Resource resource = new AWSResource().withId("id");
    BasicJanitorRuleEngine engine = new BasicJanitorRuleEngine();
    Assert.assertTrue(engine.isValid(resource));
}
Also used : AWSResource(com.netflix.simianarmy.aws.AWSResource) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) Test(org.testng.annotations.Test)

Aggregations

AWSResource (com.netflix.simianarmy.aws.AWSResource)109 Resource (com.netflix.simianarmy.Resource)102 Test (org.testng.annotations.Test)89 TestMonkeyCalendar (com.netflix.simianarmy.aws.janitor.rule.TestMonkeyCalendar)64 DateTime (org.joda.time.DateTime)63 Date (java.util.Date)41 MonkeyCalendar (com.netflix.simianarmy.MonkeyCalendar)21 BeforeTest (org.testng.annotations.BeforeTest)9 JsonNode (org.codehaus.jackson.JsonNode)7 AWSClient (com.netflix.simianarmy.client.aws.AWSClient)6 AWSResourceType (com.netflix.simianarmy.aws.AWSResourceType)5 HashSet (java.util.HashSet)5 RowMapper (org.springframework.jdbc.core.RowMapper)5 LinkedList (java.util.LinkedList)4 Tag (com.amazonaws.services.ec2.model.Tag)3 HashMap (java.util.HashMap)3 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)2 LaunchConfiguration (com.amazonaws.services.autoscaling.model.LaunchConfiguration)2 AutoScalingInstanceDetails (com.amazonaws.services.autoscaling.model.AutoScalingInstanceDetails)1 Instance (com.amazonaws.services.autoscaling.model.Instance)1