forked from RetailMeNot/SeleniumGridScaler
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathAutomationReaperTaskTest.java
More file actions
107 lines (98 loc) · 4.75 KB
/
AutomationReaperTaskTest.java
File metadata and controls
107 lines (98 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.rmn.qa.task;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
import com.rmn.qa.AutomationContext;
import com.rmn.qa.AutomationDynamicNode;
import com.rmn.qa.AutomationUtils;
import com.rmn.qa.MockVmManager;
import junit.framework.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
public class AutomationReaperTaskTest {
@Test
public void testShutdown() {
MockVmManager ec2 = new MockVmManager();
Reservation reservation = new Reservation();
Instance instance = new Instance();
String instanceId = "foo";
instance.setInstanceId(instanceId);
instance.setLaunchTime(AutomationUtils.modifyDate(new Date(),-5,Calendar.HOUR));
reservation.setInstances(Arrays.asList(instance));
ec2.setReservations(Arrays.asList(reservation));
AutomationReaperTask task = new AutomationReaperTask(null,ec2);
task.run();
Assert.assertTrue("Node should be terminated as it was empty", ec2.isTerminated());
}
@Test
// Tests that a node that is not old enough is not terminated
public void testNoShutdownTooRecent() {
MockVmManager ec2 = new MockVmManager();
Reservation reservation = new Reservation();
Instance instance = new Instance();
String instanceId = "foo";
instance.setInstanceId(instanceId);
instance.setLaunchTime(AutomationUtils.modifyDate(new Date(),-15,Calendar.MINUTE));
reservation.setInstances(Arrays.asList(instance));
ec2.setReservations(Arrays.asList(reservation));
AutomationReaperTask task = new AutomationReaperTask(null,ec2);
task.run();
Assert.assertFalse("Node should NOT be terminated as it was not old", ec2.isTerminated());
}
@Test
// Tests that a node that is being tracked internally is not shut down
public void testNoShutdownNodeTracked() {
MockVmManager ec2 = new MockVmManager();
Reservation reservation = new Reservation();
Instance instance = new Instance();
String instanceId = "foo";
AutomationContext.getContext().addNode(new AutomationDynamicNode("faky",instanceId,null,null,new Date(),1));
instance.setInstanceId(instanceId);
instance.setLaunchTime(AutomationUtils.modifyDate(new Date(),-5,Calendar.HOUR));
reservation.setInstances(Arrays.asList(instance));
ec2.setReservations(Arrays.asList(reservation));
AutomationReaperTask task = new AutomationReaperTask(null,ec2);
task.run();
Assert.assertFalse("Node should NOT be terminated as it was tracked internally", ec2.isTerminated());
}
@Test
// Tests that the hardcoded name of the task is correct
public void testTaskName() {
AutomationReaperTask task = new AutomationReaperTask(null,null);
Assert.assertEquals("Name should be the same",AutomationReaperTask.NAME, task.getDescription() );
}
@Test
public void testNodeMatch(){
Reservation reservation = new Reservation();
Instance instance = new Instance();
String instanceId = "foo";
instance.setInstanceId(instanceId);
instance.setLaunchTime(AutomationUtils.modifyDate(new Date(),-5,Calendar.HOUR));
AutomationContext.getContext().addNode(new AutomationDynamicNode("faky",instanceId,null,null,new Date(),1));
List<Tag> tags = new ArrayList<>();
Tag nodeTag = new Tag("LaunchSource","SeleniumGridScalerPlugi_" + instanceId);
tags.add(nodeTag);
instance.setTags(tags);
reservation.setInstances(Arrays.asList(instance));
String expectedTag = "Tags: [{Key: LaunchSource,Value: SeleniumGridScalerPlugi_foo}]";
Assert.assertTrue("The node tag should match!",reservation.toString().contains(expectedTag));
}
@Test
public void testNodeDoNotMatch(){
Reservation reservation = new Reservation();
Instance instance = new Instance();
String instanceId = "foo";
instance.setInstanceId(instanceId);
instance.setLaunchTime(AutomationUtils.modifyDate(new Date(),-5,Calendar.HOUR));
AutomationContext.getContext().addNode(new AutomationDynamicNode("faky",instanceId,null,null,new Date(),1));
List<Tag> tags = new ArrayList<>();
Tag nodeTag = new Tag("LaunchSource","SeleniumGridScalerPlugi_" + instanceId);
tags.add(nodeTag);
instance.setTags(tags);
reservation.setInstances(Arrays.asList(instance));
String expectedTag = "Tags: [{Key: LaunchSource,Value: SeleniumGridScalerPlugi_nofoo}]";
reservation.setInstances(Arrays.asList(instance));
Assert.assertFalse("The node tag should not match!",reservation.toString().contains(expectedTag));
}
}